Updated on 2025-08-22 GMT+08:00

Network Policies

Network policies are designed by Kubernetes to restrict pod access. Like a firewall at the application layer, network policies enhance network security. The capabilities of network policies are determined by the network add-ons available in the cluster.

By default, if a namespace does not have any policies configured, pods in the namespace accept traffic from any ingress sources and send traffic to any egress destinations.

There are three kinds of selectors available for network policies:

  • namespaceSelector: selects particular namespaces for which all pods should be allowed as ingress sources or egress destinations.
  • podSelector: selects particular pods in the same namespace as the network policy which should be allowed as ingress sources or egress destinations.
  • ipBlock: selects particular IP address ranges that are allowed as ingress sources or egress destinations. (Only egress IP address ranges can be specified.)

Relationships Between Network Policies and Cluster Types

Cluster Type

CCE Standard Cluster

CCE Standard Cluster

CCE Turbo Cluster

Network Model

Tunnel

VPC

Cloud Native Network 2.0

NetworkPolicy

Enabled by default

Disabled by default (To use network policies, enable DataPlane V2 when creating a cluster.)

Disabled by default (To use network policies, enable DataPlane V2 when creating a cluster.)

Data plane implementation

OpenvSwitch

eBPF

eBPF

Cluster version for ingress rules

All versions

Cluster version: v1.27.16-r30, v1.28.15-r20, v1.29.13-r0, v1.30.10-r0, v1.31.6-r0, or later

Cluster version: v1.27.16-r10, v1.28.15-r0, v1.29.10-r0, v1.30.6-r0, or later

Cluster version for egress rules

v1.23 and later

Selector for ingress rules

namespaceSelector

podSelector

namespaceSelector

podSelector

IPBlock

namespaceSelector

podSelector

IPBlock

Selector for egress rules

namespaceSelector

podSelector

IPBlock

Supported OS

EulerOS

CentOS

Huawei Cloud EulerOS 2.0

Huawei Cloud EulerOS 2.0

Huawei Cloud EulerOS 2.0

IPv6 network policies

Not supported

Not supported

Supported

Secure containers

Not supported

Not supported

Not supported

IPBlock scope

Not limited

Subnets within the pod CIDR block, Service CIDR block, and node IP addresses

Subnets within the pod CIDR block, Service CIDR block, and node IP addresses

Limit ClusterIP access through workload labels

Not supported

Supported

Supported

Limit the internal cloud server CIDR block of 100.125.0.0/16

Supported

Supported

Not supported

SCTP

Not supported

Supported

Not supported

Always allow access to pods on a node from other nodes

Supported

Supported

Supported

Configure EndPort in network policies

Not supported

Supported

Not supported

  • CCE DataPlane V2 is released with restrictions. To use this feature, submit a service ticket to CCE.
  • Secure containers (such as Kata as the container runtime) are not supported by network policies.
  • If you upgrade a CCE standard cluster with a tunnel network to a version that supports egress rules in in-place mode, the rules will not work because the node OS is not upgraded. In this case, reset the node.
  • When a network policy is enabled for a cluster with a tunnel network, a pod's source IP address is embedded in the optional field of packets it sends to any Service CIDR block. This enables the configuration of network policy rules on the destination pod, taking into account the source IP address of the pod.

