Manage agent network access with TrafficPolicy
TrafficPolicy controls network access for agent applications in ACK clusters. It lets you implement multi-level, priority-based network policies that use matching conditions such as CIDR, service, and FQDN for fine-grained control of pod ingress and egress traffic.
How it works
TrafficPolicy and GlobalTrafficPolicy
ACK clusters provide two types of network policy resources:
|
Resource type |
Scope |
Use cases |
|
TrafficPolicy |
Namespace-scoped. Applies to pods with specific labels in a designated namespace. |
Configure fine-grained network rules for a specific application. |
|
GlobalTrafficPolicy |
Cluster-scoped. Applies to pods with specific labels across the entire cluster. |
Implement a cluster-wide security baseline, such as blocking access to the instance metadata service. |
Policy enforcement rules
TrafficPolicy rules are evaluated based on the following principles:
-
Sorted by priority: Priorities range from 1 to 1000, where lower values have higher precedence. Higher-priority policies are evaluated first.
-
Ordered evaluation: Rules within a single policy are evaluated sequentially from top to bottom.
-
First match wins: Evaluation stops after the first matching rule is found, and subsequent rules are not processed.
-
Default deny: If a policy selects a pod, it denies all traffic that is not explicitly allowed.
The order of rules directly affects matching results. You must place specific allow or deny rules before any fallback rule.
Supported matching conditions
|
Direction |
Match condition |
Description |
|
Ingress |
|
Matches based on the source IP range. |
|
|
Matches based on the source service. Important
Policies based on selectors for resources such as services or workloads require dynamic resolution of pod IPs, which are then propagated to the agent. This process introduces latency compared to static IP or CIDR rules, making it unsuitable for real-time enforcement. |
|
|
|
Matches based on the destination port and protocol (Layer 4). |
|
|
Egress |
|
Matches based on the destination IP range. |
|
|
Matches based on the destination service. Important
Policies based on selectors for resources such as services or workloads require dynamic resolution of pod IPs, which are then propagated to the agent. This process introduces latency compared to static IP or CIDR rules, making it unsuitable for real-time enforcement. |
|
|
|
Matches based on the destination domain name. |
|
|
|
Matches based on the destination port and protocol (Layer 4). |
Ingress rules do not support FQDN matching. FQDN rules have the following constraints:
-
Dynamic resolution mechanism: The controller resolves DNS records in real time to obtain IP addresses and synchronizes these addresses with the nodes. Because DNS records can change at any time, the IP addresses corresponding to an FQDN are dynamic. Wildcard domains are not supported.
-
Access control limitation: If multiple different domain names resolve to the same IP address, the system cannot use the domain name to distinguish traffic for allow and deny rules. Instead, rules are executed based on priority.
-
Best practice: Given the preceding limitations, use FQDN rules only for access control on internal services to ensure policy accuracy and stability.
Comparison with Kubernetes NetworkPolicy
Compared to the native Kubernetes NetworkPolicy, TrafficPolicy and GlobalTrafficPolicy offer the following advantages:
|
Feature |
Kubernetes NetworkPolicy |
TrafficPolicy and GlobalTrafficPolicy |
|
Priority control |
Not supported |
Supports priorities from 1 to 1000 |
|
Explicit deny |
Not supported |
Supported |
|
FQDN-based rules |
Not supported |
Supported |
|
Supported cluster scale |
Up to 5,000 nodes per cluster |
|
|
Fast scaling for agent workloads |
High management complexity |
Optimized for this scenario |
Prerequisites
-
The cluster version is 1.24 or later. Only IPv4 rules are supported. IPv6 rules are not supported.
-
This feature applies only to pods in ACS clusters or to ECI pods.
-
The Poseidon component is installed, its version is v0.7.0 or later, and the TrafficPolicy feature is enabled.
-
To use Layer 4 port filtering, the component version must be v0.7.3 or later.
Configuration examples
Quick start
This example shows how to configure a basic network policy for an agent application to allow access to the DNS service and the public network.
Step 1: Enable TrafficPolicy for the agent pod
Save the following example to agent-pod.yaml, and then run kubectl apply -f agent-pod.yaml to deploy the example Pod.
A Pod requires spec.tolerations to be scheduled to ACS virtual nodes.
-
network.alibabacloud.com/enable-network-policy-agent: "true": Enables the network policy agent. -
network.alibabacloud.com/network-policy-mode: "traffic-policy": Specifies thetraffic-policymode.
apiVersion: v1
kind: Pod
metadata:
name: agent-pod
annotations:
network.alibabacloud.com/enable-network-policy-agent: "true"
network.alibabacloud.com/network-policy-mode: "traffic-policy"
labels:
app: agent
alibabacloud.com/acs: "true"
spec:
tolerations:
- key: "virtual-kubelet.io/provider"
operator: "Equal"
value: "alibabacloud"
effect: "NoSchedule"
containers:
- name: agent
image: registry.aliyuncs.com/acs/ubuntu:latest
command: ["sleep", "infinity"]
Step 2: Create a TrafficPolicy
Create the following YAML file named agent-basic-policy.yaml to allow the agent to access the DNS service and the public network.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-basic-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
egress:
rules:
# Allow access to the DNS service
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
# Allow access to the public network
- action: allow
to:
- cidr: 0.0.0.0/0
Step 3: Apply and verify the policy
-
Apply the policy.
kubectl apply -f agent-basic-policy.yaml -
Check the policy status.
kubectl get trafficpolicy -n defaultExpected output:
NAME PRIORITY AGE agent-basic-policy 100 10s -
Verify network connectivity.
# Exec into the pod kubectl exec -it agent-pod -n default -- /bin/bash # Install network utilities apt-get update && apt-get install -y curl dnsutils # Test public network access (should succeed) curl -I https://www.aliyun.com # Test DNS resolution (should succeed) nslookup www.aliyun.com
Allow access to specific services and domains
This policy allows the agent to access the DNS service, a specific domain, and a specific IP range, while denying access to the instance metadata service and all other destinations.
Replace rds-xxx.aliyuncs.com with the actual domain name of your RDS instance.apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-egress-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
egress:
rules:
# Deny access to the instance metadata service
- action: deny
to:
- cidr: 100.100.100.200/32
# Allow access to a specific IP range
- action: allow
to:
- cidr: 100.96.0.0/10
# Allow access to a specific domain
- action: allow
to:
- fqdn: rds-xxx.aliyuncs.com
# Allow access to the DNS service
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
# Fallback rule: Deny access to all other destinations
- action: deny
to:
- cidr: 0.0.0.0/0
Deny pod access to private networks
This policy explicitly denies access to private network ranges while allowing access to the public network.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-internet-only-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
egress:
rules:
# Allow DNS
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
# Deny access to private networks
- action: deny
to:
- cidr: 10.0.0.0/8
- action: deny
to:
- cidr: 172.16.0.0/12
- action: deny
to:
- cidr: 192.168.0.0/16
# Allow access to the public network
- action: allow
to:
- cidr: 0.0.0.0/0
Allow access from a specific service
This policy restricts access to the agent pod, allowing incoming requests only from the Gateway service.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-ingress-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
ingress:
rules:
# Allow traffic from the Gateway service
- action: allow
from:
- service:
name: sandbox-gateway
namespace: sandbox-system
# Fallback rule: Deny traffic from all other sources
- action: deny
from:
- cidr: 0.0.0.0/0
Configure L4 port and protocol filtering
|
Field |
Type |
Description |
|
|
string |
Protocol type. Valid values are |
|
|
integer |
The port number. Use with |
|
|
integer |
The end of the port range, inclusive. It must be greater than or equal to |
Rule behavior
-
Multiple ports in a
portslist are combined with an OR operator. A match on any port satisfies the condition. -
Within a single rule, the
portsfield and the peer (fromorto) are combined with an AND operator. Both conditions must be met. -
If
portsis not declared, the rule matches based only on the peer and does not restrict the port, which is equivalent to an L3 rule.
Example: Allow traffic on a specific TCP port
The following policy allows the agent to access the destination server only on TCP port 80. Traffic on all other ports is blocked by the implicit deny rule.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-l4-tcp-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
egress:
rules:
- action: allow
to:
- cidr: 10.0.0.10/32
ports:
- protocol: TCP
port: 80
Allow UDP and TCP 53 for DNS
DNS queries primarily use UDP, but they may fall back to TCP for large responses. Therefore, you should allow traffic on both protocols.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-dns-l4-policy
namespace: default
spec:
priority: 50
selector:
matchLabels:
app: agent
egress:
rules:
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
Example: Allow a TCP port range
Use port and endPort to define a continuous range of ports, which is common for high-numbered business service ports.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-port-range-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
egress:
rules:
- action: allow
to:
- cidr: 10.0.0.0/8
ports:
- protocol: TCP
port: 8080
endPort: 8090
Allow ingress on a specific port
This policy allows other pods to access the current pod only on TCP port 80. Ingress traffic on all other ports is implicitly denied.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: agent-ingress-l4-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
ingress:
rules:
- action: allow
from:
- cidr: 0.0.0.0/0
ports:
- protocol: TCP
port: 80
Cluster-wide access control
The following global policy denies access from a specific network segment to pods in the cluster, while allowing all other traffic.
apiVersion: network.alibabacloud.com/v1alpha1
kind: GlobalTrafficPolicy
metadata:
name: global-security-baseline
spec:
priority: 1000 # Low priority, acts as a fallback policy
selector: {} # Applies to all pods in the cluster
ingress:
rules:
# Deny access from the Sandbox network segment
- action: deny
from:
- cidr: 10.8.0.0/16
# Allow all other sources
- action: allow
from:
- cidr: 0.0.0.0/0
egress:
rules:
- action: allow
to:
- cidr: 0.0.0.0/0
If theselectorin a GlobalTrafficPolicy is set to an empty object{}, the policy applies to all pods across the entire cluster.
Combine multi-level security policies
You can combine multiple TrafficPolicy resources to implement layered control using different priorities. The following table provides a recommended priority layering scheme and configuration examples.
|
Priority range |
Purpose |
Example scenario |
|
1-49 (High priority) |
Infrastructure services |
DNS, monitoring, log collection |
|
50-499 (Medium priority) |
Business services |
Databases, caches, message queues |
|
500-1000 (Low priority) |
Fallback policies |
Global security baselines, public network access control |
Infrastructure services (High priority)
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: infra-policy
namespace: default
spec:
priority: 10
selector:
matchLabels:
app: agent
egress:
rules:
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
- action: allow
to:
- service:
namespace: monitoring
name: prometheus
Business services (Medium priority)
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: business-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: agent
egress:
rules:
- action: allow
to:
- service:
namespace: default
name: redis
- action: allow
to:
- service:
namespace: default
name: mysql
Public network access
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: internet-policy
namespace: default
spec:
priority: 500
selector:
matchLabels:
app: agent
egress:
rules:
- action: allow
to:
- cidr: 0.0.0.0/0
Configure OpenClaw network policy
The following is a complete network isolation policy for the OpenClaw application, which includes both ingress and egress rules.
Policies based on selectors for resources such as services or workloads require dynamic resolution of pod IPs, which are then propagated to the agent. This process introduces latency compared to static IP or CIDR rules, making it unsuitable for real-time enforcement.
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: openclaw-policy
namespace: default
spec:
priority: 100
selector:
matchLabels:
app: openclaw
ingress:
rules:
# Allow traffic from the management component's vSwitch
- action: allow
from:
- cidr: 172.16.10.0/24
# Allow traffic from Gateway and Sandbox Manager
#- action: allow
# from:
# - service:
# name: sandbox-gateway
# namespace: sandbox-system
# - service:
# name: sandbox-manager
# namespace: sandbox-system
# Deny all other sources
- action: deny
from:
- cidr: 0.0.0.0/0
egress:
rules:
# Deny access to the instance metadata service
- action: deny
to:
- cidr: 100.100.100.200/32
# Allow access to the DNS service
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
# Allow access to the LLM proxy service
- action: allow
to:
- service:
namespace: llm-proxy
name: llm-proxy
# Deny access to private networks
- action: deny
to:
- cidr: 10.0.0.0/8
- action: deny
to:
- cidr: 172.16.0.0/12
- action: deny
to:
- cidr: 192.168.0.0/16
# Allow access to the public network
- action: allow
to:
- cidr: 0.0.0.0/0
Best practices
Follow the principle of least privilege
Grant pods only the network access they require. Explicitly list allowed services and addresses, and deny all other traffic by default.
egress:
rules:
# Explicitly allow required services
- action: allow
to:
- service:
namespace: kube-system
name: kube-dns
- action: allow
to:
- fqdn: api.aliyun.com
# Deny access to private networks
- action: deny
to:
- cidr: 10.0.0.0/8
# Allow public network access as needed
- action: allow
to:
- cidr: 0.0.0.0/0
Design precise label selectors
Use multi-dimensional labels to precisely target pods and avoid applying policies to unintended workloads.
# Recommended: Multi-dimensional labels
selector:
matchLabels:
app: agent
environment: production
team: ai-platform
# Avoid: Overly broad labels
selector:
matchLabels:
app: agent
Label design recommendations:
-
app: Identifies the application type. -
environment: Distinguishes between environments (for example, production, staging, and development). -
team: Identifies the owning team.
Combine with GlobalTrafficPolicy
Use GlobalTrafficPolicy to establish a cluster-wide security baseline, and use TrafficPolicy for fine-grained, application-level control:
|
Policy layer |
Type |
Priority |
Responsibility |
|
Application layer |
TrafficPolicy |
50-200 |
Fine-grained network rules for a specific application |
|
Global layer |
GlobalTrafficPolicy |
1000 |
Cluster security baseline (for example, deny traffic from a Sandbox network segment) |
Audit policies regularly
Establish a regular audit mechanism for your policies:
-
Check if the TrafficPolicy meets the latest security requirements.
-
Review denied connection logs to identify potential issues.
-
Remove policies that are no longer in use.
-
Adjust policies based on business changes.
-
Verify that priority settings are reasonable.
Example audit script:
#!/bin/bash
echo "=== All TrafficPolicies ==="
kubectl get trafficpolicy --all-namespaces -o wide
echo -e "\n=== All GlobalTrafficPolicies ==="
kubectl get globaltrafficpolicy -o wide
echo -e "\n=== Agent Pod List ==="
kubectl get pods -l app=agent --all-namespaces
Manage policies with version control
Manage your TrafficPolicy configurations using a Git repository and organize them into directories by purpose.
traffic-policies/
├── base/ # Base policies
│ ├── global-security-baseline.yaml
│ └── dns-access.yaml
├── applications/ # Application policies
│ ├── openclaw-policy.yaml
│ └── agent-policy.yaml
└── environments/ # Environment-specific overrides
├── production/
│ └── overrides.yaml
└── staging/
└── overrides.yaml
FAQ
Policy is not taking effect
Symptom: You created a TrafficPolicy, but it does not restrict network access as expected.
Troubleshooting steps:
-
Check the Poseidon component status: Confirm that the component is installed, its version is v0.7.0 or later, and the TrafficPolicy feature is enabled.
-
Check pod annotations: Ensure the pod has the required annotations configured.
-
Check pod labels: Verify that the pod labels exactly match the policy's
selector.matchLabels.kubectl get pod <pod-name> --show-labels -
Check rule order: Make sure specific rules are placed before any fallback rule.
kubectl get trafficpolicy <policy-name> -o yaml
Policy conflicts
Symptom: Multiple policies apply to the same pod, causing unexpected network behavior.
Troubleshooting steps:
-
List all policies and their priorities:
kubectl get trafficpolicy,globaltrafficpolicy -A -o wide -
Check if any policies with the same priority are targeting the same pod.
-
Adjust priority values to ensure higher-priority policies use lower numerical values.
-
Merge conflicting policies to avoid duplicate definitions.
Network connection timeouts
Symptom: A pod is unable to connect to a required address or service.
Troubleshooting steps:
-
Verify that the policy includes all necessary
allowrules, such as for the DNS service. -
Check if a fallback rule is too strict and is blocking essential network access.
-
Add the necessary
allowrule to your policy:egress: rules: - action: allow to: - service: namespace: <namespace> name: <service-name> -
Ensure the
allowrule has a higher priority than thedenyrule, or place theallowrule before thedenyrule within the same policy.
Pod cannot be accessed
Symptom: Other services cannot access the target pod.
Troubleshooting steps:
-
Check the ingress rules to ensure that traffic from the required source services is allowed.
-
Add an
allowrule for the source service:ingress: rules: - action: allow from: - service: name: <source-service> namespace: <source-namespace>
CRD reference
TrafficPolicy fields
apiVersion: network.alibabacloud.com/v1alpha1
kind: TrafficPolicy
metadata:
name: <policy-name> # Policy name, unique within the namespace
namespace: <namespace> # The namespace of the policy
spec:
priority: <1-1000> # Priority, a lower value indicates higher precedence
selector: # Pod selector
matchLabels:
<label-key>: <label-value>
ingress: # Ingress rules (optional)
rules:
- action: allow|deny
from:
- cidr: <cidr>
- service:
name: <name>
namespace: <namespace>
egress: # Egress rules (optional)
rules:
- action: allow|deny
to:
- cidr: <cidr>
- service:
name: <name>
namespace: <namespace>
- fqdn: <domain>
GlobalTrafficPolicy fields
apiVersion: network.alibabacloud.com/v1alpha1
kind: GlobalTrafficPolicy
metadata:
name: <policy-name> # Policy name, unique within the cluster
spec:
priority: <1-1000> # Priority, a lower value indicates higher precedence
selector: # Pod selector (empty {} means cluster-wide)
matchLabels: {}
ingress: # Ingress rules (optional)
rules:
- action: allow|deny
from:
- cidr: <cidr>
- service:
name: <name>
namespace: <namespace>
egress: # Egress rules (optional)
rules:
- action: allow|deny
to:
- cidr: <cidr>
- service:
name: <name>
namespace: <namespace>
- fqdn: <domain>
Match condition reference
Ingress rules
|
Field |
Type |
Description |
Example |
|
|
string |
Source IP range |
|
|
|
object |
Source Service (includes |
|
|
|
string |
Source namespace |
|
Egress rules
|
Field |
Type |
Description |
Example |
|
|
string |
Destination IP range |
|
|
|
object |
Target Service (including |
|
|
|
string |
Destination domain name |
|