In a Kubernetes cluster is it possible to obtain a container/pod IP address from inside a different container

3/12/2019

Is it possible to determine the neighbour container or pod IP address, e.g. given its name? More specifically, if I have two running containers/pods C1 (ip=1.2.3.4) and C2 (ip=5.6.7.8) and I execute:

  ME$ kubectl exec -it C1 bash
  C1$ magic_command_to_get_ip C2
  5.6.7.8

Is there a magic_command_to_get_ip?

I am new to Kubernetes and I used to resolve names to addresses in Docker using getent hosts tasks.$SERVICE, but apparently it does not work with KB.

-- João Matos
kubernetes

2 Answers

3/12/2019

Possible duplicate of this thread - How to know a Pod's own IP address from inside a container in the Pod?

While you can use the POD_HOST=$(kubectl get pod $POD_NAME --template={{.status.podIP}}) where your bash_variable can be an enum of all available pods in the cluster.

-- Raunak Jhawar
Source: StackOverflow

3/12/2019

Each pod has a service account mounted to it.

You can check that with

kubectl decsribe pod pod_name

Output:

Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from xxxxx-token-xxx (ro)

The command suggested by Raunak should work if your service account has permission to execute that command.

Please make sure the service account you are using has permission to run that command.

The error stated in comments (Forbidden): pods "cassandra-0" is forbidden: User "system:serviceaccount:default:default" is beacuse of that.

More about service accounts docs

-- Vineet Palan
Source: StackOverflow