1.题目概述

  • 在命名空间fubar中创建网络策略allow-port-from-namespace
  • 只允许ns my-app中的pod连上fubar中pod的80端口
  • 注意:这里有两个ns,一个为fubar(目标pod的ns),另一个为my-app(访问源的pod)

    2.解析

  • 拷贝 services-networking/network-policies 中的案例,删掉不必要的部分

  • 设置网络策略所属的 ns 为 fubar,端口为 80
  • 设置 namespaceSelector 为源ns my-app 的labels

    3.参考答案

    1. apiVersion: networking.k8s.io/v1
    2. kind: NetworkPolicy
    3. metadata:
    4. name: allow-port-from-namespace
    5. namespace: fubar
    6. spec:
    7. podSelector:
    8. matchLabels: {}
    9. policyTypes:
    10. - Ingress
    11. ingress:
    12. - from:
    13. - namespaceSelector:
    14. matchLabels:
    15. app: my-app
    16. ports:
    17. - protocol: TCP
    18. port: 80