1. 程式人生 > >k8s的LNMP部署

k8s的LNMP部署

架構圖:

在這裡插入圖片描述

映象

docker login 192.168.1.40:5000
nginx:
docker build -t  192.168.1.40:5000/lnmp/nginx .
docker push 192.168.1.40:5000/lnmp/nginx

php:
docker bulid -t 192.168.1.40:5000/lnmp/php .
docker push 192.168.1.40:5000/lnmp/php

mysql:
docker pull mysql:5.6
docker tag 1f47fade220d 192.168.1.40/lnmp/mysql
docker push 192.168.1.40:5000/lnmp/mysql
[[email protected] ~]# curl -u liaochao:123456 192.168.1.40:5000/v2/_catalog
{"repositories":["lnmp/mysql","lnmp/nginx","lnmp/php","nginx","php"]}

認證:

映象準備好以後我們需要配置一些認證檔案,比如,mysql的賬戶密碼,倉庫的賬戶密碼等等,我們需要將這些放入k8s的認證中
mysql:
建立mysql密碼的認證

kubectl create secret generic mysql-pass --from-literal=password=123456

檢測:

[[email protected] LNMP]# kubectl get secret
NAME                  TYPE                                  DATA      AGE
default-token-5bjhk   kubernetes.io/service-account-token   3         24d
liaochao-https        kubernetes.io/tls                     2         6d
mysql-pass            Opaque                                1         19s

建立倉庫認證:
檢視認證資訊:(bash64 -wo 代表以64位轉碼展示並且不換行)

cat .docker/config.json |base64 -w0

然後我們根據得到的資料建立認證:

[[email protected] LNMP]# cat docker-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: registrypullsecret
data:
  .dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEuNDA6NTAwMCI6IHsKCQkJImF1dGgiOiAiYkdsaGIyTm9ZVzg2TVRJek5EVTIiCgkJfQoJfSwKCSJIdHRwSGVhZGVycyI6IHsKCQkiVXNlci1BZ2VudCI6ICJEb2NrZXItQ2xpZW50LzE4LjA2LjAtY2UgKGxpbnV4KSIKCX0KfQ==
type: kubernetes.io/dockerconfigjson

建立

kubectl create -f docker-secret.yaml

檢測:

[[email protected] LNMP]# kubectl get secret
NAME                  TYPE                                  DATA      AGE
default-token-5bjhk   kubernetes.io/service-account-token   3         24d
liaochao-https        kubernetes.io/tls                     2         7d
mysql-pass            Opaque                                1         23m
registrypullsecret    kubernetes.io/dockerconfigjson        1         12s

配置lnmp環境

pv 資料儲存:

[[email protected] LNMP]# cat pv.yaml
apiVersion: v1
#使用glusterfs做儲存,不懂glusterfs的請參考其他文章
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "gv1"
---
#使用nfs做儲存
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wp-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /opt/container_data/wp-wwwroot
    server: 192.168.1.39

php配置:

[[email protected] LNMP]# cat php-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress-php
  labels:
    app: wordpress
spec:
  ports:
    - port: 9000
  selector:
    app: wordpress-php
    tier: frontend
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: wordpress-php
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress-php
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress-php
        tier: frontend
    spec:
      containers:
      - name: php
        image: 192.168.1.40:5000/lnmp/php
        ports:
        - containerPort: 9000
          name: wordpress
        volumeMounts:
#用於php解析的目錄
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
##拉取映象時的使用者認證,沒有的時候註釋掉這2行
      imagePullSecrets:
        - name: registrypullsecret
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pvc

nginx配置:
此處nginx和php共用一個pv,pvc

[[email protected] LNMP]# cat nginx-deployment.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-wp-config
data:
  site.conf: |-
    server {
        listen 80;
        server_name localhost;
        root html;
        index index.html index.php;

        location ~ \.php$ {
            root html;
            fastcgi_pass wordpress-php:9000;
            fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_connect_timeout 60s;
            fastcgi_read_timeout 300s;
            fastcgi_send_timeout 300s;
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-nginx
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress-nginx
    tier: frontend
  type: NodePort
  sessionAffinity: ClientIP
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: wordpress-nginx
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress-nginx
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress-nginx
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: 192.168.1.40:5000/lnmp/nginx
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /usr/local/nginx/html
        - name: config
          mountPath: /usr/local/nginx/conf/vhost/site.conf
          subPath: site.conf
      imagePullSecrets:
        - name: registrypullsecret
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pvc
      - name: config
        configMap:
          name: nginx-wp-config

yaml檔案準備好以後,建立:

kubectl create -f mysql-deployment.yaml
kubectl create -f php-deployment.yaml
kubectl create -f nginx-deployment.yaml

檢測儲存:

kubectl get pv,pvc
[[email protected] lnmp]# kubectl get pv,pvc
NAME                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                    STORAGECLASS   REASON    AGE
persistentvolume/mysql-pv   20Gi       RWX            Retain           Bound     default/mysql-pv-claim                            17m
persistentvolume/wp-pv      5Gi        RWX            Retain           Bound     default/wp-pvc                                    2h

NAME                                   STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/mysql-pv-claim   Bound     mysql-pv   20Gi       RWX                           17m
persistentvolumeclaim/wp-pvc           Bound     wp-pv      5Gi        RWX                           2h

檢測節點:(保證所有節點都是running)

kubectl get pod -o wide

檢測nginx servers暴露出來的埠:

kubectl get svc

然後根據nginxserver暴露出來的埠測試nginx頁面是否能訪問
http://192.168.1.40:49665/
利用phpinfo測試php解析

[[email protected] wp-wwwroot]# cat test.php
<?php phpinfo(); ?>

部署wordpress

此處我們將nfs掛載的共享路徑為:/opt/container_data/wp-wwwroot/
我們將wordpress放在此處:
資料庫選擇:wordpress ,主機名為:wordpress-mysql,賬戶密碼選擇:root 123456