经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Kubernetes » 查看文章
k8s实战案例之运行WordPress - Linux-1874
来源:cnblogs  作者:Linux-1874  时间:2023/8/21 8:57:33  对本文有异议

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、准备PHP镜像

2.1.1、php镜像目录文件

2.1.2、构建php镜像Dockerfile

  1. root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php# cat Dockerfile
  2. #PHP Base Image
  3. FROM harbor.ik8s.cc/baseimages/magedu-centos-base:7.9.2009
  4. 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
  5. ADD www.conf /opt/remi/php56/root/etc/php-fpm.d/www.conf
  6. #RUN useradd nginx -u 2019
  7. ADD run_php.sh /usr/local/bin/run_php.sh
  8. EXPOSE 9000
  9. CMD ["/usr/local/bin/run_php.sh"]
  10. root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php#

2.1.3、运行php镜像脚本

  1. root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php# cat run_php.sh
  2. #!/bin/bash
  3. #echo "nameserver 10.20.254.254" > /etc/resolv.conf
  4. /opt/remi/php56/root/usr/sbin/php-fpm
  5. #/opt/remi/php56/root/usr/sbin/php-fpm --nodaemonize
  6. tail -f /etc/hosts
  7. root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/php#

2.1.4、构建php镜像脚本

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

2.1.5、构建php镜像

2.2、准备nginx-wordpress镜像

2.2.1、nginx-wordpress镜像目录文件

2.2.2、构建nginx-wordpress镜像Dockerfile

  1. root@k8s-master01:~/k8s-data/dockerfile/web/magedu/wordpress/nginx# cat Dockerfile
  2. FROM harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.22.0
  3. ADD nginx.conf /apps/nginx/conf/nginx.conf
  4. ADD run_nginx.sh /apps/nginx/sbin/run_nginx.sh
  5. RUN mkdir -pv /home/nginx/wordpress
  6. RUN chown nginx.nginx /home/nginx/wordpress/ -R
  7. EXPOSE 80 443
  8. CMD ["/apps/nginx/sbin/run_nginx.sh"]
  9. 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

  1. root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress# cat Dockerfile
  2. #Nginx Base Image
  3. FROM harbor.ik8s.cc/baseimages/magedu-centos-base:7.9.2009
  4. 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
  5. ADD nginx-1.22.0.tar.gz /usr/local/src/
  6. 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
  7. root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress#

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

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

  1. root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress# cat build-command.sh
  2. #!/bin/bash
  3. #docker build -t harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.20.2 .
  4. #sleep 1
  5. #docker push harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.20.2
  6. nerdctl build -t harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.22.0 .
  7. nerdctl push harbor.ik8s.cc/pub-images/nginx-base-wordpress:v1.22.0
  8. root@k8s-master01:~/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress#

2.2.2.4、构建nginx-base-wordpress镜像

2.2.3、运行nginx-wordpress镜像脚本

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

2.2.4、构建nginx-wordpress镜像脚本

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

2.2.5、构建nginx-wordpress镜像

3、运行WordPress站点

3.1、在k8s上运行wordpress的配置清单

  1. kind: Deployment
  2. #apiVersion: extensions/v1beta1
  3. apiVersion: apps/v1
  4. metadata:
  5. labels:
  6. app: wordpress-app
  7. name: wordpress-app-deployment
  8. namespace: magedu
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: wordpress-app
  14. template:
  15. metadata:
  16. labels:
  17. app: wordpress-app
  18. spec:
  19. containers:
  20. - name: wordpress-app-nginx
  21. image: harbor.ik8s.cc/magedu/wordpress-nginx:v1
  22. imagePullPolicy: Always
  23. ports:
  24. - containerPort: 80
  25. protocol: TCP
  26. name: http
  27. - containerPort: 443
  28. protocol: TCP
  29. name: https
  30. volumeMounts:
  31. - name: wordpress
  32. mountPath: /home/nginx/wordpress
  33. readOnly: false
  34. - name: wordpress-app-php
  35. image: harbor.ik8s.cc/magedu/wordpress-php-5.6:v1
  36. #imagePullPolicy: IfNotPresent
  37. imagePullPolicy: Always
  38. ports:
  39. - containerPort: 9000
  40. protocol: TCP
  41. name: http
  42. volumeMounts:
  43. - name: wordpress
  44. mountPath: /home/nginx/wordpress
  45. readOnly: false
  46. volumes:
  47. - name: wordpress
  48. nfs:
  49. server: 192.168.0.42
  50. path: /data/k8sdata/magedu/wordpress
  51. ---
  52. kind: Service
  53. apiVersion: v1
  54. metadata:
  55. labels:
  56. app: wordpress-app
  57. name: wordpress-app-spec
  58. namespace: magedu
  59. spec:
  60. type: NodePort
  61. ports:
  62. - name: http
  63. port: 80
  64. protocol: TCP
  65. targetPort: 80
  66. nodePort: 30031
  67. - name: https
  68. port: 443
  69. protocol: TCP
  70. targetPort: 443
  71. nodePort: 30033
  72. selector:
  73. app: wordpress-app

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

