kubernetes-downward-api

In the kubernetes environment, because the container is dynamically assigned to the node, that is, the specific information of the running environment cannot be known before the container is started

How to get the pod IP of the current container in the program? Or is it the memory limit of the current container?

Kubernetes uses the downward API to inject metadata about pod into containers and clusters through environment variables and files.

apiVersion: v1
kind: Pod
metadata:
  name: getpodip
spec:
  containers:
  - image: nginx
    name: getpodip
    env: 
    - name: POD_IP
      valueFrom:
        fieldRef: 
          fieldPath: status.podIP
    - name: MEM_LIMITS
      valueFrom:
        resourceFieldRef: 
          containerName: getpodip
          resource: limits.memory

However, since the environment variables cannot be changed after the container is started, you can also mount the information to the disk volume, so that after each modification, the program can update the information by re reading the contents of the file

apiVersion: v1
kind: Pod
metadata:
  name: getpodip
spec:
  containers:
  - image: nginx
    name: getpodip
    volumeMounts:
      - name: pod-info
        mountPath: /pod-info
        readOnly: true
      - name: pod-resource
        mountPath: /pod-resource
        readOnly: true
  volumes:
    - name: pod-info
      downwardAPI:
        items:
          - path: POD_IP
            fieldRef: 
              fieldPath: metadata.name
    - name: pod-resource
      downwardAPI:
        items:
          - path: "cpu_limit"
            resourceFieldRef:
              containerName: getpodip
              resource: limits.cpu
              divisor: 1m
          - path: "cpu_request"
            resourceFieldRef:
              containerName: getpodip
              resource: requests.cpu
              divisor: 1m
          - path: "mem_limit"
            resourceFieldRef:
              containerName: getpodip
              resource: limits.memory
              divisor: 1Mi
          - path: "mem_request"
            resourceFieldRef:
              containerName: getpodip
              resource: requests.memory
              divisor: 1Mi

But I don’t know why here is metadata Name cannot be replaced with status podIP

The Pod "getpodip" is invalid: 
* spec.volumes[0].downwardAPI.fieldRef.fieldPath: Unsupported value: "status.podIP": supported values: "metadata.annotations", "metadata.labels", "metadata.name", "metadata.namespace", "metadata.uid"
* spec.containers[0].volumeMounts[0].name: Not found: "pod-info"
  • Env vars using valueFrom.fieldRef:
    •  spec.nodeName – the node’s name
    •  status.hostIP – the node’s IP
    •  metadata.name – the pod’s name
    •  metadata.namespace – the pod’s namespace
    •  status.podIP – the pod’s IP address
    •  spec.serviceAccountName – the pod’s service account name
    •  metadata.uid – the pod’s UID
    •  metadata.labels[”] – the value of the pod’s label (for example, metadata.labels[‘mylabel’])
    •  metadata.annotations[”] – the value of the pod’s annotation (for example, metadata.annotations[‘myannotation’])
  • DownwardAPI volumes via items of fieldRef:
    •  spec.nodeName – the node’s name
    •  status.hostIP – the node’s IP
    •  metadata.name – the pod’s name
    •  metadata.namespace – the pod’s namespace
    •  status.podIP – the pod’s IP address
    •  spec.serviceAccountName – the pod’s service account name
    •  metadata.uid – the pod’s UID
    •  metadata.labels- the value of the pod’s label (for example, metadata.labels[‘mylabel’])
    •  metadata.annotations – the value of the pod’s annotation (for example, metadata.annotations[‘myannotation’])
Send a Message