Kubernetes Pod定义中的IPvariables扩展

我有一个docker图像,需要通过命令行传递容器IP地址(或主机名)。

有没有可能在容器命令定义中扩展pod主机名或IP? 如果没有,在一个kuberneted部署容器中获得它的更好方法是什么?

在AWS中,我通常通过联系EC2元数据服务来获得它,我可以做一些类似的联系kubernetes api,只要我可以获得pod的名称/ id?

谢谢。

在V1.1(即将发布)中,您可以通过向下的api将pod的IP公开为环境variables(请注意, 发布的文档适用于v1.0,不包括向下API中的pod IP)。

在v1.1之前,获得这个的最好方法可能是从pod中查询API。 有关如何访问API,请参阅从Pod访问API。 pod的名字是你的$HOSTNAME ,你可以find这样的IP:

 wget -O - ${KUBERNETES_RO_SERVICE_HOST}:${KUBERNETES_RO_SERVICE_PORT}/api/v1/namespaces/default/pods/${HOSTNAME} | grep podIP 

虽然我build议你使用jsonparsing器,比如jq

编辑:

只是想添加pod IP在重新启动时不被保留,这就是为什么通常的build议是build立一个指向你的pod的服务。 如果您确实使用服务,则服务IP将被修复,并作为代理,即使在重新启动时也是如此。 服务IP作为环境variables提供,如FOO_SERVICE_HOSTFOO_SERVICE_PORT

根据您的pod设置,您可以使用hostname -i

例如

 $ kubectl exec ${POD_NAME} hostname -i 10.245.2.109 

man hostname

 ... -i, --ip-address Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip-addresses instead. -I, --all-ip-addresses Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output. ...