what is the redis headless service used for in kubernetes cluster
Asked Answered
L

1

6

Today I am facing a problem that the redis in the kubernetes cluster shows error like this:

2021-11-16T05:32:18 [INFO] - increment delete failed...,MobcError(RedisCMDError(An error was signalled by the server: You can't write against a read only replica.))

I check my redis config and found that the config url like this:

redisConnectionStr="redis://default:[email protected]:6379/3"

I check the servcie and found it mapped to the redis cluster master and replica, I was wonder the headness service user for what situation? why not only keep the master servcie and replica servcie? If read operate, use any of the servcie would be fine. If write, only use the master service. when should i use the headness service?

Licentiate answered 16/11, 2021 at 5:39 Comment(0)
B
8

why not only keep the master service and replica servcie?

You can do it, if you could implement it. as redis helm maybe dont use specific labels to run Read/Write replicas so it's hard to divert and create the different services for read & write.

If you are deploying the Redis using the helm it will create the two services into the K8s cluster

one is Headless service and another one normal service with the ClusterIP.

In the application side, we should be using the normal service.

So idea is that headless return the replicas IPs which will be used further by the application to manage the cluster if you are using the sentinel or Redis cluster.

If you are using the Normal service which will move or route the traffic automatically to Read/Write replicas based on the condition.

While if you are using the sentinel you can ping the sentinel service and it will return master and read replicas IP.

Inside code you can use as per requirement now as you are getting both write and read ip with sentinel.

Inside the helm chart you can see one configuration

sentinel:
  enabled: true

In the bitnami docs they explicitly mention This command will return the address of the current master, which can be accessed from inside the cluster..

Read more at : https://github.com/bitnami/charts/tree/master/bitnami/redis#master-replicas-with-sentinel

Barajas answered 16/11, 2021 at 6:18 Comment(4)
you mean if i use the master servcie it would route read to replica and write to the master? @Harsh ManvarLicentiate
i am sure about Redis arch you have set up but if you are using the Redis with sentinel you will get both ips when you will hit the service of Redis. Current master and current read replicas IP.Barajas
here one nice example : github.com/redis/redis-py#sentinel-support which code of python.Barajas
i would not suggest using just master service if your code is smart if your redis is supporting sentinel it's fine to use the smart way shown above in python code. different read-write config but more depends on your setup and code.Barajas

© 2022 - 2024 — McMap. All rights reserved.