如何让NATS Helm重启后关联已创建的多个PVC
NATS Helm Chart重启后StatefulSet关联已有PVC的解决方案
背景
初始部署NATS时,通过Helm Chart提供Persistent Volume(PV)后,系统自动创建了3副本StatefulSet及对应Persistent Volume Claim(PVC):
- StatefulSet副本:
nats-0、nats-1、nats-2 - 自动生成的PVC:
nats-js-pvc-nats-0、nats-js-pvc-nats-1、nats-js-pvc-nats-2
手动提供的3个PV(存放在infra目录):
apiVersion: v1 kind: PersistentVolume metadata: name: nats-pv-0 spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: standard hostPath: path: /data/my-nats-pv-0
apiVersion: v1 kind: PersistentVolume metadata: name: nats-pv-0 spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: standard hostPath: path: /data/my-nats-pv-0
apiVersion: v1 kind: PersistentVolume metadata: name: nats-pv-1 spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: standard hostPath: path: /data/my-nats-pv-2
问题
集群重启后,新创建的StatefulSet无法关联已有的PVC,Pod长期处于停滞状态。执行kubectl describe nats-<POD编号>查看,Pod提示找不到PVC,且PV与PVC已处于持久化绑定状态。
已尝试的无效配置
尝试1:使用变量占位符
############################ # # # Jetstream File Storage # # # ############################ fileStorage: enabled: true storageDirectory: /data # Set for use with existing PVC existingClaim: nats-js-pvc-$(POD_NAME) #jetstream-pvc claimStorageSize: 1Gi # Use below block to create new persistent volume # only used if existingClaim is not specified # size: 1Gi # storageClassName: standard # accessModes: # - ReadWriteOnce # annotations: # key: "value" # Use below if fileStorage is not enabled but you are persisting # data using an alternative to PVC (e.g. hostPath) # These set the corresponding jetstream configuration in nats.conf. # store_dir: "/data" # max_file: "10Gi"
尝试2:数组形式指定PVC列表
############################ # # # Jetstream File Storage # # # ############################ fileStorage: enabled: true storageDirectory: /data # Set for use with existing PVC existingClaim: - name: nats-js-pvc-nats-0 - name: nats-js-pvc-nats-1 - name: nats-js-pvc-nats-2 #jetstream-pvc claimStorageSize: 1Gi # Use below block to create new persistent volume # only used if existingClaim is not specified # size: 1Gi # storageClassName: standard # accessModes: # - ReadWriteOnce # annotations: # key: "value" # Use below if fileStorage is not enabled but you are persisting # data using an alternative to PVC (e.g. hostPath) # These set the corresponding jetstream configuration in nats.conf. # store_dir: "/data" # max_file: "10Gi"
尝试3:数组形式带存储大小指定PVC
############################ # # # Jetstream File Storage # # # ############################ fileStorage: enabled: true storageDirectory: /data # Set for use with existing PVC existingClaim: - name: nats-js-pvc-nats-0 claimStorageSize: 1Gi - name: nats-js-pvc-nats-1 claimStorageSize: 1Gi - name: nats-js-pvc-nats-2 claimStorageSize: 1Gi # Use below block to create new persistent volume # only used if existingClaim is not specified # size: 1Gi # storageClassName: standard # accessModes: # - ReadWriteOnce # annotations: # key: "value" # Use below if fileStorage is not enabled but you are persisting # data using an alternative to PVC (e.g. hostPath) # These set the corresponding jetstream configuration in nats.conf. # store_dir: "/data" # max_file: "10Gi"
尝试4:数组字符串形式指定PVC
############################ # # # Jetstream File Storage # # # ############################ fileStorage: enabled: true storageDirectory: /data # Set for use with existing PVC existingClaim: [nats-js-pvc-nats-0,nats-js-pvc-nats-1,nats-js-pvc-nats-2] #jetstream-pvc claimStorageSize: 1Gi # Use below block to create new persistent volume # only used if existingClaim is not specified # size: 1Gi # storageClassName: standard # accessModes: # - ReadWriteOnce # annotations: # key: "value" # Use below if fileStorage is not enabled but you are persisting # data using an alternative to PVC (e.g. hostPath) # These set the corresponding jetstream configuration in nats.conf. # store_dir: "/data" # max_file: "10Gi"
注:还尝试过nats-js-pvc-$(POD_NAME)、nats-js-pvc-(POD_NAME)等变量形式
正确配置方案
针对StatefulSet的特性,每个Pod需要绑定对应编号的PVC,需使用Helm模板变量动态匹配。正确配置如下:
############################ # # # Jetstream File Storage # # # ############################ fileStorage: enabled: true storageDirectory: /data # 关联已有PVC:使用Helm模板变量匹配Pod名称 existingClaim: nats-js-pvc-{{ .Pod.Name }} claimStorageSize: 1Gi # 注释自动创建PVC的配置,避免Helm尝试生成新PVC # size: 1Gi # storageClassName: standard # accessModes: # - ReadWriteOnce # annotations: # key: "value" # Use below if fileStorage is not enabled but you are persisting # data using an alternative to PVC (e.g. hostPath) # These set the corresponding jetstream configuration in nats.conf. # store_dir: "/data" # max_file: "10Gi"
关键说明
- 动态匹配PVC:利用Helm模板变量
{{ .Pod.Name }},StatefulSet的每个Pod(nats-0/nats-1/nats-2)会自动关联对应的PVC(nats-js-pvc-nats-0/nats-js-pvc-nats-1/nats-js-pvc-nats-2) - 配置格式:
existingClaim需为字符串形式,而非数组,StatefulSet的卷声明模板会为每个Pod生成独立的卷引用 - 前置检查:
- 确保PVC与NATS实例处于同一Namespace
- 确认PVC状态为
Bound,对应PV存在且可用
- 生效方式:执行
helm upgrade <RELEASE_NAME> nats/nats -f <配置文件路径>更新部署,使配置生效
内容的提问来源于stack exchange,提问作者ASG4
相关产品推荐
相关产品推荐

