Kubernetes-为MySQL 数据库提供持久化存储
-
创建 PV 和 PVC。
-
部署 MySQL。
-
向 MySQL 添加数据。
-
模拟节点宕机故障,Kubernetes 将 MySQL 自动迁移到其他节点。
-
验证数据一致性。
首先创建 PV 和 PVC,配置如下:
[root@master ~]# vi mysql-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /nfsdata/mysql-pv
server: 192.168.0.90
[root@master ~]# vi mysql-pvc.yml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
[root@master ~]# mkdir /nfsdata/mysql-pv
创建 mysql-pv 和 mysql-pvc:
[root@master ~]# kubectl apply -f mysql-pv.yml
persistentvolume “mysql-pv” created
[root@master ~]# kubectl apply -f mysql-pvc.yml
persistentvolumeclaim “mysql-pvc” created
[root@master ~]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv1 1Gi RWO Retain Bound default/mysql-pvc nfs 23m
mysql-pv 1Gi RWO Retain Available nfs 12s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound mypv1 1Gi RWO nfs 10s
接下来部署 MySQL,配置文件如下:
[root@master ~]# vi mysql.yml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
PVC mysql-pvc Bound 的 PV mysql-pv 将被 mount 到 MySQL 的数据目录 var/lib/mysql。
[root@master ~]# kubectl apply -f mysql.yml
service “mysql” created
deployment.apps “mysql” created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-57c44d9d5f-vw8ps 1/1 Running 0 1m 192.168.104.30 node2
MySQL 被部署到node2,下面通过客户端访问 Service mysql:
[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 7d
mysql ClusterIP 10.98.119.184 3306/TCP 15m
[root@master ~]# kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client – mysql -h 10.98.119.184 -ppassword
If you don’t see a command prompt, try pressing enter.
mysql>
更新数据库:
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table my_id(id int(4));
Query OK, 0 rows affected (0.04 sec)
mysql> insert my_id values(111);
Query OK, 1 row affected (0.01 sec)
mysql> select * from my_id;
±-----+
| id |
±-----+
| 111 |
±-----+
1 row in set (0.00 sec)
关闭 node2,模拟节点宕机故障。
一段时间后,Kubernetes 将 MySQL 迁移到 node1。
[root@master ~]# kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client – mysql -h 10.104.38.54 -ppassword
If you don’t see a command prompt, try pressing enter.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from my_id;
±-----+
| id |
±-----+
| 111 |
±-----+
1 row in set (0.03 sec)
MySQL 服务恢复,数据也完好无损。