• K8s---Pod搭建LNMP


    方式一:同一pod内部署nginx+php+mysql

    [root@master-test www]## vim 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.184.127  	#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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112

    master安装nfs(注意节点也要安装nfs)
    [root@master-test www] # yum -y install nfs-utils
    [root@master-test www] # echo “/www *(rw,sync,no_root_squash)” >> /etc/exports
    [root@master-test www] # systemctl start rpcbind
    [root@master-test www] # systemctl start nfs
    [root@master-test www] # systemctl enable nfs
    [root@master-test www] # systemctl enable rpcbind
    [root@master-test www] # echo “test” > /www/index.html
    [root@master-test www] # vim /www/index.php

    
    
    • 1
    • 2
    • 3

    添加nginx配置文件default.conf
    [root@master-test www] # vim /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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    [root@master-test www] # 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(简称)

    [root@master-test www]# kubectl apply -f nginx+php+mysql.yaml

    测试:
    [root@master-test www]# curl 192.168.184.127:30010

    test
    
    • 1

    [root@master-test www]# curl 192.168.184.127: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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    修改
    [root@master-test www]#vim /www/index.php

    <h1>Test php-mysql h1>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    再次访问,success成功啦
    [root@master-test www]# curl 192.168.184.127: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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    方式二:nginx+php同一个Pod、mysql另一个Pod

    [root@master-test www]# vim 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.184.127    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    nginx配置文件default.conf(和1一样,不用修改)
    [root@master-test www]##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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    master安装nfs(注意节点也要安装nfs)
    [root@master-test www] # yum -y install nfs-utils
    [root@master-test www] # echo “/www *(rw,sync,no_root_squash)” >> /etc/exports
    [root@master-test www] # systemctl start rpcbind
    [root@master-test www] # systemctl start nfs
    [root@master-test www] # systemctl enable nfs
    [root@master-test www] # systemctl enable rpcbind
    [root@master-test www] # echo “test” > /www/index.html
    [root@master-test www] # vim /www/index.php

    
     
    
    • 1
    • 2
    • 3
    • 4

    [root@master-test www] ## kubectl apply -f nginx+php.yaml

    [root@master-test www]# vim 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    [root@master-test www] # kubectl apply -f mysql.yaml

    测试:
    [root@master-test www]# vim /www/index.php

    <h1>Test php-mysql h1>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    再次访问,success成功啦
    [root@master-test ~]# curl 192.168.184.127: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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    方式三:php一个Pod、nginx一个Pod、mysql一个Pod

    [root@master-test ~]# vim 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.184.127    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    [root@master-test ~]# kubectl aapply -f php.yml

    [root@master-test ~]# vim 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.184.127    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    重新配置文件nginx.con配置文件、链接php的地址需要换成php的clusterIP或者servicename
    [root@master-test ~]## vim /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   php:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    删除:
    [root@master-test ~]# kubectl delete configmaps ngin-conf

    重新创建:
    [root@master-test ~]#kubectl create cm ngin-conf --from-file=/conf/default.conf

    [root@master-test ~]# kubectl apply -f nginx.yaml
    [root@master-test www] # vim 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    [root@master-test ~]# kubectl apply -f mysql.yaml

    [root@master-test ~]# vim /www/index.php

    <h1>Test php-mysql h1>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    再次访问,success成功啦
    [root@master-test ~]# curl 192.168.184.127: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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    this.$set,更新vue视图
    Android Studio的笔记--Module新建和使用
    【Java基础】Java基础知识
    c语言(函数栈帧的创建和销毁)
    ubantu(linux)下安装qt6遇到的问题
    如何排查oracle连接数不足问题
    java与es8实战之一:以builder pattern开篇
    C++之STL中vector的使用
    SpringBoot-JWT生成
    MSDC 4.3 接口规范(4)
  • 原文地址:https://blog.csdn.net/weixin_42272246/article/details/126404363