Secrets

    You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see

    This topic discusses important properties of secrets and provides an overview on how developers can use them.

    The object type provides a mechanism to hold sensitive information such as passwords, OKD client configuration files, dockercfg files, private source repository credentials, and so on. Secrets decouple sensitive content from the pods. You can mount secrets into containers using a volume plug-in or the system can use secrets to perform actions on behalf of a pod.

    YAML Secret Object Definition

    YAML Opaque Secret Object Definition

    1. apiVersion: v1
    2. kind: Secret
    3. metadata:
    4. name: mysecret
    5. type: Opaque (1)
    6. data:
    7. username: dXNlci1uYW1l
    8. password: cGFzc3dvcmQ=
    1Specifies an opaque secret.

    Docker Configuration JSON File Secret Object Definition

    1. apiVersion: v1
    2. kind: Secret
    3. metadata:
    4. name: aregistrykey
    5. namespace: myapps
    6. type: kubernetes.io/dockerconfigjson (1)
    7. data:
    8. .dockerconfigjson:bm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg== (2)

    Key properties include:

    • Secret data can be referenced independently from its definition.

    • Secret data can be shared within a namespace.

    You must create a secret before creating the pods that depend on that secret.

    When creating secrets:

    • Create a secret object with secret data.

    • Update the pod’s service account to allow the reference to the secret.

    • Create a pod, which consumes the secret as an environment variable or as a file (using a secret volume).

    You can use the create command to create a secret object from a JSON or YAML file:

    The value in the type field indicates the structure of the secret’s key names and values. The type can be used to enforce the presence of user names and keys in the secret object. If you do not want validation, use the opaque type, which is the default.

    • kubernetes.io/service-account-token. Uses a service account token.

    • kubernetes.io/dockercfg. Uses the .dockercfg file for required Docker credentials.

    • kubernetes.io/dockerconfigjson. Uses the for required Docker credentials.

    • kubernetes.io/basic-auth. Use with Basic Authentication.

    • kubernetes.io/ssh-auth. Use with .

    • kubernetes.io/tls. Use with TLS certificate authorities

    Specify if you do not want validation, which means the secret does not claim to conform to any convention for key names or values. An opaque secret, allows for unstructured key:value pairs that can contain arbitrary values.

    You can specify other arbitrary types, such as example.com/my-secret-type. These types are not enforced server-side, but indicate that the creator of the secret intended to conform to the key/value requirements of that type.

    For examples of differet secret types, see the in Using Secrets.

    When you modify the value of a secret, the value (used by an already running pod) will not dynamically change. To change a secret, you must delete the original pod and create a new pod (perhaps with an identical PodSpec).

    Updating a secret follows the same workflow as deploying a new container image. You can use the kubectl rolling-update command.

    The resourceVersion value in a secret is not specified when it is referenced. Therefore, if a secret is updated at the same time as pods are starting, then the version of the secret will be used for the pod will not be defined.

    Secrets in Volumes and Environment Variables

    See of YAML files with secret data.

    After you create a secret, you can:

    1. Create the pod to reference your secret:

      1. $ oc create -f <your_yaml_file>.yaml
    2. Get the logs:

      1. $ oc logs secret-example-pod
    3. Delete the pod:

      1. $ oc delete pod secret-example-pod

    See for more information.

    Source Clone Secrets

    Service serving certificate secrets are intended to support complex middleware applications that need out-of-the-box certificates. It has the same settings as the server certificates generated by the administrator tooling for nodes and masters.

    To secure communication to your service, have the cluster generate a signed serving certificate/key pair into a secret in your namespace. To do this, set the service.alpha.openshift.io/serving-cert-secret-name annotation on your service with the value set to the name you want to use for your secret. Then, your PodSpec can mount that secret. When it is available, your pod will run. The certificate will be good for the internal service DNS name, <service.name>.<service.namespace>.svc.

    The certificate and key are in PEM format, stored in tls.crt and tls.key respectively. The certificate/key pair is automatically replaced when it gets close to expiration. View the expiration date in the service.alpha.openshift.io/expiry annotation on the secret, which is in RFC3339 format.

    Other pods can trust cluster-created certificates (which are only signed for internal DNS names), by using the CA bundle in the /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt file that is automatically mounted in their pod.

    The signature algorithm for this feature is x509.SHA256WithRSA. To manually rotate, delete the generated secret. A new certificate is created.

    Restrictions

    To use a secret, a pod needs to reference the secret. A secret can be used with a pod in three ways:

    • to populate environment variables for containers.

    • as files in a volume mounted on one or more of its containers.

    • by kubelet when pulling images for the pod.

    Volume type secrets write data into the container as a file using the volume mechanism. imagePullSecrets use service accounts for the automatic injection of the secret into all pods in a namespaces.

    When a template contains a secret definition, the only way for the template to use the provided secret is to ensure that the secret volume sources are validated and that the specified object reference actually points to an object of type **Secret**. Therefore, a secret needs to be created before any pods that depend on it. The most effective way to ensure this is to have it get injected automatically through the use of a service account.

    Secret API objects reside in a namespace. They can only be referenced by pods in that same namespace.

    Individual secrets are limited to 1MB in size. This is to discourage the creation of large secrets that would exhaust apiserver and kubelet memory. However, creation of a number of smaller secrets could also exhaust memory.

    Secret keys must be in a DNS subdomain.

    Example 1. YAML Secret That Will Create Four Files

    1File contains decoded values.
    2File contains decoded values.
    3File contains the provided string.
    4File contains the provided data.

    Example 2. YAML of a Pod Populating Files in a Volume with Secret Data

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: secret-example-pod
    5. spec:
    6. containers:
    7. - name: secret-test-container
    8. image: busybox
    9. command: [ "/bin/sh", "-c", "cat /etc/secret-volume/*" ]
    10. volumeMounts:
    11. # name must match the volume name below
    12. - name: secret-volume
    13. mountPath: /etc/secret-volume
    14. readOnly: true
    15. volumes:
    16. secret:
    17. secretName: test-secret
    18. restartPolicy: Never

    Example 3. YAML of a Pod Populating Environment Variables with Secret Data

    1. apiVersion: v1
    2. metadata:
    3. name: secret-example-pod
    4. spec:
    5. containers:
    6. - name: secret-test-container
    7. image: busybox
    8. command: [ "/bin/sh", "-c", "export" ]
    9. env:
    10. - name: TEST_SECRET_USERNAME_ENV_VAR
    11. valueFrom:
    12. secretKeyRef:
    13. name: test-secret
    14. key: username
    15. restartPolicy: Never

    Example 4. YAML of a Build Config Populating Environment Variables with Secret Data

    1. apiVersion: v1
    2. kind: BuildConfig
    3. metadata:
    4. name: secret-example-bc
    5. spec:
    6. strategy:
    7. sourceStrategy:
    8. env:
    9. - name: TEST_SECRET_USERNAME_ENV_VAR
    10. valueFrom:
    11. secretKeyRef:
    12. name: test-secret
    13. key: username

    Troubleshooting

    If a fails with (service’s **service.alpha.openshift.io/serving-cert-generation-error** annotation contains):

    1. $ oc delete secret <secret_name>
    2. $ oc annotate service <service_name> service.alpha.openshift.io/serving-cert-generation-error-