<p>This is my application code:</p>

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

# Connect to Redis
redis = Redis(host=os.getenv("REDIS", "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>Here want to connect a Redis host in the Kubernetes cluster. So put a environment variable <code>REDIS</code> to set value in Kubernetes' manifest file.</p>

<p>This is the Kubernetes deployment manifest file:</p>

<pre><code>apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "name" . }}
    chart: {{ template "chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "name" . }}
        release: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: REDIS
              value: {{ template "fullname" . }}-master-svc
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
</code></pre>

<p>In order to connect Redis host in the same cluster, set environment value as:</p>

<pre><code>      env:
        - name: REDIS
          value: {{ template "fullname" . }}-master-svc
</code></pre>

<p>About Redis cluster, installed by official <a href="https://github.com/kubernetes/charts/tree/master/stable/redis-ha" rel="nofollow noreferrer">redis-ha</a> chart. Its master service manifest file is:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: {{ template "fullname" . }}-master-svc
  annotations:
{{ toYaml .Values.servers.annotations | indent 4 }}
spec:
  ports:
  - port: 6379
    protocol: TCP
    targetPort: 6379
  selector:
    app: "redis-ha"
    redis-node: "true"
    redis-role: "master"
    release: "{{ .Release.Name }}"
  type: "{{ .Values.servers.serviceType }}"
</code></pre>

<p>But it seems that the application pod didn't connect to the Redis master service name successfully. When I got accessed URL:</p>

<pre><code>export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services wishful-rabbit-mychart)
export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
</code></pre>

<p>from browser, got this result:</p>

<pre><code>Hello World!

Hostname: wishful-rabbit-mychart-85dc7658c6-9blg5
Visits: cannot connect to Redis, counter disabled
</code></pre>

<p>More information about <code>pods</code> and <code>services</code>:</p>

<pre><code>kubectl get po
NAME                                               READY     STATUS    RESTARTS   AGE
wishful-rabbit-mychart-bdfdf6558-8fjmb             1/1       Running   0          8m
wishful-rabbit-mychart-bdfdf6558-9khfs             1/1       Running   0          7m
wishful-rabbit-mychart-bdfdf6558-hgqxv             1/1       Running   0          8m
wishful-rabbit-mychart-sentinel-8667dd57d4-9njwq   1/1       Running   0          37m
wishful-rabbit-mychart-sentinel-8667dd57d4-jsq6d   1/1       Running   0          37m
wishful-rabbit-mychart-sentinel-8667dd57d4-ndqss   1/1       Running   0          37m
wishful-rabbit-mychart-server-746f47dfdd-2fn4s     1/1       Running   0          37m
wishful-rabbit-mychart-server-746f47dfdd-bwgrq     1/1       Running   0          37m
wishful-rabbit-mychart-server-746f47dfdd-spkkm     1/1       Running   0          37m

kubectl get svc
NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                          ClusterIP   10.96.0.1        &lt;none&gt;        443/TCP        3h
wishful-rabbit-mychart              NodePort    10.101.103.224   &lt;none&gt;        80:30033/TCP   37m
wishful-rabbit-mychart-master-svc   ClusterIP   10.108.128.167   &lt;none&gt;        6379/TCP       37m
wishful-rabbit-mychart-sentinel     ClusterIP   10.107.63.208    &lt;none&gt;        26379/TCP      37m
wishful-rabbit-mychart-slave-svc    ClusterIP   10.99.211.111    &lt;none&gt;        6379/TCP       37m
</code></pre>

<p>What's the right usage?</p>

<hr>

<h1>Redis Server Pod Env</h1>

<pre><code>kubectl exec -it wishful-rabbit-mychart-server-746f47dfdd-2fn4s -- sh
/ # printenv
KUBERNETES_PORT=tcp://10.96.0.1:443
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP_PORT=6379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP_ADDR=10.108.128.167
KUBERNETES_SERVICE_PORT=443
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP_PROTO=tcp
WISHFUL_RABBIT_MYCHART_SERVICE_HOST=10.101.103.224
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP_PORT=6379
HOSTNAME=wishful-rabbit-mychart-server-746f47dfdd-2fn4s
SHLVL=1
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP_PROTO=tcp
HOME=/root
WISHFUL_RABBIT_MYCHART_SENTINEL_SERVICE_HOST=10.107.63.208
WISHFUL_RABBIT_MYCHART_PORT=tcp://10.101.103.224:80
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP=tcp://10.99.211.111:6379
WISHFUL_RABBIT_MYCHART_SERVICE_PORT=80
WISHFUL_RABBIT_MYCHART_SENTINEL_SERVICE_PORT=26379
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT=tcp://10.107.63.208:26379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP=tcp://10.108.128.167:6379
WISHFUL_RABBIT_MYCHART_PORT_80_TCP_ADDR=10.101.103.224
REDIS_CHART_PREFIX=wishful-rabbit-mychart-
TERM=xterm
WISHFUL_RABBIT_MYCHART_PORT_80_TCP_PORT=80
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
WISHFUL_RABBIT_MYCHART_PORT_80_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP_ADDR=10.107.63.208
WISHFUL_RABBIT_MYCHART_PORT_80_TCP=tcp://10.101.103.224:80
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_SERVICE_HOST=10.99.211.111
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP_PORT=26379
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
WISHFUL_RABBIT_MYCHART_MASTER_SVC_SERVICE_HOST=10.108.128.167
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_SERVICE_PORT=6379
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT=tcp://10.99.211.111:6379
WISHFUL_RABBIT_MYCHART_SERVICE_PORT_HTTP=80
REDIS_SENTINEL_SERVICE_HOST=redis-sentinel
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP_ADDR=10.99.211.111
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP=tcp://10.107.63.208:26379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT=tcp://10.108.128.167:6379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_SERVICE_PORT=6379
</code></pre>

<hr>

<h1>Application Pod Env</h1>

<pre><code>kubectl exec -it wishful-rabbit-mychart-85dc7658c6-8wlq6 -- sh
# printenv
KUBERNETES_SERVICE_PORT=443
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP_PORT=6379
KUBERNETES_PORT=tcp://10.96.0.1:443
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP_ADDR=10.108.128.167
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP_PROTO=tcp
WISHFUL_RABBIT_MYCHART_SERVICE_HOST=10.101.103.224
HOSTNAME=wishful-rabbit-mychart-85dc7658c6-8wlq6
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP_PORT=6379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP_PROTO=tcp
PYTHON_PIP_VERSION=9.0.1
WISHFUL_RABBIT_MYCHART_SENTINEL_SERVICE_HOST=10.107.63.208
HOME=/root
GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF
REDIS=wishful-rabbit-mychart-master-svc
WISHFUL_RABBIT_MYCHART_SERVICE_PORT=80
WISHFUL_RABBIT_MYCHART_PORT=tcp://10.101.103.224:80
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP=tcp://10.99.211.111:6379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT_6379_TCP=tcp://10.108.128.167:6379
WISHFUL_RABBIT_MYCHART_SENTINEL_SERVICE_PORT=26379
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT=tcp://10.107.63.208:26379
WISHFUL_RABBIT_MYCHART_PORT_80_TCP_ADDR=10.101.103.224
NAME=World
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
WISHFUL_RABBIT_MYCHART_PORT_80_TCP_PORT=80
WISHFUL_RABBIT_MYCHART_PORT_80_TCP_PROTO=tcp
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
LANG=C.UTF-8
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP_ADDR=10.107.63.208
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_SERVICE_HOST=10.99.211.111
WISHFUL_RABBIT_MYCHART_PORT_80_TCP=tcp://10.101.103.224:80
PYTHON_VERSION=2.7.14
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP_PORT=26379
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
WISHFUL_RABBIT_MYCHART_MASTER_SVC_SERVICE_HOST=10.108.128.167
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/app
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT=tcp://10.99.211.111:6379
WISHFUL_RABBIT_MYCHART_SERVICE_PORT_HTTP=80
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_SERVICE_PORT=6379
WISHFUL_RABBIT_MYCHART_SLAVE_SVC_PORT_6379_TCP_ADDR=10.99.211.111
WISHFUL_RABBIT_MYCHART_SENTINEL_PORT_26379_TCP=tcp://10.107.63.208:26379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_SERVICE_PORT=6379
WISHFUL_RABBIT_MYCHART_MASTER_SVC_PORT=tcp://10.108.128.167:6379
</code></pre>

<h2>All Endpoints</h2>

<pre><code>kubectl get ep
NAME                                ENDPOINTS                                              AGE
kubernetes                          10.0.2.15:8443                                         4h
wishful-rabbit-mychart              172.17.0.5:80,172.17.0.6:80,172.17.0.7:80              1h
wishful-rabbit-mychart-master-svc   &lt;none&gt;                                                 1h
wishful-rabbit-mychart-sentinel     172.17.0.11:26379,172.17.0.12:26379,172.17.0.8:26379   1h
wishful-rabbit-mychart-slave-svc    &lt;none&gt;
</code></pre>

<hr>

<h1>Describe Redis Master Service</h1>

<pre><code>kubectl describe svc wishful-rabbit-mychart-master-svc
Name:              wishful-rabbit-mychart-master-svc
Namespace:         default
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=redis-ha,redis-node=true,redis-role=master,release=wishful-rabbit
Type:              ClusterIP
IP:                10.108.128.167
Port:              &lt;unset&gt;  6379/TCP
TargetPort:        6379/TCP
Endpoints:         &lt;none&gt;
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>

<hr>

<h1>Describe Redis Server Pod</h1>

<pre><code>kubectl describe po wishful-rabbit-mychart-server-746f47dfdd-2fn4s
Name:           wishful-rabbit-mychart-server-746f47dfdd-2fn4s
Namespace:      default
Node:           minikube/192.168.99.100
Start Time:     Fri, 02 Feb 2018 15:28:42 +0900
Labels:         app=mychart
                chart=mychart-0.1.0
                heritage=Tiller
                name=redis-server
                pod-template-hash=3029038988
                podIP=172.17.0.10
                redis-node=true
                redis-role=master
                release=wishful-rabbit
                runID=cbf8e0
Annotations:    kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"wishful-rabbit-mychart-server-746f47dfdd","uid":"4fcb0dfc-07e2-1...
Status:         Running
IP:             172.17.0.10
Controlled By:  ReplicaSet/wishful-rabbit-mychart-server-746f47dfdd
Containers:
  redis:
    Container ID:   docker://2734d60bd44a1446ec6556369359ed15b866a4589abe1e6d394f9252114c6d4d
    Image:          quay.io/smile/redis:4.0.6r2
    Image ID:       docker-pullable://quay.io/smile/redis@sha256:8948a952920d4495859c984546838d4c9b4c71e0036eef86570922d91cacb3df
    Port:           6379/TCP
    State:          Running
      Started:      Fri, 02 Feb 2018 15:28:44 +0900
    Ready:          True
    Restart Count:  0
    Environment:
      REDIS_SENTINEL_SERVICE_HOST:  redis-sentinel
      REDIS_CHART_PREFIX:           wishful-rabbit-mychart-
    Mounts:
      /redis-master-data from data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-wfv2q (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  data:
    Type:    EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
  default-token-wfv2q:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-wfv2q
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  &lt;none&gt;
Tolerations:     &lt;none&gt;
Events:          &lt;none&gt;
</code></pre>

<p>There is a discrepancy between your <code>wishful-rabbit-mychart-master-svc</code> service's <code>Selector</code> and your master redis pods' <code>labels</code>.</p>

<p>Your service is searching for pods with the following labels:</p>

<pre><code>app=redis-ha
redis-node=true
redis-role=master
release=wishful-rabbit
</code></pre>

<p>While your pods have the following labels:</p>

<pre><code>app=mychart
chart=mychart-0.1.0
heritage=Tiller
name=redis-server
pod-template-hash=3029038988
podIP=172.17.0.10
redis-node=true
redis-role=master
release=wishful-rabbit
runID=cbf8e0
</code></pre>

<p>As you can see the <code>app</code> label is different (<code>redis-ha</code> in your service, <code>mychart</code> in your pods).</p>

<p>This causes the service to be unbounded (it doesn't know where to forward the incoming traffic).</p>

<p>While I don't know the actual cause of this configuration error, I can suggest a solution to make it work.</p>

<p>You have to edit your redis service and change its selector attribute in order to match the pod's labels. Just run:</p>

<pre><code>kubectl edit svc wishful-rabbit-mychart-master-svc
</code></pre>

<p>and change <code>app: "redis-ha"</code> with <code>app: "mychart"</code>.</p>

<p>Your application should suddenly be able to reach your redis instance.</p>
