?mysql使用pv和pvc實踐操作記錄
[root@kubernetes1 ~]# mkdir /data/k8s/mysqlpv1
創建pv
[root@kubernetes1 ~]# cat mysql-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysqlpv1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs1
nfs:
path: /data/k8s/mysqlpv1
server: 192.168.211.135
[root@kubernetes1 ~]# kubectl apply -f mysql-pv.yml
persistentvolume "mysqlpv1" created
[root@kubernetes1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv1 1Gi RWO Retain Bound default/mypvc1 nfs 47m
mysqlpv1 1Gi RWO Retain Available nfs1 3s
創建pvc
[root@kubernetes1 ~]# cat mysql-pvc1.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysqlpvc1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs1
[root@kubernetes1 ~]# kubectl apply -f mysql-pvc1.yml
persistentvolumeclaim "mysqlpvc1" created
[root@kubernetes1 ~]#
[root@kubernetes1 ~]# kubectl get pvc -o wide
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc1 Bound mypv1 1Gi RWO nfs 48m
mysqlpvc1 Bound mysqlpv1 1Gi RWO nfs1 36s
[root@kubernetes1 ~]#
部署mysql
yml文件參考如下:
一些說明:
--- 這個符號是分隔符
[root@kubernetes1 ~]# cat mysql-svc.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-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysqlpvc1
[root@kubernetes1 ~]#
[root@kubernetes1 ~]# kubectl apply -f mysql-svc.yml
service "mysql" unchanged
deployment.apps "mysql" created
[root@kubernetes1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-7bd85dbb6f-5fjlx 1/1 Running 0 3m 10.244.1.106 kubernetes2
可以看到pod在kubernetes2上生成
在這個node執行命令
[root@kubernetes2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13b98755ae54 docker.io/mysql@sha256:0267b9b43034ed630e94f846ca825140994166c6c7d39d43d4dbe8d1404e1129 "docker-entrypoint..." 5 minutes ago Up 5 minutes k8s_mysql_mysql-7bd85dbb6f-5fjlx_default_0fae8658-7b73-11e8-bdc5-000c292f3b91_0
inspect下
[root@kubernetes2 ~]# docker inspect 13b98755ae54
可以看到是使用了mysqlpv1
生成數據,測試效果:
在容器操作數據庫:
創建數據庫t1
[root@kubernetes2 ~]# docker exec 13b98755ae54 mysql -uroot -ppassword -e "create database t1"
Warning: Using a password on the command line interface can be insecure.
[root@kubernetes2 ~]# docker exec 13b98755ae54 mysql -uroot -ppassword -e "show databases"
Warning: Using a password on the command line interface can be insecure.
Database
information_schema
mysql
performance_schema
t1
[root@kubernetes2 ~]#
在t1中創建數據表t1
[root@kubernetes2 ~]# docker exec 13b98755ae54 mysql -uroot -ppassword -e "use t1;create table t1(i int);"
Warning: Using a password on the command line interface can be insecure.
數據表t1插入數據並檢索
[root@kubernetes2 ~]# docker exec 13b98755ae54 mysql -uroot -ppassword -e "use t1;insert into t1 values (1),(3),(3);"
Warning: Using a password on the command line interface can be insecure.
[root@kubernetes2 ~]# docker exec 13b98755ae54 mysql -uroot -ppassword -e "use t1;select * from t1;"
Warning: Using a password on the command line interface can be insecure.
i
1
3
3
[root@kubernetes2 ~]#
已經生成了數據,我們看看nfs存儲目錄的情況和容器存儲目錄的情況
nfs目錄
[root@kubernetes1 ~]# ls /data/k8s/mysqlpv1/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema t1
[root@kubernetes1 ~]#
[root@kubernetes1 ~]# ls /data/k8s/mysqlpv1/t1
db.opt t1.frm t1.ibd
[root@kubernetes1 ~]#
容器
[root@kubernetes2 ~]# docker exec 13b98755ae54 ls /var/lib/mysql
auto.cnf
ib_logfile0
ib_logfile1
ibdata1
mysql
performance_schema
t1
[root@kubernetes2 ~]# docker exec 13b98755ae54 ls /var/lib/mysql/t1
db.opt
t1.frm
t1.ibd
[root@kubernetes2 ~]#
數據對比可知,pvc使用成功
現在模擬故障,kubernetes2機器死掉
kubernetes2關機
[root@kubernetes2 ~]# init 0
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(kubernetes2) at 16:34:27.
[root@kubernetes1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-7bd85dbb6f-5fjlx 1/1 Unknown 0 30m 10.244.1.106 kubernetes2
mysql-7bd85dbb6f-jwwfv 1/1 Running 0 38s 10.244.2.114 kubernetes3
pod自動切換到kubernetes3了
到kubernetes3上操作容器數據庫
[root@kubernetes3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5bb97bd16595 84581e99d807 "nginx -g 'daemon ..." 3 minutes ago Up 3 minutes k8s_nginx-deployment_nginx-deployment-6b5c99b6fd-tcwfc_default_48c5442e-7b77-11e8-bdc5-000c292f3b91_0
eec205b40845 97fdbdd65c6a "docker-entrypoint..." 3 minutes ago Up 3 minutes k8s_mysql_mysql-7bd85dbb6f-jwwfv_default_48b8e8ad-7b77-11e8-bdc5-000c292f3b91_0
檢索數據
[root@kubernetes3 ~]# docker exec eec205b40845 mysql -uroot -ppassword -e "use t1;select * from t1;"
Warning: Using a password on the command line interface can be insecure.
i
1
3
3
[root@kubernetes3 ~]#
數據完好無損
?mysql使用pv和pvc實踐操作記錄