【问题标题】:Kubernetes Volume Binding results in stale dataKubernetes 卷绑定导致数据陈旧
【发布时间】:2020-02-19 02:13:43
【问题描述】:

我的 Kubernetes 部署是这样组成的:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{{ .Release.Name }}-{{ .Values.web.service.name }}"
  namespace: "{{ .Values.namespace }}"
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      name: "{{ .Values.web.deployment.selector }}"
  template:
    metadata:
      labels:
        name: "{{ .Values.web.deployment.selector }}"
    spec:
      {{- if $.Values.vault.serviceAccount }}
      serviceAccountName: "{{ $.Release.Name }}-vault-auth"
      automountServiceAccountToken: true
      {{- end }}
      volumes:
        - name: shared-data
          emptyDir: {}
        - name: vault-token
          emptyDir:
            medium: Memory
        - name: company-config
          configMap:
            name: "{{ .Release.Name }}-config"
            items:
              - key: companyRootCA.crt
                path: companyRootCA.crt
        - name: vault-consul-config
          configMap:
            name: "{{ .Release.Name }}-vault-configmap"
            items:
              - key: vault_agent.hcl
                path: vault_agent.hcl
              - key: consul_template_config.hcl
                path: consul_template_config.hcl
              - key: config.tmpl
                path: config.tmpl
      containers:
        - name: vault-agent-auth
          image: vault
          volumeMounts:
            - name: company-config
              mountPath: /etc/pki/ca-trust/source/anchors/companyRootCA.crt
              subPath: companyRootCA.crt
            - name: vault-consul-config
              mountPath: /etc/vault/vault_agent.hcl
              subPath: vault_agent.hcl
            - name: vault-token
              mountPath: /home/vault/
          env:
            - name: VAULT_ADDR
              value: "{{ .Values.vault.endpoint }}"
            - name: VAULT_NAMESPACE
              value: "company/devops/tarchon/{{ .Values.environmentName }}"
          args:
            [
              "agent",
              "-config=/etc/vault/vault_agent.hcl",
              "-log-level=debug"
            ]
        - name: consul-template
          image: hashicorp/consul-template:alpine
          imagePullPolicy: Always
          volumeMounts:
            - name: company-config
              mountPath: /etc/pki/ca-trust/source/anchors/companyRootCA.crt
              subPath: companyRootCA.crt
            - name: vault-consul-config
              mountPath: /etc/consul-template/consul_template_config.hcl
              subPath: consul_template_config.hcl
            - name: vault-token
              mountPath: /home/vault
            - name: vault-consul-config
              mountPath: /etc/templates/config.tmpl
              subPath: config.tmpl
            - name: shared-data
              mountPath: /etc/secrets
          env:
            - name: HOME
              value: /home/vault
            - name: VAULT_ADDR
              value: "{{ .Values.vault.endpoint }}"
            - name: VAULT_NAMESPACE
              value: "company/devops/tarchon/{{ .Values.environmentName }}"
          args:
            [
              "-config=/etc/consul-template/consul_template_config.hcl",
              "-log-level=trace",
            ]
        - name: "{{ .Values.web.service.name }}"
          image: "{{ .Values.image.registry }}:{{ .Values.image.tag }}"
          imagePullPolicy: "{{ .Values.image.imagePullPolicy }}"
          args: [
            "bash", 
            "-c", 
            "python manage.py collectstatic --noinput && gunicorn --bind :8000 --workers 3 ecops_cross_team_platform_backend.wsgi:application"
          ]
          volumeMounts:
            - name: shared-data
              mountPath: /usr/src/app/config.json
              subPath: config.json
          {{- if $.Values.environmentVariables }}
          env:
            {{- range $key, $value := $.Values.environmentVariables }}
            - name: {{ $key }}
              valueFrom:
                configMapKeyRef:
                  name: "{{ $.Release.Name }}-config"
                  key: {{ $key | quote }}
            {{- end }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.web.service.port }}
          resources: {}
      restartPolicy: Always
status: {}

consul-template 生成一个文件/etc/secrets/config.json,其凭据来自共享卷shared-data 中的 HashiCorp Vault。

在我的应用程序容器中,我将文件绑定到不同的目录 (/usr/src/app/config.json)(因为与consul-template 生成文件的位置相比,应用程序希望文件位于不同的目录中)。

问题是,每当文件config.json 在安装在consul-template 容器中的卷中更新时,修改不会传播到其他容器,所以我最终得到的应用程序容器具有陈旧的非工作数据。

一开始我以为是readOnlyvolumeMount选项引起的问题,但是去掉后问题依旧存在。

【问题讨论】:

  • 您能否执行到您的 pod 并验证是否生成了 /etc/secrets/config.json,如果还有其他问题也可以记录。还有一个建议(不知道是否可行),尝试从其他容器中删除 subPath 并提供/usr/src/app
  • 是的,/etc/secrets/config.json 文件在创建 pod 时在两个容器中正确生成。问题是 24 小时后(因为 Vault 凭据 TTL 是 24 小时),consul-template/etc/secrets/config.json 文件中生成一个新凭据,它应该传播到其他容器,但由于这个应用程序由于它使用过时而中断非工作数据库凭据。
  • 我试图删除 subPath,但它覆盖了整个 /usr/src/app 文件夹,破坏了整个应用程序。
  • 是的,它会覆盖,这是预期的行为。但想检查它是否正在更新文件。如果您在该文件夹中只有文件,它应该可以工作。如果有办法可以更改配置路径,那么你可以试试这个。

标签: kubernetes


【解决方案1】:

正如anmol在cmets中所说,确实是在目录中使用subPath绑定单个文件。

解决方案是删除 subPath 并将shared-data 卷绑定到一个单一范围的文件夹(即/usr/src/app/credentials),这样就不会出现其他问题。

解决方案:

volumeMounts:
  - name: shared-data
    mountPath: /usr/src/app/credentials

代替:

volumeMounts:
  - name: shared-data
    mountPath: /usr/src/app/config.json
    subPath: config.json

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 2014-11-22
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多