3.2、在nfs服务器准备存放wordpress代码文件目录

  1. root@harbor:~# tail -1 /etc/exports
  2. /data/k8sdata/magedu/wordpress *(rw,no_root_squash)
  3. root@harbor:~# mkdir -pv /data/k8sdata/magedu/wordpress
  4. mkdir: created directory '/data/k8sdata/magedu/wordpress'
  5. root@harbor:~# exportfs -av
  6. exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/kuboard".
  7. Assuming default behaviour ('no_subtree_check').
  8. NOTE: this default has changed since nfs-utils version 1.0.x
  9. exportfs: /etc/exports [2]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/volumes".
  10. Assuming default behaviour ('no_subtree_check').
  11. NOTE: this default has changed since nfs-utils version 1.0.x
  12. exportfs: /etc/exports [3]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/pod-vol".
  13. Assuming default behaviour ('no_subtree_check').
  14. NOTE: this default has changed since nfs-utils version 1.0.x
  15. exportfs: /etc/exports [4]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/myserver".
  16. Assuming default behaviour ('no_subtree_check').
  17. NOTE: this default has changed since nfs-utils version 1.0.x
  18. exportfs: /etc/exports [5]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/mysite".
  19. Assuming default behaviour ('no_subtree_check').
  20. NOTE: this default has changed since nfs-utils version 1.0.x
  21. exportfs: /etc/exports [7]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/images".
  22. Assuming default behaviour ('no_subtree_check').
  23. NOTE: this default has changed since nfs-utils version 1.0.x
  24. exportfs: /etc/exports [8]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/static".
  25. Assuming default behaviour ('no_subtree_check').
  26. NOTE: this default has changed since nfs-utils version 1.0.x
  27. exportfs: /etc/exports [11]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-1".
  28. Assuming default behaviour ('no_subtree_check').
  29. NOTE: this default has changed since nfs-utils version 1.0.x
  30. exportfs: /etc/exports [12]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-2".
  31. Assuming default behaviour ('no_subtree_check').
  32. NOTE: this default has changed since nfs-utils version 1.0.x
  33. exportfs: /etc/exports [13]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-3".
  34. Assuming default behaviour ('no_subtree_check').
  35. NOTE: this default has changed since nfs-utils version 1.0.x
  36. exportfs: /etc/exports [16]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis-datadir-1".
  37. Assuming default behaviour ('no_subtree_check').
  38. NOTE: this default has changed since nfs-utils version 1.0.x
  39. exportfs: /etc/exports [18]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis0".
  40. Assuming default behaviour ('no_subtree_check').
  41. NOTE: this default has changed since nfs-utils version 1.0.x
  42. exportfs: /etc/exports [19]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis1".
  43. Assuming default behaviour ('no_subtree_check').
  44. NOTE: this default has changed since nfs-utils version 1.0.x
  45. exportfs: /etc/exports [20]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis2".
  46. Assuming default behaviour ('no_subtree_check').
  47. NOTE: this default has changed since nfs-utils version 1.0.x
  48. exportfs: /etc/exports [21]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis3".
  49. Assuming default behaviour ('no_subtree_check').
  50. NOTE: this default has changed since nfs-utils version 1.0.x
  51. exportfs: /etc/exports [22]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis4".
  52. Assuming default behaviour ('no_subtree_check').
  53. NOTE: this default has changed since nfs-utils version 1.0.x
  54. exportfs: /etc/exports [23]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/redis5".
  55. Assuming default behaviour ('no_subtree_check').
  56. NOTE: this default has changed since nfs-utils version 1.0.x
  57. exportfs: /etc/exports [27]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-1".
  58. Assuming default behaviour ('no_subtree_check').
  59. NOTE: this default has changed since nfs-utils version 1.0.x
  60. exportfs: /etc/exports [28]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-2".
  61. Assuming default behaviour ('no_subtree_check').
  62. NOTE: this default has changed since nfs-utils version 1.0.x
  63. exportfs: /etc/exports [29]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-3".
  64. Assuming default behaviour ('no_subtree_check').
  65. NOTE: this default has changed since nfs-utils version 1.0.x
  66. exportfs: /etc/exports [30]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-4".
  67. Assuming default behaviour ('no_subtree_check').
  68. NOTE: this default has changed since nfs-utils version 1.0.x
  69. exportfs: /etc/exports [31]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/mysql-datadir-5".
  70. Assuming default behaviour ('no_subtree_check').
  71. NOTE: this default has changed since nfs-utils version 1.0.x
  72. exportfs: /etc/exports [34]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/jenkins-data".
  73. Assuming default behaviour ('no_subtree_check').
  74. NOTE: this default has changed since nfs-utils version 1.0.x
  75. exportfs: /etc/exports [35]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/jenkins-root-data".
  76. Assuming default behaviour ('no_subtree_check').
  77. NOTE: this default has changed since nfs-utils version 1.0.x
  78. exportfs: /etc/exports [37]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/k8sdata/magedu/wordpress".
  79. Assuming default behaviour ('no_subtree_check').
  80. NOTE: this default has changed since nfs-utils version 1.0.x
  81. exporting *:/data/k8sdata/magedu/wordpress
  82. exporting *:/data/k8sdata/magedu/jenkins-root-data
  83. exporting *:/data/k8sdata/magedu/jenkins-data
  84. exporting *:/data/k8sdata/magedu/mysql-datadir-5
  85. exporting *:/data/k8sdata/magedu/mysql-datadir-4
  86. exporting *:/data/k8sdata/magedu/mysql-datadir-3
  87. exporting *:/data/k8sdata/magedu/mysql-datadir-2
  88. exporting *:/data/k8sdata/magedu/mysql-datadir-1
  89. exporting *:/data/k8sdata/magedu/redis5
  90. exporting *:/data/k8sdata/magedu/redis4
  91. exporting *:/data/k8sdata/magedu/redis3
  92. exporting *:/data/k8sdata/magedu/redis2
  93. exporting *:/data/k8sdata/magedu/redis1
  94. exporting *:/data/k8sdata/magedu/redis0
  95. exporting *:/data/k8sdata/magedu/redis-datadir-1
  96. exporting *:/data/k8sdata/magedu/zookeeper-datadir-3
  97. exporting *:/data/k8sdata/magedu/zookeeper-datadir-2
  98. exporting *:/data/k8sdata/magedu/zookeeper-datadir-1
  99. exporting *:/data/k8sdata/magedu/static
  100. exporting *:/data/k8sdata/magedu/images
  101. exporting *:/data/k8sdata/mysite
  102. exporting *:/data/k8sdata/myserver
  103. exporting *:/pod-vol
  104. exporting *:/data/volumes
  105. exporting *:/data/k8sdata/kuboard
  106. root@harbor:~#

