k8s实战案例之运行WordPress
阅读原文时间:2023年08月20日阅读:9

1、WordPress架构

LNMP案例之基于Nginx+PHP实现WordPress博客站点,要求Nginx+PHP运⾏在同⼀个Pod的不同容器;nginx主要作用是接入站点请求,如果请求静态资源nginx就直接响应;如果请求的是一个动态php资源,就将对应请求转发给另一个php容器进行处理;在一个pod中运行多容器,网络名称空间是共享的,所以nginx可以将对应请求转发至127.0.0.1:9000这个端口来调用php来处理对应php请求;pod中如果有数据产生,pod通过pvc/pv将对应数据存储到远端存储上;客户端访问通过防火墙,负载均衡器将请求调度到后端node上处理,如果请求所在节点没有运行对应pod,那么对应node会根据路由来转发请求,最终会将请求转发给pod所在节点进行处理;

2、镜像准备

2.1.1、php镜像目录文件

2.1.2、构建php镜像Dockerfile

root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php# cat Dockerfile
#PHP Base Image
FROM harbor.ik8s.cc/baseimages/magedu-centos-base:7.9.2009 

RUN yum install -y  https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm && yum install  php56-php-fpm php56-php-mysql -y
ADD www.conf /opt/remi/php56/root/etc/php-fpm.d/www.conf
#RUN useradd nginx -u 2019
ADD run_php.sh /usr/local/bin/run_php.sh
EXPOSE 9000

CMD ["/usr/local/bin/run_php.sh"]
root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php#

2.1.3、运行php镜像脚本

root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php# cat run_php.sh
#!/bin/bash
#echo "nameserver 10.20.254.254" > /etc/resolv.conf

/opt/remi/php56/root/usr/sbin/php-fpm
#/opt/remi/php56/root/usr/sbin/php-fpm --nodaemonize
tail -f /etc/hosts
root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php# 

2.1.4、构建php镜像脚本

root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php# cat build-command.sh
#!/bin/bash
TAG=$1
#docker build -t harbor.ik8s.cc/magedu/wordpress-php-5.6:${TAG} .
nerdctl build -t harbor.ik8s.cc/magedu/wordpress-php-5.6:${TAG} .
echo "镜像制作完成,即将上传至Harbor服务器"
sleep 1
nerdctl push harbor.ik8s.cc/magedu/wordpress-php-5.6:${TAG}
echo "镜像上传完成"
root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php#

2.1.5、构建php镜像

2.2.1、nginx-wordpress镜像目录文件

2.2.2、构建nginx-wordpress镜像Dockerfile

root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx# cat Dockerfile
FROM harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.22.0 

ADD nginx.conf /apps/nginx/conf/nginx.conf
ADD run_nginx.sh /apps/nginx/sbin/run_nginx.sh
RUN mkdir -pv /home/nginx/wordpress
RUN chown nginx.nginx /home/nginx/wordpress/ -R

EXPOSE 80 443

CMD ["/apps/nginx/sbin/run_nginx.sh"]
root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx#

该镜像依赖nginx-base-wordpress镜像,在nginx-base-wordpress镜像基础上添加nginx的配置文件,添加运行nginx脚本以及创建存放wordpress代码目录以及更改目录权限等;

2.2.2.1、构建依赖镜像nginx-base-wordpress目录文件

2.2.2.2、构建nginx-base-wordpress镜像Dockerfile

root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress# cat Dockerfile
#Nginx Base Image
FROM harbor.ik8s.cc/baseimages/magedu-centos-base:7.9.2009 

RUN yum install -y vim wget tree  lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-1.22.0.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.22.0 && ./configure --prefix=/apps/nginx  && make && make install && ln -sv  /apps/nginx/sbin/nginx /usr/sbin/nginx  &&rm -rf /usr/local/src/nginx-1.22.0.tar.gz
root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress#

该镜像主要做了安装编译环境,放nginx源码包进去编译nginx;

2.2.2.3、构建nginx-base-wordpress镜像脚本

root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress# cat build-command.sh
#!/bin/bash
#docker build -t harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.20.2  .
#sleep 1
#docker push  harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.20.2

nerdctl build -t  harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.22.0  .
nerdctl push harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.22.0
root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress#

2.2.2.4、构建nginx-base-wordpress镜像

2.2.3、运行nginx-wordpress镜像脚本

