1

I am new to Redis and Kubernetes and have a Kubernetes cluster setup of 3 Redis and 3 Sentinel

kubernetes % kubectl -n redis get pods
NAME         READY   STATUS    RESTARTS   AGE
redis-0      1/1     Running   0          7d16h
redis-1      1/1     Running   0          7d15h
redis-2      1/1     Running   0          8d
sentinel-0   1/1     Running   0          9d
sentinel-1   1/1     Running   0          9d
sentinel-2   1/1     Running   0          9d

I have successfully connected the sentinel and Redis-master to each other and was able to test basic HA operations using Redis-cli by exec into the pods, now I wanna connect my external java application to this Redis-cluster,

Now best to my understanding we are supposed to connect to Sentinel and Sentinel will guide us Redis-master pod where write operations can be executed.

So I had a few doubts regarding this, Can I connect to any of the sentinel servers and be able to execute my operations or should I always connect to a master? if we are connecting to a sentinel and if the master Sentinel goes down, what should be the plan of action or best practices in this regard, I have read a few blogs but can't seem to reach a clear understanding ?

What should I do to be able to connect my Spring boot to connect to this cluster? I read a bit on this too and it seems I can't connect to a minikube cluster directly from my IntelliJ/local machine and need to create an image and deploy it in a same namespace, is there any workaround for this ?

below is the yaml file for my redis service

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    name: redis 
  ports:
  - port: 6379
    targetPort: 6379

Thanks

Avinav gupta
  • 67
  • 2
  • 10

1 Answers1

0

Yes you should be able to connect to any sentinel. From https://redis.io/topics/sentinel-clients

The client should iterate the list of Sentinel addresses. For every address it should try to connect to the Sentinel, using a short timeout (in the order of a few hundreds of milliseconds). On errors or timeouts the next Sentinel address should be tried.

If all the Sentinel addresses were tried without success, an error should be returned to the client.

The first Sentinel replying to the client request should be put at the start of the list, so that at the next reconnection, we'll try first the Sentinel that was reachable in the previous connection attempt, minimizing latency.

If you are using a redis client library that supports sentinel, you can just pass all the redis sentinel addresses to the client and the client library takes care of connection logic for you as recommended by Redis documentation above.

Since you are on kubernetes, you can make it more simpler. Instead of deploying the sentinels as Statefulset with 3 replicas like you have done, deploy it as a Deployment with 3 replicas. Create a service object for the redis sentinel. Pass this service address as the sentinel adress to your redis client library. This way you don't need to work with multiple sentinel addresses and k8s would automatically take care of removing the sentinels that are down from the service endpoints if it is not reachable etc so all your clients don't need to discover the sentinels that are online.

You could also use a redis operator like https://github.com/spotahome/redis-operator which would takes care of deployment lifecycle of sentinel based redis HA clusters.

Shashank V
  • 10,007
  • 2
  • 25
  • 41
  • Using that as a service you lose the benefits of the having 3 sentinel deployments since they fail together if the service goes down. – Mahdi Yusuf Jul 13 '22 at 00:44