All Products
Search
Document Center

Container Compute Service:Configure a pod to use a Secret

Last Updated:Dec 05, 2024

If your workload needs to connect to a backend database or verify the identity of a client, you need to store sensitive information in your cluster, such as usernames, passwords, and certificates. To prevent data leakage, we recommend that you store sensitive information in Secrets provided by Alibaba Cloud Container Compute Service (ACS). This topic describes how to create a Secret in the ACS console. This topic also describes how to mount a Secret as a volume to a pod and expose a Secret as environment variables for a pod.

Prerequisites

Create a Secret

The following example shows how to create a Secret named secret-test.

  1. Log on to the ACS console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side pane, choose Workloads > Deployments.

  3. On the Deployments page, click Create from YAML in the upper-right corner.

  4. Select a template or Custom, copy the following YAML content to the editor, and then click Create.

    The following YAML content is used to create a Secret named secret-test:

    apiVersion: v1
    kind: Secret
    metadata:
      name: secret-test
    type: Opaque
    data:
      username: YWRtaW4=  # The Base64-encoded ciphertext, which is admin in plaintext.   
      password: MTIzNDU=  # The Base64-encoded ciphertext, which is 12345 in plaintext.

You can also create a Secret in the ACS console. For more information, see Create a Secret.

Mount the Secret as a volume to a pod

You can mount the Secret as a volume to a pod by using the following methods:

Use the CLI

A mounted Secret can be used as a file in a pod. In this example, the Secret secret-test that contains the username and password information is stored as a file in the /srt directory.

  1. Create a file named example0.yaml and copy the following content to the file:

    apiVersion: v1
    kind: Pod
    metadata:
      name: pod0
    spec:
      containers:
      - name: redis
        image: redis
        volumeMounts:
        - name: srt
          mountPath: "/srt"
          readOnly: true
      volumes:
      - name: srt
        secret:
          secretName: secret-test
  2. Run the following command to create a pod to which the Secret secret-test is mounted:

    kubectl apply -f example0.yaml
  3. Run the following command to check whether the Secret is mounted to the pod:

    kubectl describe pod pod0 | grep -A 4 Volumes

    Expected output:

    Volumes:
      srt:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  secret-test
        Optional:    false

Use the ACS console

  1. Log on to the ACS console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side pane, choose Workloads > Deployments.

  3. On the Deployments page, click Create from Image.

  4. On the Basic Information wizard page, configure the parameters and click Next.

  5. On the Container wizard page, click Add Local Storage in the Volume section. Set PV Type to Secret, Mount Source to the Secret that you created in Create a Secret, and Container Path to the path to be accessed in the container. After you complete the configuration, click Next.

    The following figure shows an example on how to configure the volume.

    配置数据卷

  6. On the Advanced wizard page, configure the parameters and click Create.

Expose the Secret as environment variables for a pod

You can expose the Secret as environment variables for a pod by using the following methods:

Use the CLI

In this example, the username and password stored in the Secret secret-test are referenced in environment variables of a pod.

  1. Create a file named example1.yaml and copy the following content to the file:

    apiVersion: v1
    kind: Pod
    metadata:
      name: pod1
    spec:
      containers:
      - name: redis
        image: redis
        env:
          - name: USERNAME
            valueFrom:
              secretKeyRef:
                name: secret-test
                key: username
          - name: PASSWORD
            valueFrom:
              secretKeyRef:
                name: secret-test
                key: password
  2. Run the following command to create a pod to which the Secret secret-test is mounted:

    kubectl apply -f example1.yaml
  3. Run the following command to check whether the Secret is mounted to the pod:

    kubectl describe pod pod1 | grep -A 2 Environment

    Expected output:

        Environment:
          USERNAME:                 <set to the key 'username' in secret 'secret-test'>  Optional: false
          PASSWORD:                 <set to the key 'password' in secret 'secret-test'>  Optional: false

Use the ACS console

  1. Log on to the ACS console. In the left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side pane, choose Workloads > Deployments.

  3. On the Deployments page, click Create from Image in the upper-right corner.

  4. On the Basic Information wizard page, configure the parameters and click Next.

  5. On the Container wizard page, click 环境变量 in the Environments section. Set Type to Secrets, enter the name of the variable, and set Value/ValueFrom to the Secret that you created in Create a Secret and the username or password field in the Secret.

    The following figure shows an example on how to configure the environment variables.

    变量名称