8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

1.制作Nginx镜像

1.1 使用nginx:1.21.1官方镜像

# 下载官方镜像
docker pull nginx:1.21.1

# 打本地harbor的tag
docker tag 192.168.1.110/base/nginx:1.21.1

# 上传到本地harbor仓库
docker push 192.168.1.110/base/nginx:1.21.1

1.2 编写nginx配置文件的configmap的yaml并创建

k8s部署nginx时,在deployment yaml中使用configmap的方式挂载使用nginx配置文件

# nginx-configmap.yaml
cat nginx-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: yan-test
data:
 default: |
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /var/www/html;
            index  index.html index.htm index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/www/html;
        }

        location ~ \.php$ {
            root           /var/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
    }

# 创建
kubectl apply -f nginx-configmap.yaml

# 查看
# -n yan-test: namespace为yan-test
kubectl get configmaps -n yan-test
NAME               DATA   AGE
nginx-config       1      4d17h

2.制作PHP镜像

基础镜像使用Ubuntu20.04,打tag后上传到本地harbor仓库

2.1 制作镜像使用的文件列表

-rw-r--r-- 1 root root  4427 11月  4 11:40 php-fpm.conf
-rw-r--r-- 1 root root 74562 11月  4 10:44 php.ini
-rwxr-xr-x 1 root root    85 11月  5 18:39 run_php-fpm.sh*
-rw-r--r-- 1 root root   387 11月  5 16:33 sources.list
-rw-r--r-- 1 root root 18589 11月  4 11:54 www.conf

2.2 PHP镜像Dockerfile

FROM 192.168.1.110/base/ubuntu:20.04

COPY sources.list /etc/apt
COPY run_php-fpm.sh /

RUN apt update && \
    DEBIAN_FRONTEND=noninteractive apt-get install software-properties-common -y && \
    add-apt-repository ppa:ondrej/php -y && \
    apt install --no-install-recommends --no-install-suggests -y inetutils-ping vim curl procps net-tools software-properties-common php5.6-fpm php5.6-mys
ql php5.6-curl php5.6-gd php5.6-mbstring php5.6-mcrypt php5.6-xml php5.6-xmlrpc php5.6-zip php5.6-opcache

COPY php-fpm.conf /etc/php/5.6/fpm/php-fpm.conf
COPY www.conf /etc/php/5.6/fpm/pool.d/www.conf
COPY php.ini /etc/php/5.6/fpm/php.ini

CMD ["/run_php-fpm.sh"]

2.3 PHP其他配置文件

# php-fpm.conf
注释以下行,否则启动php会提示找不到pid文件
#pid = /run/php/php5.6-fpm.pid

# php.ini
为了网站安全,将一下参数设置为0(关闭)
cgi.fix_pathinfo=0

# www.conf
在www模块开启9000端口监听
[www]
listen = 0.0.0.0:9000

2.4 run_php-fpm.sh PHP启动脚本

#!/bin/bash
/usr/sbin/php-fpm5.6 -y /etc/php/5.6/fpm/php-fpm.conf
tail -f /etc/hosts

2.5 构建镜像并上传到本地harbor

docker build -t 192.168.1.110/web/php:2021-11-5_1853 .

docker push 192.168.1.110/web/php:2021-11-5_1853

3.部署StatefulSet mysql一主多从集群

3.1部署

参考之前的另一篇博客:https://www.cnblogs.com/yanql/p/15440022.html
mysql-0主库容器的地址为:mysql-0.mysql.yan-test.svc.fx.local
yan-test: namespace
fx: k8s cluste name

3.2 为wordpress网站创建数据库,用户和用户密码。

mysql > CREATE DATABASE wordpress;

mysql > GRANT ALL PRIVILEGES ON wordpress.* TO 'worpress'@'%' IDENTIFIED BY 'wordpress';

4.部署WordPress

4.1 编写k8s的nginx,PHP的svc,deployment yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-php
  namespace: yan-test
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
      nodePort: 30080
  selector:
    app: nginx-php
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  namespace: yan-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-php
  template:
    metadata:
      labels:
        app: nginx-php
    spec:
      containers:
        - name: nginx-ct
          image: 192.168.1.110/base/nginx:1.21.1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              protocol: TCP
              name: http
          resources:
            limits:
              cpu: 2
              memory: 2Gi
            requests:
              cpu: 500m
              memory: 1Gi
          volumeMounts:
          - name: php-html
            mountPath: "/var/www/html"
          - name: nginx-config
            mountPath: "/etc/nginx/conf.d"
        - name: php-ct
          image: 192.168.1.110/web/php:2021-11-5_1853
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 9000
              protocol: TCP
              name: http
          resources:
            limits:
              cpu: 2
              memory: 2Gi
            requests:
              cpu: 500m
              memory: 1Gi
          volumeMounts:
          - name: php-html
            mountPath: "/var/www/html"
      volumes:
        - name: php-html
          nfs: 
            server: 192.168.2.10
            path: /data/k8s-data/nginx/html
        - name: nginx-config
          configMap:
            name: nginx-config
            items:
              - key: default
                path: default.conf

4.2 准备wordpress代码文件

wget https://cn.wordpress.org/wordpress-5.0.1-zh_CN.tar.gz

tar xf wordpress-5.0.1-zh_CN.tar.gz

# 将解压后的wordpress php代码移动到nginx容器中/var/www/html的挂载目录(NFS)
mv wordpress/* /data/k8s-data/nginx/html/

4.3 使用nginx-php.yml部署启动nginx,PHP容器

kubctl apply -f nginx-php.yml

kubectl get pod -n yan-test
NAME                           READY   STATUS    RESTARTS   AGE
mysql-0                        2/2     Running   0          4d18h
mysql-1                        2/2     Running   1          4d18h
mysql-2                        2/2     Running   1          4d18h
nginx-deploy-744c8fb58-4s9tg   2/2     Running   0          15m

4.4 配置代理

nginx:80 --> k8s svc (nginx-php):30080

server {
        listen       80;
        server_name  yan.wd.com;
        charset utf-8;
 
        location / {
                proxy_pass  http://192.168.2.17:30080;
                proxy_set_header    Host   $host;
        }
}

4.5 web页面初始化wordpress

8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

8.4 k8s实现Nginx+Php+WordPress+MySQL实现完全容器化的web站点案例

上一篇:Elasticsearch 查看模板与mapping


下一篇:Vue中为什么使用 + axios详解