资源文件

apiVersion: v1
kind: Pod
metadata:
  name: busybox-pod1
  labels:
    app: busybox-pod1
spec:
  containers:
  - name: busybox
    image: busybox:1.28
    command: ["sleep", "360000"]
    tty: true
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox-pod2
  labels:
    app: busybox-pod2
spec:
  containers:
  - name: busybox
    image: busybox:1.28
    command: ["sleep", "360000"]
    tty: true
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-tcp-block-icmp
  namespace: default  # 根据您的实际 namespace 修改
spec:
  podSelector:
    matchLabels:
      app: busybox-pod1  # 选择 busybox-pod1,您可以根据需要调整
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: busybox-pod2  # 允许 busybox-pod2 发送的流量
    ports:
    - protocol: TCP
      port: 8080  # 指定要允许的 TCP 端口号,您可以根据需要修改
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: busybox-pod2  # 允许 busybox-pod1 访问 busybox-pod2
    ports:
    - protocol: TCP
      port: 8080  # 同样指定要允许的 TCP 端口号

验证