官方文档:https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-persistent-volume-storage/
题目1:按要求创建PV
设置配置环境kubectl config use-context k8s
创建名为app-data的persistent volume,容量为1Gi,访问模式为ReadWriteMany。volume,类型为hostPath,位于/srv/app-data。
解答:
# 1.切换环境
kubectl config use-context k8s
# 2.编写yaml文件
[root@master2 pv]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: app-data
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/srv/app-data"
[root@master2 pv]# kubectl apply -f pv.yaml
persistentvolume/app-data created
[root@master2 pv]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
app-data 1Gi RWX Retain Available 4s
题目2:创建和使⽤PVC
设置配置环境kubectl config use-context k8s
创建一个新的PersistentVolumeClaim:
- 名称:pvvolume
- class:csi-hostpath-sc
- 容量:10Mi
创建一个新的pod,此pod 将作为volume挂载到PersistentVolumeClaim:
- 名称:web-server
- image: nginx
- 挂载路径: /usr/share/nginx/html
配置新的pod,以对volume具有ReadWriteOnce 权限。
最后,使用kubectl edit 或者kubectl patch 将PersistentVolumeClaim的容量扩展为70Mi,并记录此次更改。
解答2:
# 根据题目,可以看出我们需要一个pv,让新建的pvc绑定到pv上,可以在题目1的yaml修改增加storageClassName: csi-hostpath-sc,以便让pvc自动绑定到这个pv上,考试的时候看看是否有了
# 1.切换配置环境
kubectl config user-context k8s
# 2.创建pvc
[root@master2 pvc]# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvvolume
spec:
storageClassName: "csi-hostpath-sc"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Mi
kubectl apply -f pvc.yaml
[root@master2 pvc]# kubectl get pvc pvvolume
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvvolume Bound app-data 1Gi RWO csi-hostpath-sc 27m
# 3.创建pod,并挂载使用pvc
[root@master2 pvc]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: web-server
spec:
containers:
- name: nginx-pvc
image: docker.io/janakiramm/myapp:v1
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: nginx-data
volumes:
- name: nginx-data
persistentVolumeClaim:
claimName: pvvolume
[root@master2 pvc]# kubectl apply -f pod.yaml
pod/web-server created
[root@master2 pvc]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-server 1/1 Running 0 4s
# 4.修改pvc容量:扩容
[root@master2 pvc]# kubectl edit pv app-data
persistentvolume/app-data edited
[root@master2 pvc]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
app-data 2Gi RWO Retain Bound default/pvvolume csi-hostpath-sc 23m