<p>What is the correct way to address a host node from inside containers?</p>
<p>I have a container that resides on a host node, and the host node has a web server running on it. The container needs to be able to hit web server on the host node.</p>
<p>I expected to find an alias for the host node like node..cluster.local (10.1.111.192), but I can't find it in the documentation.</p>
<p>The environment is microk8s with kubedns enabled.</p>
<p><a href="https://i.stack.imgur.com/5gjWF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5gjWF.png" alt="Drawing" /></a></p>
<p>The address assigned to the host on the calico interface is accessible from inside the node: 10.1.111.192
and I found in the documentation that I can add a hostalias-pod, so I could add the alias, eg. node.local (10.1.111.192). <a href="https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/</a></p>
<p>Hardcoding the IP doesn't seem graceful, but I'm in a single-node environment, so it's not likely to matter if the node address doesn't change (does this ever change?). This is a small project where I'm trying to learn though, so I wanted to find the most correct way to do this.</p>

timoq
<p>You can use the <a href="https://kubernetes.io/docs/concepts/workloads/pods/downward-api/" rel="nofollow noreferrer">downward API</a> to get the underlying hostname, worth to mention that it will return the <code>IP</code> of the node where the pod is running on.</p>
<pre><code>      env:
        - name: HOST_IP
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
</code></pre>
<p>so from inside pod, you will be able to reach that particular host</p>
<pre><code>curl $HOST_IP:8080
</code></pre>
<p>A complete example</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: print-host-ip
spec:
  containers:
    - name: print-host-ip
      image: gcr.io/google_containers/busybox
      env:
        - name: HOST_IP
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
      command: [ &quot;/bin/sh&quot;, &quot;-c&quot;, 'echo &quot;host ip is $HOST_IP&quot;' ]
</code></pre>
