5、配置网络策略–NetworkPolicy
1. 考题内容:
2. 答题思路:
1、根据题目分析要创建怎样的网络策略
2、按题目要求查看ns corp-net的label
3、编写yaml,其中注意 namespace、label、port
3. 官网地址:
https://kubernetes.io/zh-cn/docs/concepts/services-networking/network-policies/
4. 考题答案:
切换环境
# 1).查看ns corp-net的label
kubectl get ns corp-net --show-labels# 2).新建fubar.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:name: allow-port-from-namespacenamespace: fubar
spec:podSelector: {}policyTypes:- Ingressingress:- from:- namespaceSelector:matchLabels:kubernetes.io/metadata.name: corp-net- podSelector: {}ports:- protocol: TCPport: 8080# 3).创建fubar.yaml
kubectl apply -f fubar.yaml
5. 验证:
# 1).查看创建的networkpolicy
kubectl get networkpolicy -n fubar