1. 程式人生 > >K8s之實踐Pod搭建LNMP

K8s之實踐Pod搭建LNMP

 

K8s之實踐Pod搭建LNMP

1、同一pod下的nginx+php+mysql

nginx+php+mysql.yaml檔案
---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
type: Qpaque
data:
#echo -n "Passwd123" |base64
  password: UGFzc3dkMTIz
#echo -n "tmp" |base64
  database: dG1w
#echo -n "123" |base64
  passwd: MTIz
 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-php-mysql
spec:
  selector:
    matchLabels:
      app: nginx-php-mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-php-mysql
    spec:
      containers:
      - name: php
        image: registry.cn-shenzhen.aliyuncs.com/user-sum/php
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: nginx-data
          mountPath: /var/www/html/
 
      - name: nginx
        image: registry.cn-shenzhen.aliyuncs.com/user-sum/alpine:nginx1.18.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d/
 
      - image: mysql:5.7
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=password=Passwd123
              name: mysql-pass
              key: password
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=database=tmp
              name: mysql-pass
              key: database
        - name: MYSQL_USER
          value: sun
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              #kubectl create secret generic mysql-pass --from-literal=passwd=123
              name: mysql-pass
              key: passwd
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
 
      volumes:
      - name: nginx-data
        nfs:
         server: 192.168.10.10  #nfs server伺服器IP
         path: "/www"
#kubectl create cm ngin-conf --from-file=/conf/default.conf
      - name: nginx-conf
        configMap:
         name: ngin-conf
         items:
         - key: default.conf
           path: add.conf
      - name: mysql-persistent-storage
        emptyDir: {}
 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-php-mysql
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30010
  selector:
    app: nginx-php-mysql
  master安裝nfs(注意work節點也要安裝nfs並啟動)
# yum -y install nfs-utils
# systemctl start nfs
# echo "/www    192.168.10.0/24(rw,sync,no_root_squash)" >> /etc/exports
# systemctl restart nfs

 

# echo "test" > /www/index.html
# cat /www/index.php
<?php
  phpinfo();
?>

 

  配置檔案default.conf # cat /conf/default.conf  
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

# kubectl create cm ngin-conf --from-file=/conf/default.conf

 

注意:採用configMap的掛載方式,如果修改了檔案,需要刪除configMap並重新建立 建立:kubectl create cm ngin-conf --from-file=/conf/default.conf 檢視:kubectl describe cm 刪除:kubectl delete configmaps ngin-conf cm=configmaps(簡稱)  
# kubectl apply -f nginx+php+mysql.yaml

 

  測試:
[root@master-test www]# curl 192.168.10.10:30010
test
[root@master-test www]# curl 192.168.10.10:30010/index.php -I
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 04 Nov 2020 02:50:30 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16

 

  修改vim /www/index.php
<h1>Test php-mysql </h1>
<?php
  mysqli_connect('127.0.0.1','sun','123') or die('failed');
  echo 'success';
?>

 

再次訪問,success成功啦
[root@master-test www]# curl 192.168.10.10:30010/index.php -i
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 04 Nov 2020 02:54:15 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.16
 
<h1>Test php-mysql </h1>
success[root@master-test www]#

 

   

2、nginx+php同一個Pod、mysql另一個Pod

  nginx+php.yaml檔案
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-php
spec:
  selector:
    matchLabels:
      app: nginx-php
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-php
    spec:
      containers:
      - name: php
        image: registry.cn-shenzhen.aliyuncs.com/user-sum/php
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: nginx-data
          mountPath: /var/www/html/
 
      - name: nginx
        image: registry.cn-shenzhen.aliyuncs.com/user-sum/alpine:nginx1.18.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d/
 
      volumes:
      - name: nginx-data
        nfs:
         server: 192.168.10.10  #nfs server伺服器IP
         path: "/www"
      - name: nginx-conf
        configMap:
         name: ngin-conf
         items:
         - key: default.conf
           path: add.conf
 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-php
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30010
  selector:
    app: nginx-php

 

  配置檔案default.conf(和1一樣,不用修改) # cat /conf/default.conf  
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

