Unable to properly connect to Redis in Kubernetes Unable to properly connect to Redis in Kubernetes kubernetes kubernetes

Unable to properly connect to Redis in Kubernetes


Always the first thing to check when a kubernetes service does not behave as expected is to check the endpoints of the corresponding service. In your case kubectl get ep redis.

If my assumption is correct it should show you something like this

NAME      ENDPOINTS   AGEredis     <none>      42d

This means that your service does not select/match any pods.

In your service spec there is the key selector: this selector has to match the labels of the actual deployment you have. You are selecting for all pods with the labels name: redis-proxy and role: proxy which are potentially not matching any pod.

You can run kubectl get pod --show-labels=true to show the labels on the pods and change your service accordingly.

I don't know what the port 0 means in this context. Sometimes it is used to do only DNS resolution with the service.


From the deployment you posted above:

apiVersion: v1kind: Podmetadata:  labels:    name: redis    redis-sentinel: "true"    role: master  name: redis-masterspec:  containers:    - name: master      image: k8s.gcr.io/redis:v1      env:        - name: MASTER          value: "true"      ports:        - containerPort: 6379      resources:        limits:          cpu: "0.1"      volumeMounts:        - mountPath: /redis-master-data          name: data    - name: sentinel      image: kubernetes/redis:v1      env:        - name: SENTINEL          value: "true"      ports:        - containerPort: 26379  volumes:    - name: data      emptyDir: {}

You can see the container port of the sentinel is 26379

Thus in the service (from the example)

apiVersion: v1kind: Servicemetadata:  labels:    name: sentinel    role: service  name: redis-sentinelspec:  ports:    - port: 26379      targetPort: 26379  selector:    redis-sentinel: "true"

It again uses port 26379

From the ioredis docs (host modified for your use case):

var redis = new Redis({  sentinels: [{ host: 'redis-sentinel', port: 26379 }],  name: 'mymaster'});redis.set('foo', 'bar');

The sentinel is not technically a proxy, ioredis first connects to the sentinel to find our which node is the master, then is given connection info for that node.

tl;dr;

Change the service back to the one used in the example, and use redis-sentinel as host and 26379 for port.