<p>I am trying to have 1 redis master with 2 redis replicas tied to a 3 Quorum Sentinel on Kubernetes. I am very new to Kubernetes.</p>
<p>My initial plan was to have the master running on a pod tied to 1 Kubernetes SVC and the 2 replicas running on their own pods tied to another Kubernetes SVC. Finally, the 3 Sentinel pods will be tied to their own SVC. The replicas will be tied to the master SVC (because without svc, ip will change). The sentinel will also be configured and tied to master and replica SVCs. But I'm not sure if this is feasible because when master pod crashes, how will one of the replica pods move to the master SVC and become the master? Is that possible?</p>
<p>The second approach I had was to wrap redis pods in a replication controller and the same for sentinel as well. However, I'm not sure how to make one of the pods master and the others replicas with a replication controller.</p>
<p>Would any of the two approaches work? If not, is there a better design that I can adopt? Any leads would be appreciated.</p>

<p>You can deploy Redis Sentinel using the <a href="https://helm.sh/" rel="noreferrer">Helm</a> package manager and the <a href="https://github.com/bitnami/charts/tree/master/bitnami/redis" rel="noreferrer">Redis Helm Chart</a>.<br />
If you don't have <code>Helm3</code> installed yet, you can use this <a href="https://helm.sh/docs/intro/install/" rel="noreferrer">documentation</a> to install it.</p>
<p>I will provide a few explanations to illustrate how it works.</p>
<hr />
<p>First we need to get the <code>values.yaml</code> file from the Redis Helm Chart to customize our installation:</p>
<pre><code>$ wget https://raw.githubusercontent.com/bitnami/charts/master/bitnami/redis/values.yaml
</code></pre>
<p>We can configure a lot of parameters in the <code>values.yaml</code> file , but for demonstration purposes I only enabled Sentinel and set the redis password:<br />
<strong>NOTE:</strong> For a list of parameters that can be configured during installation, see the <a href="https://github.com/bitnami/charts/tree/master/bitnami/redis#parameters" rel="noreferrer">Redis Helm Chart Parameters</a> documentation.</p>
<pre><code># values.yaml

global:
  redis:
    password: redispassword
...
replica:
  replicaCount: 3
...
sentinel:
  enabled: true
...
</code></pre>
<p>Then we can deploy Redis using the configuration from the  <code>values.yaml</code> file:<br />
<strong>NOTE:</strong> It will deploy a three Pod cluster (one master and two slaves) managed by the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noreferrer">StatefulSets</a> with a <code>sentinel</code> container running inside each Pod.</p>
<pre><code>$ helm install redis-sentinel bitnami/redis --values values.yaml
</code></pre>
<p>Be sure to carefully read the <em><strong>NOTES</strong></em> section of the chart installation output. It contains many useful information (e.g. how to connect to your database from outside the cluster)</p>
<p>After installation, check redis <code>StatefulSet</code>, <code>Pods</code> and <code>Services</code> (<a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="noreferrer">headless service</a> can be used for internal access):</p>
<pre><code>$ kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP
redis-sentinel-node-0   2/2     Running   0          2m13s   10.4.2.21
redis-sentinel-node-1   2/2     Running   0          86s     10.4.0.10
redis-sentinel-node-2   2/2     Running   0          47s     10.4.1.10


$ kubectl get sts
NAME                  READY   AGE
redis-sentinel-node   3/3     2m41s

$ kubectl get svc
NAME                      TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)              AGE
redis-sentinel            ClusterIP   10.8.15.252   &lt;none&gt;        6379/TCP,26379/TCP   2m
redis-sentinel-headless   ClusterIP   None          &lt;none&gt;        6379/TCP,26379/TCP   2m
</code></pre>
<p>As you can see, each <code>redis-sentinel-node</code> Pod contains the <code>redis</code> and <code>sentinel</code> containers:</p>
<pre><code>$ kubectl get pods redis-sentinel-node-0 -o jsonpath={.spec.containers[*].name}
redis sentinel
</code></pre>
<p>We can check the <code>sentinel</code> container logs to find out which <code>redis-sentinel-node</code> is the master:</p>
<pre><code>$ kubectl logs -f redis-sentinel-node-0 sentinel
...
1:X 09 Jun 2021 09:52:01.017 # Configuration loaded
1:X 09 Jun 2021 09:52:01.019 * monotonic clock: POSIX clock_gettime
1:X 09 Jun 2021 09:52:01.019 * Running mode=sentinel, port=26379.
1:X 09 Jun 2021 09:52:01.026 # Sentinel ID is 1bad9439401e44e749e2bf5868ad9ec7787e914e
1:X 09 Jun 2021 09:52:01.026 # +monitor master mymaster 10.4.2.21 6379 quorum 2
...
1:X 09 Jun 2021 09:53:21.429 * +slave slave 10.4.0.10:6379 10.4.0.10 6379 @ mymaster 10.4.2.21 6379
1:X 09 Jun 2021 09:53:21.435 * +slave slave 10.4.1.10:6379 10.4.1.10 6379 @ mymaster 10.4.2.21 6379
...
</code></pre>
<p>As you can see from the logs above, the <code>redis-sentinel-node-0</code> Pod is the master and the <code>redis-sentinel-node-1</code> &amp; <code>redis-sentinel-node-2</code> Pods are slaves.</p>
<p>For testing, let's delete the master and check if sentinel will switch the master role to one of the slaves:</p>
<pre><code>    $ kubectl delete pod redis-sentinel-node-0
    pod &quot;redis-sentinel-node-0&quot; deleted
    
    $ kubectl logs -f redis-sentinel-node-1 sentinel
    ...                                                                                           
    1:X 09 Jun 2021 09:55:20.902 # Executing user requested FAILOVER of 'mymaster'
    ...
    1:X 09 Jun 2021 09:55:22.666 # +switch-master mymaster 10.4.2.21 6379 10.4.1.10 6379
    ...
    1:X 09 Jun 2021 09:55:50.626 * +slave slave 10.4.0.10:6379 10.4.0.10 6379 @ mymaster 10.4.1.10 6379
    1:X 09 Jun 2021 09:55:50.632 * +slave slave 10.4.2.22:6379 10.4.2.22 6379 @ mymaster 10.4.1.10 6379
</code></pre>
<p>A new master (<code>redis-sentinel-node-2</code> <code>10.4.1.10</code>) has been selected, so everything works as expected.</p>
<p>Additionally, we can display more information by connecting to one of the Redis nodes:</p>
<pre><code>$ kubectl run --namespace default redis-client --restart='Never' --env REDIS_PASSWORD=redispassword --image docker.io/bitnami/redis:6.2.1-debian-10-r47 --command -- sleep infinity
pod/redis-client created
$ kubectl exec --tty -i redis-client --namespace default -- bash
I have no name!@redis-client:/$ redis-cli -h redis-sentinel-node-1.redis-sentinel-headless -p 6379 -a $REDIS_PASSWORD
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-sentinel-node-1.redis-sentinel-headless:6379&gt; info replication
# Replication
role:slave
master_host:10.4.1.10
master_port:6379
master_link_status:up
...
</code></pre>
