任务

创建一个名为 pod-restriction 的 NetworkPolicy 来限制对在 namespace dev-team 中运行的 Pod products-service 的访问。 只允许以下 Pod 连接到 Pod products-service

  • namespace qaqa 中的 Pod
  • 位于任何 namespace,带有标签 environment: testing 的 Pod 注意:确保应用 NetworkPolicy。
    你可以在 /cks/net/po.yaml 找到一个模板清单文件。
     

做题

  1. 检查相关namespace的标签, 没有就打一个
# 查标签
kubectl get ns products-service --show-labels
kubectl get ns qaqa --show-labels

# 打标签
kubectl label ns xxx name=xxx
  1. 编辑网络策略的yaml文件
    vim cks-09.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: pod-restriction
  namespace: dev-team
spec:
  podSelector:
    matchLabels:
      name: products-service #目标pod的标签
  policyTypes:
  - Ingress
  ingress:
  - from: #第一个from,选中namespace为qaqa的所有pod
    - namespaceSelector:
        matchLabels:
          name: qaqa
  - from: #第二个from,选中所有namespace中标签符合enviroment=testing的pod,注意podSelector前面没有横线,表示与namespaceSelector为并列关系。
    - namespaceSelector: {}
      podSelector:
        matchLabels:
          environment: testing
  1. 生效
    kubectl apply -f cks-09.yaml

  2. 检查
    kubectl describe networkpolicy pod-restriction -n dev-team
     

参考

https://kubernetes.io/zh/docs/concepts/services-networking/network-policies/#networkpolicy-resource

posted on   eryoung2  阅读(95)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示