Get the App
SLTechnology News&Howtos  ›  Servers  › 

Get Pod information in kubernetes container

Shulou Source: shulou.com Published: 2022-06-03 05:15:42 09月21日 Update

This article describes how Pods can obtain information about containers and nodes on which they are running (kubernetes since 1.7).

1. Compile busybox-env.yaml file

apiVersion: v1kind: Podmetadata: name: busybox-envspec: containers: - name: busybox-container image: busybox command: - sleep - "3600" env: - name: MY_NODE_NAME #Get node name valueFrom: fieldRef: fieldPath: spec.nodeName - name: MY_POD_NAME #Get pod name valueFrom: fieldRef: fieldPath: metadata.name - name: MY_POD_NAMESPACE #Get pod namespace valueFrom: fieldRef: fieldPath: metadata.namespace - name: MY_POD_IP #Get pod IP valueFrom: fieldRef: fieldPath: status.podIP

2. Apply yaml

$ kubectl apply -f busybox-env.yaml

3. Go inside the container to see if the variable exists

$ kubectl exec -it busybox-env sh$ env | egrep "MY_POD"MY_POD_NAMESPACE=defaultMY_POD_IP=10.23.2.1MY_POD_NAME=busybox-env$ env | egrep "MY_NODE"MY_NODE_NAME=k8s-node01

You can see that the variables are already loaded into the container environment variables

Tags: Container variable information name file environment application compile run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info MariaDB Shulou Technology Huawei