k8s部署wordpress

nginx

nginx.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /apps/nginx/wordpress;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
            root           /apps/nginx/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}

Dockerfile

FROM nginx:1.23.3

ADD nginx.conf /etc/nginx/conf.d/default.conf
RUN mkdir -pv /apps/nginx/wordpress
RUN chown nginx.nginx /apps/nginx/wordpress/ -R

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off"] 

build_nginx_image

# docker build -t  uhub.service.ucloud.cn/wgs-test/nginx:1.23.3 .
Sending build context to Docker daemon  3.584kB
Step 1/4 : FROM nginx:1.23.3
 ---> 3f8a00f137a0
Step 2/4 : ADD nginx.conf /etc/nginx/conf.d/default.conf
 ---> Using cache
 ---> 369485e3e7b4
Step 3/4 : RUN mkdir -pv /apps/nginx/wordpress
 ---> Using cache
 ---> e4409511b7ae
Step 4/4 : RUN chown nginx.nginx /apps/nginx/wordpress/ -R
 ---> Using cache
 ---> 5879b0d8d576
Successfully built 5879b0d8d576
Successfully tagged uhub.service.ucloud.cn/wgs-test/nginx:1.23.3

验证nginx

# docker run -ti --rm  -p 80:80 --name nginx-test uhub.service.ucloud.cn/wgs-test/nginx:1.23.3
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/02/22 08:42:17 [notice] 1#1: using the "epoll" event method
2023/02/22 08:42:17 [notice] 1#1: nginx/1.23.3
2023/02/22 08:42:17 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/02/22 08:42:17 [notice] 1#1: OS: Linux 5.4.0-128-generic
2023/02/22 08:42:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/02/22 08:42:17 [notice] 1#1: start worker processes
2023/02/22 08:42:17 [notice] 1#1: start worker process 28
2023/02/22 08:42:17 [notice] 1#1: start worker process 29
2023/02/22 08:42:17 [notice] 1#1: start worker process 30
2023/02/22 08:42:17 [notice] 1#1: start worker process 31

php

Dockerfile

FROM php:7.4.33-fpm 

RUN sed -i 's@user = www-data@user = nginx@' /usr/local/etc/php-fpm.d/www.conf \
    && sed -i 's@group = www-data@group = nginx@' /usr/local/etc/php-fpm.d/www.conf \
    && useradd nginx -u 101 \
    && mkdir -pv /apps/nginx/wordpress \
    && docker-php-ext-install mysqli \
    && php -m

build_php_image

# docker build -t  uhub.service.ucloud.cn/wgs-test/wordpress-php:7.4.33-fpm .

验证php

# docker run -it --rm uhub.service.ucloud.cn/wgs-test/wordpress-php:7.4.33-fpm
[22-Feb-2023 08:54:13] NOTICE: fpm is running, pid 1
[22-Feb-2023 08:54:13] NOTICE: ready to handle connections

部署wordpress代码

下载wordpress

# wget -O wordpress-zh_CN-6.1.1.tar.gz  https://cn.wordpress.org/latest-zh_CN.tar.gz

解压wordpress

# tar xf wordpress-zh_CN-6.1.1.tar.gz 

复制wordpress代码

# mvv wordpress /data/k8s-data/

修改wordpress代码文件权限

# chown -R 101.101 /data/k8s-data/wordpress/

wordpress环境

namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: wgs-wordpress

persistentvolume.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: wordpress-datadir-pv
  namespace: wgs-wordpress
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 192.168.174.102
    path: /data/k8s-data/wordpress

persistentvolumeclaim.yaml

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-datadir-pvc
  namespace: wgs-wordpress
spec:
  volumeName: wordpress-datadir-pv
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

wordpress.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-deployment
  namespace: wgs-wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress-app
  template:
    metadata:
      labels:
        app: wordpress-app
    spec:
      containers:
      - name: wordpress-app-nginx
        image: uhub.service.ucloud.cn/wgs-test/nginx:1.23.3 
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        - containerPort: 443
          protocol: TCP
          name: https
        volumeMounts:
        - name: wordpress
          mountPath: /apps/nginx/wordpress
          readOnly: false

      - name: wordpress-app-php
        image: uhub.service.ucloud.cn/wgs-test/wordpress-php:7.4.33-fpm
        imagePullPolicy: Always
        ports:
        - containerPort: 9000
          protocol: TCP
          name: http
        volumeMounts:
        - name: wordpress
          mountPath: /apps/nginx/wordpress
          readOnly: false

      volumes:
        - name: wordpress
          persistentVolumeClaim:
            claimName: wordpress-datadir-pvc 


---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-spec
  namespace: wgs-wordpress
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30031
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
    nodePort: 30033
  selector:
    app: wordpress-app

创建资源

# kubectl apply -f .

查看资源

查看pv资源

# kubectl get pv -n wgs-wordpress
NAME                   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                 STORAGECLASS   REASON   AGE
wordpress-datadir-pv   5Gi        RWO            Retain           Bound    wgs-wordpress/wordpress-datadir-pvc                           4m53s

查看pvc资源

# kubectl get pvc -n wgs-wordpress
NAME                    STATUS   VOLUME                 CAPACITY   ACCESS MODES   STORAGECLASS   AGE
wordpress-datadir-pvc   Bound    wordpress-datadir-pv   5Gi        RWO                           5m25s

查看pod资源

# kubectl get pod -n wgs-wordpress -o wide
NAME                                        READY   STATUS    RESTARTS   AGE   IP              NODE              NOMINATED NODE   READINESS GATES
wordpress-app-deployment-795f54d944-p8hrt   2/2     Running   0          25s   172.20.89.172   192.168.174.108   <none>           <none>

测试wordpress换环境

测试nginx

添加index.html

# echo "test nginx" > /data/k8s-data/wordpress/index.html

访问nginx

# curl http://192.168.174.108:30031
test nginx

测试php

添加index.php

<?php
    phpinfo();
?>

访问nginx

设置wordpress

访问wordpress

访问wordpress文章

 

posted @ 2023-02-23 17:38  小吉猫  阅读(147)  评论(0编辑  收藏  举报