A secret is a Kubernetes resource used to store sensitive information, such as authentication credentials and keys required by workloads. The content of a secret is defined by users. After a secret is created, containerized workloads can securely use it through volume mounts or environment variable injection.
Secrets decouple sensitive data from container images, preventing sensitive data from being exposed in plaintext within images or configuration files. This significantly reduces the risk of data leakage and improves the security and portability of workloads.
Secrets cannot be used in static pods.
Parameter | Description |
|---|---|
Name | Name of the secret you create, which must be unique. |
Namespace | Namespace to which the secret belongs. If you do not specify this parameter, the value default is used by default. |
Description | Description of a secret. |
Secret Type | Type of the secret you create.
|
Data | Workload secret data can be used in containers.
|
Tag Management (Optional) | Labels of the secret. Click Add Label and enter key-value pairs. The key and value must contain 1 to 63 characters that start and end with a letter or digit. Only letters, digits, hyphens (-), underscores (_), and periods (.) are allowed. |
The new secret is displayed in the key list.
# echo -n "content-to-be-encoded" | base64******
vi cce-secret.yaml
The following YAML file uses the Opaque type as an example. For details about other types, see Secret Resource File Configuration Example.
apiVersion: v1kind: Secretmetadata:name: mysecrettype: Opaquedata:<your_key>: <your_value><your_key>: <your_value> # Enter a key-value pair. The value must be encoded using Base64.
kubectl create -f cce-secret.yaml
You can query the secret after creation.
kubectl get secret -n default
This section describes configuration examples of secret resource description files.
The secret.yaml file is defined as shown below. The data field is filled in as a key-value pair, and the value field must be encoded using Base64. For details, see Base64 Encoding.
apiVersion: v1kind: Secretmetadata:name: mysecret #Secret namenamespace: default #Namespace. The default value is defaultdefault.data:<your_key>: <your_value><your_key>: <your_value> # Enter a key-value pair. The value must be encoded using Base64.type: Opaquetype: Opaque
The secret.yaml file is defined as shown below. The value of .dockerconfigjson must be encoded using Base64. For details, see Base64 Encoding.
apiVersion: v1kind: Secretmetadata:name: mysecret #Secret namenamespace: default #Namespace. The default value is defaultdefault.data:.dockerconfigjson: eyJh.dockerconfigjson: eyJh********** # Content encoded using Base64.type: kubernetes.io/dockerconfigjsontype: kubernetes.io/dockerconfigjson
To obtain the .dockerconfigjson content, take the following steps:
echo -n "username:password" | base64
Command output:
dXNlcm5hbWU6cGFzc3dvcmQ=
echo -n '{"auths":{"addressaddress":{"username":"usernameusername","password":"passwordpassword","auth":"dXNlcm5hbWU6cGFzc3dvcmQ=dXNlcm5hbWU6cGFzc3dvcmQ="}}}' | base64
Command output:
eyJhdXRocyI6eyJhZGRyZXNzIjp7InVzZXJuYW1lIjoidXNlcm5hbWUiLCJwYXNzd29yZCI6InBhc3N3b3JkIiwiYXV0aCI6ImRYTmxjbTVoYldVNmNHRnpjM2R2Y21RPSJ9fX0=
The encoded content is the .dockerconfigjson content.
The value of tls.crt and tls.key must be encoded using Base64. For details, see Base64 Encoding.
kind: SecretapiVersion: v1metadata:name: mysecret #Secret namenamespace: default #Namespace. The default value is defaultdefault.data:tls.crt: LS0tLS1CRU*****FURS0tLS0tLS0tLS1CRU*****FURS0tLS0t # Certificate content, which must be encoded using Base64.tls.key: LS0tLS1CRU*****VZLS0tLS0=LS0tLS1CRU*****VZLS0tLS0= # Private key content, which must be encoded using Base64.type: kubernetes.io/tlstype: kubernetes.io/tls
The value of tls.crt and tls.key must be encoded using Base64. For details, see Base64 Encoding.
kind: SecretapiVersion: v1metadata:name: mysecret #Secret namenamespace: default #Namespace. The default value is defaultdefault.data:tls.crt: LS0tLS1CRU*****FURS0tLS0tLS0tLS1CRU*****FURS0tLS0t # Certificate content, which must be encoded using Base64.tls.key: LS0tLS1CRU*****VZLS0tLS0=LS0tLS1CRU*****VZLS0tLS0= # Private key content, which must be encoded using Base64.type: IngressTLStype: IngressTLS
To perform Base64 encoding on a string, run the following command:
echo -n "Content to be encoded" | base64
After creating a secret, you can update or delete it as described in Table 2.
The secret list contains system secret resources that can be queried only. The system secret resources cannot be updated or deleted.
Operation | Description |
|---|---|
Editing a secret's YAML file | Click Edit YAML in the row where the target secret resides to edit its YAML file. |
Updating a secret |
|
Deleting a secret | Select the secret to be deleted and choose More > Delete. Follow the prompts to delete the secret. |
Deleting secrets in batches |
|