WordPress 数据迁移到Kubernetes (k8s)并配置持久化及网站域名修改

2023年 8月 10日 58.7k 0

演示环境:http://i4t.cn 会始终保证可访问状态

架构说明

海外Nginx主要是解决域名需要备案的问题,frp为了解决家里Kubernetes集群没有公网IP,应用无法访问的问题。

WordPress镜像已经将Nginx和PHP封装在一起了,所以pod最好是运行2个,防止一个php资源请求处理不过来的问题。相关的插件安装配置最好使用一个Centos镜像用于导入导出

本次相关服务版本

  • Kubernetes 1.24
  • WordPress 6.2
  • Mysql 8

文中使用nfs作为默认sc存储引擎,之间的搭建说过太多次了,这里就是直接使用,不讲sc的创建和nfs的相关 配置

Mysql服务创建

我们先为WordPress创建一个ns命名空间

  1. [root@k8s-01 ~]# kubectl create ns wordpress

为mysql创建一个pvc存储

这里我已经有sc了,所以我直接创建pvc即可

  1. [root@k8s-01 wordpress]# cat mysql-pvc.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: mysql-pvc
  6. namespace: wordpress
  7. spec:
  8. accessModes:
  9. - ReadWriteOnce
  10. resources:
  11. requests:
  12. storage: 50Gi
  13. storageClassName: nfs-storage

创建mysql-pvc

  1. [root@k8s-01 wordpress]# kubectl apply -f mysql-pvc.yaml
  2. persistentvolumeclaim/mysql-pvc created
  3. [root@k8s-01 wordpress]# kubectl get pvc -n wordpress|grep mysql-pvc
  4. mysql-pvc Bound pvc-7812e224-b4fb-4a36-be2c-0fc36e161ddc 50Gi RWO nfs-storage 18s
  5. [root@k8s-01 wordpress]#

首先先创建mysql deployment服务

  1. [root@k8s-01 wordpress]# cat mysql-deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: mysql
  6. namespace: wordpress
  7. labels:
  8. app: mysql
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: mysql
  14. template:
  15. metadata:
  16. labels:
  17. app: mysql
  18. spec:
  19. containers:
  20. - name: mysql
  21. image: mysql:8.0
  22. args:
  23. - --default_authentication_plugin=mysql_native_password
  24. - --character-set-server=utf8mb4
  25. - --collation-server=utf8mb4_unicode_ci
  26. env:
  27. - name: MYSQL_ROOT_PASSWORD
  28. value: MYSQL_ROOT_PASSWORD #mysql密码,自行修改
  29. ports:
  30. - containerPort: 3306
  31. volumeMounts:
  32. - mountPath: "/var/lib/mysql"
  33. name: mysql-data
  34. livenessProbe:
  35. exec:
  36. command:
  37. - sh
  38. - '-c'
  39. - 'mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}'
  40. volumes:
  41. - name: mysql-data
  42. persistentVolumeClaim:
  43. claimName: mysql-pvc #创建mysql-pvc

进入mysql容器,开启connection_control.so插件

如果不开启,后面mysql日志会有大量警告日志

  1. [root@k8s-01 wordpress]# kubectl exec -it -n wordpress mysql-7fddbb85bb-xzcmf bash
  2. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  3. bash-4.4# mysql -uroot -p
  4. #下面执行sql语句
  5. mysql> INSTALL PLUGIN CONNECTION_CONTROL SONAME 'connection_control.so';
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> INSTALL PLUGIN CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS SONAME 'connection_control.so';
  8. Query OK, 0 rows affected (0.00 sec)

mysql-svc yaml文件如下

  1. [root@k8s-01 wordpress]# cat mysql-svc.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: mysql
  6. namespace: wordpress
  7. spec:
  8. selector:
  9. app: mysql
  10. ports:
  11. - protocol: TCP
  12. port: 3306
  13. targetPort: 3306
  14. nodePort: 31306
  15. type: NodePort

创建mysql-svc

  1. [root@k8s-01 wordpress]# vim mysql-svc.yaml
  2. [root@k8s-01 wordpress]# kubectl apply -f mysql-svc.yaml
  3. service/mysql created
  4. [root@k8s-01 wordpress]#
  5. [root@k8s-01 wordpress]# kubectl get svc -n wordpress |grep mysql
  6. mysql NodePort 10.102.12.169 <none> 3306:31306/TCP 6s

创建WordPress服务

接下来运行WordPress容器

为WordPress创建一个pvc

  1. [root@k8s-01 wordpress]# cat wp-pvc.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: wp-pvc
  6. namespace: wordpress
  7. spec:
  8. accessModes:
  9. - ReadWriteOnce
  10. resources:
  11. requests:
  12. storage: 50Gi
  13. storageClassName: nfs-storage

