CKS真題 -- 網絡策略NetworkPolicy

任務

創建一個名爲 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章