This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.3.10. What is a secret?
The Secret object type provides a mechanism to hold sensitive information such as passwords, OpenShift Container Platform 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
- 1
- Indicates the structure of the secret’s key names and values.
- 2
- The allowable format for the keys in the
datafield must meet the guidelines in theDNS_SUBDOMAINvalue in the Kubernetes identifiers glossary. - 3
- The value associated with keys in the
datamap must be base64 encoded. - 4
- Entries in the
stringDatamap are converted to base64 and the entry are then moved to thedatamap automatically. This field is write-only. The value is only be returned by thedatafield. - 5
- The value associated with keys in the
stringDatamap is made up of plain text strings.
3.10.1. Properties of secrets 复制链接链接已复制到粘贴板!
Key properties include:
- Secret data can be referenced independently from its definition.
- Secret data volumes are backed by temporary file-storage facilities (tmpfs) and never come to rest on a node.
- Secret data can be shared within a namespace.
3.10.2. Types of Secrets 复制链接链接已复制到粘贴板!
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.
Specify one of the following types to trigger minimal server-side validation to ensure the presence of specific key names in the secret data:
-
kubernetes.io/service-account-token. Uses a service account token. -
kubernetes.io/dockercfg. Uses the.dockercfgfile for required Docker credentials. -
kubernetes.io/dockerconfigjson. Uses the.docker/config.jsonfile for required Docker credentials. -
kubernetes.io/basic-auth. Use with basic authentication. -
kubernetes.io/ssh-auth. Use with SSH key authentication. -
kubernetes.io/tls. Use with TLS certificate authorities.
Specify type= Opaque 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.
3.10.3. Updates to secrets 复制链接链接已复制到粘贴板!
When you modify the value of a secret, the value used by an already running pod does not dynamically change. To change a secret, you must delete the original pod and create a new pod, in some cases 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, the version of the secret that is used for the pod is not defined.
Currently, it is not possible to check the resource version of a secret object that was used when a pod was created. It is planned that pods report this information, so that a controller could restart ones using a old resourceVersion. In the interim, do not update the data of existing secrets, but create new ones with distinct names.
3.10.4. Creating secrets 复制链接链接已复制到粘贴板!
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 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
secretvolume.
Procedure
Use the create command to create a secret object from a JSON or YAML file:
oc create -f <filename>
$ oc create -f <filename>Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, you can create a secret from your local
.docker/config.jsonfile:oc create secret generic dockerhub \ --from-file=.dockerconfigjson=<path/to/.docker/config.json> \ --type=kubernetes.io/dockerconfigjson$ oc create secret generic dockerhub \ --from-file=.dockerconfigjson=<path/to/.docker/config.json> \ --type=kubernetes.io/dockerconfigjsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command generates a JSON specification of the secret named
dockerhuband creates the object.Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Specifies an opaque secret.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.10.4.1. Using secrets 复制链接链接已复制到粘贴板!
After creating secrets, you can create a pod to reference your secret, get logs, and delete the pod.
Procedure
Create the pod to reference your secret:
oc create -f <your_yaml_file>.yaml
$ oc create -f <your_yaml_file>.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Get the logs:
oc logs secret-example-pod
$ oc logs secret-example-podCopy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the pod:
oc delete pod secret-example-pod
$ oc delete pod secret-example-podCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
Example YAML files with secret data:
YAML Secret That Will Create Four Files
Copy to Clipboard Copied! Toggle word wrap Toggle overflow YAML of a Pod Populating Files in a Volume with Secret Data
Copy to Clipboard Copied! Toggle word wrap Toggle overflow YAML of a Pod Populating Environment Variables with Secret Data
Copy to Clipboard Copied! Toggle word wrap Toggle overflow YAML of a Build Config Populating Environment Variables with Secret Data
Copy to Clipboard Copied! Toggle word wrap Toggle overflow