<p>I have followed these links </p>

<ol>
<li><a href="https://medium.com/zero-to/setup-persistence-redis-cluster-in-kubertenes-7d5b7ffdbd98" rel="nofollow noreferrer">https://medium.com/zero-to/setup-persistence-redis-cluster-in-kubertenes-7d5b7ffdbd98</a>, </li>
<li><a href="https://github.com/sanderploegsma/redis-cluster" rel="nofollow noreferrer">https://github.com/sanderploegsma/redis-cluster</a></li>
</ol>

<p>to build a Redis sharded cluster on AKS, But they are connected by using Pod IP's but I need to connect to that cluster using "Python" to feed the data into it, Since they are connected internally using Pod IPs I am not able to connect using Python. Alternatively instead of 6 replicas of one statefulset, I have created 6 different statefulsets with 6 services and all exposed externally as "Load Balancer", But this command they used to make a cluster </p>

<pre><code>kubectl exec -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 \
$(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
</code></pre>

<p>I don’t know how to edit it to use loadbalancer IP instead of Pod IP, so with this 6 external IP's I am not able to create a cluster. I need a Redis sharded cluster on Azure Kubernetes Services which can be accessible externally from Python library "redis-py-cluster" </p>

<p>Thanks in advance</p>

dheeru dev
<p><strong>theory</strong></p>

<p>It is not very clear from your question as there is no answer formy comment under the initial post, but it looks like you have been following this guide: <a href="https://medium.com/zero-to/setup-persistence-redis-cluster-in-kubertenes-7d5b7ffdbd98" rel="nofollow noreferrer">Setup Persistence Redis Cluster in Kubernetes</a>.</p>

<p>Lets decompose the command you are referring to:</p>

<pre><code>kubectl exec -it redis-cluster-0 -- redis-trib create --replicas 1 $(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
</code></pre>

<p>the <code>kubectl</code> syntax is:</p>

<pre><code>kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...] [options]
</code></pre>

<p>That is why:
<code>kubectl  exec -it redis-cluster-0</code>   stands for "Execute a command in a container" with "Pass stdin to the container" and "Stdin is a TTY" options on <code>redis-cluster-0</code> pod. </p>

<p>The second part is <code>redis-trib create --replicas 1 &lt;IPs&gt;</code> and</p>

<pre><code>$(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
</code></pre>

<p>merely lists IPs of pods in your StatefulSet/s created with <a href="https://gist.github.com/Tiroshan/c531d1e4de39a0f9519f0e32ee9010eb#file-redis-cluster-deployment-yaml" rel="nofollow noreferrer">redis-cluster-deployment.yaml</a> .  </p>

<p>Taking into consideration that the <code>app=redis-cluster</code> label we are using in that command is taken from the<code>spec/template/metadata/labels</code> you can adjust the command the way you like or just list the IPs manually insted of getting them with <code>kubectl get pods -l</code></p>

<pre><code>apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
...
spec:
  serviceName: redis-cluster
  replicas: 6
  template:
    metadata:
      labels:
        app: redis-cluster    # This is the label we are using in that command
</code></pre>

<p><strong>my answer</strong>:</p>

<p>You can either use the IPs or adjust labels you are using in <code>kubectl get pods -l ...</code> part of the command.
Can't provide precise answer as I didn't receive steps to reproduce.</p>

<p><strong>my attempt</strong>:</p>

<p>I'm not sure how exactly you've been creating your statefulsets; however, you still can create a redis cluster on them.</p>

<p>In my case I have used the following YAMLs: <a href="https://github.com/nkolchenko/technocore/blob/master/redis-cluster-deployment-1.yaml" rel="nofollow noreferrer">redis-cluster-deployment-1.yaml</a> and <a href="https://github.com/nkolchenko/technocore/blob/master/redis-cluster-deployment-2.yaml" rel="nofollow noreferrer">redis-cluster-deployment-2.yaml</a> . </p>

<pre><code>$ kubectl create -f redis-cluster-deployment-1.yaml 
statefulset.apps/redis-cluster-set-1 created

$ kubectl create -f redis-cluster-deployment-2.yaml 
statefulset.apps/redis-cluster-set-2 created

$ kubectl get pods -o wide 
NAME                    READY   STATUS    RESTARTS   AGE   IP           NODE                                            
redis-cluster-set-1-0   1/1     Running   0          52m   10.12.0.22   gke-6v3n
redis-cluster-set-1-1   1/1     Running   0          51m   10.12.1.17   gke-m7z8
redis-cluster-set-1-2   1/1     Running   0          50m   10.12.1.18   gke-m7z8
redis-cluster-set-2-0   1/1     Running   0          51m   10.12.0.23   gke-6v3n
redis-cluster-set-2-1   1/1     Running   0          50m   10.12.1.19   gke-m7z8
redis-cluster-set-2-2   1/1     Running   0          14m   10.12.0.24   gke-6v3n


$ kubectl exec -it redis-cluster-set-1-0 -- redis-trib create --replicas 1 $(kubectl get pods -l app=redis-cluster-set-app -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
&gt;&gt;&gt; Creating cluster
&gt;&gt;&gt; Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.12.0.22:6379
10.12.1.17:6379
10.12.1.18:6379
Adding replica 10.12.1.19:6379 to 10.12.0.22:6379
Adding replica 10.12.0.24:6379 to 10.12.1.17:6379
Adding replica 10.12.0.23:6379 to 10.12.1.18:6379
....
</code></pre>

<p>the important part here is to specify correct <code>pod name</code> and <code>app=</code> label . </p>

<p>In my case , the <code>$(kubectl get pods -l app=redis-cluster-set-app -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')</code> command results in a following list of IPs (you can compare them with the output from above.</p>

<pre><code>$ kubectl get pods -l app=redis-cluster-set-app -o jsonpath='{range.items[*]}{.status.podIP}:6379 '
10.12.0.22:6379 10.12.1.17:6379 10.12.1.18:6379 10.12.0.23:6379 10.12.1.19:6379 10.12.0.24:6379
</code></pre>

<p>Hope that helps.</p>

<p><strong>Update</strong> (06-Dec-2019):</p>

<blockquote>
  <p>have created a service LoadBalancer" and I am pretty sure I have given a correct "serviceName" under "spec". That LoadBalancer gave me an external Ip. After I created a redis sharded cluster, when I try to connect to that external IP using python, It was able to connect to only one node of redis and asks for other 5 nodes and eventually failed with "Timeout" error</p>
</blockquote>

<p>I did the same and tested it 
Redis cluster on 3 nodes:</p>

<pre><code>$ kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP           
redis-cluster-set-1-0   1/1     Running   0          20h   10.12.1.30
redis-cluster-set-1-1   1/1     Running   0          20h   10.12.0.29
redis-cluster-set-1-2   1/1     Running   0          20h   10.12.1.31
</code></pre>

<p>Created some data and tried to get it out from Redis cluster.</p>

<pre><code>$ kubectl exec -it redis-cluster-set-1-0 -- redis-cli SET Australia Sydney
OK
$ kubectl exec -it redis-cluster-set-1-0 -- redis-cli GET Australia
"Sydney"
</code></pre>

<p>it worked so I tried to query the same data from another node in cluster</p>

<pre><code>$ kubectl exec -it redis-cluster-set-1-2 -- redis-cli GET Australia
(error) MOVED 1738 10.12.1.30:6379
</code></pre>

<p>Redis cluster replied with Internal IP in <code>MOVED</code> response.</p>

<p>I think that internal IP adress is the reason for that "timeOut" error.</p>

<p>At the same time it is possible to get data when logged into that pod directly</p>

<pre><code>$ kubectl exec -it redis-cluster-set-1-2 -- bash
root@redis-cluster-set-1-2:/data# redis-cli GET Australia
(error) MOVED 1738 10.12.1.30:6379

root@redis-cluster-set-1-2:/data# redis-cli -h 10.12.1.30  GET Australia
"Sydney"
</code></pre>

<p>From this  <a href="https://github.com/docker-library/redis/issues/157#issuecomment-412689512" rel="nofollow noreferrer">Github Issue</a>  it seems that redis doesn't proxy the connection through the Redis cluster, to correct node but rather simply passes the IP address of the appropriate server back to the client, which then initiates the connection directly. </p>

<p>(slight off-topic, but still worth mentioning).
While checking on that I have found an [another document] that explains (<a href="https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gke" rel="nofollow noreferrer">https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gke</a>)  how to connect to a Redis instance from a Google Kubernetes Engine cluster (with LoadBalancer).</p>
