前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在kubernetes集群中部署nginx+mysql+php应用

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

作者头像
菲宇
发布2019-06-12 16:49:56
4.3K0
发布2019-06-12 16:49:56
举报
文章被收录于专栏:菲宇菲宇

本文将介绍在kubernetes环境中部署一套php应用系统。前端web采用nginx、中间件php以fastcgi的方式运行,后台数据库由mysql主从提供支撑。 各服务组件之间的调用采用dns解析服务名的方式进行,数据和配置文件持久化采用hostPath。

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

# cat dockerfile 
FROM docker.io/openshift/base-centos7:latest
MAINTAINER feiyu "akwangj@126.com"
RUN yum makecache
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  
RUN sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /etc/php-fpm.d/www.conf
RUN sed -i 's/listen.allowed_clients = 127.0.0.1/;listen.allowed_clients = 127.0.0.1/' /etc/php-fpm.d/www.conf

EXPOSE 9000
CMD ["/sbin/php-fpm"]

# docker build -t php:0.1 .

二、部署php

# cat php-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-server
  labels:
    name: php-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-server
  template:
    metadata:
      labels:
        app: php-server
    spec:
      containers:
      - name: php-server
        image: php:0.1
        volumeMounts:
        - mountPath: /var/www/html/
          name: nginx-data
        ports:
        - containerPort: 9000
      volumes:
      - name: nginx-data
        hostPath:
         path: /root/k8s/nmp/html
# cat php-svc.yaml   
apiVersion: v1
kind: Service
metadata:
  name: php
spec:
  ports:
  - name: php
    port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    app: php-server

kubectl apply -f php-deploy.yaml -f php-svc.yaml

三、部署nginx

cat nginx-deploy.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: nginx-php
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d/
      volumes:
      - name: nginx-data
        hostPath:
         path: /root/k8s/nmp/html
      - name: nginx-conf
        hostPath:
         path: /root/k8s/nmp/conf

cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-php
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30003
  selector:
    app: nginx-php

nginx配置文件

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

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

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

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

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

kubectl apply -f nginx-deploy.yaml -f nginx-svc.yaml 网页访问phpinfo页面测试

四、部署mysql

vi mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-mysql
  labels:
    app: mysql
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
            #kubectl create secret generic mysql-pass --from-literal=password=Passwd123
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-claim
vi mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
    - port: 3306
      targetPort: 3306
  selector:
    app: mysql

mysql> grant all privileges on *.* to 'root'@'%' identified by 'Passwd123'; Query OK, 0 rows affected, 1 warning (0.05 sec)

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

五、下载Discuz应用部署,测试php和mysql主从的连通性 下载地址:http://www.discuz.net/thread-3796882-1-1.html

# cd /root/k8s/nmp/html
# unzip Discuz_X3.3_SC_UTF8.zip 
# mv upload/* ./

通过访问网页进行部署

数据库服务器选择这里是个坑

安装成功,正常访问

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年10月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档