2

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?

Community
  • 1
  • 1
CHENJIAN
  • 1,810
  • 13
  • 24

1 Answers1

0

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.

Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
  • Thanks and I can get your points clearly. But, in fact, I had deployed a **Redis cluster** by using the command:`redis-trib.rb create -- replicas 1 host1:port1 ... hostN:portN` Then there is a service in container_B of pod_B connecting that redis cluster to read and write the values. That is the reason why I need to get the **PortIP** of pod_A(redis cluster). – CHENJIAN Mar 27 '17 at 06:00
  • I also need to get podB's ip addresss in podA. Do you find the solution for this? Use environment variable or else? Let me know if you have the solution. Thanks. – Hua Zhang Apr 23 '17 at 00:11
  • Finally, I got the solution using service (environment variable and dns). Here is the reference: [kubernetes service](https://kubernetes.io/docs/concepts/services-networking/service/) For example, the Service "redis-master" which exposes TCP port 6379 and has been allocated cluster IP address 10.0.0.11. REDIS_MASTER_SERVICE_HOST=10.0.0.11 REDIS_MASTER_SERVICE_PORT=6379 – Hua Zhang Apr 23 '17 at 01:08
  • @HuaZhang Using environment variable and DNS, is a statical idea to deal with this problem, but not a dynamical one. After the service **restarted**, like the service of "Redis-Master" stoped and then restarted, the IP address had changed from `10.0.0.11` to `10.0.0.21`, you must fix the ENV_values manually. If you want to change those ENV_values by themselves, you can get the IP address by using **the Kubernetes API**, while obviously, the cluster of Kubernetets has itself own service of "Kubernetes". – CHENJIAN May 31 '17 at 05:22