All Products
Search
Document Center

Use the Downward API

Last Updated: Nov 24, 2021

The Downward API allows you to generate a file from pod information and mount the file to the directory in which a volume is mounted in a container. This topic describes how to use the Downward API by mounting a volume.

Prerequisites

Virtual Kubelet is deployed in a Kubernetes cluster.

Note

By default, Alibaba Cloud Serverless Kubernetes (ASK) clusters are integrated with Virtual Kubelet. For other types of clusters, you must deploy Virtual Kubelet in the clusters. For more information, see Connect Kubernetes to Elastic Container Instance.

Parameter description

The following table describes the pod parameters whose values can be mounted to a directory in which a volume is mounted in a container.

Parameter

Description

metadata.name

The name of the pod.

metadata.namespace

The namespace of the pod.

metadata.uid

The UID of the pod.

metadata.labels['<KEY>']

The label value of the pod.

metadata.annotations['<KEY>']

The annotation value of the pod.

spec.serviceAccountName

The name of the pod service account.

spec.nodeName

The name of the node.

status.podIP

The IP address of the node.

Note

You can use the Downward API to mount pod fields, but you cannot mount container fields such as limits.cpu, requests.cpu, limits.memory, requests.memory, limits.ephemeral-storage, and requests.ephemeral-storage.

Configuration example

  1. Create the YAML configuration file of the pod.

    Create a pod.yaml file. The following file content specifies that metadata.labels and metadata.annotations are mounted to the /etc/podinfo directory as files.

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-downwardapi
      labels:
        region: cn-beijing
        zone: cn-beijing-h
      annotations:
        eci/test: virtual-kubelet
    spec:
    
      containers:
        - name: nginx
          image: nginx
          command: ["sh", "-c"]
          args:
          - while true; do cat /etc/podinfo/labels; sleep 5; cat /etc/podinfo/annotations; sleep 5; done
          volumeMounts:
            - name: podinfo
              mountPath: /etc/podinfo
      volumes:
        - name: podinfo
          downwardAPI:
            items:
              - path: "labels"
                fieldRef:
                  fieldPath: metadata.labels
              - path: "annotations"
                fieldRef:
                  fieldPath: metadata.annotations
  2. Create a pod.

    kubectl create -f pod.yaml
  3. View the results.

    View the file directory in the pod. The label and annotation information about the pod is mounted to the labels and annotations files in the /etc/podinfo directory.

    downwardapi