Using Ingress Rules Through YAML

  • Scenario 1: Controlled by a preset network policy, a pod can only be accessed by pods with specific labels.
    Figure 1 podSelector

    The pod labeled with role=db only permits access to its port 6379 from pods labeled with role=frontend. To achieve this, take the following steps:

    1. Create the access-demo1.yaml file.
      vim access-demo1.yaml

      File content:

      apiVersion: networking.k8s.io/v1
      kind: NetworkPolicy
      metadata:
        name: access-demo1
        namespace: default
      spec:
        podSelector:                  # The rule takes effect for pods with the role=db label.
          matchLabels:
            role: db
        ingress:                      # This is an ingress rule.
        - from:
          - podSelector:              # Only allows the access of the pods labeled with role=frontend.
              matchLabels:
                role: frontend
          ports:                      # Only TCP can be used to access port 6379.
          - protocol: TCP
            port: 6379
    2. Run the following command to create the network policy defined in the access-demo1.yaml file:
      kubectl apply -f access-demo1.yaml

      Expected output:

      networkpolicy.networking.k8s.io/access-demo1 created
  • Scenario 2: Controlled by a preset network policy, a pod can only be accessed by pods in a specific namespace.
    Figure 2 namespaceSelector

    The pod labeled with role=db only permits access to its port 6379 from pods in the namespace labeled with project=myproject. To achieve this, take the following steps:

    1. Create the access-demo2.yaml file.
      vim access-demo2.yaml

      File content:

      apiVersion: networking.k8s.io/v1
      kind: NetworkPolicy
      metadata:
        name: access-demo2
      spec:
        podSelector:                  # The rule takes effect for pods with the role=db label.
          matchLabels:
            role: db
        ingress:                      # This is an ingress rule.
        - from:
          - namespaceSelector:        # Only allows the access of the pods in the namespace labeled with project=myproject.
              matchLabels:
                project: myproject
          ports:                      # Only TCP can be used to access port 6379.
          - protocol: TCP
            port: 6379
    2. Run the following command to create the network policy defined in the access-demo2.yaml file:
      kubectl apply -f access-demo2.yaml

      Expected output:

      networkpolicy.networking.k8s.io/access-demo2 created

Using Egress Rules Through YAML

Clusters of v1.23 or later that use a tunnel network support egress rules.

Egress rules are only available for CCE Turbo clusters or other CCE clusters using a VPC network. The cluster version is v1.27.16-r10, v1.28.15-r0, v1.29.10-r0, v1.30.6-r0 or later, and DataPlane V2 must be enabled. Additionally, nodes in these clusters must run HCE OS 2.0.

  • Scenario 1: Controlled by a preset network policy, pods can only access specific addresses.
    Figure 3 IPBlock

    The pods labeled role=db only allow access to 172.16.0.0/16, excluding 172.16.0.40/32. To achieve this, take the following steps:

    1. Create the access-demo3.yaml file.
      vim access-demo3.yaml

      File content:

      apiVersion: networking.k8s.io/v1
      kind: NetworkPolicy
      metadata:
        name: access-demo3
        namespace: default
      spec:
        policyTypes:                  # This policy type must be specified for egress rules.
          - Egress
        podSelector:                  # The rule takes effect for pods with the role=db label.
          matchLabels:
            role: db
        egress:                       # This is an egress rule.
        - to:
          - ipBlock:
              cidr: 172.16.0.0/16    # Allows access to this CIDR block in the outbound direction.
              except:
              - 172.16.0.40/32        # Blocks access to this CIDR block. This CIDR block is in the allowed CIDR block.
    2. Run the following command to create the network policy defined in the access-demo3.yaml file:
      kubectl apply -f access-demo3.yaml

      Expected output:

      networkpolicy.networking.k8s.io/access-demo3 created
  • Scenario 2: Controlled by a preset network policy, a pod can only be accessed by pods with specific labels, while this pod itself can only access specific pods.
    Figure 4 Using both ingress and egress

    The pod labeled with role=db only permits access to its port 6379 from pods labeled with role=frontend, and this pod can only access the pods labeled with role=web. You can use the same rule to configure both ingress and egress in a network policy. To achieve this, take the following steps:

    1. Create the access-demo4.yaml file.
      vim access-demo4.yaml

      File content:

      apiVersion: networking.k8s.io/v1
      kind: NetworkPolicy
      metadata:
        name: access-demo4
        namespace: default
      spec:
        policyTypes:
        - Ingress
        - Egress
        podSelector:                  # The rule takes effect for pods with the role=db label.
          matchLabels:
            role: db
        ingress:                      # This is an ingress rule.
        - from:
          - podSelector:              # Only allows the access of the pods labeled with role=frontend.
              matchLabels:
                role: frontend
          ports:                      # Only TCP can be used to access port 6379.
          - protocol: TCP
            port: 6379
        egress:                       # This is an egress rule.
        - to:
          - podSelector:              # The rule takes effect for pods with the role=web label.
              matchLabels:
                role: web
    2. Run the following command to create the network policy defined the access-demo4.yaml file:
      kubectl apply -f access-demo4.yaml

      Expected output:

      networkpolicy.networking.k8s.io/access-demo4 created