综合题目之创建sc,pv,pvc,pod关联pvc

< 注意对于部署pod的位置必须和pv在同一节点,所以说动态创建pv的好处这里可以体现,
当前问题分析
- 错误信息显示
nodeAffinity字段是不可变的(immutable),这意味着您不能直接修改已存在的 PV 的节点亲和性设置 - 您有两个节点:controlplane(有污点)和 node01(无污点)
- 您需要将 PV 绑定到 node01 节点
解决方案步骤
1. 首先删除现有的 PV 和 PVC
kubectl delete pvc fast-pvc-cka
kubectl delete pv fast-pv-cka
2. 重新创建正确的 PV 配置
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: fast-pv-cka
spec:
capacity:
storage: 50Mi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: fast-storage
local:
path: /tmp/fast-data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node01
EOF
3. 创建 PVC
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: fast-pvc-cka
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-storage
resources:
requests:
storage: 30Mi
volumeName: fast-pv-cka
EOF
4. 创建 Pod(无需容忍,因为会调度到 node01)
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: fast-pod-cka
spec:
nodeSelector:
kubernetes.io/hostname: node01 # 添加节点选择器
containers:
- name: nginx-container
image: nginx:latest
volumeMounts:
- name: fast-storage
mountPath: "/app/data"
volumes:
- name: fast-storage
persistentVolumeClaim:
claimName: fast-pvc-cka
EOF
验证步骤
- 检查 PV 状态:
kubectl get pv fast-pv-cka -o wide
- 检查 PVC 绑定:
kubectl get pvc fast-pvc-cka -o wide
- 检查 Pod 运行情况:
kubectl get pod fast-pod-cka -o wide
kubectl describe pod fast-pod-cka
关键注意事项
- nodeAffinity 不可变:一旦 PV 创建后,就不能修改其 nodeAffinity 设置,必须删除重建
- 节点目录准备:确保 node01 节点上存在
/tmp/fast-data目录 - 调度逻辑:由于 PV 绑定到 node01,Pod 会自动调度到该节点,无需额外配置
浙公网安备 33010602011771号