3.3、运行WordPress

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

3.4、创建PHP测试页

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

3.5、访问PHP测试页

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

4、初始化WordPress站点

4.1、k8s中MySQL创建数据库

  1. root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl exec -it mysql-0 bash -n magedu
  2. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  3. Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)
  4. root@mysql-0:/# mysql
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 905
  7. Server version: 5.7.36-log MySQL Community Server (GPL)
  8. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> CREATE DATABASE wordpress;
  14. Query OK, 1 row affected (0.02 sec)
  15. mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"%" IDENTIFIED BY
  16. -> "wordpress";
  17. Query OK, 0 rows affected, 1 warning (0.01 sec)
  18. mysql> show databases;
  19. +------------------------+
  20. | Database |
  21. +------------------------+
  22. | information_schema |
  23. | mydb |
  24. | mysql |
  25. | performance_schema |
  26. | sys |
  27. | wordpress |
  28. | xtrabackup_backupfiles |
  29. +------------------------+
  30. 7 rows in set (0.02 sec)
  31. mysql>

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

4.2、k8s中测试MySQL连接

  1. root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get svc -n magedu
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. magedu-consumer-server NodePort 10.100.208.121 <none> 80:49630/TCP 8d
  4. magedu-dubboadmin-service NodePort 10.100.244.92 <none> 80:31080/TCP 8d
  5. magedu-provider-spec NodePort 10.100.187.168 <none> 80:44873/TCP 8d
  6. mysql ClusterIP None <none> 3306/TCP 65d
  7. mysql-read ClusterIP 10.100.15.127 <none> 3306/TCP 65d
  8. redis ClusterIP None <none> 6379/TCP 74d
  9. redis-access NodePort 10.100.117.185 <none> 6379:36379/TCP 74d
  10. wordpress-app-spec NodePort 10.100.189.214 <none> 80:30031/TCP,443:30033/TCP 25m
  11. zookeeper ClusterIP 10.100.237.95 <none> 2181/TCP 76d
  12. zookeeper1 NodePort 10.100.63.118 <none> 2181:32181/TCP,2888:30541/TCP,3888:31200/TCP 76d
  13. zookeeper2 NodePort 10.100.199.43 <none> 2181:32182/TCP,2888:32670/TCP,3888:32264/TCP 76d
  14. zookeeper3 NodePort 10.100.41.9 <none> 2181:32183/TCP,2888:31329/TCP,3888:32546/TCP 76d
  15. root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl get pods
  16. NAME READY STATUS RESTARTS AGE
  17. bash 1/1 Running 4 (41m ago) 13d
  18. root@k8s-master01:~/k8s-data/yaml/magedu/wordpress# kubectl exec -it bash bash
  19. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  20. [root@bash ~]# mysql -uwordpress -pwordpress -hmysql.magedu
  21. Welcome to the MariaDB monitor. Commands end with ; or \g.
  22. Your MySQL connection id is 1502
  23. Server version: 5.7.36 MySQL Community Server (GPL)
  24. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  25. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  26. MySQL [(none)]> show databases;
  27. +--------------------+
  28. | Database |
  29. +--------------------+
  30. | information_schema |
  31. | wordpress |
  32. +--------------------+
  33. 2 rows in set (0.01 sec)
  34. MySQL [(none)]>

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

