Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know a Pod's own IP address from inside a container in the Pod?

Tags:

kubernetes

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.

Edit: 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.

like image 246
yanana Avatar asked Jun 10 '15 04:06

yanana


1 Answers

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 

Recreate the pod/rc and then try

echo $MY_POD_IP 

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

Cheers

like image 100
PiersyP Avatar answered Sep 20 '22 19:09

PiersyP