You need to enable JavaScript to run this app.
Volcengine Kubernetes Engine

Volcengine Kubernetes Engine

Copy page
Download PDF
Container Network
Using a NetworkPolicy for network access control
Copy page
Download PDF
Using a NetworkPolicy for network access control
A Kubernetes NetworkPolicy implements policy-based network access control. You can use a NetworkPolicy to control pod traffic by IP address or port. This topic describes the configuration methods and use cases of NetworkPolicies.​
tip​
Usage limits​
  • VKE NetworkPolicies are supported only in clusters that use the VPC-CNI network model.​
  • The vpc-cni add-on must be upgraded to version v1.6.4 or later.​
  • When using a NetworkPolicy to control traffic, consider cluster scale limitations. For details, see Limit.​
  • A NetworkPolicy does not support the following scenarios.​
  • should allow egress access to server in CIDR block​
  • should deny ingress access to updated pod​
  • should allow ingress access from updated pod​
  • should enforce except clause while egress access to server in CIDR block​
  • should enforce ingress policy allowing any port traffic to a server on a specific protocol [Feature:UDP]​
  • should ensure an IP overlapping both IPBlock.CIDR and IPBlock.Except is allowed​
  • should properly isolate pods that are selected by a policy allowing SCTP, even if the plugin doesn't support SCTP​
Procedure​
Step 1: Enable NetworkPolicy​
  1. Log in to the VKE console.​
  1. In the left navigation bar, click Cluster. On the page that appears, click the name of the target cluster to go to the management page of the cluster.​
  1. In the left navigation bar, click Addons.​
  1. On the Network tab, find the vpc-cni add-on, move the mouse over the add-on card, click Configuration, and enable the NetworkPolicy feature.​
Note​
  • It takes about 1 minute for NetworkPolicy to take effect after being enabled (or disabled).​
  • Modifying this configuration causes the add-on to restart. Perform this operation during off-peak hours.​
  1. Click Confirm.​
Step 2: Configure a NetworkPolicy​
After enabling the NetworkPolicy feature, you can specify a network policy for specific pods by configuring a NetworkPolicy. In the NetworkPolicy, you define traffic direction by using ingress and egress, and you filter ingress and egress targets (namespaces and pods) by using ipBlock, namespaceSelector, and podSelector.​
The following content provides an example configuration and a description of key fields.​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: network-policy-demo # NetworkPolicy name.​
namespace: default # NetworkPolicy namespace.​
spec:​
podSelector: # Pod selector. Use labels to select pods that require network control. If no specific label is specified, all pods in the cluster are selected.​
matchLabels:​
app: nginx​
policyTypes: # Traffic direction in which the network policy takes effect. Specify Ingress for inbound control or Egress for outbound control.​
- Ingress​
- Egress​
ingress: # Inbound policy configuration.​
- from: # Traffic source in the inbound policy. Only specified source objects can access the specified pods.​
- ipBlock: # Source CIDR block for inbound traffic. This field supports only IP addresses external to the cluster and does not support internal cluster addresses (such as node IP addresses).​
cidr: 192.168.1.0/24​
except: # Exclude IP addresses or sub-CIDR blocks from the source CIDR block for inbound traffic control. These IP addresses within the CIDR block are not allowed to access Pods.​
- 192.168.1.100/32​
- namespaceSelector: # Namespace selector. It allows pods in namespaces with specified labels to access specified pods.​
matchLabels:​
namespace: doc-demo​
- podSelector: # Pod selector. It allows pods with specified labels to access specified pods.​
matchLabels:​
app: nginx​
ports: # The allowed ports.​
- protocol: TCP # Allowed port protocol.​
port: 6379 # The allowed port number.​
egress: # Egress policy configuration.​
- to: # Traffic destination in the egress policy. Pods can only access specified destination objects.​
- ipBlock: # Target CIDR block for outbound traffic. This field supports only IP addresses external to the cluster and does not support internal cluster addresses (such as node IP addresses).​
cidr: 10.0.0.0/24​
except: # Exclude IP addresses or sub-CIDR blocks from the destination CIDR for outbound traffic to deny Pod access​
- 10.0.1.100/32​
- namespaceSelector: # Namespace selector. It allows only specified pods to access other pods in namespaces with specified labels.​
matchLabels:​
namespace: doc-demo​
- podSelector: # Pod selector. It allows only specified pods to access other pods with specified labels.​
matchLabels:​
app: nginx​
ports: # The allowed ports.​
- protocol: TCP # Allowed port protocol.​
port: 5978 # The allowed port number.​
​
Key fields are described in the table below:​
​
Parameter​
Description​
policyTypes​
Specify the traffic control direction in the NetworkPolicy policy. Options include:​
  • Ingress: Ingress traffic policy for controlling external traffic entering specified pods.​
  • Egress: Egress traffic policy for controlling access to external destinations by specified pods.​