master安裝nfs(注意work節點也要安裝nfs並啟動)
# yum -y install nfs-utils
# systemctl start nfs
# echo "/www    192.168.10.0/24(rw,sync,no_root_squash)" >> /etc/exports
# systemctl restart nfs

 

# echo "test" > /www/index.html
# cat /www/index.php
<?php
  phpinfo();
?>
 
# kubectl apply -f nginx+php.yaml

 

  mysql.yaml
---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
type: Qpaque
data:
#echo -n "Passwd123" |base64
  password: UGFzc3dkMTIz
#echo -n "tmp" |base64
  database: dG1w
#echo -n "123" |base64
  passwd: MTIz
 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=password=Passwd123
              name: mysql-pass
              key: password
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=database=tmp
              name: mysql-pass
              key: database
        - name: MYSQL_USER
          value: sun
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              #kubectl create secret generic mysql-pass --from-literal=passwd=123
              name: mysql-pass
              key: passwd
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
    targetPort: 3306
  selector:
    app: mysql

 

 

# kubectl apply -f mysql.yaml

 

    測試: 修改vim /www/index.php
<h1>Test php-mysql </h1>
<?php
  mysqli_connect('10.106.159.184','sun','123') or die('failed');
  echo 'success';
?>

 

再次訪問,success成功啦
[root@master-test ~]# curl 192.168.10.10:30010/index.php -i
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 04 Nov 2020 03:40:58 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.16
 
<h1>Test php-mysql </h1>
success[root@master-test ~]#

 

 

3、php一個Pod、nginx一個Pod、mysql一個Pod

  php.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php
spec:
  selector:
    matchLabels:
      app: php
  replicas: 1
  template:
    metadata:
      labels:
        app: php
    spec:
      containers:
      - name: php
        image: registry.cn-shenzhen.aliyuncs.com/user-sum/php
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: nginx-data
          mountPath: /var/www/html/
 
      volumes:
      - name: nginx-data
        nfs:
         server: 192.168.10.10  #nfs server伺服器IP
         path: "/www"
 
---
apiVersion: v1
kind: Service
metadata:
  name: php
spec:
  ports:
  - name: php
    port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    app: php
# kubectl aapply -f php.yml
 
nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: registry.cn-shenzhen.aliyuncs.com/user-sum/alpine:nginx1.18.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d/
      volumes:
      - name: nginx-data
        nfs:
         server: 192.168.10.10  #nfs server伺服器IP
         path: "/www"
      - name: nginx-conf
        configMap:
         name: ngin-conf
         items:
         - key: default.conf
           path: add.conf
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30010
  selector:
    app: nginx

 

  配置檔案nginx.conf   # cat /conf/default.conf  
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   10.108.129.142:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

刪除:kubectl delete configmaps ngin-conf 重新建立:kubectl create cm ngin-conf --from-file=/conf/default.conf  
# kubectl apply -f nginx.yaml

 

  mysql.yaml
 ---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
  namespace: default
type: Qpaque
data:
#echo -n "Passwd123" |base64
  password: UGFzc3dkMTIz
#echo -n "tmp" |base64
  database: dG1w
#echo -n "123" |base64
  passwd: MTIz
 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=password=Passwd123
              name: mysql-pass
              key: password
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=database=tmp
              name: mysql-pass
              key: database
        - name: MYSQL_USER
          value: sun
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              #kubectl create secret generic mysql-pass --from-literal=passwd=123
              name: mysql-pass
              key: passwd
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
    targetPort: 3306
  selector:
    app: mysql

 

# kubectl apply -f mysql.yaml

 

      測試: 修改vim /www/index.php
<h1>Test php-mysql </h1>
<?php
  mysqli_connect('10.109.228.202','sun','123') or die('failed');
  echo 'success';
#  phpinfo();
?>

 

再次訪問,success成功啦
[root@master-test ~]# curl 192.168.10.10:30010/index.php -i
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 04 Nov 2020 05:24:57 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.16
 
<h1>Test php-mysql </h1>
success[root@master-test ~]#

 

  &nbs