root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx# cat run_nginx.sh
#!/bin/bash
#echo "nameserver 10.20.254.254" > /etc/resolv.conf
#chown nginx.nginx /home/nginx/wordpress/ -R
/apps/nginx/sbin/nginx
tail -f /etc/hosts
root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx#

2.2.4、构建nginx-wordpress镜像脚本

root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx# cat build-command.sh
#!/bin/bash
TAG=$1
#docker build -t harbor.ik8s.cc/magedu/wordpress-nginx:${TAG} .
nerdctl build -t harbor.ik8s.cc/magedu/wordpress-nginx:${TAG} .
echo "镜像制作完成,即将上传至Harbor服务器"
sleep 1
nerdctl push  harbor.ik8s.cc/magedu/wordpress-nginx:${TAG}
echo "镜像上传完成"
root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx#

2.2.5、构建nginx-wordpress镜像

3、运行WordPress站点

kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress-app
  template:
    metadata:
      labels:
        app: wordpress-app
    spec:
      containers:
      - name: wordpress-app-nginx
        image: harbor.ik8s.cc/magedu/wordpress-nginx:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        - containerPort: 443
          protocol: TCP
          name: https
        volumeMounts:
        - name: wordpress
          mountPath: /home/nginx/wordpress
          readOnly: false

      - name: wordpress-app-php
        image: harbor.ik8s.cc/magedu/wordpress-php-5.6:v1
        #imagePullPolicy: IfNotPresent
        imagePullPolicy: Always
        ports:
        - containerPort: 9000
          protocol: TCP
          name: http
        volumeMounts:
        - name: wordpress
          mountPath: /home/nginx/wordpress
          readOnly: false

      volumes:
      - name: wordpress
        nfs:
          server: 192.168.0.42
          path: /data/k8sdata/magedu/wordpress 

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-spec
  namespace: magedu
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

该清单主要描述了用deplopment控制器部署wordpress,wordpress pod中运行nginx和php容器,两个容器共用/home/nginx/wordpress目录,该目录是远端nfs存储上共享出来的目录,通常情况下该目录放置wordpress代码文件;

root@harbor:~# tail -1 /etc/exports
/data/k8sdata/magedu/wordpress *(rw,no_root_squash)
root@harbor:~# mkdir -pv /data/k8sdata/magedu/wordpress
mkdir: created directory '/data/k8sdata/magedu/wordpress'
root@harbor:~# exportfs -av
exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/kuboard".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [2]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/volumes".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [3]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/pod-vol".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [4]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/myserver".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [5]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/mysite".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [7]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/images".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [8]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/static".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [11]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-1".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [12]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-2".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [13]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-3".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [16]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis-datadir-1".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [18]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis0".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [19]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis1".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [20]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis2".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [21]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis3".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [22]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis4".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [23]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis5".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [27]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-1".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [28]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-2".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [29]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-3".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [30]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-4".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [31]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-5".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [34]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/jenkins-data".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [35]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/jenkins-root-data".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exportfs: /etc/exports [37]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/wordpress".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

exporting *:/data/k8sdata/magedu/wordpress
exporting *:/data/k8sdata/magedu/jenkins-root-data
exporting *:/data/k8sdata/magedu/jenkins-data
exporting *:/data/k8sdata/magedu/mysql-datadir-5
exporting *:/data/k8sdata/magedu/mysql-datadir-4
exporting *:/data/k8sdata/magedu/mysql-datadir-3
exporting *:/data/k8sdata/magedu/mysql-datadir-2
exporting *:/data/k8sdata/magedu/mysql-datadir-1
exporting *:/data/k8sdata/magedu/redis5
exporting *:/data/k8sdata/magedu/redis4
exporting *:/data/k8sdata/magedu/redis3
exporting *:/data/k8sdata/magedu/redis2
exporting *:/data/k8sdata/magedu/redis1
exporting *:/data/k8sdata/magedu/redis0
exporting *:/data/k8sdata/magedu/redis-datadir-1
exporting *:/data/k8sdata/magedu/zookeeper-datadir-3
exporting *:/data/k8sdata/magedu/zookeeper-datadir-2
exporting *:/data/k8sdata/magedu/zookeeper-datadir-1
exporting *:/data/k8sdata/magedu/static
exporting *:/data/k8sdata/magedu/images
exporting *:/data/k8sdata/mysite
exporting *:/data/k8sdata/myserver
exporting *:/pod-vol
exporting *:/data/volumes
exporting *:/data/k8sdata/kuboard
root@harbor:~#


