All Products
Search
Document Center

Container Service for Kubernetes:Use the host network

Last Updated:Sep 05, 2023

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

Procedure

  1. 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
  2. Run the following command to create a pod:

    kubectl apply -f host-network.yaml
  3. 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>