4.3、在nfs服务器上上传wordpress代码

  1. root@harbor:~# wget https://cn.wordpress.org/wordpress-5.6.10-zh_CN.tar.gz
  2. --2023-08-19 17:00:28-- https://cn.wordpress.org/wordpress-5.6.10-zh_CN.tar.gz
  3. Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.253
  4. Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.253|:443... connected.
  5. HTTP request sent, awaiting response... 200 OK
  6. Length: 16316134 (16M) [application/octet-stream]
  7. Saving to: wordpress-5.6.10-zh_CN.tar.gz
  8. wordpress-5.6.10-zh_CN.tar.gz 100%[========================================================================>] 15.56M 2.14MB/s in 8.7s
  9. 2023-08-19 17:00:38 (1.79 MB/s) - wordpress-5.6.10-zh_CN.tar.gz saved [16316134/16316134]
  10. root@harbor:~# ll
  11. total 635096
  12. drwx------ 8 root root 4096 Aug 19 17:00 ./
  13. drwxr-xr-x 22 root root 4096 May 10 13:57 ../
  14. -rw------- 1 root root 8360 Aug 6 07:33 .bash_history
  15. -rw-r--r-- 1 root root 3106 Oct 15 2021 .bashrc
  16. drwx------ 3 root root 4096 Apr 20 16:45 .cache/
  17. drwx------ 3 root root 4096 Jun 5 17:55 .config/
  18. drwx------ 2 root root 4096 Apr 22 07:21 .docker/
  19. -rw------- 1 root root 20 Jun 6 14:44 .lesshst
  20. -rw-r--r-- 1 root root 161 Jul 9 2019 .profile
  21. drwx------ 2 root root 4096 Apr 20 16:41 .ssh/
  22. -rw------- 1 root root 9366 Aug 19 16:33 .viminfo
  23. -rw-r--r-- 1 root root 633942863 Apr 17 19:00 harbor-offline-installer-v2.8.0.tgz
  24. drwxr-xr-x 2 root root 4096 Aug 6 03:18 jenkins-data/
  25. drwxr-xr-x 2 root root 4096 Aug 6 03:18 jenkins-root-data/
  26. -rw-r--r-- 1 root root 16316134 Oct 18 2022 wordpress-5.6.10-zh_CN.tar.gz
  27. root@harbor:~# tar -xf wordpress-5.6.10-zh_CN.tar.gz -C /data/k8sdata/magedu/
  28. root@harbor:~# ll /data/k8sdata/magedu/wordpress/
  29. total 228
  30. drwxr-xr-x 5 1006 1006 4096 Oct 18 2022 ./
  31. drwxr-xr-x 22 root root 4096 Aug 19 16:26 ../
  32. -rw-r--r-- 1 1006 1006 405 Feb 6 2020 index.php
  33. -rw-r--r-- 1 1006 1006 19915 Oct 18 2022 license.txt
  34. -rw-r--r-- 1 1006 1006 7278 Oct 18 2022 readme.html
  35. -rw-r--r-- 1 root root 20 Aug 19 16:33 test.php
  36. -rw-r--r-- 1 1006 1006 7101 Jul 28 2020 wp-activate.php
  37. drwxr-xr-x 9 1006 1006 4096 Oct 18 2022 wp-admin/
  38. -rw-r--r-- 1 1006 1006 351 Feb 6 2020 wp-blog-header.php
  39. -rw-r--r-- 1 1006 1006 2328 Oct 8 2020 wp-comments-post.php
  40. -rw-r--r-- 1 1006 1006 2913 Oct 18 2022 wp-config-sample.php
  41. drwxr-xr-x 5 1006 1006 4096 Oct 18 2022 wp-content/
  42. -rw-r--r-- 1 1006 1006 3939 Jul 30 2020 wp-cron.php
  43. drwxr-xr-x 25 1006 1006 12288 Oct 18 2022 wp-includes/
  44. -rw-r--r-- 1 1006 1006 2496 Feb 6 2020 wp-links-opml.php
  45. -rw-r--r-- 1 1006 1006 3300 Feb 6 2020 wp-load.php
  46. -rw-r--r-- 1 1006 1006 49831 Nov 9 2020 wp-login.php
  47. -rw-r--r-- 1 1006 1006 8454 Oct 17 2022 wp-mail.php
  48. -rw-r--r-- 1 1006 1006 20975 Nov 12 2020 wp-settings.php
  49. -rw-r--r-- 1 1006 1006 31337 Sep 30 2020 wp-signup.php
  50. -rw-r--r-- 1 1006 1006 4816 Oct 17 2022 wp-trackback.php
  51. -rw-r--r-- 1 1006 1006 3236 Jun 8 2020 xmlrpc.php
  52. root@harbor:~#

