案例一:
需求:aming命名空间下所有Pod可以互相访问,也可以访问其他命名空间Pod,但其他命名空间不能访问aming命名空间Pod。
1、创建几个Pod
在default命名空间里创建busybox Pod
$ k run busybox --image=registry.cn-hangzhou.aliyuncs.com/abroad_images/busybox:latest -- sleep 3600
在aming命名空间里创建busybox Pod
$ k run busybox --image=registry.cn-hangzhou.aliyuncs.com/abroad_images/busybox:latest -n aming -- sleep 3600
在aming命名空间里创建web pod
$ k run web --image=nginx:1.21.6 -n aming
2、在没有创建NetworkPolicy的情况下测试
查看default命名空间的busybox IP
$ k get po -owide | grep busybox
busybox 1/1 Running 0 102s 172.17.125.14 k8s-node01 <none> <none>
查看aming命名空间的web IP
$ k get po -owide -n aming | grep web
web 1/1 Running 0 102s 172.18.195.17 k8s-master03 <none> <none>
aming命名空间的busybox ping default命名空间的busybox IP
$ k exec busybox -n aming -- ping 172.17.125.14
64 bytes from 172.17.125.14: seq=0 ttl=62 time=0.391 ms
64 bytes from 172.17.125.14: seq=1 ttl=62 time=0.253 ms
...
...
...
aming命名空间的busybox ping aming命名空间的web IP
$ k exec busybox -n aming -- ping 172.18.195.17
64 bytes from 172.18.195.17: seq=0 ttl=62 time=0.350 ms
64 bytes from 172.18.195.17: seq=1 ttl=62 time=0.231 ms
...
...
...
default命名空间的busybox ping aming命名空间的web IP
$ k exec busybox -- ping 172.18.195.17
64 bytes from 172.18.195.17: seq=0 ttl=62 time=0.286 ms
64 bytes from 172.18.195.17: seq=1 ttl=62 time=0.325 ms
...
...
...
3、创建networkpolicy的YAML
$ vi deny-all-namespaces.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-namespaces
namespace: aming
spec:
podSelector: {} # 为空,表示匹配本命名空间所有Pod
policyTypes:
- Ingress
ingress:
- from:
- podSelector: {} # 为空,表示匹配该命名空间所有Pod,即允许该命名空间所有Pod访问,没有定义namespaceSelector,也就是说不允许其它namespace的Pod访问。
4、应用YAML
$ k apply -f deny-all-namespaces.yaml
5、再次测试
aming命名空间的busybox ping default命名空间的busybox IP
$ k exec busybox -n aming -- ping 172.17.125.14
64 bytes from 172.17.125.14: seq=0 ttl=62 time=0.391 ms
64 bytes from 172.17.125.14: seq=1 ttl=62 time=0.253 ms
...
...
...
aming命名空间的busybox ping aming命名空间的web IP
$ k exec busybox -n aming -- ping 172.18.195.17
64 bytes from 172.18.195.17: seq=0 ttl=62 time=0.350 ms
64 bytes from 172.18.195.17: seq=1 ttl=62 time=0.231 ms
...
...
...
default命名空间的busybox ping aming命名空间的web IP,无法Ping通
$ k exec busybox -- ping 172.18.195.17
6、恢复
$ k delete po busybox --force
$ k delete po busybox -n aming --force
$ k delete po web -n aming
$ k delete -f deny-all-namespaces.yaml
案例二:
通过PodSelector限制:只允许标签为 app: dev 的 Pod 访问标签为 app: test 的 Pod 的 80 端口
1、创建networkpolicy的YAML
$ vi pod-selector.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: app-to-app
namespace: aming
spec:
podSelector:
matchLabels:
app: test
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: dev
ports:
- protocol: TCP
port: 80
2、应用YAML
$ k apply -f pod-selector.yaml
3、创建测试pod
$ k run web01 --image=nginx:1.21.6 -n aming -l 'app=test'
$ k run app01 --image=nginx:1.21.6 -n aming -l 'app=dev'
$ k run app02 --image=nginx:1.21.6 -n aming
查看label
$ k get pod -n aming --show-labels
NAME READY STATUS RESTARTS AGE LABELS
app01 1/1 Running 0 63s app=dev
app02 1/1 Running 0 46s run=app02
web01 1/1 Running 0 75s app=test
4、查看web01的IP
$ k get po -n aming -owide | grep web
web01 1/1 Running 0 2m10s 172.25.244.216 k8s-master01 <none> <none>
5、测试,观察到只有app01可以正常访问
$ k exec -n aming app01 -- curl 172.25.244.216
$ k exec -n aming app02 -- curl 172.25.244.216
6、恢复
$ k delete po app01 -n aming
$ k delete po app02 -n aming
$ k delete po web01 -n aming
$ k delete -f pod-selector.yaml
案例三:
限制namespace:只允许来自命名空间 test 中的所有 Pod 访问命名空间 aming 中的所有 Pod 的 80 端口。
1、创建networkpolicy的YAML
$ vi allow-ns.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-ns
namespace: aming
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: test
ports:
- protocol: TCP
port: 80
2、应用YAML
$ k apply -f allow-ns.yaml
3、创建测试ns
$ k create ns test
4、创建测试pod
$ k run web01 --image=nginx:1.21.6 -n aming
$ k run web02 --image=nginx:1.21.6 -n test
$ k run web03 --image=nginx:1.21.6
$ k run web04 --image=nginx:1.21.6 -n aming
5、查看web01和web04的IP
$ k get po -n aming -owide | grep web01
web01 1/1 Running 0 37s 172.25.244.217 k8s-master01 <none> <none>
$ k get po -n aming -owide | grep web04
web04 1/1 Running 0 4m20s 172.25.92.74 k8s-master02 <none> <none>
6、给ns设置标签
$ k label ns test name=test
7、查看ns label
$ k get ns --show-labels | grep test
test Active 118s kubernetes.io/metadata.name=test,name=test
8、测试,观察到只有命名空间 test 中的所有 Pod 访问命名空间 aming 中的所有 Pod 的 80 端口。
$ k -n test exec web02 -- curl 172.25.244.217
$ k -n test exec web02 -- curl 172.25.92.74
$ k exec web03 -- curl 172.25.244.217
$ k -n aming exec web04 -- curl 172.25.244.217
9、恢复
$ k delete po web01 -n aming
$ k delete po web02 -n test
$ k delete po web03
$ k delete po web04 -n aming
$ k delete ns test
$ k delete -f allow-ns.yaml