Use secrets to configure a deployment

Secrets are a special kind of configmaps, and are recommended to be used to configure sensible information. All variable values must be stored in base64 encoding.

This is an example of a secret.

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: dXNlcm5hbWU=
  password: cGFzc3dvcmQ=

Load file to kubernetes cluster.

  kubectl apply -f <path>

Use in a deployment file.

apiVersion: v1
kind: Deployment
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret # file name of secret
            key: username # property name
            optional: false 

      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret # file name of secret
            key: password # property name
            optional: false 
  restartPolicy: Never

Last updated