You need to enable JavaScript to run this app.
Volcengine Kubernetes Engine

Volcengine Kubernetes Engine

Copy page
Download PDF
Configuration management
Using Secrets in containers
Copy page
Download PDF
Using Secrets in containers
This topic describes the scenarios and procedures for using Secrets in containers.​
Background​
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.​
​
Usage methods​
Description​
Application scenario​
Environment variable injection​
​
Injects key-value pairs from the Secret directly into container environment variables.​
Short sensitive parameters accessed via environment variables in code: Database passwords, API keys, connection strings, and more.​
Data volume mounting​
Mounts data stored in the Secret as a file into a specified container directory. This method supports automatic synchronization and hot updates.​
File-based sensitive data read by applications: TLS certificates, private keys, kubeconfig, complete credential files, and more.​
​
Preparation​
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.​
​
apiVersion: v1​
kind: Secret​
metadata:​
name: demo-secret # Secret name. Workloads use this name to reference the Secret.​
namespace: default # Namespace of the Secret. The namespace must match the target workload's namespace.​
type: Opaque # General Secret of type Opaque. It stores any user-defined data.​
data:​
# The values in the data field must be a Base64-encoded string.​
username: YWRtaW4= # Base64-encoded result of the plaintext admin.​
password: MTIzNDU2 # Base64-encoded result of the plaintext 123456.​
stringData:​
# The stringData field accepts plaintext input, which Kubernetes automatically encodes to Base64 upon creation.​
DB_HOST: mysql.default.svc.cluster.local​
DB_PORT: "3306"​
​
Procedure​
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
​
  1. Log in to the VKE console. ​
  1. In the left navigation bar, click Cluster.​
  1. On the Cluster page, click the name of the target cluster.​
  1. Click Workload > Deployment. On the Deployment page, click Create Deployment.​
  1. 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.​
  1. Click Confirm to create the Deployment.​
  1. Check the Deployment creation result and verify whether the environment variables have been injected successfully.​
  1. On the Deployment page, find the newly created Deployment and click its name to enter the Deployment details page.​
  1. 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.​
  1. 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.​
  • ​
    env | grep -E "username|password|DB_HOST|DB_PORT"​
    ​
  • The output is as follows:​
  • ​
    username=admin​
    password=123456​
    DB_HOST=mysql.default.svc.cluster.local​
    DB_PORT=3306​
    ​
​
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
​
  1. Log in to the VKE console. ​
  1. In the left navigation bar, click Cluster.​
  1. On the Cluster page, click the name of the target cluster.​
  1. Click Workload > Deployment. On the Deployment page, click Create Deployment.​
  1. 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.​
  1. Click Confirm to create the Deployment.​
  1. Check the Deployment creation result and verify whether the environment variables have been injected successfully.​
  1. On the Deployment page, find the newly created Deployment and click its name to enter the Deployment details page.​
  1. 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.​
  1. 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.​
  • ​
    ls /etc/secret/​
    ​
  • If the output includes DB_HOST, DB_PORT, password, and username, the mounting is successful.​
  1. Run the following command to check whether the Secret's content is correct.​
  • ​
    $ cat /etc/secret/password​
    ​
  • If the output is the value of password (such as 123456), the Secret's content is correct.​
​
Subsequent operations​
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.​
Last updated: 2026.07.24 14:51:38