创建nfs存储目录
[[email protected] ~]# mkdir /data/k8s/mysqlpv1
创建pv
[[email protected] ~]# 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
[[email protected] ~]# kubectl apply -f mysql-pv.yml
persistentvolume "mysqlpv1" created
[[email protected] ~]# 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
[[email protected] ~]# cat mysql-pvc1.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysqlpvc1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs1
[[email protected] ~]# kubectl apply -f mysql-pvc1.yml
persistentvolumeclaim "mysqlpvc1" created
[[email protected] ~]#
[[email protected] ~]# 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
[[email protected] ~]#
部署mysql
yml文件参考如下:
一些说明:
--- 这个符号是分隔符
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# kubectl apply -f mysql-svc.yml
service "mysql" unchanged
deployment.apps "mysql" created
[[email protected] ~]# 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执行命令
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13b98755ae54 docker.io/[email protected]:0267b9b43034ed630e94f846ca825140994166c6c7d39d43d4dbe8d1404e1129 "docker-entrypoint..." 5 minutes ago Up 5 minutes k8s_mysql_mysql-7bd85dbb6f-5fjlx_default_0fae8658-7b73-11e8-bdc5-000c292f3b91_0
inspect下
[[email protected] ~]# docker inspect 13b98755ae54
可以看到是使用了mysqlpv1
生成数据,测试效果:
在容器操作数据库:
创建数据库t1
[[email protected] ~]# docker exec 13b98755ae54 mysql -uroot -ppassword -e "create database t1"
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# 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
[[email protected] ~]#
在t1中创建数据表t1
[[email protected] ~]# 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插入数据并检索
[[email protected] ~]# 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.
[[email protected] ~]# 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
[[email protected] ~]#
已经生成了数据,我们看看nfs存储目录的情况和容器存储目录的情况
nfs目录
[[email protected] ~]# ls /data/k8s/mysqlpv1/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema t1
[[email protected] ~]#
[[email protected] ~]# ls /data/k8s/mysqlpv1/t1
db.opt t1.frm t1.ibd
[[email protected] ~]#
容器
[[email protected] ~]# docker exec 13b98755ae54 ls /var/lib/mysql
auto.cnf
ib_logfile0
ib_logfile1
ibdata1
mysql
performance_schema
t1
[[email protected] ~]# docker exec 13b98755ae54 ls /var/lib/mysql/t1
db.opt
t1.frm
t1.ibd
[[email protected] ~]#
数据对比可知,pvc使用成功
现在模拟故障,kubernetes2机器死掉
kubernetes2关机
[[email protected] ~]# init 0
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(kubernetes2) at 16:34:27.
[[email protected] ~]# 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上操作容器数据库
[[email protected] ~]# 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
检索数据
[[email protected] ~]# 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
[[email protected] ~]#
数据完好无损
原文地址:http://blog.51cto.com/goome/2134294