kind搭建k8s集群中验证PV和PVC
作者:互联网
1、什么是PV、PVC
PV(PersistentVolume):持久化卷
PVC(PersistentVolumeClaim):持久化声明
为何有了Volumes,还要引进PV、PVC?因为Volumes的类型有很多,每次切换不同的类型如hotsPath,第三方的云存储,都需要手动改动yaml的对应的类型,且每种类型的写法声明方式都不同,导致不灵活不好维护。为了解决该问题,通过PV来抽象了各种类型,具体实现由具体存储方去实现,客户端只需要通过PVC声明,指定对应的storageClassName,即可匹配符合的第三方PV,无需关心其实现细节,且无需做任何修改。
2、通过mysql来实操PV、PVC
一、创建PV,类型为hostPath:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual #与请求的PVC对应
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/var/lib/mysql"
kubectl apply -f mysql-pv.yaml
查看状态:
kubectl get pv
二、创建PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: manual #与PV的对应
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
kubectl apply -f mysql-pvc.yaml
查看状态:
由STATUS为Bound可知已经与上面的PV绑定。
三、创建Mysql Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-pv-deploy
spec:
replicas: 1
selector:
matchLabels:
name: k8s-mysql
template:
metadata:
labels:
name: k8s-mysql
spec:
containers:
- name: mysql
image: registry.cn-shenzhen.aliyuncs.com/luozhilian/k8s:mysql5.6
env:
- name: MYSQL_ROOT_PASSWORD
value: password
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-persistent-storage
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim: # 此处使用pvc声明
claimName: mysql-pv-claim
---
kind: Service
apiVersion: v1
metadata:
name: k8s-mysql-service
spec:
type: NodePort
ports:
- port: 5000
targetPort: 3306
nodePort: 32000
selector:
name: k8s-mysql
kubectl apply -f mysql-deploy.yaml
kubectl get pod,svc -o wide
用Navicat连接mysql,创建一个测试数据库:
查看node1主机的挂载的路径数据 /var/lib/mysql/:
删除mysql 对应的pod,让k8s重新创建pod:
kubectl delete pod mysql-pv-deploy-d7b7fd5b5-m6n44
再登录连接mysql,查看创建的数据库是否还存在:
如上图,依然存在。
鸣谢:
https://www.cnblogs.com/roluodev/p/13951300.html
标签:kind,PV,name,PVC,mysql,pv,k8s 来源: https://www.cnblogs.com/yakniu/p/16499384.html