接下来我们要到数据库中,创建mysql 对应库和用户数据

  1. CREATE DATABASE abcdocker DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
  2. create user 'i4t'@'%' identified by 'abcdocker';
  3. grant all privileges on *.* to 'i4t'@'%';
  4. #这里可以根据需求自行创建
  5. #我这里的数据库名称为abcdocker,用户为i4t

因为wordpress是php文件,后面可能会调整php的配置,我们需要为wordpress-deplpoyment创建一个configmap,用于php参数的调整

  1. [root@k8s-01 wordpress]# cat wordpress-configmap.yaml
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: wordpress-configmap
  6. namespace: wordpress
  7. data:
  8. uploads.ini: |
  9. file_uploads = On
  10. memory_limit 128M
  11. upload_max_filesize 64M
  12. post_max_size 64M
  13. max_execution_time 300
  14. max_input_time 300
  15. WORDPRESS_CONFIG_EXTRA: |
  16. define('WP_MEMORY_LIMIT', '64M');
  17. @ini_set( 'upload_max_size', '64M' );
  18. @ini_set( 'post_max_size', '64M');
  19. @ini_set( 'max_execution_time', '300' );

WordPress-deployment文件如下

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: wordpress
  6. name: wordpress-deployment
  7. namespace: wordpress
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: wordpress
  13. template:
  14. metadata:
  15. annotations:
  16. labels:
  17. app: wordpress
  18. spec:
  19. containers:
  20. - env:
  21. - name: WORDPRESS_DB_HOST
  22. value: mysql
  23. - name: WORDPRESS_DB_NAME
  24. value: i4t
  25. - name: WORDPRESS_DB_USER
  26. value: i4t
  27. - name: WORDPRESS_DB_PASSWORD
  28. value: MTZmYTU0Nz
  29. image: wordpress:6.2-apache
  30. imagePullPolicy: IfNotPresent
  31. name: wordpress
  32. ports:
  33. - containerPort: 80
  34. name: wordpress
  35. protocol: TCP
  36. volumeMounts:
  37. - mountPath: /var/www/html
  38. name: wordpress-persistent-storage
  39. - mountPath: /usr/local/etc/php/conf.d/uploads.ini
  40. name: wordpress-configmap
  41. subPath: uploads.ini
  42. volumes:
  43. - name: wordpress-persistent-storage
  44. persistentVolumeClaim:
  45. claimName: wp-pvc
  46. - configMap:
  47. defaultMode: 420
  48. name: wordpress-configmap
  49. name: wordpress-configmap

请自行修改数据库的DB配置文件,根据需求修改

  1. #相关变量,自行修改
  2. - name: WORDPRESS_DB_HOST
  3. value: mysql
  4. - name: WORDPRESS_DB_NAME
  5. value: i4t
  6. - name: WORDPRESS_DB_USER
  7. value: i4t
  8. - name: WORDPRESS_DB_PASSWORD
  9. value: MTZmYTU0Nz

svc 文件如下

  1. [root@k8s-01 wordpress]# cat wordpress-svc.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: wordpress-svc
  6. namespace: wordpress
  7. spec:
  8. selector:
  9. app: wordpress
  10. ports:
  11. - protocol: TCP
  12. port: 80
  13. targetPort: 80
  14. nodePort: 31307
  15. type: NodePort

接下来我们检查所有的服务启动状态

  1. [root@k8s-01 wordpress]# kubectl get pod,svc -n wordpress
  2. NAME READY STATUS RESTARTS AGE
  3. pod/mysql-7fddbb85bb-xzcmf 1/1 Running 0 46m
  4. pod/wordpress-deployment-5665566b9b-cm82w 1/1 Running 0 4m42s
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. service/mysql NodePort 10.102.12.169 <none> 3306:31306/TCP 19m
  7. service/wordpress-svc NodePort 10.98.207.64 <none> 80:31307/TCP 68s
  8. [root@k8s-01 wordpress]#
  9. [root@k8s-01 wordpress]#
  10. [root@k8s-01 wordpress]# curl 10.98.207.64 -I
  11. HTTP/1.1 302 Found
  12. Date: Wed, 09 Aug 2023 08:00:13 GMT
  13. Server: Apache/2.4.56 (Debian)
  14. X-Powered-By: PHP/8.0.29
  15. Expires: Wed, 11 Jan 1984 05:00:00 GMT
  16. Cache-Control: no-cache, must-revalidate, max-age=0
  17. X-Redirect-By: WordPress
  18. Location: http://10.98.207.64/wp-admin/install.php
  19. Content-Type: text/html; charset=UTF-8

使用nodeport访问测试

我这里直接访问nodeport ip:端口测试

启动Centos容器

