k8s里pod之间是如何进行网络隔离的

NetworkPolicy用来控制Pod与Pod之间的网络通信,它也支持针对Namespace进行限制。基于白名单模式,符合规则的对象通过,不符合的拒绝。应用场景举例:
- Pod A不能访问Pod B;
- 开发环境所有Pod不能访问测试命名空间;
- 提供对外访问时,限制外部IP;
说明:必需字段:apiVersion、 kind 和 metadata 字段。apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata:name: test-network-policynamespace: defaultspec:podSelector:matchLabels:role: dbpolicyTypes:ingress:cidr: 172.17.0.0/16except:matchLabels:project: myprojectmatchLabels:role: frontendports:port: 6379egress:cidr: 10.0.0.0/24ports:port: 5978
- podSelector:定义目标Pod的匹配标签,即哪些Pod会生效此策略;
- policyTypes:表示给定的策略是应用于目标Pod的入站流量(Ingress)还是出站流量(Egress),或两者兼有。如果NetworkPolicy未指定policyTypes则默认情况下始终设置Ingress。
- ingress:定义入流量限制规则,from用来定义白名单对象,比如网段、命名空间、Pod标签,Ports定义目标端口。
- egress:定义出流量限制规则,定义可以访问哪些IP和端口
在没有创建NetworkPolicy的情况下测试kubectl run busybox --image=busybox -- sleep 3600 ## default命名空间里创建busybox Podkubectl run busybox --image=busybox -n aming -- sleep 3600 ## aming命名空间里创建busybox Podkubectl run web --image=nginx:1.23.2 -n aming ## aming命名空间里创建web pod
创建networkpolicy的YAMLkubectl exec busybox -n aming -- ping 10.18.235.161 ##aming命名空间的busybox ping default命名空间的busybox IPkubectl exec busybox -n aming -- ping 10.18.235.162 ##aming命名空间的busybox ping aming命名空间的web IPkubectl exec busybox -- ping 10.18.235.162 ##default命名空间的busybox ping aming命名空间的web IP
应用YAMLvi deny-all-namespaces.yaml ##内容如下apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata:name: deny-all-namespacesnamespace: amingspec:podSelector: {} # 为空,表示匹配本命名空间所有PodpolicyTypes:ingress:
kubectl apply -f deny-all-namespaces.yaml
测试:
将刚刚创建的所有资源删除:kubectl exec busybox -n aming -- ping 10.18.235.161 ##aming命名空间的busybox ping default命名空间的busybox IPkubectl exec busybox -n aming -- ping 10.18.235.162 ##aming命名空间的busybox ping aming命名空间的web IPkubectl exec busybox -- ping 10.18.235.162 ##default命名空间的busybox ping aming命名空间的web IP
案例二:通过PodSelector限制kubectl delete po busybox --forcekubectl delete po busybox -n aming --forcekubectl delete po web -n amingkubectl delete -f deny-all-namespaces.yaml
应用YAMLvi pod-selector.yaml ##内容如下apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata:name: app-to-appnamespace: amingspec:podSelector:matchLabels:app: testpolicyTypes:ingress:matchLabels:app: devports:port: 80
kubectl apply -f pod-selector.yaml
创建测试pod
查看web01的IPkubectl run web01 --image=nginx:1.23.2 -n aming -l 'app=test'kubectl get pod web01 -n aming --show-labelskubectl run app01 --image=nginx:1.23.2 -n aming -l 'app=dev'kubectl run app02 --image=nginx:1.23.2 -n aming
kubectl describe po web01 -n aming |grep -i ip
测试
测试成功后,删除掉刚刚创建的资源kubectl exec -n aming app01 -- curl 10.18.235.170kubectl exec -n aming app02 -- curl 10.18.235.170
案例三:限制namespacekubectl delete po app01 -n amingkubectl delete po app02 -n amingkubectl delete po web01 -n amingkubectl delete -f pod-selector.yaml
应用YAMLvi allow-ns.yaml #内容如下apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata:name: allow-nsnamespace: amingspec:podSelector: {}policyTypes:ingress:matchLabels:name: testports:port: 80
kubectl apply -f allow-ns.yaml
创建测试ns
kubectl create ns test
创建测试pod
查看web01的IPkubectl run web01 --image=nginx:1.23.2 -n amingkubectl run web02 --image=nginx:1.23.2 -n testkubectl run web03 --image=nginx:1.23.2kubectl run web04 --image=nginx:1.23.2 -n aming
kubectl describe po web01 -n aming |grep -i ip
查看ns label
kubectl get ns --show-labels
给ns设置标签
kubectl label namespace test name=test
测试:
kubectl -n test exec web02 -- curl 10.18.235.172 #可以访问kubectl exec web03 -- curl 10.18.235.172 #不可以访问kubectl -n aming exec web04 -- curl 10.18.235.172 #不可以访问,即使同一个命名空间也无法访问
以上为NetworkPolicy的主要内容,你看明白了吗?

浙公网安备 33010602011771号