Consume secrets using the Downward API
When creating pods, you can use the downward API to inject secrets so image and application authors can create an image for specific environments.
Procedure
-
Create a secret to inject:
-
Create a
secret.yamlfile similar to the following:apiVersion: v1 kind: Secret metadata: name: mysecret data: password: <password> username: <username> type: kubernetes.io/basic-auth -
Create the secret object from the
secret.yamlfile by using the following command:$ oc create -f secret.yaml
-
-
Create a pod that references the
usernamefield from the aboveSecretobject:-
Create a
pod.yamlfile similar to the following:apiVersion: v1 kind: Pod metadata: name: dapi-env-test-pod spec: securityContext: runAsNonRoot: true seccompProfile: type: RuntimeDefault containers: - name: env-test-container image: gcr.io/google_containers/busybox command: [ "/bin/sh", "-c", "env" ] env: - name: MY_SECRET_USERNAME valueFrom: secretKeyRef: name: mysecret key: username securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL] restartPolicy: Never # ... -
Create the pod from the
pod.yamlfile by using the following command:$ oc create -f pod.yaml
-
Verification
-
Check the container logs for the
MY_SECRET_USERNAMEvalue by using the following command:$ oc logs -p dapi-env-test-pod