Use context: kubectl config use-context k8s-c1-H

Create a new PersistentVolume named safari-pv . It should have a capacity of 2Gi , accessMode ReadWriteOnce , hostPath /Volumes/Data and no storageClassName defined.

Next create a new PersistentVolumeClaim in Namespace project-tiger named safari-pvc . It should request 2Gi storage, accessMode ReadWriteOnce and should not define a storageClassName. The PVC should bound to the PV correctly.

Finally create a new Deployment safari in Namespace project-tiger which mounts that volume at /tmp/safari-data . The Pods of that Deployment should be of image httpd:2.4.41-alpine .


译文

创建一个新的PersistentVolume,名为 safari-pv 。它的容量为 2Gi ,访问模式为 ReadWriteOnce ,主机路径为 /Volumes/Data ,不定义存储类别名称。

接下来在namespace project-tiger创建一个新的 PersistentVolumeClaim ,命名为 safari-pvc 。它应该请求 2Gi 存储,访问模式 ReadWriteOnce ,并且不应该定义存储类名称。PVC应该正确地绑定到PV上。

最后在名字空间 project-tiger 中创建一个新的部署 safari ,将该卷挂载在 /tmp/safari-data 。该部署Pod的镜像是 httpd:2.4.41-alpine


解答

pv可参考
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-pv

vim 6_pv.yaml

6_pv.yaml

# 6_pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: safari-pv
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/Volumes/Data"

创建pv

k -f 6_pv.yaml

pvc可参考
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-pvc

vim 6_pvc.yaml

6_pvc.yaml

# 6_pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: safari-pvc
namespace: project-tiger
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi

创建pvc

k -f 6_pvc.yaml create

检查pv,pvc绑定情况

k -n project-tiger get pv,pvc

create-pv-2-0

创建一个部署并挂载卷,可以参考
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#create-a-pod

k -n project-tiger create deploy safari \
--image=httpd:2.4.41-alpine $do > 6_dep.yaml
vim 6_dep.yaml

6_dep.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: safari
name: safari
namespace: project-tiger
spec:
replicas: 1
selector:
matchLabels:
app: safari
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: safari
spec:
volumes: # add
- name: data # add
persistentVolumeClaim: # add
claimName: safari-pvc # add
containers:
- image: httpd:2.4.41-alpine
name: container
volumeMounts: # add
- name: data # add
mountPath: /tmp/safari-data # add

创建pod

k -f 6_dep.yaml create

检查pod挂载情况

k -n project-tiger describe pod safari-xxx-xxx  | grep -i -A2 mounts

create-pv-2-1