Example:
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE
test pod_A 1/1 Running 1 17d 192.168.1.171 10-0-0-171.master
test pod_B 1/1 Running 1 17d 192.168.1.172 10-0-0-172.node
By using:
kubectl exec -it pod_B --namespace=test -- sh
I can get a shell into the container_b that is running in my pod_B.
But, how can I get the Pod_A's PortIP address of "192.168.1.171" when I am in the shell of container_b?
There is a similar question, How to know a Pod's own IP address from a container in the Pod?, and some official documents like Exposing Pod Information to Containers Through Environment Variables and Exposing Pod Information to Containers Using a DownwardApiVolumeFile. But those can not solve my problem.
-----------UPDATE-----------
Recently, I fixed it by using K8s apiserver. In container_b of Pod_B, I can get a JSON response from http://k8s_apiserver:port/api/v1/pods
, parse it, and get the PortIp of Pod_A at end. BUT, is there any easier way to deal with it?
You can use kube-dns for service-discovery between your pods.
When you create a service for your pod pod_A
with the name service_A
the service will it's own IP address which points to your pod address. The service will also get a dns name like service_A.service.default.cluster.local
. When you want to communicate from your pod_B
to pod_A
you now use the dns name and don't have to worry about the ip address from pod_A
anymore.