模拟题目:

设置配置环境:

[candidate@node-1] $ kubectl config use-context k8s

Task

  1. 在 node02 节点上创建一个文件 /opt/KDSP00101/data/index.html ,内容为 WEPKEY=7789
  2. 使用 hostPath 创建一个名为 task-pv-volumePersistentVolume ,并分配 2Gi 容量,指定该卷位于集群节点上的 /opt/KDSP00101/data ,访问模式 ReadWriteOnce 。它应该为 PersistentVolume 定义 StorageClass 名称为 keys ,它将被用来绑定 PersistentVolumeClaim 请求到这个 PersistenetVolume
  3. 创建一个名为 task-pv-claimPersistentVolumeClaim,请求容量 200Mi ,并指定访问模式 ReadWriteOnce
  4. 创建一个 pod,使用 PersistentVolmeClaim 作为一个卷,带有一个标签 app:my-storage-app ,将卷挂载到 pod 内的 /usr/share/nginx/html

参考:

https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

解答:

切换环境

kubectl config use-context k8s

1. 远程到node02 并写入文件

ssh node02
echo "WEPKEY=7789" > /opt/KDSP00101/data/index.html

2. 使用官网模板创建一个pv

pv.yaml

#pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume #根据题目要求修改
  labels:
    type: local
spec:
  storageClassName: keys #题目要求keys
  capacity:
    storage: 2Gi #题目要求2Gi
  accessModes:
    - ReadWriteOnce # 题目要求
  hostPath:
    path: "/opt/KDSP00101/data" #题目要求路径

创建

kubectl apply -f pv.yaml

3. 创建pv-claim

pv-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: keys #根据题目要求
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 200Mi #根据题目要求

创建pvc

kubectl apply -f pv-claim.yaml

4. 创建pod

考试时, 检查是否有多个node, 如果有多个节点, 则需要指定nodeSelector, 让pod和pv所在一个节点 pod-pv.yaml

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
  labels: #添加
    app: my-storage-app # 按题目要求添加
spec:
  nodeSelector:
    kubernetes.io/hostname: node02 #kubectl get nodes --show-labels获取到的标签
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim #pvc名
  containers:
    - name: task-pv-container
      image: nginx
      imagePullPolicy: IfNotPresent #使用已有镜像,加快pod运行
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html" #挂载地址
          name: task-pv-storage

创建pod

kubectl apply -f pod-pv.yaml

检查

kubectl get pod -owide | grep task-pv-pod
curl 10.244.140.65

file