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

查看:32
本文介绍了如何从 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天全站免登陆