请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
搜索

本文来自

边缘计算专区

边缘计算专区

人已关注

请添加对本版块的简短描述

在kubernetes集群中部署nginx+mysql+php应用

[复制链接]
930 abc 发表于 2018-12-15 05:50:35
  1. 本文将介绍在kubernetes环境中部署一套php应用系统。前端web采用nginx、中间件php以fastcgi的方式运行,后台数据库由mysql主从提供支撑。
  2. 各服务组件之间的调用采用dns解析服务名的方式进行,数据和配置文件持久化采用hostPath。

  3. 一、通过dockerfile创建php镜像文件

  4. # cat dockerfile
  5. FROM docker.io/openshift/base-centos7:latest
  6. MAINTAINER feiyu "akwangj@126.com"
  7. RUN yum makecache
  8. RUN yum -y install php-fpm php php-gd php-mysql php-mbstring php-xml php-mcrypt  php-imap php-odbc php-pear php-xmlrpc  
  9. RUN sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /etc/php-fpm.d/www.conf
  10. RUN sed -i 's/listen.allowed_clients = 127.0.0.1/;listen.allowed_clients = 127.0.0.1/' /etc/php-fpm.d/www.conf

  11. EXPOSE 9000
  12. CMD ["/sbin/php-fpm"]

  13. # docker build -t php:0.1 .
  14. 二、部署php

  15. # cat php-deployment.yaml
  16. apiVersion: apps/v1
  17. kind: Deployment
  18. metadata:
  19.   name: php-server
  20.   labels:
  21.     name: php-server
  22. spec:
  23.   replicas: 1
  24.   selector:
  25.     matchLabels:
  26.       app: php-server
  27.   template:
  28.     metadata:
  29.       labels:
  30.         app: php-server
  31.     spec:
  32.       containers:
  33.       - name: php-server
  34.         image: php:0.1
  35.         volumeMounts:
  36.         - mountPath: /var/www/html/
  37.           name: nginx-data
  38.         ports:
  39.         - containerPort: 9000
  40.       volumes:
  41.       - name: nginx-data
  42.         hostPath:
  43.          path: /root/k8s/nmp/html
  44. # cat php-svc.yaml   
  45. apiVersion: v1
  46. kind: Service
  47. metadata:
  48.   name: php
  49. spec:
  50.   ports:
  51.   - name: php
  52.     port: 9000
  53.     protocol: TCP
  54.     targetPort: 9000
  55.   selector:
  56.     app: php-server
  57. kubectl apply -f php-deploy.yaml -f php-svc.yaml

  58. 三、部署nginx

  59. cat nginx-deploy.yaml
  60. apiVersion: apps/v1
  61. kind: Deployment
  62. metadata:
  63.   name: nginx-php
  64. spec:
  65.   selector:
  66.     matchLabels:
  67.       app: nginx-php
  68.   replicas: 1
  69.   template:
  70.     metadata:
  71.       labels:
  72.         app: nginx-php
  73.     spec:
  74.       containers:
  75.       - name: nginx-php
  76.         image: nginx:latest
  77.         ports:
  78.         - containerPort: 80
  79.         volumeMounts:
  80.         - name: nginx-data
  81.           mountPath: /usr/share/nginx/html
  82.         - name: nginx-conf
  83.           mountPath: /etc/nginx/conf.d/
  84.       volumes:
  85.       - name: nginx-data
  86.         hostPath:
  87.          path: /root/k8s/nmp/html
  88.       - name: nginx-conf
  89.         hostPath:
  90.          path: /root/k8s/nmp/conf

  91. cat nginx-svc.yaml
  92. apiVersion: v1
  93. kind: Service
  94. metadata:
  95.   name: nginx-php
  96. spec:
  97.   type: NodePort
  98.   ports:
  99.   - name: nginx
  100.     port: 80
  101.     protocol: TCP
  102.     targetPort: 80
  103.     nodePort: 30003
  104.   selector:
  105.     app: nginx-php
  106. nginx配置文件

  107. # cat /root/k8s/nmp/conf/default.conf
  108. server {
  109.     listen       80;
  110.     server_name  localhost;

  111.     #charset koi8-r;
  112.     #access_log  /var/log/nginx/host.access.log  main;

  113.     location / {
  114.         root   /usr/share/nginx/html;
  115.         index  index.html index.htm;
  116.     }

  117.     #error_page  404              /404.html;

  118.     # redirect server error pages to the static page /50x.html
  119.     #
  120.     error_page   500 502 503 504  /50x.html;
  121.     location = /50x.html {
  122.         root   /usr/share/nginx/html;
  123.     }

  124.     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  125.     #
  126.     #location ~ \.php$ {
  127.     #    proxy_pass   http://127.0.0.1;
  128.     #}

  129.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  130.     #
  131.     location ~ \.php$ {
  132.     #    root           html;
  133.         fastcgi_pass   php:9000;
  134.         fastcgi_index  index.php;
  135.         fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
  136.         include        fastcgi_params;
  137.     }

  138.     # deny access to .htaccess files, if Apache's document root
  139.     # concurs with nginx's one
  140.     #
  141.     #location ~ /\.ht {
  142.     #    deny  all;
  143.     #}
  144. }
  145. kubectl apply -f nginx-deploy.yaml -f nginx-svc.yaml
  146. 网页访问phpinfo页面测试


  147. 四、部署mysql

  148. vi mysql-deployment.yaml
  149. apiVersion: apps/v1
  150. kind: Deployment
  151. metadata:
  152.   name: php-mysql
  153.   labels:
  154.     app: mysql
  155. spec:
  156.   replicas: 2
  157.   selector:
  158.     matchLabels:
  159.       app: mysql
  160.   template:
  161.     metadata:
  162.       labels:
  163.         app: mysql
  164.     spec:
  165.       containers:
  166.       - image: mysql:5.7
  167.         name: mysql
  168.         env:
  169.         - name: MYSQL_ROOT_PASSWORD
  170.           valueFrom:
  171.             secretKeyRef:
  172.             #kubectl create secret generic mysql-pass --from-literal=password=Passwd123
  173.               name: mysql-pass
  174.               key: password
  175.         ports:
  176.         - containerPort: 3306
  177.           name: mysql
  178.         volumeMounts:
  179.         - name: mysql-persistent-storage
  180.           mountPath: /var/lib/mysql
  181.       volumes:
  182.       - name: mysql-persistent-storage
  183.         persistentVolumeClaim:
  184.           claimName: mysql-claim
  185. vi mysql-svc.yaml
  186. apiVersion: v1
  187. kind: Service
  188. metadata:
  189.   name: mysql
  190.   labels:
  191.     app: mysql
  192. spec:
  193.   ports:
  194.     - port: 3306
  195.       targetPort: 3306
  196.   selector:
  197.     app: mysql
  198. mysql>  grant all privileges on *.* to 'root'@'%' identified by 'Passwd123';
  199. Query OK, 0 rows affected, 1 warning (0.05 sec)

  200. mysql> flush privileges;
  201. Query OK, 0 rows affected (0.04 sec)



  202. 五、下载Discuz应用部署,测试php和mysql主从的连通性
  203. 下载地址:http://www.discuz.net/thread-3796882-1-1.html
  204. ---------------------
  205. 作者:菲宇
  206. 来源:CSDN
  207. 原文:https://blog.csdn.net/bbwangj/article/details/82954187
  208. 版权声明:本文为博主原创文章,转载请附上博文链接!
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表