concept downwardAPI volume in category kubernetes

appears as: downwardAPI volume, downwardAPI volumes
Kubernetes in Action

This is an excerpt from Manning's book Kubernetes in Action.

Both these problems are solved by the Kubernetes Downward API. It allows you to pass metadata about the pod and its environment through environment variables or files (in a downwardAPI volume). Don’t be confused by the name. The Downward API isn’t like a REST endpoint that your app needs to hit so it can get the data. It’s a way of having environment variables or files populated with values from the pod’s specification or status, as shown in figure 8.1.

8.1.3. Passing metadata through files in a downwardAPI volume

If you prefer to expose the metadata through files instead of environment variables, you can define a downwardAPI volume and mount it into your container. You must use a downwardAPI volume for exposing the pod’s labels or its annotations, because neither can be exposed through environment variables. We’ll discuss why later.

Listing 8.3. Pod with a downwardAPI volume: downward-api-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: downward
  labels:                                     #1
    foo: bar                                  #1
  annotations:                                #1
    key1: value1                              #1
    key2: |                                   #1
      multi                                   #1
      line                                    #1
      value                                   #1
spec:
  containers:
  - name: main
    image: busybox
    command: ["sleep", "9999999"]
    resources:
      requests:
        cpu: 15m
        memory: 100Ki
      limits:
        cpu: 100m
        memory: 4Mi
    volumeMounts:                             #2
    - name: downward                          #2
      mountPath: /etc/downward                #2
  volumes:
  - name: downward                            #3
    downwardAPI:                              #3
      items:
      - path: "podName"                       #4
        fieldRef:                             #4
          fieldPath: metadata.name            #4
      - path: "podNamespace"
        fieldRef:
          fieldPath: metadata.namespace
      - path: "labels"                            #5

        fieldRef:                                 #5

          fieldPath: metadata.labels              #5

      - path: "annotations"                       #6
        fieldRef:                                 #6
          fieldPath: metadata.annotations         #6
      - path: "containerCpuRequestMilliCores"
        resourceFieldRef:
          containerName: main
          resource: requests.cpu
          divisor: 1m
      - path: "containerMemoryLimitBytes"
        resourceFieldRef:
          containerName: main
          resource: limits.memory
          divisor: 1
Figure 8.3. Using a downwardAPI volume to pass metadata to the container
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest