2

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
  • 6,102
  • 5
  • 41
  • 76

2 Answers2

1

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
  • 341
  • 2
  • 9
0

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
  • 1,541
  • 1
  • 12
  • 21
  • This requires having kubectl installed inside each container, correct? – João Matos Mar 12 '19 at 14:51
  • I don't agree that this is not a duplicate, as I am trying to determine the neighbours IP and not its own – João Matos Mar 12 '19 at 14:58
  • kubectl is a control utility to work with a k8s cluster as a whole and not limited to pods. It is used to communicate with the API server of the cluster and hence is not required to be installed in every pod – Raunak Jhawar Mar 12 '19 at 14:59
  • 1
    Well, I am trying to achieve the neighbours ip *from inside a container*. I installed Kubectl inside a container and still the command does not work: Error from server (Forbidden): pods "cassandra-0" is forbidden: User "system:serviceaccount:default:default" cannot get resource "pods" in API group "" in the namespace "default" – João Matos Mar 12 '19 at 15:02