root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl apply -f wordpress.yaml
deployment.apps/wordpress-app-deployment created
service/wordpress-app-spec created
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get pods -n magedu
NAME                                            READY   STATUS      RESTARTS       AGE
magedu-consumer-deployment-798c7d785b-fp4b9     1/1     Running     2 (16m ago)    8d
magedu-consumer-deployment-798c7d785b-wmv9p     1/1     Running     2 (16m ago)    8d
magedu-consumer-deployment-798c7d785b-zqm74     1/1     Running     2 (16m ago)    8d
magedu-dubboadmin-deployment-798c4dfdd8-kvfvh   1/1     Running     2 (16m ago)    8d
magedu-provider-deployment-6fccc6d9f5-k6z7m     1/1     Running     2 (16m ago)    8d
magedu-provider-deployment-6fccc6d9f5-nl4zd     1/1     Running     2 (16m ago)    8d
magedu-provider-deployment-6fccc6d9f5-p94rb     1/1     Running     2 (16m ago)    8d
mysql-0                                         2/2     Running     10 (16m ago)   65d
mysql-1                                         2/2     Running     10 (16m ago)   65d
mysql-2                                         2/2     Running     10 (16m ago)   65d
redis-0                                         1/1     Running     7 (16m ago)    74d
redis-1                                         1/1     Running     7 (16m ago)    74d
redis-2                                         1/1     Running     7 (16m ago)    74d
redis-3                                         1/1     Running     7 (16m ago)    74d
redis-4                                         1/1     Running     7 (16m ago)    74d
redis-5                                         1/1     Running     7 (16m ago)    74d
ubuntu1804                                      0/1     Completed   0              74d
wordpress-app-deployment-64c956bf9c-6qp8q       2/2     Running     0              38s
zookeeper1-675c5477cb-vmwwq                     1/1     Running     9 (16m ago)    76d
zookeeper2-759fb6c6f-7jktr                      1/1     Running     9 (16m ago)    76d
zookeeper3-5c78bb5974-vxpbh                     1/1     Running     9 (16m ago)    76d
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get svc -n magedu
NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                        AGE
magedu-consumer-server      NodePort    10.100.208.121   <none>        80:49630/TCP                                   8d
magedu-dubboadmin-service   NodePort    10.100.244.92    <none>        80:31080/TCP                                   8d
magedu-provider-spec        NodePort    10.100.187.168   <none>        80:44873/TCP                                   8d
mysql                       ClusterIP   None             <none>        3306/TCP                                       65d
mysql-read                  ClusterIP   10.100.15.127    <none>        3306/TCP                                       65d
redis                       ClusterIP   None             <none>        6379/TCP                                       74d
redis-access                NodePort    10.100.117.185   <none>        6379:36379/TCP                                 74d
wordpress-app-spec          NodePort    10.100.189.214   <none>        80:30031/TCP,443:30033/TCP                     47s
zookeeper                   ClusterIP   10.100.237.95    <none>        2181/TCP                                       76d
zookeeper1                  NodePort    10.100.63.118    <none>        2181:32181/TCP,2888:30541/TCP,3888:31200/TCP   76d
zookeeper2                  NodePort    10.100.199.43    <none>        2181:32182/TCP,2888:32670/TCP,3888:32264/TCP   76d
zookeeper3                  NodePort    10.100.41.9      <none>        2181:32183/TCP,2888:31329/TCP,3888:32546/TCP   76d
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress#


root@harbor:/data/k8sdata/magedu/wordpress# ll
total 12
drwxr-xr-x  2 root root 4096 Aug 19 16:33 ./
drwxr-xr-x 22 root root 4096 Aug 19 16:26 ../
-rw-r--r--  1 root root   20 Aug 19 16:33 test.php
root@harbor:/data/k8sdata/magedu/wordpress# cat test.php
<?php
phpinfo();
?>
root@harbor:/data/k8sdata/magedu/wordpress#

能够正常访问到php测试页面,说明nginx+php环境准备ok,远端nfs存储挂载ok;

4、初始化WordPress站点

root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl exec -it mysql-0 bash -n magedu
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)
root@mysql-0:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 905
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.02 sec)

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"%" IDENTIFIED BY
    -> "wordpress";
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show databases;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mydb                   |
| mysql                  |
| performance_schema     |
| sys                    |
| wordpress              |
| xtrabackup_backupfiles |
+------------------------+
7 rows in set (0.02 sec)

mysql>

