<p>I have a redis statefulset running on my cluster, with 3 nodes (master, slave, slave), the master node is for write only, and the other 2 nodes for read, they replication is working fine, but I want to connect a backend application to this redis-cluster and somehow manage the requests, in which the write requests goes to master and the reads goes to slaves, any idea how I can achieve this ? Does it have to be controlled in application level, or is there any solution in Kubernetes to achieve this?</p>
<p>In addition, I want to know about the situation when I am scaling up the statefulset replicas or scale down, then the traffic direction still should be going to appropriate pod.</p>
<p>My Kubernetes cluster is running on premises.</p>

Moss
<p>Not sure how you have setup the Redis and you are running the <strong>Redis cluster</strong> or running <strong>Redis master-slave</strong> with <strong>sentinel</strong> option.</p>
<p><a href="https://redis.io/docs/management/sentinel/" rel="nofollow noreferrer">Official doc</a></p>
<p>If you have used <strong>helm chart</strong> they have the option to enable the <strong>sentinel</strong>.</p>
<pre><code>sentinel:
  enabled: true
</code></pre>
<p>Check this <a href="https://github.com/bitnami/charts/tree/main/bitnami/redis#choose-between-redis-helm-chart-and-redis-cluster-helm-chart" rel="nofollow noreferrer">Github Doc</a> there is two helm charts or topology available and which one you have used.</p>
<p>So the idea behind sentinel is it writes or gives you back the <strong>current IP</strong> of the master and slave.</p>
<p>For example below the python code snippet, when you will hit the service of <strong>Redis</strong> it will give you both <strong>master</strong> and <strong>slave IP</strong>, you can use it as per need either <strong>write</strong> or <strong>read</strong>.</p>
<pre><code>from redis import Sentinel
sentinel = Sentinel([('&lt;**Single K8s service Name**&gt;', 26379)], socket_timeout=0.1)
sentinel.discover_master('mymaster')
('127.0.0.1', 6379)
sentinel.discover_slaves('mymaster')
[('127.0.0.1', 6380)]
</code></pre>
<p><strong>Reference</strong> : <a href="https://github.com/redis/redis-py#sentinel-support" rel="nofollow noreferrer">https://github.com/redis/redis-py#sentinel-support</a></p>
<p>Ref for few more answer might be helpful :</p>
<ul>
<li><a href="https://stackoverflow.com/a/68551193/5525824">https://stackoverflow.com/a/68551193/5525824</a></li>
<li><a href="https://stackoverflow.com/a/70271427/5525824">https://stackoverflow.com/a/70271427/5525824</a></li>
<li><a href="https://stackoverflow.com/a/70454335/5525824">https://stackoverflow.com/a/70454335/5525824</a></li>
</ul>
