why cluster node's ip is not current pod's ip

2/25/2020

I am login kubernetes's(v1.15.2) pod to check current redis cluster ip,but the problem is redis cluster(redis:5.0.1-alpine) ip is not current pod's ip:

~ ⌚ 23:45:03
$ kubectl exec -it redis-app-0 /bin/ash
/data # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:1E:E0:1E
          inet addr:172.30.224.30  Bcast:172.30.231.255  Mask:255.255.248.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:78447 errors:0 dropped:0 overruns:0 frame:0
          TX packets:64255 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:74638073 (71.1 MiB)  TX bytes:74257972 (70.8 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:3922 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3922 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:297128 (290.1 KiB)  TX bytes:297128 (290.1 KiB)

/data # /usr/local/bin/redis-cli -c
127.0.0.1:6379> cluster nodes
a1ecebe5c9dc2f9edbe3c239c402881da10da6de 172.30.224.22:6379@16379 myself,master - 0 1582644797000 0 connected
127.0.0.1:6379>

the pod's ip is:172.30.224.30,and redis ip is:172.30.224.22,what is the problem?is it possible to fix it?

-- Dolphin
kubernetes
redis

1 Answer

2/25/2020

How is this IP defined in your redis config ? Redis must've taken it from somewhere. Keep in mind that Pod ip is subject to change when the Pod is recreated so you have no guarantee that IP address defined in your Redis node statically will remain unchanged. I would even say that you can be almost 100% sure that it will change.

You can check IP address of a particular interface in a few ways. One of them is by running:

hostname -I | awk '{print $1}'

You can try to add an init container to your Pod running simple bash script which would check current host ip address e.g. using the above command and then would populate redis config accordingly. But it seems to me an overkill and I'm almost sure it can be done more easily. If your redis node by default binds to all IP addresses (0.0.0.0), cluster nodes output should show you current ip address of your Pod.

Are you providing Redis configuration to your Pod via some ConfigMap ?

Please share more details related to your Deployment so you can get more accurate answer that resolves your particular issue.

-- mario
Source: StackOverflow