Implements Kubernetes NetworkPolicies for Pod-level network segmentation, enforcing least-privilege communication, zero-trust access, and restricting lateral movement in clusters with Calico or Cilium.
npx claudepluginhub killvxk/cybersecurity-skills-zhThis skill uses the workspace's default tool permissions.
Kubernetes NetworkPolicy(网络策略)是实现集群内 Pod 间通信安全的原生机制。通过定义精细的入站(Ingress)和出站(Egress)规则,可以强制执行最小权限原则并防止横向移动(Lateral Movement)。本技能涵盖默认拒绝(Default Deny)模式的实施、应用特定策略配置、跨命名空间隔离以及出站限制,使用 Calico、Cilium 或 Antrea 等 CNI 插件。
Implements Kubernetes NetworkPolicies for pod-level network segmentation with default deny-all, DNS egress, and app-specific ingress rules. Secures traffic between pods, namespaces, and endpoints.
Implements Kubernetes NetworkPolicies for pod traffic segmentation with YAML for default deny, DNS egress, and service-to-service rules using Calico/Cilium CNIs.
Implements Kubernetes network segmentation with Calico NetworkPolicy and GlobalNetworkPolicy for zero-trust Pod communication. Guides Calico installation and policy examples like default deny and namespace isolation.
Share bugs, ideas, or general feedback.
Kubernetes NetworkPolicy(网络策略)是实现集群内 Pod 间通信安全的原生机制。通过定义精细的入站(Ingress)和出站(Egress)规则,可以强制执行最小权限原则并防止横向移动(Lateral Movement)。本技能涵盖默认拒绝(Default Deny)模式的实施、应用特定策略配置、跨命名空间隔离以及出站限制,使用 Calico、Cilium 或 Antrea 等 CNI 插件。
| 类型 | 说明 | 用途 |
|---|---|---|
| Ingress(入站) | 控制到 Pod 的入站流量 | 限制哪些来源可以访问 Pod |
| Egress(出站) | 控制从 Pod 发出的流量 | 限制 Pod 可以访问哪些目标 |
| 默认拒绝 | 隐式拒绝所有未匹配流量 | 零信任(Zero Trust)基础 |
| 插件 | NetworkPolicy | 扩展策略 | eBPF |
|---|---|---|---|
| Calico | 是 | GlobalNetworkPolicy | 否 |
| Cilium | 是 | CiliumNetworkPolicy | 是 |
| Antrea | 是 | ClusterNetworkPolicy | 否 |
| Weave | 是 | 否 | 否 |
# 检查已安装的 CNI 插件
kubectl get pods -n kube-system | grep -E "calico|cilium|antrea|weave"
# 验证 NetworkPolicy 支持(测试策略)
kubectl apply -f - <<EOF
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-policy
namespace: default
spec:
podSelector: {}
policyTypes:
- Ingress
EOF
kubectl delete networkpolicy test-policy
# default-deny-all.yaml - 拒绝所有入站和出站流量
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: production
spec:
podSelector: {} # 匹配命名空间中的所有 Pod
policyTypes:
- Ingress
- Egress
# 为关键命名空间应用默认拒绝策略
for ns in production staging default; do
kubectl apply -f default-deny-all.yaml -n $ns
done
# 验证策略已应用
kubectl get networkpolicies --all-namespaces
# allow-dns-egress.yaml - 允许 Pod 进行 DNS 解析
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: production
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
三层应用(前端 → 后端 → 数据库)示例:
# frontend-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-policy
namespace: production
spec:
podSelector:
matchLabels:
app: frontend
policyTypes:
- Ingress
- Egress
ingress:
- from:
- ipBlock:
cidr: 0.0.0.0/0 # 允许外部流量访问前端
ports:
- protocol: TCP
port: 443
- protocol: TCP
port: 80
egress:
- to:
- podSelector:
matchLabels:
app: backend
ports:
- protocol: TCP
port: 8080
- ports: # DNS
- protocol: UDP
port: 53
---
# backend-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-policy
namespace: production
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
egress:
- to:
- podSelector:
matchLabels:
app: database
ports:
- protocol: TCP
port: 5432
- ports: # DNS
- protocol: UDP
port: 53
---
# database-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: database-policy
namespace: production
spec:
podSelector:
matchLabels:
app: database
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: backend
ports:
- protocol: TCP
port: 5432
egress:
- ports: # 仅 DNS
- protocol: UDP
port: 53
# allow-monitoring.yaml - 允许监控命名空间抓取指标
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-monitoring-scrape
namespace: production
spec:
podSelector: {} # 应用到所有 Pod
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
podSelector:
matchLabels:
app: prometheus
ports:
- protocol: TCP
port: 9090
- protocol: TCP
port: 8080
# 为监控命名空间添加 Label
kubectl label namespace monitoring kubernetes.io/metadata.name=monitoring
# restrict-egress.yaml - 限制后端服务的出站流量
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-egress-restrict
namespace: production
spec:
podSelector:
matchLabels:
app: backend-service
policyTypes:
- Egress
egress:
# 允许访问同命名空间内的数据库
- to:
- podSelector:
matchLabels:
tier: database
ports:
- protocol: TCP
port: 5432
# 允许访问外部 API(特定 CIDR)
- to:
- ipBlock:
cidr: 10.0.0.0/8
except:
- 10.0.10.0/24 # 排除敏感子网
ports:
- protocol: TCP
port: 443
# 允许 DNS
- ports:
- protocol: UDP
port: 53
# block-metadata.yaml - 阻断 AWS/GCP 实例元数据服务
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: block-cloud-metadata
namespace: production
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 0.0.0.0/0
except:
- 169.254.169.254/32 # AWS/GCP 元数据
- 100.100.100.200/32 # Alibaba Cloud 元数据
# 测试 Pod 间连通性
kubectl exec -it frontend-pod -n production -- curl http://backend-service:8080
# 验证被阻断的连接
kubectl exec -it frontend-pod -n production -- curl http://database-service:5432
# 应该超时或被拒绝
# 使用 netcat 测试端口连通性
kubectl exec -it test-pod -- nc -zv backend-service 8080
# 列出所有命名空间的策略
kubectl get networkpolicies --all-namespaces
# 查看特定策略详情
kubectl describe networkpolicy backend-policy -n production
# 以 YAML 格式导出策略
kubectl get networkpolicy -n production -o yaml