How to get Pod information Downward API in container in kubernetes
This article mainly shows you "how to get Pod information Downward API in the container in kubernetes", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to get Pod information Downward API in the container in kubernetes".
One: preface
In some clusters, each node in the cluster needs to write its own ID and process-bound IP and other information into the configuration file in advance, read the information when the process starts, and then publish it to some place similar to the service registry, so as to realize the automatic discovery of cluster nodes.
Downward API can be used at this time, the specific approach is to write a pre-start script or Init Container, through the environment variables or files to obtain the Pod's own name, IP address and other information, and then write it into the configuration file of the main program, and finally start the main program.
Second: the way of realization
Downward API can inject Pod information into the container in two ways.
1. Environment variables: for a single variable, you can inject Pod information and Container information into the container.
2.Volume mount: generates a file of array class information and mounts it inside the container.
Three: examples
Environment variable mode, injecting Pod information into environment variable dapi-test-pod.yaml
ApiVersion: v1
Kind: Pod
Metadata:
Name: dapi-test-pod
Namespace: default
Spec:
Containers:
-name: test-container
Image: busybox
Command: ["/ bin/sh", "- c", "env"]
Env:
-name: MY_POD_NAME
ValueFrom:
FieldRef:
FieldPath: metadata.name
-name: MY_POD_NAMESPACE
ValueFrom:
FieldRef:
FieldPath: metadata.namespace
-name: MY_POD_NAMES
ValueFrom:
FieldRef:
FieldPath: status.podIP
Kubectl create-f dapi-test-pod.yaml
Kubectl logs dapi-test-pod
The above is all the contents of the article "how to get Pod information Downward API in the container in kubernetes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!