目录
前提条件
部署MySQL和WordPress
编写yaml文件
应用yaml文件
存在问题及解决方案
创建PV(持久化卷)
创建一个PVC(持久化卷声明)
部署添加PVC
查看PV对应的主机存储
删除资源
查看资源
删除deployment和service
查看主机数据
删除PVC和PV
删除主机数据
前提条件
- 拥有Kubernetes集群环境,可参考:Kubernetes集群搭建
- 理解Kubernetes部署知识,可参考:使用Kubernetes部署第一个应用、Deloyment控制器
部署MySQL和WordPress
编写yaml文件
部署mysql和wordpress,需要编写对应的deployment,同时需要暴露应用,需要编写对应的service。为了简便这里把这些deployment和service全部编写在同一个yaml中。
[root@k8s-master01 db8]# vi wordpress-mysql.yaml
内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql8
labels:
app: wordpress-mysql8
spec:
replicas: 1
selector:
matchLabels:
app: wordpress-mysql8
template:
metadata:
labels:
app: wordpress-mysql8
spec:
containers:
- name: mysql8
image: registry.cn-hangzhou.aliyuncs.com/my-common-images/mysql:8.4.3
env:
- name: MYSQL_ROOT_PASSWORD
value: "your_mysql_root_password"
- name: MYSQL_DATABASE
value: "wordpress"
- name: MYSQL_USER
value: "wordpress_user"
- name: MYSQL_PASSWORD
value: "your_wordpress_user_password"
ports:
- containerPort: 3306
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql8-service
labels:
app: wordpress-mysql8
spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
selector:
app: wordpress-mysql8
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: registry.cn-hangzhou.aliyuncs.com/my-common-images/wordpress:latest
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql8-service
- name: WORDPRESS_DB_USER
value: "wordpress_user"
- name: WORDPRESS_DB_PASSWORD
value: "your_wordpress_user_password"
- name: WORDPRESS_DB_NAME
value: "wordpress"
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-service
labels:
app: wordpress
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: wordpress
注意:记得将 your_mysql_root_password
和 your_wordpress_user_password
替换为安全的自定义密码,并且根据集群的网络和安全策略进行必要的调整,如配置防火墙以允许外部访问 NodePort
。
应用yaml文件
[root@k8s-master01 db8]# kubectl apply -f wordpress-mysql.yaml
deployment.apps/wordpress-mysql8 created
service/wordpress-mysql8-service created
deployment.apps/wordpress created
service/wordpress-service created
查看部署,查看服务
[root@k8s-master01 db8]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
wordpress 1/1 1 1 12s
wordpress-mysql8 1/1 1 1 12s
[root@k8s-master01 db8]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 78d
wordpress-mysql8-service ClusterIP 10.15.203.40 3306/TCP 4m56s
wordpress-service NodePort 10.10.170.192 80:32201/TCP 4m56s
看到映射的NodePort 端口为32201,每个人查到的不一样,以查到的端口为准
浏览器访问节点ip:32201
选择合适语言,这里使用English为例,点击Continue
根据提示填写信息,测试环境密码建议修改简单些,便于测试。
根据提示输入登录信息,进入WordPress主页,如下
存在问题及解决方案
存在一个问题,MySQL的数据存储在Pod中,如果过程中Pod被删除重建,MySQL的数据也被删除了,可使用PV和PVC来解决这个问题。
创建PV(持久化卷)
Kubernetes 集群中创建一个持久化卷,它代表了集群中的一块存储资源,可以是本地磁盘、网络存储等。以下是一个使用本地磁盘路径创建 PV 的示例,假设你想将数据存储在主机的/root/datatest/mysql
目录下(可以根据实际情况修改这个路径):
创建PV的yaml文件
[root@k8s-master01 db8]# vi mysql-pv.yaml
内容如下
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 10Gi # 根据需求设置存储容量,这里设置为10GB
accessModes:
- ReadWriteOnce
hostPath:
path: /root/datatest/mysql
创建 PV
[root@k8s-master01 db8]# kubectl apply -f mysql-pv.yaml
persistentvolume/mysql-pv created
创建一个PVC(持久化卷声明)
接下来,创建一个持久化卷声明,它用于向 Kubernetes 集群请求所需的存储资源。PVC 会与合适的 PV 进行绑定,以获取实际的存储。
[root@k8s-master01 db8]# vi mysql-pvc.yaml
内容如下
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi # 根据实际需求设置请求的存储容量,这里设置为8GB,应小于等于PV的容量
创建 PVC
[root@k8s-master01 db8]# kubectl apply -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pvc created
部署添加PVC
修改wordpress-mysql.yaml的MySQL Deployment,在spec.template.spec.containers
下的mysql8
容器部分),添加以下内容:
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
修改后的 完整yaml文件内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql8
labels:
app: wordpress-mysql8
spec:
replicas: 1
selector:
matchLabels:
app: wordpress-mysql8
template:
metadata:
labels:
app: wordpress-mysql8
spec:
containers:
- name: mysql8
image: registry.cn-hangzhou.aliyuncs.com/my-common-images/mysql:8.4.3
env:
- name: MYSQL_ROOT_PASSWORD
value: "your_mysql_root_password"
- name: MYSQL_DATABASE
value: "wordpress"
- name: MYSQL_USER
value: "wordpress_user"
- name: MYSQL_PASSWORD
value: "your_wordpress_user_password"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql8-service
labels:
app: wordpress-mysql8
spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
selector:
app: wordpress-mysql8
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: registry.cn-hangzhou.aliyuncs.com/my-common-images/wordpress:latest
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql8-service
- name: WORDPRESS_DB_USER
value: "wordpress_user"
- name: WORDPRESS_DB_PASSWORD
value: "your_wordpress_user_password"
- name: WORDPRESS_DB_NAME
value: "wordpress"
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-service
labels:
app: wordpress
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: wordpress
完成上述修改后,再次执行apply命令来更新 MySQL 的部署,使其使用映射到主机的持久化存储。
[root@k8s-master01 db8]# kubectl apply -f wordpress-mysql.yaml
deployment.apps/wordpress-mysql8 configured
service/wordpress-mysql8-service unchanged
deployment.apps/wordpress unchanged
service/wordpress-service unchanged
重新访问浏览器
节点ip:32201
重新根据提示设置WordPress,进入WordPress主页,能正常访问。
查看PV对应的主机存储
查看pod在哪台主机运行
[root@k8s-master01 db8]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress-6b69d95596-jgbnw 1/1 Running 0 53m 10.244.85.225 k8s-node01
wordpress-mysql8-64c987b8cd-vxqtk 1/1 Running 0 6m59s 10.244.85.230 k8s-node01
看到pod在k8s-node01机器运行,到对应的k8s-node01机器查看pv对应的存储目录
# 在运行pod的机器执行
[root@k8s-node01 ~]# ls /root/datatest/mysql/
‘#ib_16384_0.dblwr’ auto.cnf ca-key.pem ib_buffer_pool mysql.ibd private_key.pem sys
‘#ib_16384_1.dblwr’ binlog.000001 ca.pem ibdata1 mysql.sock public_key.pem undo_001
‘#innodb_redo’ binlog.000002 client-cert.pem ibtmp1 mysql_upgrade_history server-cert.pem undo_002
‘#innodb_temp’ binlog.index client-key.pem mysql performance_schema server-key.pem wordpress
查看到相应的MySQL卷挂载到主机的数据。
删除资源
查看资源
[root@k8s-master01 db8]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
wordpress 1/1 1 1 81m
wordpress-mysql8 1/1 1 1 81m
[root@k8s-master01 db8]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 78d
wordpress-mysql8-service ClusterIP 10.15.203.40 3306/TCP 82m
wordpress-service NodePort 10.10.170.192 80:32201/TCP 82m
[root@k8s-master01 db8]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
mysql-pv 10Gi RWO Retain Bound default/mysql-pvc 62m
mysql-pv-volume 20Gi RWO Retain Bound default/mysql-pv-claim manual 39d
[root@k8s-master01 db8]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
mysql-pv-claim Bound mysql-pv-volume 20Gi RWO manual 39d
mysql-pvc Bound mysql-pv 10Gi RWO 57m
删除deployment和service
[root@k8s-master01 db8]# kubectl delete deploy wordpress wordpress-mysql8
deployment.apps “wordpress” deleted
deployment.apps “wordpress-mysql8” deleted
[root@k8s-master01 db8]# kubectl delete svc wordpress-mysql8-service wordpress-service
service “wordpress-mysql8-service” deleted
service “wordpress-service” deleted
查看主机数据
查看PV对应的主机目录,发现数据还在
[root@k8s-node01 ~]# ls /root/datatest/mysql/
‘#ib_16384_0.dblwr’ auto.cnf ca-key.pem ib_buffer_pool mysql.sock public_key.pem undo_001
‘#ib_16384_1.dblwr’ binlog.000001 ca.pem ibdata1 mysql_upgrade_history server-cert.pem undo_002
‘#innodb_redo’ binlog.000002 client-cert.pem mysql performance_schema server-key.pem wordpress
‘#innodb_temp’ binlog.index client-key.pem mysql.ibd private_key.pem sys
删除PVC和PV
[root@k8s-master01 db8]# kubectl delete pvc mysql-pvc
persistentvolumeclaim “mysql-pvc” deleted
[root@k8s-master01 db8]# kubectl delete pv mysql-pv
persistentvolume “mysql-pv” deleted
[root@k8s-master01 db8]# kubectl get pvc
No resources found in default namespace.
[root@k8s-master01 db8]# kubectl get pv
No resources found
查看PV对应的主机目录,数据依然存在
[root@k8s-node01 ~]# ls /root/datatest/mysql/
‘#ib_16384_0.dblwr’ auto.cnf ca-key.pem ib_buffer_pool mysql.sock public_key.pem undo_001
‘#ib_16384_1.dblwr’ binlog.000001 ca.pem ibdata1 mysql_upgrade_history server-cert.pem undo_002
‘#innodb_redo’ binlog.000002 client-cert.pem mysql performance_schema server-key.pem wordpress
‘#innodb_temp’ binlog.index client-key.pem mysql.ibd private_key.pem sys
删除PV
主要是从 Kubernetes 的资源管理角度进行操作,它不会自动删除主机上对应的目录。这是因为PV
只是对存储资源的一种抽象和管理方式,它与主机文件系统是松耦合的。
删除主机数据
如果需要删除数据并且确认这些数据是可以删除数据,则需要手动删除卷挂载的主机数据。
[root@k8s-node01 ~]# rm -rf /root/datatest/mysql
完成!enjoy it!