ipBlock​
Static CIDR block. This field is used to specify IP addresses external to the cluster. Traffic between pods and specified external IP addresses will be controlled by the NetworkPolicy policy.​
tip​
This field only supports IP addresses external to the cluster and does not support internal cluster addresses, such as IP addresses of nodes.​
namespaceSelector​
Namespace selector. It selects, by label, the namespaces that require network traffic control. All pods in the matched namespaces are governed by the NetworkPolicy.​
podSelector​
Pod selector. It selects pods that need network traffic control by label. Pods that meet the filter conditions are governed by the NetworkPolicy.​
​
Validating results​
Confirming configurations​
  1. In the left navigation bar, click Workload > Resource Explorer.​
  1. From the Category drop-down list, select API Group and use the name networking.k8s.io to search for the resource object.​
  1. Click the resource name to enter the resource details page, where you can see the NetworkPolicy resource category.​
  1. Click the name of the NetworkPolicy resource to enter the resource details page, where you can view, modify, or delete the NetworkPolicy configuration.​
Checking traffic​
After the configuration is complete, you can create multiple workloads in the cluster and verify whether the configured traffic rules in the NetworkPolicy are applied effectively to pods. For example, if you allow only mutual access within the same namespace, deploy multiple pods in different namespaces. Log in to a pod and run the ping command to verify that it can access only other pods in the same namespace and cannot access pods in other namespaces. The example is as follows:​
​
root@deployment-demo-a-7796***:/# ping 192.168.2.159​
PING 192.168.2.159 (192.168.2.159) 56(84) bytes of data.​
64 bytes from 192.168.2.159: icmp_seq=1 ttl=64 time=0.021 ms​
64 bytes from 192.168.2.159: icmp_seq=2 ttl=64 time=0.030 ms​
64 bytes from 192.168.2.159: icmp_seq=3 ttl=64 time=0.027 ms​
64 bytes from 192.168.2.159: icmp_seq=4 ttl=64 time=0.021 ms​
--- 192.168.2.159 ping statistics ---​
4 packets transmitted, 4 received, 0% packet loss, time 3057ms​
rtt min/avg/max/mdev = 0.021/0.024/0.030/0.004 ms​
root@deployment-demo-a-7796***:/# ping 192.168.2.157​
PING 192.168.2.157 (192.168.2.157) 56(84) bytes of data.​
​
​
Configuration examples​
Allowing mutual access within the same namespace​
The following YAML configuration example defines a NetworkPolicy that only allows pods within the same namespace to access each other and prevents pod access across namespaces.​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: demo-1 # NetworkPolicy resource name.​
namespace: default # Namespace to which the NetworkPolicy belongs.​
spec:​
podSelector: {}​
policyTypes:​
- Ingress​
ingress:​
- from:​
- podSelector: {}​
​
Denying all inbound traffic​
The following YAML configuration example defines a NetworkPolicy that denies any inbound traffic (pods cannot be accessed by any object).​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: demo-2 # NetworkPolicy resource name.​
namespace: default # Namespace to which the NetworkPolicy belongs.​
spec:​
podSelector: {}​
policyTypes:​
- Ingress​
ingress: []​
​
Allowing access only from a specified namespace​
The following YAML configuration example defines a NetworkPolicy that only allows pods in the namespace with the name: demo-workspace label to access other pods in the cluster.​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: demo-3 # NetworkPolicy resource name.​
namespace: default # Namespace to which the NetworkPolicy belongs.​
spec:​
podSelector: {}​
policyTypes:​
- Ingress​
ingress:​
- from:​
- namespaceSelector: # Namespace selector.​
matchLabels: # Namespace labels for matching.​
name: demo-workspace​
​
Allowing access only to a specified namespace​
The following YAML configuration example defines a NetworkPolicy that only allows pods in the cluster to access pods in the namespace with the name: demo-workspace label.​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: demo-4 # NetworkPolicy resource name.​
namespace: default # Namespace to which the pod belongs.​
spec:​
podSelector: {}​
policyTypes:​
- Egress​
egress:​
- to:​
- namespaceSelector: # Namespace labels for matching.​
matchLabels:​
name: demo-workspace​
​
Allowing access only to a specified CIDR block​
The following YAML configuration example defines a NetworkPolicy that only allows pods in the cluster to access external objects within the CIDR block 172.16.0.0/16, over TCP, and on port 8080. It prevents access to other network objects or ports.​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: demo-5 # NetworkPolicy resource name.​
namespace: default # Namespace to which the pod belongs.​
spec:​
podSelector: {}​
policyTypes:​
- Egress​
egress:​
- to:​
- ipBlock:​
cidr: 172.16.0.0/16 # Pod CIDR​
ports: # Specified ports.​
- protocol: TCP # Port protocol.​
port: 8080 # Port number.​
​
Allowing access only from a specified CIDR block​
The following YAML configuration example defines a NetworkPolicy that only allows pods in the cluster to be accessed by external objects within the CIDR block 172.16.0.0/16 via TCP and port 80.​
​
apiVersion: networking.k8s.io/v1​
kind: NetworkPolicy​
metadata:​
name: demo-6 # NetworkPolicy resource name.​
namespace: default # Namespace to which the pod belongs.​
spec:​
podSelector: {}​
policyTypes:​
- Ingress​
ingress:​
- from:​
- ipBlock:​
cidr: 172.16.0.0/16 # Pod CIDR​
ports: # Specified ports.​
- protocol: TCP # Port protocol.​
port: 80 # Port number.​
​
​
Last updated: 2026.09.03 15:56:09