This topic describes the scenarios and procedures for using Secrets in containers.
Enterprise application deployment often involves sensitive information such as database passwords, API keys, and TLS certificates. Hardcoding this information into images or workload YAML files poses serious security risks and environment dependency issues. Secrets decouple sensitive data from applications by storing passwords, keys, certificates, and other non-plaintext configurations separately and enable granular access control in combination with Kubernetes RBAC and namespace isolation.
Secrets serve a similar purpose to ConfigMaps, with the key distinction being that Secrets are dedicated to sensitive data and stored in Base64-encoded format in etcd, while ConfigMaps store non-sensitive plaintext configurations.
After creating a Secret, you can use it in containers via two methods.
Prepare the Secret in advance. For details, see Creating Secrets. To facilitate validation of Secret usage, this topic uses the sample Secret file demo-secret.yaml below. Scenario 1: Environment variable injection
This section illustrates how to use a Secret to set environment variables for a workload (in this example, a Deployment).
Configure through the console
Configure through kubectl
- In the left navigation bar, click Cluster.
- On the Cluster page, click the name of the target cluster.
- Click Workload > Deployment. On the Deployment page, click Create Deployment.
- On the Create Deployment page, configure the following parameters as described below and other parameters as needed. For detailed instructions, see Creating Deployments.
- In Step 2 Container configuration, click the + icon in the Environment variables section to use the Secret prepared in the prerequisites as the environment variables for the Deployment.
- Click Confirm to create the Deployment.
- Check the Deployment creation result and verify whether the environment variables have been injected successfully.
- On the Deployment page, find the newly created Deployment and click its name to enter the Deployment details page.
- On the Deployment details page, click the List of instances tab to view whether all pods corresponding to the Deployment are in the status of Running.
- Click the Pod name to enter the Pods details page. Click Console in the upper-right corner of the page to log in to the Pod and then run the following command to verify whether the environment variables have been injected successfully.
- The output is as follows:
- The following Deployment YAML file secret-env-demo.yaml serves as an example. For detailed YAML configuration instructions, see Creating Deployments.
- Run the following command to create the Deployment.
- Run the following command to check whether the workload is running normally. If the Pods are in the Running state, the Deployment has been created successfully.
- Run the following command to verify that the environment variables have been injected successfully.
- The expected output is as follows: the Secret is automatically decoded to plaintext when injected into environment variables.
Scenario 2: Data volume mounting
This section illustrates how to mount a Secret as a data volume for a workload (in this example, a Deployment).
Configure through the console
Configure through kubectl
- In the left navigation bar, click Cluster.
- On the Cluster page, click the name of the target cluster.
- Click Workload > Deployment. On the Deployment page, click Create Deployment.
- On the Create Deployment page, configure the following parameters as described below and other parameters as needed. For detailed instructions, see Creating Deployments.
- In Step 2 Container configuration, configure Storage configuration parameters as described below.
- For Add volume, click the + icon and configure the Secret as a volume.
- For Mount volume, click the + icon to mount the Secret.
- Click Confirm to create the Deployment.
- Check the Deployment creation result and verify whether the environment variables have been injected successfully.
- On the Deployment page, find the newly created Deployment and click its name to enter the Deployment details page.
- On the Deployment details page, click the List of instances tab to view whether all pods corresponding to the Deployment are in the status of Running.
- Click the Pod name to enter the Pods details page. Click Console in the upper-right corner of the page to log in to the Pod and then run the following command to verify whether the Secret has been mounted successfully.
- If the output includes DB_HOST, DB_PORT, password, and username, the mounting is successful.
- Run the following command to check whether the Secret's content is correct.
- If the output is the value of password (such as 123456), the Secret's content is correct.
- The following Deployment YAML file secret-volume-demo.yaml serves as an example. For detailed YAML configuration instructions, see Creating Deployments.
- Run the following command to create the Deployment.
- Run the following command to check whether the workload is running normally. If the Pods are in the Running state, the Deployment has been created successfully.
- Run the following command to verify that the Secret is mounted successfully.
- The output should include the username file (the Deployment mounts only the username key).
- Run the following command to check whether the Secret data is correct.
- The output should be the decoded plaintext admin.
To apply Secret updates to your workload promptly, follow the instructions below:
- If you modify environment variables using a Secret, you need to restart the Pods for the changes to take effect.
- The default synchronization cycle is approximately 60 seconds, with a possible maximum delay of 1-2 minutes. To apply the changes immediately, restart the Pods.
tip
Files mounted via subPath will not be updated automatically. You need to restart the Pods manually.