使⽤k8s中运⾏的mysql服务,作为mysql服务器;

root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get svc -n magedu
NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                        AGE
magedu-consumer-server      NodePort    10.100.208.121   <none>        80:49630/TCP                                   8d
magedu-dubboadmin-service   NodePort    10.100.244.92    <none>        80:31080/TCP                                   8d
magedu-provider-spec        NodePort    10.100.187.168   <none>        80:44873/TCP                                   8d
mysql                       ClusterIP   None             <none>        3306/TCP                                       65d
mysql-read                  ClusterIP   10.100.15.127    <none>        3306/TCP                                       65d
redis                       ClusterIP   None             <none>        6379/TCP                                       74d
redis-access                NodePort    10.100.117.185   <none>        6379:36379/TCP                                 74d
wordpress-app-spec          NodePort    10.100.189.214   <none>        80:30031/TCP,443:30033/TCP                     25m
zookeeper                   ClusterIP   10.100.237.95    <none>        2181/TCP                                       76d
zookeeper1                  NodePort    10.100.63.118    <none>        2181:32181/TCP,2888:30541/TCP,3888:31200/TCP   76d
zookeeper2                  NodePort    10.100.199.43    <none>        2181:32182/TCP,2888:32670/TCP,3888:32264/TCP   76d
zookeeper3                  NodePort    10.100.41.9      <none>        2181:32183/TCP,2888:31329/TCP,3888:32546/TCP   76d
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get pods
NAME   READY   STATUS    RESTARTS      AGE
bash   1/1     Running   4 (41m ago)   13d
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl exec -it bash bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@bash ~]# mysql -uwordpress -pwordpress -hmysql.magedu
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1502
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+
2 rows in set (0.01 sec)

MySQL [(none)]>

这里需要注意一点mysql在k8s中是以svc的方式向外提供服务,所以测试我们需要写mysql的svc名称;如果在同一名称空间写mysql svc名称即可,如果跨名称空间需要写mysql svc名称.mysql svc所在名称空间名称,这样coredns才能正常解析mysql svc;

root@harbor:~# wget https://cn.wordpress.org/wordpress-5.6.10-zh_CN.tar.gz
--2023-08-19 17:00:28--  https://cn.wordpress.org/wordpress-5.6.10-zh_CN.tar.gz
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.253
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.253|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16316134 (16M) [application/octet-stream]
Saving to: ‘wordpress-5.6.10-zh_CN.tar.gz’

wordpress-5.6.10-zh_CN.tar.gz         100%[========================================================================>]  15.56M  2.14MB/s    in 8.7s    

2023-08-19 17:00:38 (1.79 MB/s) - ‘wordpress-5.6.10-zh_CN.tar.gz’ saved [16316134/16316134]

