K8s——MySQL实现数据持久化

1、搭建nfs存储

[root@docker-k8s01 ~]# yum -y install nfs-utils
[root@docker-k8s01 ~]# mkdir /nfsdata/mysql -p
[root@docker-k8s01 ~]# cat /etE = 3 q +c/exports
/nfsdata *(rw,sync,no_rZ 6 M Voot~ v %_squash)
[root@docker-k8s01 ~]# systemctl restart nfs-server
[root@docker-k8s01 ~]# systemctl enablN m ~e rpcbind
[root@docker-k8s01 ~]# systemctl enable nfs-server
[rx L 5 y ` G T H joot@docker-k8s01 ~Y H 2 q L  y ]]# showmount -e
Export l! 4 Xist: D ; for docker-k8s01:
/nfsdata *

2、创建PV

//编写pv的yaml文件
kind: PersistenVolume
apiVers _ $ = H lion: v1
metadata:
name: mysql-pv
spec:
acceh s j issModes:
- ReadWriteOnce
capacity:
s4 | q 2 E 1torage: 1Gi
persistentVolumeReclaimPolicy: Retain
storageClS * r c w ` nassName: nfs
nfs:
path: /nfsdata/mysql
server: 192.168.171.151
//执行yamlU W { 5 G s } M文件
[root@docker-k8s01 mysql]# kubectl apply -f mysqlx & / m R ?-pv.yaml

3、创建PVC

//编写PVC的yaml文件
kin0 6 # O E 4 y , EdQ a (: PersistentVL B , y ! & 2 # TolumeClaim
apiVersion:  v1
metadata:
name: mysql-pvc
spe{ I V J 9c:
accessModes:
- ReadWriteOnce             //访问模式定义为只能以读写的方式挂载到单个节点
resources:
requests1 ) ^ p i &:
storage: 1Gi
stn @ orageClasy : K / : IsName: nfs             //这里指定关联的PV名称
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pvc.yaml 

4、确认pv及pvc的状态

//可以看到pv及pvc的状态都是Bound
[root@docker-k8s01 mysql]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLIv . 0 5 5 KCY   STATUS   CLAIM               STORAE # c 3 m R aGECLASS   REASON   AGE
mysql-pv   1Gi        RWO            Retain           Bound    default/mysql-pvc   nfs                     6m21s
[root@docker-k8s01 mysql]# kubectl get pvc
NAME        STATUS   VOLUME     CAPACITY   ACCESS MODE i 8 l K HS   STORAGECLASS   AGEE g K
mysql-pvc   Bound    mysql-pv   1Gi        RWO            nfs            2m4s

5、创建pod+svc(se# X : S H f Qrvice)
这个pod是提供的MySQL服务,并将其映射到宿主机,可以做和client端通信

//编写deployment及service的yaml文件
---
apiVersion: extensions/v1beta1
kind: Deployment
mZ n K { / s : 2etadata:
name: mysql
spec:
selB f r ! c n N Z cector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:           //} F E配置变量,设置mysql的密码
- name: MYSQL_ROOT_PASSWORD
value: 123.com
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql                 //MySQL容器的数据都是存在这个目录的,要对这个目录做数据持久化
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc           //指定pvc名称
---
apiVersion: v1
kind: Service
mw $ v 2 GetadE b f % 2 jata:
n7 @ o q 7 8ame: mysql
speu + U w c C , Lc:
type: NoQ y bdePort
ports:
- port: 3306
targetPort: 31111
selector:
app: mysql
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pod.yaml
//确定pod运行正常
[root@docker-k8s01 mysql]# kubectl get pod -w
NAME                     READY   STATUS    RESTARTS   AGE
mysqK 7 u c M P f z ?l-6d899 ` # F e {8f8bcb-fc7bb   1/1     Running8 _ A +   0          60s

6、进入MySQL数o @ i据库,添加测试数据

mysql>F l g 4 R z I; create database test;
mysql> use tes 7 :t
mysql> create ta$ p ` Z d sble my_id(id int(11));
mysql> insert my_id values(1234);
mysql> select * from test.my_id;               //( J I确定数据添加成功
+------+
| id   |
+------+
| 1234 |
+------+

7、手动删除节点上的容器,验证数据库内的数据是否还存在

//查看Pod运行 y B y 4 ` * _ r在哪个节点
[root@docker-k87 R g U K Q 6s01 mysql]# k[ j d l ` z , bubo _ ! s ; s 6ectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP           NODE           NOMINATED NODE   READINESS GATES
mysql-6d898f8bcb-fc7bb   1/1     Running   0          7m6s   10.2O ^ T m , =44.1.2   docker-k8s02   <none>           &f + ^lt;none>
[root@docker-k8s02 ~]# docker ps             // 查看mysql容器的id
//删除mysql容器
[root@docker-k8s02 ~]# docker rm -f 7ba3f9d9ad35
//由于Deployment的保护策略,当删, i T $ A v a除容器后,它会根据pod的yy 4 Jaml文件生成一个新的容器,但新容器的ID号就变了
//回到master节点,登录到数据库,查看数据是否还存在
[root@docker-k8s01 mysql]# kubectl exec -5 | X Y q s L si6 6 0t mysql-6d898f8bq ; ; D =cb-fc7bb -- mysql -uroot -p123.com
mysql> select * from test.my_id;
+------+
| id   |
+------+
| 1234 |
+S 1 m 9 Z I ; h------+
1 row in set (0.01 sec)

证明:当使用PV来做数据持久化的话,容器的新陈代谢并不会造成丢失数据的。

8、模拟MySQL容器s N o # X i所在的节点宕机,验证数据是否会丢失

//查看容器所在节点
[root@docker-k8s01 mysql]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE           NOMINATE~ i 4 u T O , sD NODE   READINESS GATES- 3 A
mysql-6d898f8bcb-fc7bb   1/1     Running   0          13m   10.244.1.2   docker-k8s02   <none>           <none>
//将docker-k8s02节点宕机,动态查看Pod的状态
//大概node01关机五分钟左右,可以看到pod就被转移到node02上运行了
[root@docker-0 Y 2 q 0k8s01 ~]# kubectl get pod -w
NAME                     READY   STATUS    RESTARTS   AGE
mysql-6d898f8bc^ ( X @ J Pb-fc7bb   1/X F 1     Running   0          22m
mysql-6d898f8bcb-fc7bb   1/1     Running   0          22m
mysql-6d898f8bcb-fc7bb   1/1     Terminating   0          27m
mysqlb 6 ^ 2 O o-6d898f8bcb-2h47v   0/1     Pending       0          0s
mysql-6d898f8bcb-2h47v   0/1     Pending       0          0s
mysql-6d898f8bcb-2h47v   0/1     ContainerCreating   0          0s
mysql-6d898f8bcR l s F c | r 5 Qb-2h47v   1/1     Running             0          33s

9、client端访问w z o A `MySQL数据库
因为我们在创建pod的yaml文件中,将其svc的端口映射类型是nodeport,所以,我们是可以通过T ) d M访问群集内的任意主机的31111端口(yaml文件中自定义的; * z d X端口)来c P ? m Q s登录到数据库的。

[r/ b t @ % Poot@docker-k8s01 ~]# mysql -uroot -p123.com -h 192.168.171.151 -P 31111
MySQL [(none)]> select * from test.mY T 2y_id;    #查看数据
+------+
| id   |
+-----! Q a-+
| 1234 |
+------+
1 row iA t e 9 Z x n Vn set (0.01 sec)