<p>I try to understand hostIP and hostPort in Kubernetes.</p>
<p>Here is my cluster configuration :</p>
<p>3 vagrant nodes :</p>
<pre><code>nodes = [
  { :hostname =&gt; 'k8s-master', :ip =&gt; '192.168.150.200', :ram =&gt; 4096 },
  { :hostname =&gt; 'k8s-minion1', :ip =&gt; '192.168.150.201', :ram =&gt; 4096 },
  { :hostname =&gt; 'k8s-minion2', :ip =&gt; '192.168.150.202', :ram =&gt; 4096 },
]
</code></pre>
<p>I write the following manifest to test it :</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: firstpod
spec:
  containers:
  - name: container
    image: nginx
    ports:
    - containerPort: 80
      hostIP: 10.0.0.1
      hostPort: 8080
</code></pre>
<p>I deploy with <code>kubectl apply -f port.yml</code>
The pod runs on k8s-minion2</p>
<pre><code>kubectl get pods -o wide gives :
NAME       READY     STATUS    RESTARTS   AGE       IP          NODE
firstpod   1/1       Running   0          2m        10.38.0.3   k8s-minion2
</code></pre>
<p>I can curl the ngnix from inside the cluster as follows:</p>
<pre><code>#ssh inside the cluster
    vagrant ssh k8s-master
#curl the containerPort on the pod ip
    curl 10.38.0.3:80
</code></pre>
<p>But, I have no idea how to use hostIp and hostPort.
curl 10.0.0.1:8080 gives :</p>
<pre><code>curl: (7) Failed to connect to 10.0.0.1 port 80: Connection timed out
</code></pre>
<p>and curling the node or the cluster Ip gives :</p>
<pre><code>curl: (7) Failed to connect to 10.38.0.3 port 8080: Connection refused
</code></pre>
<p>So where is port 8080 open and what does hostIp intended for?</p>
<p>Thanks</p>

<p>If you take a look at the kubernetes API <a href="https://v1-18.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#pod-v1-core" rel="nofollow noreferrer">reference</a> you'll find that <code>hostIP</code> is IP that is being assigned once the pod is scheduled into node.</p>
<blockquote>
<p><code>hostIP</code> (<em>string</em>)   - IP address of the host to which the pod is
assigned. Empty if not yet scheduled.</p>
</blockquote>
<p>This can be further <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">exposed</a> as env inside the pod if needed (<code>spec.hostIP</code>)</p>
<p>With <code>hostPort</code> you can expose container port to the external network at the address <code>&lt;hostIP&gt;:&lt;hostPort&gt;</code>, where the hostIP is the IP address of the Kubernetes node where the container is running and the hostPort is the port requested by the user. You can read more about <a href="http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/" rel="nofollow noreferrer">here</a>.</p>
<p>If you want to reach your pod there are also other ways to do that, such as <code>ClusterIP</code> or <code>NodePort</code> depending if the request comes internally or externally.  This <a href="https://rtfm.co.ua/en/kubernetes-clusterip-vs-nodeport-vs-loadbalancer-services-and-ingress-an-overview-with-examples/" rel="nofollow noreferrer">article</a> goes thru them and their differences.</p>
