//使用kubectl describe
[root@master ~]# kubectl describe pod test-pod
mount.nfs: mounting 192.168.1.70:/nfsdata/pv1 failed, reason given by server: No such file or directory //提示没有文件或目录
创建目录,再查看pod状态:
[root@master ~]# mkdir /nfsdata/pv1
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-pod 1/1 Running 0 12m 10.244.1.3 node01 <none> <none>
[root@master ~]# kubectl delete pod test-pod
[root@master ~]# kubectl delete pvc test-pvc
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
test-pv 1Gi RWO Recycle Available nfs 42h
[root@master ~]# ls /nfsdata/pv1/
[root@master ~]#
//验证成功,数据已经回收
通常情况下不会设置为自动删除,不然就和emptyDir就差不多了
删除pv,修改回收策略:
之前是先创建PV—>PVC—>Pod,现在调整一下,先创建PV—>—Pod—>PVC
[root@master ~]# vim nfs-pv.yaml
persistentVolumeReclaimPolicy: Retain
[root@master ~]# kubectl apply -f nfs-pv.yaml
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
test-pv 1Gi RWO Retain Available nfs 7s
[root@master ~]# kubectl apply -f pod.yaml
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-pod 0/1 Pending 0 5s //Pending正在被调度
[root@master ~]# kubectl describe pod test-pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 41s (x2 over 41s) default-scheduler persistentvolumeclaim "test-pvc" not found
//没有发现对应的pvc
创建pvc
[root@master ~]# kubectl apply -f nfs-pvc.yaml
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-pod 1/1 Running 0 114s
验证Retain(管理员手动删除)回收策略:
[root@master ~]# kubectl exec test-pod touch /mydata/k8s
[root@master ~]# ls /nfsdata/pv1/
k8s
[root@master ~]# kubectl delete pod test-pod
[root@master ~]# kubectl delete pvc test-pvc
[root@master ~]# ls /nfsdata/pv1/
k8s
//可以看到并没有回收
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
test-pv 1Gi RWO Retain Available nfs 6s
mysql对数据持久化的应用:
//这里就不再创建PV,PVC了,用之前的就行
[root@master ~]# kubectl apply -f nfs-pvc.yaml
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-pvc Bound test-pv 1Gi RWO nfs 7s
创建Deploment资源对象,mysql容器
[root@master ~]# vim mysql.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-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: 123.com
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: test-pvc
[root@master ~]# kubectl get deployments.
NAME READY UP-TO-DATE AVAILABLE AGE
test-mysql 1/1 1 1 61s
进入容器创建数据,验证是否应用PV:
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-mysql-569f8df4db-fnnxc 1/1 Running 0 32m 10.244.1.5 node01 <none> <none>
[root@master ~]# kubectl exec -it test-mysql-569f8df4db-fnnxc -- mysql -u root -p123.com
mysql> create database yun33; //创建数据库
mysql> use yun33; //选择使用数据路
Database changed
mysql> create table my_id( id int(4)); 创建表
mysql> insert my_id values(9527); //在表中插入数据
mysql> select * from my_id; //查看表中所有数据
+------+
| id |
+------+
| 9527 |
+------+
1 row in set (0.00 sec)
[root@master ~]# ls /nfsdata/pv1/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 k8s mysql performance_schema yun33
关闭node01节点,模拟节点宕机:
[root@master ~]# kubectl get pod -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-mysql-569f8df4db-fnnxc 1/1 Running 0 36m 10.244.1.5 node01 <none> <none>
test-mysql-569f8df4db-fnnxc 1/1 Terminating 0 38m 10.244.1.5 node01 <none> <none>
test-mysql-569f8df4db-2m5rd 0/1 Pending 0 0s <none> <none> <none> <none>
test-mysql-569f8df4db-2m5rd 0/1 Pending 0 0s <none> node02 <none> <none>
test-mysql-569f8df4db-2m5rd 0/1 ContainerCreating 0 0s <none> node02 <none> <none>
test-mysql-569f8df4db-2m5rd 1/1 Running 0 2s 10.244.2.4 node02 <none> <none>
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
test-mysql-569f8df4db-2m5rd 1/1 Running 0 20s 10.244.2.4 node02 <none> <none>
test-mysql-569f8df4db-fnnxc 1/1 Terminating 0 38m 10.244.1.5 node01 <none> <none>
验证:在node02上新生成的pod,它内部是否有我们创建的数据
[root@master ~]# kubectl exec -it test-mysql-569f8df4db-2m5rd -- mysql -u root -p123.com
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| yun33 |
+--------------------+
4 rows in set (0.01 sec)
mysql> use yun33;
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> show tables;
+-----------------+
| Tables_in_yun33 |
+-----------------+
| my_id |
+-----------------+
1 row in set (0.01 sec)
mysql> select * from my_id;
+------+
| id |
+------+
| 9527 |
+------+
1 row in set (0.01 sec)
[root@master ~]# ls /nfsdata/pv1/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 k8s mysql performance_schema yun33