Kubernetes pod redirect to the same port in another pod

11/9/2018

I have a deployment with a php image that connects with a redis deployment in the 6379 port.

The problem is that the php application connects in the host 127.0.0.1 of its own pod, but the redis is in another pod (and have its own ClusterIP service).

I can't change the app code, so I want to redirect the 6379 port of php pod for the same port on the redis port.

How I can do this?

-- mayconfsbrito
docker
kubernetes
service

1 Answer

11/10/2018

kubernetes uses socat for doing port-forwarding from kubectl, so if they trust it that much, it seems reasonable you could trust it, too.

Place it in a 2nd container that runs alongside your php container, run socat in forwarding mode, and hope for the best:

containers:
- name: my-php
  # etc
- name: redis-hack
  image: my-socat-container-image-or-whatever
  command:
  - socat
  - TCP4-LISTEN:6379,fork
  - TCP4:$(REDIS_SERVICE_HOST):$(REDIS_SERVICE_PORT)
  # assuming, of course, your Redis ClusterIP service is named `redis`; adjust accordingly

Since all Pods share the same network namespace, the second container will listen on "127.0.0.1" also

Having said all of that, as commentary on your situation, it is a terrible situation to introduce this amount of hackery to work around a very, very simple problem of just not having the app hard-code "127.0.0.1" as the redis connection host

-- mdaniel
Source: StackOverflow