root@harbor:~# ll
total 635096
drwx------  8 root root      4096 Aug 19 17:00 ./
drwxr-xr-x 22 root root      4096 May 10 13:57 ../
-rw-------  1 root root      8360 Aug  6 07:33 .bash_history
-rw-r--r--  1 root root      3106 Oct 15  2021 .bashrc
drwx------  3 root root      4096 Apr 20 16:45 .cache/
drwx------  3 root root      4096 Jun  5 17:55 .config/
drwx------  2 root root      4096 Apr 22 07:21 .docker/
-rw-------  1 root root        20 Jun  6 14:44 .lesshst
-rw-r--r--  1 root root       161 Jul  9  2019 .profile
drwx------  2 root root      4096 Apr 20 16:41 .ssh/
-rw-------  1 root root      9366 Aug 19 16:33 .viminfo
-rw-r--r--  1 root root 633942863 Apr 17 19:00 harbor-offline-installer-v2.8.0.tgz
drwxr-xr-x  2 root root      4096 Aug  6 03:18 jenkins-data/
drwxr-xr-x  2 root root      4096 Aug  6 03:18 jenkins-root-data/
-rw-r--r--  1 root root  16316134 Oct 18  2022 wordpress-5.6.10-zh_CN.tar.gz
root@harbor:~# tar -xf wordpress-5.6.10-zh_CN.tar.gz -C /data/k8sdata/magedu/
root@harbor:~# ll /data/k8sdata/magedu/wordpress/
total 228
drwxr-xr-x  5 1006 1006  4096 Oct 18  2022 ./
drwxr-xr-x 22 root root  4096 Aug 19 16:26 ../
-rw-r--r--  1 1006 1006   405 Feb  6  2020 index.php
-rw-r--r--  1 1006 1006 19915 Oct 18  2022 license.txt
-rw-r--r--  1 1006 1006  7278 Oct 18  2022 readme.html
-rw-r--r--  1 root root    20 Aug 19 16:33 test.php
-rw-r--r--  1 1006 1006  7101 Jul 28  2020 wp-activate.php
drwxr-xr-x  9 1006 1006  4096 Oct 18  2022 wp-admin/
-rw-r--r--  1 1006 1006   351 Feb  6  2020 wp-blog-header.php
-rw-r--r--  1 1006 1006  2328 Oct  8  2020 wp-comments-post.php
-rw-r--r--  1 1006 1006  2913 Oct 18  2022 wp-config-sample.php
drwxr-xr-x  5 1006 1006  4096 Oct 18  2022 wp-content/
-rw-r--r--  1 1006 1006  3939 Jul 30  2020 wp-cron.php
drwxr-xr-x 25 1006 1006 12288 Oct 18  2022 wp-includes/
-rw-r--r--  1 1006 1006  2496 Feb  6  2020 wp-links-opml.php
-rw-r--r--  1 1006 1006  3300 Feb  6  2020 wp-load.php
-rw-r--r--  1 1006 1006 49831 Nov  9  2020 wp-login.php
-rw-r--r--  1 1006 1006  8454 Oct 17  2022 wp-mail.php
-rw-r--r--  1 1006 1006 20975 Nov 12  2020 wp-settings.php
-rw-r--r--  1 1006 1006 31337 Sep 30  2020 wp-signup.php
-rw-r--r--  1 1006 1006  4816 Oct 17  2022 wp-trackback.php
-rw-r--r--  1 1006 1006  3236 Jun  8  2020 xmlrpc.php
root@harbor:~#

4.4.1、查看nginx用户id

root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get pods -n magedu
NAME                                            READY   STATUS      RESTARTS       AGE
magedu-consumer-deployment-798c7d785b-fp4b9     1/1     Running     2 (53m ago)    8d
magedu-consumer-deployment-798c7d785b-wmv9p     1/1     Running     2 (53m ago)    8d
magedu-consumer-deployment-798c7d785b-zqm74     1/1     Running     2 (53m ago)    8d
magedu-dubboadmin-deployment-798c4dfdd8-kvfvh   1/1     Running     2 (53m ago)    8d
magedu-provider-deployment-6fccc6d9f5-k6z7m     1/1     Running     2 (53m ago)    8d
magedu-provider-deployment-6fccc6d9f5-nl4zd     1/1     Running     2 (53m ago)    8d
magedu-provider-deployment-6fccc6d9f5-p94rb     1/1     Running     2 (53m ago)    8d
mysql-0                                         2/2     Running     10 (53m ago)   65d
mysql-1                                         2/2     Running     10 (53m ago)   65d
mysql-2                                         2/2     Running     10 (53m ago)   65d
redis-0                                         1/1     Running     7 (53m ago)    74d
redis-1                                         1/1     Running     7 (53m ago)    74d
redis-2                                         1/1     Running     7 (53m ago)    74d
redis-3                                         1/1     Running     7 (53m ago)    74d
redis-4                                         1/1     Running     7 (53m ago)    74d
redis-5                                         1/1     Running     7 (53m ago)    74d
ubuntu1804                                      0/1     Completed   0              74d
wordpress-app-deployment-64c956bf9c-6qp8q       2/2     Running     0              37m
zookeeper1-675c5477cb-vmwwq                     1/1     Running     9 (53m ago)    76d
zookeeper2-759fb6c6f-7jktr                      1/1     Running     9 (53m ago)    76d
zookeeper3-5c78bb5974-vxpbh                     1/1     Running     9 (53m ago)    76d
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl exec -it wordpress-app-deployment-64c956bf9c-6qp8q id nginx -n magedu
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "wordpress-app-nginx" out of: wordpress-app-nginx, wordpress-app-php
uid=2088(nginx) gid=2088(nginx) groups=2088(nginx)
root@k8s-master01:~/k8s-data/yaml/magedu/wordpress#

4.4.2、在nfs服务器上更改wordpress目录权限