4.4、更改wordpress代码文件权限

4.4.1、查看nginx用户id

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

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

  1. root@harbor:~# ll /data/k8sdata/magedu/wordpress/
  2. total 228
  3. drwxr-xr-x 5 1006 1006 4096 Oct 18 2022 ./
  4. drwxr-xr-x 22 root root 4096 Aug 19 16:26 ../
  5. -rw-r--r-- 1 1006 1006 405 Feb 6 2020 index.php
  6. -rw-r--r-- 1 1006 1006 19915 Oct 18 2022 license.txt
  7. -rw-r--r-- 1 1006 1006 7278 Oct 18 2022 readme.html
  8. -rw-r--r-- 1 root root 20 Aug 19 16:33 test.php
  9. -rw-r--r-- 1 1006 1006 7101 Jul 28 2020 wp-activate.php
  10. drwxr-xr-x 9 1006 1006 4096 Oct 18 2022 wp-admin/
  11. -rw-r--r-- 1 1006 1006 351 Feb 6 2020 wp-blog-header.php
  12. -rw-r--r-- 1 1006 1006 2328 Oct 8 2020 wp-comments-post.php
  13. -rw-r--r-- 1 1006 1006 2913 Oct 18 2022 wp-config-sample.php
  14. drwxr-xr-x 5 1006 1006 4096 Oct 18 2022 wp-content/
  15. -rw-r--r-- 1 1006 1006 3939 Jul 30 2020 wp-cron.php
  16. drwxr-xr-x 25 1006 1006 12288 Oct 18 2022 wp-includes/
  17. -rw-r--r-- 1 1006 1006 2496 Feb 6 2020 wp-links-opml.php
  18. -rw-r--r-- 1 1006 1006 3300 Feb 6 2020 wp-load.php
  19. -rw-r--r-- 1 1006 1006 49831 Nov 9 2020 wp-login.php
  20. -rw-r--r-- 1 1006 1006 8454 Oct 17 2022 wp-mail.php
  21. -rw-r--r-- 1 1006 1006 20975 Nov 12 2020 wp-settings.php
  22. -rw-r--r-- 1 1006 1006 31337 Sep 30 2020 wp-signup.php
  23. -rw-r--r-- 1 1006 1006 4816 Oct 17 2022 wp-trackback.php
  24. -rw-r--r-- 1 1006 1006 3236 Jun 8 2020 xmlrpc.php
  25. root@harbor:~# chown 2088.2088 /data/k8sdata/magedu/wordpress/ -R
  26. root@harbor:~# ll /data/k8sdata/magedu/wordpress/
  27. total 228
  28. drwxr-xr-x 5 2088 2088 4096 Oct 18 2022 ./
  29. drwxr-xr-x 22 root root 4096 Aug 19 16:26 ../
  30. -rw-r--r-- 1 2088 2088 405 Feb 6 2020 index.php
  31. -rw-r--r-- 1 2088 2088 19915 Oct 18 2022 license.txt
  32. -rw-r--r-- 1 2088 2088 7278 Oct 18 2022 readme.html
  33. -rw-r--r-- 1 2088 2088 20 Aug 19 16:33 test.php
  34. -rw-r--r-- 1 2088 2088 7101 Jul 28 2020 wp-activate.php
  35. drwxr-xr-x 9 2088 2088 4096 Oct 18 2022 wp-admin/
  36. -rw-r--r-- 1 2088 2088 351 Feb 6 2020 wp-blog-header.php
  37. -rw-r--r-- 1 2088 2088 2328 Oct 8 2020 wp-comments-post.php
  38. -rw-r--r-- 1 2088 2088 2913 Oct 18 2022 wp-config-sample.php
  39. drwxr-xr-x 5 2088 2088 4096 Oct 18 2022 wp-content/
  40. -rw-r--r-- 1 2088 2088 3939 Jul 30 2020 wp-cron.php
  41. drwxr-xr-x 25 2088 2088 12288 Oct 18 2022 wp-includes/
  42. -rw-r--r-- 1 2088 2088 2496 Feb 6 2020 wp-links-opml.php
  43. -rw-r--r-- 1 2088 2088 3300 Feb 6 2020 wp-load.php
  44. -rw-r--r-- 1 2088 2088 49831 Nov 9 2020 wp-login.php
  45. -rw-r--r-- 1 2088 2088 8454 Oct 17 2022 wp-mail.php
  46. -rw-r--r-- 1 2088 2088 20975 Nov 12 2020 wp-settings.php
  47. -rw-r--r-- 1 2088 2088 31337 Sep 30 2020 wp-signup.php
  48. -rw-r--r-- 1 2088 2088 4816 Oct 17 2022 wp-trackback.php
  49. -rw-r--r-- 1 2088 2088 3236 Jun 8 2020 xmlrpc.php
  50. root@harbor:~#

4.5、通过web界面初始化数据库


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

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

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

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


5、验证k8s中MySQL数据

5.1、验证master数据

5.2、验证slave数据

原文链接:https://www.cnblogs.com/qiuhom-1874/p/17601063.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号