因为默认的WordPress容器没有vim,我们的站点目录都是挂载进去的。如果后期修改php文件比较麻烦,所以我们安装一个Centos client挂载wp-pvc,后续可以直接在Centos容器修改

  1. [root@k8s-01 wordpress]# cat centos.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. labels:
  6. app: centos
  7. name: centos-client
  8. namespace: wordpress
  9. spec:
  10. replicas: 1
  11. selector:
  12. matchLabels:
  13. app: centos
  14. template:
  15. metadata:
  16. labels:
  17. app: centos
  18. spec:
  19. containers:
  20. - args:
  21. - while true; do sleep 30; done;
  22. command:
  23. - /bin/bash
  24. - '-c'
  25. - '--'
  26. image: centos:centos7.9.2009
  27. imagePullPolicy: IfNotPresent
  28. name: centos
  29. securityContext:
  30. privileged: true
  31. volumeMounts:
  32. - mountPath: /data
  33. name: wordpress-persistent-storage
  34. volumes:
  35. - name: wordpress-persistent-storage
  36. persistentVolumeClaim:
  37. claimName: wp-pvc

迁移Mysql

我们从原来的服务器备份好Mysql数据,然后到新的服务器中导入进去

导入前可以drop database [业务库],把之前WordPress数据删除掉

  1. #k8s节点安装mysql命令
  2. [root@k8s-01 tmp]# yum install -y mysql
  3. #找到sql文件
  4. [root@k8s-01 tmp]# ls
  5. i4t_20230809_163531.sql systemd-private-ecb644fe53cf473385f7a9ba14bea147-chronyd.service-ylF0Of test.sh vmware-root_733-4248680474
  6. #查看mysql svc ip
  7. [root@k8s-01 tmp]# kubectl get svc -n wordpress
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  9. mysql NodePort 10.102.12.169 <none> 3306:31306/TCP 129m
  10. wordpress-svc NodePort 10.98.207.64 <none> 80:31307/TCP 111m
  11. #登陆到mysql中
  12. [root@k8s-01 tmp]# mysql -uroot -p -h 10.102.12.169 -P3306
  13. Enter password:
  14. Welcome to the MariaDB monitor. Commands end with ; or g.
  15. Your MySQL connection id is 1044
  16. Server version: 8.0.34 MySQL Community Server - GPL
  17. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  18. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
  19. MySQL [(none)]> use i4t;
  20. Database changed
  21. MySQL [i4t]> show tables;
  22. Empty set (0.00 sec)
  23. #写入数据
  24. MySQL [i4t]> source /tmp/i4t_20230809_163531.sql;

写入完毕后

  1. MySQL [i4t]> show tables;
  2. ...
  3. | abc1_watu_master |
  4. | abc1_watu_question |
  5. | abc1_watu_takings |
  6. | abc1_wpmailsmtp_debug_events |
  7. | abc1_wpmailsmtp_tasks_meta |
  8. | abc1_yarpp_related_cache |
  9. | abc1_zrz_card |
  10. | abc1_zrz_directmessage |
  11. | abc1_zrz_invitation |
  12. | abc1_zrz_message |
  13. | abc1_zrz_order |
  14. +-----------------------------+
  15. 49 rows in set (0.00 sec)

修改域名,需要及时修改域名,否则系统会跳转为正式环境

  • 旧域名https://i4t.com
  • 新域名http://i4t.cn
  1. UPDATE wp_options SET option_value = replace(option_value, 'https://i4t.com', 'http://i4t.cn') WHERE option_name = 'home' OR option_name = 'siteurl';
  2. UPDATE wp_posts SET guid = replace(guid, 'https://i4t.com','http:// .newurl');
  3. UPDATE wp_posts SET post_content = replace(post_content, 'https://i4t.com', 'http://i4t.cn');
  4. UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://i4t.com','http://i4t.cn');
  5. UPDATE wp_usermeta SET meta_value = replace(meta_value, 'https://i4t.com', 'http://i4t.cn');
  6. UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'https://i4t.com', 'http://i4t.cn');
  7. UPDATE wp_comments SET comment_author_url = REPLACE (comment_author_url, 'https://i4t.com','http://i4t.cn');

迁移主题及静态文件

主题静态文件、插件、附件实际上都在我们wp-content目录下,直接将这个目录打包,然后替换到容器上就可以了。

  1. #拷贝名称为web_i4t.com_20230809_013207.tar.gz 附件
  2. kubectl cp web_i4t.com_20230809_013207.tar.gz -n wordpress centos-client-75f686d587-lv2b2:/data/
  3. kubectl exec -it -n wordpress centos-client-75f686d587-lv2b2 bash

最终迁移访问效果图

相关文章

KubeSphere 部署向量数据库 Milvus 实战指南
探索 Kubernetes 持久化存储之 Longhorn 初窥门径
征服 Docker 镜像访问限制!KubeSphere v3.4.1 成功部署全攻略
那些年在 Terraform 上吃到的糖和踩过的坑
无需 Kubernetes 测试 Kubernetes 网络实现
Kubernetes v1.31 中的移除和主要变更

发布评论