10

I try to understand hostIP and hostPort in Kubernetes.

Here is my cluster configuration :

3 vagrant nodes :

nodes = [
  { :hostname => 'k8s-master', :ip => '192.168.150.200', :ram => 4096 },
  { :hostname => 'k8s-minion1', :ip => '192.168.150.201', :ram => 4096 },
  { :hostname => 'k8s-minion2', :ip => '192.168.150.202', :ram => 4096 },
]

I write the following manifest to test it :

apiVersion: v1
kind: Pod
metadata:
  name: firstpod
spec:
  containers:
  - name: container
    image: nginx
    ports:
    - containerPort: 80
      hostIP: 10.0.0.1
      hostPort: 8080

I deploy with kubectl apply -f port.yml The pod runs on k8s-minion2

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

I can curl the ngnix from inside the cluster as follows:

#ssh inside the cluster
    vagrant ssh k8s-master
#curl the containerPort on the pod ip
    curl 10.38.0.3:80

But, I have no idea how to use hostIp and hostPort. curl 10.0.0.1:8080 gives :

curl: (7) Failed to connect to 10.0.0.1 port 80: Connection timed out

and curling the node or the cluster Ip gives :

curl: (7) Failed to connect to 10.38.0.3 port 8080: Connection refused

So where is port 8080 open and what does hostIp intended for?

Thanks

1
  • Try nodeip:8080 Commented Sep 1, 2020 at 16:52

2 Answers 2

15

If you take a look at the kubernetes API reference you'll find that hostIP is IP that is being assigned once the pod is scheduled into node.

hostIP (string) - IP address of the host to which the pod is assigned. Empty if not yet scheduled.

This can be further exposed as env inside the pod if needed (spec.hostIP)

With hostPort you can expose container port to the external network at the address <hostIP>:<hostPort>, 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 here.

If you want to reach your pod there are also other ways to do that, such as ClusterIP or NodePort depending if the request comes internally or externally. This article goes thru them and their differences.

4
  • 1
    thank you @thomas for your answer. How do you understand the sentence you cited. Because It seems to me the hostIP field is simply ignored in my manifest. The ip address of the node my pod is assigned to is 192.168.150.202 and setting it to minion1 for instance does not change anything. What is the difference between host and node? and what is the valid values of the hostIp field? thx again.
    – Abdelghani
    Commented Sep 2, 2020 at 17:45
  • Host/Node is part of underlying infrastructure that your k8s cluster runs on. So you can`t change/define it's ip addresses with k8s yaml file.
    – acid_fuji
    Commented Sep 3, 2020 at 6:55
  • thx again @thomas. If I understand what you say, hostIp is populated by the system. But the documentation does not say anything about it and kubectl geet podd firstpod -o yaml does not seem to populated this field? Maybe it is deprecated or legacy? Note I am talking about a nude pod not about a service.
    – Abdelghani
    Commented Sep 3, 2020 at 12:31
  • For me it works and I can see the hostIP. I was testing this using 1.18.3. Using only pod is fine but since pods are intended to be disposable and replaceable its better to use some static like service to reach them ;)
    – acid_fuji
    Commented Sep 8, 2020 at 8:50
0

ClusterIP (10.38.0.3) will only be accessed from within the cluster. To hit the service from outside the cluster, try nodeIP:port 192.168.150.202:8080

NodePort Service type allows us to expose our services on each Node’s IP at a static port. Therefore, it allows accessing the service from outside the cluster.

1
  • No. curl 192.168.150.202:8080 results in : curl: (7) Failed to connect to 192.168.150.202 port 8080: Connexion refused. Note, I am not using any service behind the pod. This is just for testing.
    – Abdelghani
    Commented Sep 2, 2020 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.