首页 > 解决方案 > configmap 更改不会自动反映在相应的 pod 上

问题描述

apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
    kind: Deployment
    metadata:
      name: consoleservice1
    spec:
      selector:
        matchLabels:
          app: consoleservice1
      replicas: 3 # tells deployment to run 3 pods matching the template
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 1
      minReadySeconds: 5
      template: # create pods using pod definition in this template
        metadata:
          labels:
            app: consoleservice1
        spec:
          containers:
          - name: consoleservice
            image: chintamani/insightvu:ms-console1
            readinessProbe:
              httpGet:
                path: /
                port: 8385
              initialDelaySeconds: 5
              periodSeconds: 5
              successThreshold: 1
            ports:
            - containerPort: 8384
            imagePullPolicy: Always
            volumeMounts:
              - mountPath: /deploy/config
                name: config
          volumes:
            - name: config
              configMap:
                name: console-config

为了创建 configmap,我使用了这个命令:

kubectl create configmap console-config --from-file=deploy/config

在 configmap 中更改时,它不会自动反映,每次我必须重新启动 pod。我怎样才能自动完成?

标签: kuberneteskubernetes-pod

解决方案


Pod 和 configmap 在 Kubernetes 中是完全独立的,如果 configmap 发生更改,Pod 不会自动重启。

实现这一目标的选择很少。

  1. 使用 wave,它是一个 Kubernetes 控制器,如果 configmap https://github.com/pusher/wave有任何变化,它会查找特定的注释并更新部署

  2. 使用https://github.com/stakater/Reloader,reloader 可以观察 configmap 的变化并且可以更新 pod 来选择新的配置。

        kind: Deployment
        metadata:
          annotations:
            reloader.stakater.com/auto: "true"
        spec:
          template:
            metadata:
    
  3. 您可以在部署和 CI/CD 中添加自定义 configHash 注释,或者在部署应用程序时使用yqconfigmap 的哈希替换该值,以便在 configmap 发生任何更改的情况下。Kubernetes 将检测部署注解的变化,并使用新配置重新加载 pod。

yq w --inplace deployment.yaml spec.template.metadata.annotations.configHash $(kubectl get cm/configmap -oyaml | sha256sum)

        apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
        kind: Deployment
        metadata:
          name: application
        spec:
          selector:
            matchLabels:
              app: consoleservice1
          replicas: 3              
          template:
            metadata:
              labels:
                app: consoleservice1
              annotations:
                configHash: ""

参考:这里


推荐阅读