If a pod runs in the host network of the node where the pod is deployed, the pod can use the network namespace and network resources of the node. In this scenario, the pod can access loopback devices on the node, listen for requests sent to specific addresses, and monitor the traffic of other pods. This topic describes how to use the host network.
Prerequisites
A Container Service for Kubernetes (ACK) cluster is created. For more information, see Create an ACK Pro cluster.
A kubectl client is connected to the cluster. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
Procedure
Open the YAML file named host-network.yaml. If you cannot find the file in the current environment, create one. Add
hostNetwork: true
to the spec section.The following sample code shows the content of the YAML file:
apiVersion: v1 kind: Pod metadata: name: nginx spec: hostNetwork: true containers: - name: nginx image: nginx
Run the following command to create a pod:
kubectl apply -f host-network.yaml
Run the following command to check whether the pod uses the host network of the node:
kubectl get pod -o wide
The output indicates that the IP address of the pod is the same as that of the node. This means that the pod runs in the host network of the node.
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE nginx 1/1 Running 0 29s 192.168.XX.XX cn-zhangjiakou.192.168.XX.XX <none>