演示环境: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命名空间
[root@k8s-01 ~]# kubectl create ns wordpress
为mysql创建一个pvc存储
这里我已经有sc了,所以我直接创建pvc即可
[root@k8s-01 wordpress]# cat mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
namespace: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
storageClassName: nfs-storage
创建mysql-pvc
[root@k8s-01 wordpress]# kubectl apply -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pvc created
[root@k8s-01 wordpress]# kubectl get pvc -n wordpress|grep mysql-pvc
mysql-pvc Bound pvc-7812e224-b4fb-4a36-be2c-0fc36e161ddc 50Gi RWO nfs-storage 18s
[root@k8s-01 wordpress]#
首先先创建mysql deployment服务
[root@k8s-01 wordpress]# cat mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: wordpress
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
args:
- --default_authentication_plugin=mysql_native_password
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
env:
- name: MYSQL_ROOT_PASSWORD
value: MYSQL_ROOT_PASSWORD #mysql密码,自行修改
ports:
- containerPort: 3306
volumeMounts:
- mountPath: "/var/lib/mysql"
name: mysql-data
livenessProbe:
exec:
command:
- sh
- '-c'
- 'mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}'
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc #创建mysql-pvc
进入mysql容器,开启connection_control.so
插件
如果不开启,后面mysql日志会有大量警告日志
[root@k8s-01 wordpress]# kubectl exec -it -n wordpress mysql-7fddbb85bb-xzcmf bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-4.4# mysql -uroot -p
#下面执行sql语句
mysql> INSTALL PLUGIN CONNECTION_CONTROL SONAME 'connection_control.so';
Query OK, 0 rows affected (0.00 sec)
mysql> INSTALL PLUGIN CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS SONAME 'connection_control.so';
Query OK, 0 rows affected (0.00 sec)
mysql-svc yaml文件如下
[root@k8s-01 wordpress]# cat mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: wordpress
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306
nodePort: 31306
type: NodePort
创建mysql-svc
[root@k8s-01 wordpress]# vim mysql-svc.yaml
[root@k8s-01 wordpress]# kubectl apply -f mysql-svc.yaml
service/mysql created
[root@k8s-01 wordpress]#
[root@k8s-01 wordpress]# kubectl get svc -n wordpress |grep mysql
mysql NodePort 10.102.12.169 <none> 3306:31306/TCP 6s
创建WordPress服务
接下来运行WordPress容器
为WordPress创建一个pvc
[root@k8s-01 wordpress]# cat wp-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pvc
namespace: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
storageClassName: nfs-storage
接下来我们要到数据库中,创建mysql 对应库和用户数据
CREATE DATABASE abcdocker DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
create user 'i4t'@'%' identified by 'abcdocker';
grant all privileges on *.* to 'i4t'@'%';
#这里可以根据需求自行创建
#我这里的数据库名称为abcdocker,用户为i4t
因为wordpress是php文件,后面可能会调整php的配置,我们需要为wordpress-deplpoyment创建一个configmap,用于php参数的调整
[root@k8s-01 wordpress]# cat wordpress-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: wordpress-configmap
namespace: wordpress
data:
uploads.ini: |
file_uploads = On
memory_limit 128M
upload_max_filesize 64M
post_max_size 64M
max_execution_time 300
max_input_time 300
WORDPRESS_CONFIG_EXTRA: |
define('WP_MEMORY_LIMIT', '64M');
@ini_set( 'upload_max_size', '64M' );
@ini_set( 'post_max_size', '64M');
@ini_set( 'max_execution_time', '300' );
WordPress-deployment文件如下
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: wordpress
name: wordpress-deployment
namespace: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
annotations:
labels:
app: wordpress
spec:
containers:
- env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_NAME
value: i4t
- name: WORDPRESS_DB_USER
value: i4t
- name: WORDPRESS_DB_PASSWORD
value: MTZmYTU0Nz
image: wordpress:6.2-apache
imagePullPolicy: IfNotPresent
name: wordpress
ports:
- containerPort: 80
name: wordpress
protocol: TCP
volumeMounts:
- mountPath: /var/www/html
name: wordpress-persistent-storage
- mountPath: /usr/local/etc/php/conf.d/uploads.ini
name: wordpress-configmap
subPath: uploads.ini
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pvc
- configMap:
defaultMode: 420
name: wordpress-configmap
name: wordpress-configmap
请自行修改数据库的DB配置文件,根据需求修改
#相关变量,自行修改
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_NAME
value: i4t
- name: WORDPRESS_DB_USER
value: i4t
- name: WORDPRESS_DB_PASSWORD
value: MTZmYTU0Nz
svc 文件如下
[root@k8s-01 wordpress]# cat wordpress-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-svc
namespace: wordpress
spec:
selector:
app: wordpress
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 31307
type: NodePort
接下来我们检查所有的服务启动状态
[root@k8s-01 wordpress]# kubectl get pod,svc -n wordpress
NAME READY STATUS RESTARTS AGE
pod/mysql-7fddbb85bb-xzcmf 1/1 Running 0 46m
pod/wordpress-deployment-5665566b9b-cm82w 1/1 Running 0 4m42s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql NodePort 10.102.12.169 <none> 3306:31306/TCP 19m
service/wordpress-svc NodePort 10.98.207.64 <none> 80:31307/TCP 68s
[root@k8s-01 wordpress]#
[root@k8s-01 wordpress]#
[root@k8s-01 wordpress]# curl 10.98.207.64 -I
HTTP/1.1 302 Found
Date: Wed, 09 Aug 2023 08:00:13 GMT
Server: Apache/2.4.56 (Debian)
X-Powered-By: PHP/8.0.29
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
X-Redirect-By: WordPress
Location: http://10.98.207.64/wp-admin/install.php
Content-Type: text/html; charset=UTF-8
使用nodeport访问测试
我这里直接访问nodeport ip:端口测试
启动Centos容器
因为默认的WordPress容器没有vim,我们的站点目录都是挂载进去的。如果后期修改php文件比较麻烦,所以我们安装一个Centos client挂载wp-pvc,后续可以直接在Centos容器修改
[root@k8s-01 wordpress]# cat centos.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: centos
name: centos-client
namespace: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: centos
template:
metadata:
labels:
app: centos
spec:
containers:
- args:
- while true; do sleep 30; done;
command:
- /bin/bash
- '-c'
- '--'
image: centos:centos7.9.2009
imagePullPolicy: IfNotPresent
name: centos
securityContext:
privileged: true
volumeMounts:
- mountPath: /data
name: wordpress-persistent-storage
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pvc
迁移Mysql
我们从原来的服务器备份好Mysql数据,然后到新的服务器中导入进去
导入前可以drop database [业务库],把之前WordPress数据删除掉
#k8s节点安装mysql命令
[root@k8s-01 tmp]# yum install -y mysql
#找到sql文件
[root@k8s-01 tmp]# ls
i4t_20230809_163531.sql systemd-private-ecb644fe53cf473385f7a9ba14bea147-chronyd.service-ylF0Of test.sh vmware-root_733-4248680474
#查看mysql svc ip
[root@k8s-01 tmp]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql NodePort 10.102.12.169 <none> 3306:31306/TCP 129m
wordpress-svc NodePort 10.98.207.64 <none> 80:31307/TCP 111m
#登陆到mysql中
[root@k8s-01 tmp]# mysql -uroot -p -h 10.102.12.169 -P3306
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MySQL connection id is 1044
Server version: 8.0.34 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)]> use i4t;
Database changed
MySQL [i4t]> show tables;
Empty set (0.00 sec)
#写入数据
MySQL [i4t]> source /tmp/i4t_20230809_163531.sql;
写入完毕后
MySQL [i4t]> show tables;
...
| abc1_watu_master |
| abc1_watu_question |
| abc1_watu_takings |
| abc1_wpmailsmtp_debug_events |
| abc1_wpmailsmtp_tasks_meta |
| abc1_yarpp_related_cache |
| abc1_zrz_card |
| abc1_zrz_directmessage |
| abc1_zrz_invitation |
| abc1_zrz_message |
| abc1_zrz_order |
+-----------------------------+
49 rows in set (0.00 sec)
修改域名,需要及时修改域名,否则系统会跳转为正式环境
- 旧域名https://i4t.com
- 新域名http://i4t.cn
UPDATE wp_options SET option_value = replace(option_value, 'https://i4t.com', 'http://i4t.cn') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'https://i4t.com','http:// .newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'https://i4t.com', 'http://i4t.cn');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://i4t.com','http://i4t.cn');
UPDATE wp_usermeta SET meta_value = replace(meta_value, 'https://i4t.com', 'http://i4t.cn');
UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'https://i4t.com', 'http://i4t.cn');
UPDATE wp_comments SET comment_author_url = REPLACE (comment_author_url, 'https://i4t.com','http://i4t.cn');
迁移主题及静态文件
主题静态文件、插件、附件实际上都在我们wp-content
目录下,直接将这个目录打包,然后替换到容器上就可以了。
#拷贝名称为web_i4t.com_20230809_013207.tar.gz 附件
kubectl cp web_i4t.com_20230809_013207.tar.gz -n wordpress centos-client-75f686d587-lv2b2:/data/
kubectl exec -it -n wordpress centos-client-75f686d587-lv2b2 bash
最终迁移访问效果图