<p>Pods in a kubernetes cluster can be reached by sending network requests to the dns of a service that they are a member of. Network requests have to be send to <code>[service].[namespace].svc.cluster.local</code> and get load balanced between all members of that service.</p>

<p>This works fine to let some pod reach another pod, but it fails if a pod tries to reach itself via a service that he's a member of. It always results in a timeout.</p>

<p>Is this a bug in Kubernetes (in my case minikube v0.35.0) or is some additional configuration required?</p>

<hr>

<p>Here's some debug info:</p>

<p>Let's contact the service from some other pod. This works fine:</p>

<pre><code>daemon@auth-796d88df99-twj2t:/opt/docker$ curl -v -X POST -H "Accept: application/json" --data '{}' http://message-service.message.svc.cluster.local:9000/message/get-messages
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 10.107.209.9...
* TCP_NODELAY set
* Connected to message-service.message.svc.cluster.local (10.107.209.9) port 9000 (#0)
&gt; POST /message/get-messages HTTP/1.1
&gt; Host: message-service.message.svc.cluster.local:9000
&gt; User-Agent: curl/7.52.1
&gt; Accept: application/json
&gt; Content-Length: 2
&gt; Content-Type: application/x-www-form-urlencoded
&gt; 
* upload completely sent off: 2 out of 2 bytes
&lt; HTTP/1.1 401 Unauthorized
&lt; Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
&lt; X-Frame-Options: DENY
&lt; X-XSS-Protection: 1; mode=block
&lt; X-Content-Type-Options: nosniff
&lt; Content-Security-Policy: default-src 'self'
&lt; X-Permitted-Cross-Domain-Policies: master-only
&lt; Date: Wed, 20 Mar 2019 04:36:51 GMT
&lt; Content-Type: text/plain; charset=UTF-8
&lt; Content-Length: 12
&lt; 
* Curl_http_done: called premature == 0
* Connection #0 to host message-service.message.svc.cluster.local left intact
Unauthorized
</code></pre>

<p>Now we try to let the pod contact the service that he's a member of:</p>

<pre><code>Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 10.107.209.9...
* TCP_NODELAY set
* connect to 10.107.209.9 port 9000 failed: Connection timed out
* Failed to connect to message-service.message.svc.cluster.local port 9000: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to message-service.message.svc.cluster.local port 9000: Connection timed out
</code></pre>

<p>If I've read the curl debug log correctly, the dns resolves to the ip address 10.107.209.9. The pod can be reached from any other pod via that ip but the pod cannot use it to reach itself.</p>

<p>Here are the network interfaces of the pod that tries to reach itself:</p>

<pre><code>daemon@message-58466bbc45-lch9j:/opt/docker$ ip a
1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: sit0@NONE: &lt;NOARP&gt; mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
296: eth0@if297: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:09 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.9/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
</code></pre>

<p>Here is the kubernetes file deployed to minikube:</p>

<pre><code>apiVersion: v1
kind: Namespace
metadata:
  name: message

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: message
  name: message
  namespace: message
spec:
  replicas: 1
  selector:
    matchLabels:
      app: message
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: message
    spec:
      containers:
        - name: message
          image: message-impl:0.1.0-SNAPSHOT
          imagePullPolicy: Never
          ports:
            - name: http
              containerPort: 9000
              protocol: TCP
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace    
            - name: KAFKA_KUBERNETES_NAMESPACE
              value: kafka
            - name: KAFKA_KUBERNETES_SERVICE
              value: kafka-svc
            - name: CASSANDRA_KUBERNETES_NAMESPACE
              value: cassandra
            - name: CASSANDRA_KUBERNETES_SERVICE
              value: cassandra
            - name: CASSANDRA_KEYSPACE
              value: service_message
---

# Service for discovery
apiVersion: v1
kind: Service
metadata:
  name: message-service
  namespace: message
spec:
  ports:
    - port: 9000
      protocol: TCP
  selector:
    app: message
---

# Expose this service to the api gateway
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: message
  namespace: message
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
    - host: api.fload.cf
      http:
        paths:
          - path: /message
            backend:
              serviceName: message-service
              servicePort: 9000
</code></pre>

<p>This is a known <a href="https://github.com/kubernetes/minikube/issues/1568" rel="nofollow noreferrer">minikube issue</a>. The discussion contains the following workarounds:</p>

<p>1) Try:</p>

<pre><code>minikube ssh
sudo ip link set docker0 promisc on 
</code></pre>

<p>2) Use a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">headless service</a>: <code>clusterIP: None</code></p>
