K8s集群分布式存储资源负载动态调度实操
技术栈:Kubernetes v1.32.13 + Rocky Linux 8.6 + 存储系统通用 + Containerd 1.7.x
操作环境 / 对接原理 / 详细步骤 / 完整命令 / 配置文件 / 验证流程 / 排错方案
K8s集群分布式存储资源负载动态调度实操
操作环境
K8s 集群 3 节点:k8s-master(192.168.1.10), k8s-node1(192.168.1.11), k8s-node2(192.168.1.12),K8s 版本 v1.32.13
存储类型:存储系统通用,已配置对应 StorageClass 支持动态供给,PV/PVC 资源已就绪
容器运行时:Containerd 1.7.x,操作系统 Rocky Linux 8.6,内核版本已适配存储需求
集群内存在多命名空间/多租户业务,存储资源需要统一规范化管理,包含 PV/PVC/StorageClass/ResourceQuota 等资源
已配置监控告警体系(Prometheus + Grafana),存储指标可采集,具备备份恢复能力
对接原理
K8s集群分布式存储资源负载动态调度实操是 K8s 集群存储系统运维中的核心操作场景。K8s 存储体系通过 PV(PersistentVolume)和 PVC(PersistentVolumeClaim)实现存储资源的抽象与解耦,StorageClass 提供动态存储供给能力,CSI(Container Storage Interface)统一存储驱动接口。存储系统通用作为具体的存储后端,为集群中的有状态应用提供持久化存储能力。运维操作的核心目标是确保存储资源的可用性、性能、安全性和可管理性:通过规范化的 PV/PVC 管理确保持久化数据不丢失,通过 StorageClass 和动态供给提升资源分配效率,通过配额和多租户隔离实现资源管控,通过监控告警和备份容灾保障业务连续性,通过自动化脚本和 SOP 提升运维效率。所有操作需遵循业务无感知原则,对正在使用的存储资源采用灰度和滚动方式,避免影响业务运行。
详细步骤
1. 存储资源现状盘点与分类
# 1. 盘点所有 PV 资源 kubectl get pv -o wide kubectl get pv -o custom-columns='NAME:.metadata.name,STATUS:.status.phase,CAPACITY:.spec.capacity.storage,ACCESSMODES:.spec.accessModes[*],RECLAIMPOLICY:.spec.persistentVolumeReclaimPolicy,STORAGECLASS:.spec.storageClassName,AGE:.metadata.creationTimestamp' # 2. 盘点所有 PVC 资源 kubectl get pvc --all-namespaces -o wide kubectl get pvc --all-namespaces -o custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,STATUS:.status.phase,VOLUME:.spec.volumeName,CAPACITY:.status.capacity.storage,STORAGECLASS:.spec.storageClassName,ACCESSMODES:.spec.accessModes[*],AGE:.metadata.creationTimestamp' # 3. 盘点 StorageClass kubectl get storageclass -o wide kubectl describe storageclass # 4. 按命名空间/租户分类统计 for ns in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do count=$(kubectl get pvc -n $ns --no-headers 2>/dev/null | wc -l) if [ "$count" -gt 0 ]; then echo "命名空间 $ns: $count 个PVC" kubectl get pvc -n $ns -o wide fi done # 5. 识别异常状态资源 echo "=== Pending 状态 PVC ===" kubectl get pvc --all-namespaces --field-selector=status.phase=Pending -o wide echo "=== Released 状态 PV ===" kubectl get pv --field-selector=status.phase=Released -o wide echo "=== Failed 状态 PV ===" kubectl get pv --field-selector=status.phase=Failed -o wide # 6. 导出盘点结果 kubectl get pv -o yaml > /tmp/pv_inventory_$(date +%Y%m%d).yaml kubectl get pvc --all-namespaces -o yaml > /tmp/pvc_inventory_$(date +%Y%m%d).yaml echo "盘点结果已导出到 /tmp/" 2. 制定操作方案与备份防护
# 1. 备份当前所有存储资源配置(操作前必做) kubectl get pv -o yaml > /tmp/pv_backup_$(date +%Y%m%d_%H%M%S).yaml kubectl get pvc --all-namespaces -o yaml > /tmp/pvc_backup_$(date +%Y%m%d_%H%M%S).yaml kubectl get storageclass -o yaml > /tmp/sc_backup_$(date +%Y%m%d_%H%M%S).yaml kubectl get resourcequota --all-namespaces -o yaml > /tmp/quota_backup_$(date +%Y%m%d_%H%M%S).yaml # 2. 备份存储后端数据(根据存储类型执行) # NFS 备份示例 # ssh root@nfs-server "tar czf /data/nfs_backup_$(date +%Y%m%d).tar.gz /data/nfs/shared/" # Ceph 备份示例 # ceph osd pool get volumes size # rbd snap create volumes/pvc-xxx@snapshot_$(date +%Y%m%d) # 3. 制定操作方案 cat > /tmp/storage_operation_plan.md << 'EOF' # 存储操作方案 ## 一、操作目标 ## 二、影响范围评估 ## 三、操作步骤与时间窗口 ## 四、回滚方案 ## 五、验证标准 ## 六、风险点与应对措施 EOF echo "操作方案模板已创建" # 4. 确认业务窗口(低峰期操作) echo "当前时间: $(date)" echo "建议在业务低峰期执行存储操作,避免影响业务" # 5. 通知相关业务方 # echo "存储运维操作通知" | mail -s "存储运维通知" admin@example.com
3. 执行存储资源规范化操作
# 1. 批量补全 PV/PVC 标签(示例) cat > /tmp/batch_label_storage.sh << 'SCRIPT' #!/bin/bash # 为所有 PV 补全标签 for pv in $(kubectl get pv -o jsonpath='{.items[*].metadata.name}'); do sc=$(kubectl get pv $pv -o jsonpath='{.spec.storageClassName}') kubectl label pv $pv storage-type=$sc managed-by=storage-team env=prod --overwrite 2>/dev/null done # 为所有 PVC 补全标签 for ns in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do for pvc in $(kubectl get pvc -n $ns -o jsonpath='{.items[*].metadata.name}' 2>/dev/null); do kubectl label pvc $pvc -n $ns namespace=$ns managed-by=storage-team env=prod --overwrite 2>/dev/null done done echo "标签批量补全完成" SCRIPT chmod +x /tmp/batch_label_storage.sh # /tmp/batch_label_storage.sh # 2. 规范化 StorageClass 参数(示例) # kubectl patch storageclass <sc-name> -p '{"allowVolumeExpansion": true}' # 3. 设置 ResourceQuota(示例) # cat > /tmp/quota.yaml << 'EOF' # apiVersion: v1 # kind: ResourceQuota # metadata: # name: storage-quota # namespace: <namespace> # spec: # hard: # requests.storage: "500Gi" # persistentvolumeclaims: "20" # EOF # kubectl apply -f /tmp/quota.yaml # 4. 清理闲置资源(先确认无业务引用) # 识别无 Pod 挂载的 PVC # for ns in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do # for pvc in $(kubectl get pvc -n $ns -o jsonpath='{.items[*].metadata.name}' 2>/dev/null); do # used=$(kubectl get pod -n $ns -o json 2>/dev/null | grep -c "persistentVolumeClaim.*$pvc") # if [ "$used" -eq 0 ]; then # echo "闲置PVC: $ns/$pvc" # fi # done # done # 确认后删除: kubectl delete pvc <name> -n <namespace> # 5. 执行具体操作(根据标题调整) echo "执行存储资源规范化具体操作..." echo "请根据操作方案执行具体步骤" 4. 存储性能调优与参数优化
# 1. 检查当前存储性能指标 kubectl top nodes # 检查磁盘 IO(需节点权限) # kubectl debug node/<node-name> -it --image=busybox -- iostat -x 1 5 # 2. 存储挂载参数调优(NFS 示例) # mount -o hard,nfsvers=4.1,rsize=1048576,wsize=1048576,timeo=600,retrans=3 nfs-server:/share /mnt/nfs # 3. 内核 IO 调度器调优 # 查看当前调度器 # cat /sys/block/sda/queue/scheduler # 设置为 deadline(适合 SSD)或 cfq(适合 HDD) # echo deadline > /sys/block/sda/queue/scheduler # 4. 文件系统挂载参数优化 # ext4: noatime,nodiratime,barrier=0 # xfs: noatime,nodiratime,allocsize=64m # mount -o remount,noatime,nodiratime /dev/sdb1 /data # 5. StorageClass 参数调优 # kubectl patch storageclass <sc-name> -p '{"parameters": {"volumeBindingMode": "WaitForFirstConsumer"}}' # 6. 验证调优效果 # dd if=/dev/zero of=/mnt/storage/testfile bs=1G count=1 oflag=direct # dd if=/mnt/storage/testfile of=/dev/null bs=1G iflag=direct # 清理测试文件 # rm -f /mnt/storage/testfile 5. 存储监控告警与巡检
# 1. 检查存储相关监控指标 # PV/PVC 状态 kubectl get pv --no-headers | awk '{print $1, $2}' | sort | uniq -c kubectl get pvc --all-namespaces --no-headers | awk '{print $2, $3}' | sort | uniq -c # 2. 存储容量监控(需节点权限或监控系统) # df -h | grep -E '/data|/mnt|nfs|ceph' # 检查 PVC 实际使用量(需进入 Pod) # kubectl exec -it <pod-name> -- df -h | grep -E '/data|/mnt' # 3. 配置存储告警规则(Prometheus 示例) cat > /tmp/storage_alerts.yaml << 'EOF' groups: - name: storage-alerts rules: - alert: PVCPending expr: kube_persistentvolumeclaim_status_phase{phase="Pending"} > 0 for: 5m labels: severity: warning annotations: summary: "PVC 处于 Pending 状态" - alert: StorageCapacityHigh expr: (1 - kubelet_volume_stats_available_bytes / kubelet_volume_stats_capacity_bytes) * 100 > 85 for: 5m labels: severity: critical annotations: summary: "存储卷使用率超过 85%" EOF echo "告警规则模板已创建" # 4. 定时巡检脚本 cat > /tmp/storage_audit.sh << 'SCRIPT' #!/bin/bash echo "=== 存储巡检 $(date) ===" echo "--- PV 状态统计 ---" kubectl get pv --no-headers | awk '{print $2}' | sort | uniq -c echo "--- PVC 状态统计 ---" kubectl get pvc --all-namespaces --no-headers | awk '{print $3}' | sort | uniq -c echo "--- StorageClass 列表 ---" kubectl get storageclass --no-headers echo "--- 异常 PVC ---" kubectl get pvc --all-namespaces --field-selector=status.phase=Pending -o wide 2>/dev/null echo "--- 异常 PV ---" kubectl get pv --field-selector=status.phase=Released -o wide 2>/dev/null kubectl get pv --field-selector=status.phase=Failed -o wide 2>/dev/null echo "=== 巡检完成 ===" SCRIPT chmod +x /tmp/storage_audit.sh /tmp/storage_audit.sh # 5. 设置定时巡检(CronJob 或 crontab) # crontab -e # 0 2 * * * /tmp/storage_audit.sh >> /var/log/storage_audit.log 2>&1 6. 验证操作结果与业务无感知
# 1. 验证 PV/PVC 状态 kubectl get pv -o wide | head -20 kubectl get pvc --all-namespaces -o wide | head -20 # 预期:无 Pending/Failed/Released 异常状态(或已确认保留) # 2. 验证 StorageClass 配置 kubectl get storageclass -o wide kubectl describe storageclass <sc-name> | grep -E 'Parameters|Reclaim|Expansion|Mount' # 预期:参数已规范化,allowVolumeExpansion 符合预期 # 3. 验证 ResourceQuota kubectl get resourcequota --all-namespaces -o wide # 预期:各命名空间配额已设置,使用量正常 # 4. 验证标签补全 kubectl get pv -L storage-type,managed-by,env --no-headers | head -10 kubectl get pvc --all-namespaces -L namespace,managed-by,env --no-headers | head -10 # 预期:标签已补全 # 5. 业务无感知验证(抽查正在使用的 PVC) echo "=== 正在使用的 PVC(抽查)===" kubectl get pvc --all-namespaces -o wide | grep Bound | head -5 echo "=== 关联 Pod 状态(抽查)===" kubectl get pod --all-namespaces -o wide | grep Running | head -10 # 预期:业务 Pod 均正常 Running,无因存储操作导致的异常 # 6. 存储读写验证(进入业务 Pod 测试) # kubectl exec -it <pod-name> -- touch /data/test_write_$(date +%Y%m%d).txt # kubectl exec -it <pod-name> -- ls -la /data/test_write_*.txt # kubectl exec -it <pod-name> -- rm -f /data/test_write_*.txt # 预期:读写正常,无权限或挂载问题 # 7. 生成操作报告 echo "=== 存储操作报告 ===" > /tmp/storage_operation_report.txt echo "操作时间: $(date)" >> /tmp/storage_operation_report.txt echo "操作前 PV 数量: $(cat /tmp/pv_backup_*.yaml 2>/dev/null | grep -c 'kind: PersistentVolume')" >> /tmp/storage_operation_report.txt echo "操作后 PV 数量: $(kubectl get pv --no-headers | wc -l)" >> /tmp/storage_operation_report.txt echo "操作前 PVC 数量: $(cat /tmp/pvc_backup_*.yaml 2>/dev/null | grep -c 'kind: PersistentVolumeClaim')" >> /tmp/storage_operation_report.txt echo "操作后 PVC 数量: $(kubectl get pvc --all-namespaces --no-headers | wc -l)" >> /tmp/storage_operation_report.txt echo "异常状态 PV: $(kubectl get pv --field-selector=status.phase=Released --no-headers 2>/dev/null | wc -l) Released, $(kubectl get pv --field-selector=status.phase=Failed --no-headers 2>/dev/null | wc -l) Failed" >> /tmp/storage_operation_report.txt echo "异常状态 PVC: $(kubectl get pvc --all-namespaces --field-selector=status.phase=Pending --no-headers 2>/dev/null | wc -l) Pending" >> /tmp/storage_operation_report.txt cat /tmp/storage_operation_report.txt
验证流程
# 1. PV 状态验证 kubectl get pv -o wide # 预期:所有 PV 状态正常(Bound/Available),无 Failed # 2. PVC 状态验证 kubectl get pvc --all-namespaces -o wide # 预期:所有 PVC 状态为 Bound,无 Pending/Lost # 3. StorageClass 验证 kubectl get storageclass -o wide # 预期:StorageClass 存在,参数符合规范 # 4. 业务 Pod 验证 kubectl get pod --all-namespaces -o wide | grep -v Running | head -10 # 预期:无因存储问题导致的异常 Pod # 5. 存储读写验证 # kubectl exec -it <pod> -- touch /data/test.txt && echo "读写正常" # 预期:读写操作成功 # 6. 监控指标验证 # 检查 Prometheus 存储指标是否正常采集 # 预期:存储容量、IOPS、延迟等指标可查询 # 7. 操作报告验证 cat /tmp/storage_operation_report.txt # 预期:报告包含操作前后对比,无异常状态
排错方案
存储资源规划不足:检查业务增长预测、容量规划、扩容机制、监控预警、资源台账
存储性能瓶颈:检查 IOPS/带宽/延迟、存储类型、磁盘配置、网络带宽、缓存策略、并发控制
存储高可用不足:检查单点故障、多副本、跨节点、跨机房、容灾备份、故障切换、自愈机制
存储安全合规问题:检查权限管控、数据加密、访问审计、敏感数据脱敏、合规基线、漏洞修复
存储运维自动化不足:检查批量操作脚本、定时巡检、自动告警、自愈机制、台账管理、SOP 标准化
存储成本优化空间:检查闲置资源、冷热分层、压缩去重、容量规划、资源复用、成本监控
存储故障应急能力不足:检查应急预案、故障演练、备份恢复、快速切换、根因分析、复盘改进
存储版本兼容问题:检查 K8s 版本、存储驱动版本、CSI spec、内核版本、固件版本、升级验证