<p>A docker container with a small Python app inside it is deployed to a Kubernetes cluster that has a <code>redis master</code> and a <code>redis slave</code> service running in the cluster.  The Python app inside the Docker container is not able to connect to the <code>redis</code> across the cluster because the Python app is not configured properly to find <code>redis</code> on the network.  </p>

<p>What specific changes need to be made to the code below in order for the Python app in <code>app.py</code> to be able to communicate successfully with the <code>redis</code> running in the same cluster?</p>

<h2>PYTHON APP CODE</h2>

<p>Here is <code>app.py</code>  </p>

<pre><code>from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "&lt;i&gt;cannot connect to Redis, counter disabled&lt;/i&gt;"

    html = "&lt;h3&gt;Hello {name}!&lt;/h3&gt;" \
        "&lt;b&gt;Hostname:&lt;/b&gt; {hostname}&lt;br/&gt;" \
        "&lt;b&gt;Visits:&lt;/b&gt; {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
</code></pre>

<p><hr><strong>REDIS SERVICES IN THE SAME KUBERNETES CLUSTER</strong><hr>  </p>

<p>The <code>redis master</code> and <code>redis slave</code> running in the cluster are from public registries and are brought into the cluster by running <code>kubectl apply -f</code> with the following JSON:  </p>

<p>Redis Master replication controller <a href="https://raw.githubusercontent.com/kubernetes/kubernetes/v1.10.3/examples/guestbook-go/redis-master-controller.json" rel="nofollow noreferrer">JSON from this link.</a><br>
Redis Master service <a href="https://raw.githubusercontent.com/kubernetes/kubernetes/v1.10.3/examples/guestbook-go/redis-master-service.json" rel="nofollow noreferrer">JSON from this link.</a><br>
Redis Slave replication controller <a href="https://raw.githubusercontent.com/kubernetes/kubernetes/v1.10.3/examples/guestbook-go/redis-slave-controller.json" rel="nofollow noreferrer">JSON from this link.</a><br>
Redis Slave service <a href="https://raw.githubusercontent.com/kubernetes/kubernetes/v1.10.3/examples/guestbook-go/redis-slave-service.json" rel="nofollow noreferrer">JSON from this link.</a>  </p>

CodeMed
<blockquote>
  <p>What specific changes need to be made to the code below in order for the python app in app.py to be able to communicate successfully with the redis running in the same cluster?</p>
</blockquote>

<pre class="lang-py prettyprint-override"><code>redis = Redis(host="redis-master", db=0, socket_connect_timeout=2, socket_timeout=2)
</code></pre>

<p>because the <code>Service</code> you installed is named <a href="https://github.com/kubernetes/kubernetes/blob/v1.10.3/examples/guestbook-go/redis-master-service.json#L5" rel="nofollow noreferrer"><code>redis-master</code></a>, although that simple change I proposed above <em>assumes</em> that the flask app is running in the same kubernetes namespace as the <code>redis-master</code> <code>Service</code>. If that isn't true, you'll need to switch it to read:</p>

<pre><code>redis = Redis(host="redis-master.whatever-namespace.svc.cluster.local",
</code></pre>

<p>and replace <code>whatever-namespace</code> with the actual, correct, namespace. If you don't remember or know, <code>kubectl get --all-namespaces=true svc | grep redis-master</code> will remind you.</p>
