<p>I have a minikube cluster running locally (v0.17.1), with two deployments: one is a Redis instance and one is a custom app that is trying to connect to the Redis instance. My configuration is more or less copy/pasted from the official docs and the <a href="https://github.com/kubernetes/kubernetes/blob/master/examples/guestbook/all-in-one/guestbook-all-in-one.yaml" rel="nofollow noreferrer">Kubernetes guestbook example</a>. </p>

<p>Service definition and deployment:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: poller-redis
  labels:
    app: poller-redis
    tier: backend
    role: database
    target: poller
spec:
  selector:
    app: poller
    tier: backend
    role: service
  ports:
  - port: 6379
    targetPort: 6379

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: poller-redis
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: poller-redis
        tier: backend
        role: database
        target: poller
    spec:
      containers:
      - name: poller-redis
        image: gcr.io/jmen-1266/jmen-redis:a67b5f4bfd8ea8441ed66a8fcb6596f276017a1c
        ports:
        - containerPort: 6379
        env:
        - name: GET_HOSTS_FROM
          value: dns
      imagePullSecrets:
      - name: gcr-json-key
</code></pre>

<p>App deployment:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: poller
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: poller
        tier: backend
        role: service
    spec:
      containers:
      - name: poller
        image: gcr.io/jmen-1266/poller:a96a452292e894e46339309cc024cac67647cc25
        imagePullPolicy: Always
      imagePullSecrets:
      - name: gcr-json-key
</code></pre>

<p>Relevant (I hope) Kubernetes info:</p>

<pre><code>$ kubectl get services
NAME           CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes     10.0.0.1     &lt;none&gt;        443/TCP    24d
poller-redis   10.0.0.137   &lt;none&gt;        6379/TCP   20d

$ kubectl get deployments
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
poller         1         1         1            1           12d
poller-redis   1         1         1            1           4d

$ kubectl get endpoints
NAME           ENDPOINTS           AGE
kubernetes     10.0.2.15:8443      24d
poller-redis   172.17.0.7:6379     20d
</code></pre>

<p>Inside the <code>poller</code> pod (custom app), I get environment variables created for Redis:</p>

<pre><code># env | grep REDIS
POLLER_REDIS_SERVICE_HOST=10.0.0.137
POLLER_REDIS_SERVICE_PORT=6379
POLLER_REDIS_PORT=tcp://10.0.0.137:6379
POLLER_REDIS_PORT_6379_TCP_ADDR=10.0.0.137
POLLER_REDIS_PORT_6379_TCP_PORT=6379
POLLER_REDIS_PORT_6379_TCP_PROTO=tcp
POLLER_REDIS_PORT_6379_TCP=tcp://10.0.0.137:6379
</code></pre>

<p>However, if I try to connect to that port, I cannot. Doing something like:</p>

<pre><code>nc -vz poller-redis 6379
</code></pre>

<p>fails.</p>

<p>What I have noticed is that I cannot access the Redis service via its ClusterIP but I can via the IP of the pod running Redis.</p>

<p>Any ideas, please?</p>

<p>Figured this out in the end, it looks like I misunderstood how the service selectors work in Kubernetes. </p>

<p>I have posted that my service definition is:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: poller-redis
  labels:
    app: poller-redis
    tier: backend
    role: database
    target: poller
spec:
  selector:
    app: poller
    tier: backend
    role: service
  ports:
  - port: 6379
    targetPort: 6379
</code></pre>

<p>The problem is that <code>metadata.labels</code> and <code>spec.selector</code> are different, when they should actually be the same. I still do not exactly understand why this is the case judging by the Kubernetes documentation, but there you have it. Now my service definition looks like:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: poller-redis
  labels:
    app: poller-redis
    tier: backend
    role: database
    target: poller
spec:
  selector:
    app: poller-redis
    tier: backend
    role: database
    target: poller
  ports:
  - port: 6379
    targetPort: 6379
</code></pre>

<p>I also now use straight up DNS lookup (i.e. <code>ping poller-redis</code>) rather than trying to connect to <code>localhost:6379</code> from my target pods.</p>
