以下网络策略应有助于仅允许从特定部署 pod(depA)到 depB pod 的流量,因为您的问题具体是将流量从部署路由到部署,或者将允许流量返回 Internet,因为我们有允许流量输出的出口规则UDP和TCP
apiVersion: networking.k8s.io/v1
metadata:
name: foo-allow-to-hello
spec:
policyTypes:
- Ingress
- Egress
podSelector:
matchLabels:
app: depbB
ingress:
- from:
- podSelector:
matchLabels:
app: depA
egress:
- to:
- podSelector:
matchLabels:
app: depA
- ports
- protocol: UDP
port: 53
- protocol: TCP
port: 53
请注意,为执行上述政策,您的部署中的 pod 应进行相应标记。以下部署适用于上述策略,请注意 pod 上的标签与策略中提到的相同。
controllers/nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: deploymentA
spec:
replicas: 3
selector:
matchLabels:
app: depA
template:
metadata:
labels:
app: depA
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80