简体   繁体   中英

K8s, Helm Secrets and Volumes

I'm having a hard time putting together the K8s syntax for declaring a secret in a file, then mounting it in an accessible way. In short, in the simplest way, I want to store Postgres credentials (client key, client cert, server-ca cert) in files. When I try to install, it can't find the volume. I thought the volume is defined in the deployment yaml attached below.

How do I tell Helm/K8s that yes, these secrets should be mounted as volumes, create them if needed?

My secrets defined:

>kubectl get secrets |grep postgres
postgres-client-cert                             Opaque                                1      20m
postgres-client-key                              Opaque                                1      18m
postgres-server-ca                               Opaque                                1      18m

Failed attempt to deploy chart:

helm upgrade --install $APP-$TARGET_ENV ./.helm -f ./.helm/values-$TARGET_ENV.yaml -n $TARGET_ENV 
W0609 18:56:15.653459 1198995 gcp.go:120] WARNING: the gcp auth plugin is deprecated in v1.22+, unavailable in v1.25+; use gcloud instead.
To learn more, consult https://cloud.google.com/blog/products/containers-kubernetes/kubectl-auth-changes-in-gke
Error: UPGRADE FAILED: cannot patch "myapp-app" with kind Deployment: Deployment.apps "myapp-app" is invalid: [spec.template.spec.containers[0].volumeMounts[1].name: Not found: "postgres-client-key-volume", spec.template.spec.containers[0].volumeMounts[2].name: Not found: "postgres-server-ca-volume"]

.helm/templates/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
  labels:
    app: {{ .Chart.Name }}
    {{- include "app.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount | default 1 }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "app.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "app.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ .Chart.Name }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          env:
            - name: TIME_UPDATED
              value: {{ now | date "2006-01-02T15:04:05" }}
            - name: SENTRY_ENV
              value: {{ .Values.deployment.SENTRY_ENV }}
            - name: PORT
              value: {{ .Values.deployment.containerPort | quote }}
            {{- toYaml .Values.deployment.env | nindent 12 }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.sha1 | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
          - name: postgres-client-cert-volume 
            mountPath: "/var/run/secrets/postgres-client-cert"
            readOnly: true
          - name: postgres-client-key-volume 
            mountPath: "/var/run/secrets/postgres-client-key"
            readOnly: true
          - name: postgres-server-ca-volume 
            mountPath: "/var/run/secrets/postgres-server-ca"
            readOnly: true
          ports:
            - name: http
              containerPort: {{ .Values.deployment.containerPort }}
              protocol: TCP
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
        - name: postgres-client-cert-volume
          secret:
              secretName: postgres-client-cert
              optional: false
        - name: postgres-client-key
          secret:
              secretName: postgres-client-key
              optional: false
        - name: postgres-server-ca
          secret:
              secretName: postgres-server-ca
              optional: false
             
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}

Thanks, Woodsman

helm chart 正在寻找名为 postgres-server-ca-volume 的挂载,但您在第 74 行将其命名为 postgres-server-ca

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM