Do not perform any operation on the following secrets. For details, see Cluster Secrets.
The following example shows how to use a secret.
apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque data: username: ****** #The value must be Base64-encoded. password: ****** #The value must be encoded using Base64.
However, when a secret data volume mounted in subPath mode is updated, Kubernetes cannot automatically update the data in the data volume.
Using the CCE console
When creating a workload, click Environment Variables in the Container Settings area, and click Add Variable.
For example, after you import the value of username in secret mysecret as the value of workload environment variable username, an environment variable named username exists in the container.
After the workload runs properly, log in to the container and run the following statement to check whether the secret has been set as an environment variable of the workload:
printenv username
If the output is the same as the content in the secret, the secret has been set as an environment variable of the workload.
Using kubectl
vi nginx-secret.yaml
Content of the YAML file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-secret
spec:
replicas: 1
selector:
matchLabels:
app: nginx-secret
template:
metadata:
labels:
app: nginx-secret
spec:
containers:
- name: container-1
image: nginx:latest
envFrom: # Use envFrom to specify a secret to be referenced by environment variables.
- secretRef:
name: mysecret # Name of the referenced secret.
imagePullSecrets:
- name: default-secret
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-secret spec: replicas: 1 selector: matchLabels: app: nginx-secret template: metadata: labels: app: nginx-secret spec: containers: - name: container-1 image: nginx:latest env: # Set the environment variable in the workload. - name: SECRET_USERNAME # Name of the environment variable in the workload. valueFrom: # Use valueFrom to specify a secret to be referenced by environment variables. secretKeyRef: name: mysecret # Name of the referenced secret. key: username # Key in the referenced secret. - name: SECRET_PASSWORD # Add multiple environment variables to import them at the same time. valueFrom: secretKeyRef: name: mysecret key: password imagePullSecrets: - name: default-secret
kubectl apply -f nginx-secret.yaml
kubectl get pod | grep nginx-secret
nginx-secret-*** 1/1 Running 0 2m18s
kubectl exec nginx-secret-*** -- printenv SPECIAL_USERNAME SPECIAL_PASSWORD
If the output is the same as the content in the secret, the secret has been set as an environment variable of the workload.
You can mount a secret as a volume to the specified container path. Contents in a secret are user-defined. Before that, create a secret. For details, see Creating a Secret.
Using the CCE console
When creating a workload, click Data Storage in the Container Settings area. Click Add Volume and select Secret from the drop-down list.
Parameter |
Description |
---|---|
Secret |
Select the desired secret. A secret must be created beforehand. For details, see Creating a Secret. |
Mount Path |
Enter a mount point. After the secret volume is mounted, a secret file with the key as the file name and value as the file content is generated in the mount path of the container. This parameter specifies a container path to which a data volume will be mounted. Do not mount the volume to a system directory such as / or /var/run. This may cause container errors. Mount the volume to an empty directory. If the directory is not empty, ensure that there are no files that affect container startup. Otherwise, the files will be replaced, which leads to a container startup failure or workload creation failure.
NOTICE:
If the container is mounted to a high-risk directory, use an account with minimum permissions to start the container. Otherwise, high-risk files on the host may be damaged. |
Subpath |
Enter a subpath of the mount path.
|
Permission |
Read-only, indicating that data volume in the path is read-only. |
After the workload runs properly, the username and password files will be generated in the /etc/foo directory in this example. The contents of the files are secret values.
Access the container and run the following statement to view the username or password file in the container:
cat /etc/foo/username
The expected output is the same as the content in the secret.
Using kubectl
vi nginx-secret.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-secret spec: replicas: 1 selector: matchLabels: app: nginx-secret template: metadata: labels: app: nginx-secret spec: containers: - name: container-1 image: nginx:latest volumeMounts: - name: foo mountPath: /etc/foo # Mount to the /etc/foo directory. readOnly: true volumes: - name: foo secret: secretName: mysecret # Name of the referenced secret.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-secret spec: replicas: 1 selector: matchLabels: app: nginx-secret template: metadata: labels: app: nginx-secret spec: containers: - name: container-1 image: nginx:latest volumeMounts: - name: foo mountPath: /etc/foo # Mount to the /etc/foo directory. readOnly: true volumes: - name: foo secret: secretName: mysecret # Name of the referenced secret. items: - key: username # Name of the referenced key. path: my-group/my-username # Mapping path of the secret key
kubectl apply -f nginx-secret.yaml
kubectl get pod | grep nginx-secret
nginx-secret-*** 1/1 Running 0 2m18s
kubectl exec nginx-secret-*** -- cat /etc/foo/username
The expected output is the same as the content in the secret.