[英]Programmatically get the name of the pod that a container belongs to in Kubernetes?
Is there a way to programmatically get the name of the pod that a container belongs to in Kubernetes? 有没有办法以编程方式获取容器属于Kubernetes的pod的名称? If so how? 如果是这样的话? I'm using fabric8's java client but curl or something similar will be fine as well. 我正在使用fabric8的java客户端,但卷曲或类似的东西也会很好。
Note that I don't want to find the pod using a specific label since then (I assume) I may not always find the right pod if it's scaled with a replication controller. 请注意,从那时起我不想使用特定标签找到pod(我假设)如果使用复制控制器缩放,我可能无法找到合适的pod。
You can tell Kubernetes to put the pod name in an environment variable of your choice using the downward API . 您可以告诉Kubernetes使用向下API将pod名称放在您选择的环境变量中。
For example: 例如:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/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_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
restartPolicy: Never
The pod name is written to /etc/hostname
so it's possible to read it from there. pod名称写入/etc/hostname
因此可以从那里读取它。 In Java (which I'm using) you can also get the hostname (and thus the name of the pod) by calling System.getenv("HOSTNAME")
. 在Java(我正在使用)中,您还可以通过调用System.getenv("HOSTNAME")
获取主机名(以及pod的名称System.getenv("HOSTNAME")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.