Kubenetes POD hostnetwork cause nodePort does not work

2/27/2017

I encounter a issue: 1) declare hostNetwork=true for POD in a deployment, and create the deployment. 2) Declare a service with nodePort to the deployment pods

I found the nodePort cannot be accessed by serviceClusterIP:nodePort in the host which the pod is not running on.

While I remove hostnetwork=true, the serviceClusterIP:nodePort can be accessed by any host node in the cluster.

What's the worong?

My kubernetes version is listed below, I am using weave net.

# kubectl version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.1", GitCommit:"82450d03cb057bab0950214ef122b67c83fb11df", GitTreeState:"clean", BuildDate:"2016-12-14T00:57:05Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.0", GitCommit:"58b7c16a52c03e4a849874602be42ee71afdcab1", GitTreeState:"clean", BuildDate:"2016-12-12T23:31:15Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
-- Bo Wang
kubernetes
weave

1 Answer

2/27/2017

In Kubernetes, nodePort is a mechanism to map a port on a pod's network interface out to a port on all nodes.

The normal case, when you don't say hostNetwork=true is that each pod has its own "network namespace" - it has its own virtual network device with a unique IP address and it has a localhost interface on 127.0.0.1 that is shared by all containers in the pod.

When you ask for your pod to use the host's network interface by saying hostNetwork=true, it has none of the above; it just uses the host network devices. And the nodePort mechanism is not available in this mode.

Possibly there is an error message somewhere to tell you about this - look in kubectl events or in kubelet's log file. If you cannot find an error anywhere please file this fact as a bug against Kubernetes.

-- Bryan
Source: StackOverflow