root@harbor:~# ll /data/k8sdata/magedu/wordpress/
total 228
drwxr-xr-x  5 1006 1006  4096 Oct 18  2022 ./
drwxr-xr-x 22 root root  4096 Aug 19 16:26 ../
-rw-r--r--  1 1006 1006   405 Feb  6  2020 index.php
-rw-r--r--  1 1006 1006 19915 Oct 18  2022 license.txt
-rw-r--r--  1 1006 1006  7278 Oct 18  2022 readme.html
-rw-r--r--  1 root root    20 Aug 19 16:33 test.php
-rw-r--r--  1 1006 1006  7101 Jul 28  2020 wp-activate.php
drwxr-xr-x  9 1006 1006  4096 Oct 18  2022 wp-admin/
-rw-r--r--  1 1006 1006   351 Feb  6  2020 wp-blog-header.php
-rw-r--r--  1 1006 1006  2328 Oct  8  2020 wp-comments-post.php
-rw-r--r--  1 1006 1006  2913 Oct 18  2022 wp-config-sample.php
drwxr-xr-x  5 1006 1006  4096 Oct 18  2022 wp-content/
-rw-r--r--  1 1006 1006  3939 Jul 30  2020 wp-cron.php
drwxr-xr-x 25 1006 1006 12288 Oct 18  2022 wp-includes/
-rw-r--r--  1 1006 1006  2496 Feb  6  2020 wp-links-opml.php
-rw-r--r--  1 1006 1006  3300 Feb  6  2020 wp-load.php
-rw-r--r--  1 1006 1006 49831 Nov  9  2020 wp-login.php
-rw-r--r--  1 1006 1006  8454 Oct 17  2022 wp-mail.php
-rw-r--r--  1 1006 1006 20975 Nov 12  2020 wp-settings.php
-rw-r--r--  1 1006 1006 31337 Sep 30  2020 wp-signup.php
-rw-r--r--  1 1006 1006  4816 Oct 17  2022 wp-trackback.php
-rw-r--r--  1 1006 1006  3236 Jun  8  2020 xmlrpc.php
root@harbor:~# chown 2088.2088 /data/k8sdata/magedu/wordpress/ -R
root@harbor:~# ll /data/k8sdata/magedu/wordpress/
total 228
drwxr-xr-x  5 2088 2088  4096 Oct 18  2022 ./
drwxr-xr-x 22 root root  4096 Aug 19 16:26 ../
-rw-r--r--  1 2088 2088   405 Feb  6  2020 index.php
-rw-r--r--  1 2088 2088 19915 Oct 18  2022 license.txt
-rw-r--r--  1 2088 2088  7278 Oct 18  2022 readme.html
-rw-r--r--  1 2088 2088    20 Aug 19 16:33 test.php
-rw-r--r--  1 2088 2088  7101 Jul 28  2020 wp-activate.php
drwxr-xr-x  9 2088 2088  4096 Oct 18  2022 wp-admin/
-rw-r--r--  1 2088 2088   351 Feb  6  2020 wp-blog-header.php
-rw-r--r--  1 2088 2088  2328 Oct  8  2020 wp-comments-post.php
-rw-r--r--  1 2088 2088  2913 Oct 18  2022 wp-config-sample.php
drwxr-xr-x  5 2088 2088  4096 Oct 18  2022 wp-content/
-rw-r--r--  1 2088 2088  3939 Jul 30  2020 wp-cron.php
drwxr-xr-x 25 2088 2088 12288 Oct 18  2022 wp-includes/
-rw-r--r--  1 2088 2088  2496 Feb  6  2020 wp-links-opml.php
-rw-r--r--  1 2088 2088  3300 Feb  6  2020 wp-load.php
-rw-r--r--  1 2088 2088 49831 Nov  9  2020 wp-login.php
-rw-r--r--  1 2088 2088  8454 Oct 17  2022 wp-mail.php
-rw-r--r--  1 2088 2088 20975 Nov 12  2020 wp-settings.php
-rw-r--r--  1 2088 2088 31337 Sep 30  2020 wp-signup.php
-rw-r--r--  1 2088 2088  4816 Oct 17  2022 wp-trackback.php
-rw-r--r--  1 2088 2088  3236 Jun  8  2020 xmlrpc.php
root@harbor:~#

这里的数据库主机需要填写mysql svc名称.名称空间名称;如果你的mysql是主从架构,这里就需要填写主库的svc名称.名称空间;

页面提示不错,这里表示我们写的mysql主机地址正常;点击现在安装即可;

填写好站点名称,用户名密码 点击安装wordpress即可;

能够看到成功,表示wordpress初始化成功;填写我们刚才设置的密码点击登录即可;

5、验证k8s中MySQL数据

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器