如何从Pod中的容器内部知道Pod自己的IP地址? [英] How to know a Pod's own IP address from inside a container in the Pod?

查看:2200
本文介绍了如何从Pod中的容器内部知道Pod自己的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kubernetes为每个容器分配一个IP地址,但是如何从Pod中的容器获取IP地址?我无法从文档中找到方法.

Kubernetes assigns an IP address for each container, but how can I acquire the IP address from a container in the Pod? I couldn't find the way from documentations.

我将在Kubernetes中运行Aerospike集群.并且配置文件需要自己的IP地址.而且我正在尝试使用confd设置主机名.如果设置了环境变量,我会使用它.

I'm going to run Aerospike cluster in Kubernetes. and the config files need its own IP address. And I'm attempting to use confd to set the hostname. I would use the environment variable if it was set.

推荐答案

最简单的答案是通过添加以下定义的配置块,确保您的pod或复制控制器yaml/json文件将pod IP作为环境变量添加. (下面的块还使名称和名称空间可用于Pod)

The simplest answer is to ensure that your pod or replication controller yaml/json files add the pod IP as an environment variable by adding the config block defined below. (the block below additionally makes the name and namespace available to the pod)

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

重新创建pod/rc,然后尝试

Recreate the pod/rc and then try

echo $MY_POD_IP

还运行env来查看kubernetes还为您提供了什么.

also run env to see what else kubernetes provides you with.

欢呼

这篇关于如何从Pod中的容器内部知道Pod自己的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