NetworkPolicy案例

来自AI助手的总结
通过NetworkPolicy按命名空间和标签控制Pod访问权限。
NetworkPolicy案例

一、创建几个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

二、在没有创建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
...
...
...

三、创建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访问。

四、应用YAML

$ k apply -f deny-all-namespaces.yaml

五、再次测试

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

六、恢复

$ 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 端口

七、创建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

八、应用YAML

$ k apply -f pod-selector.yaml

九、创建测试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

十、查看web01的IP

$ k get po -n aming -owide | grep web

web01   1/1     Running   0          2m10s   172.25.244.216   k8s-master01   <none>           <none>

十一、测试,观察到只有app01可以正常访问

$ k exec -n aming app01 -- curl 172.25.244.216
$ k exec -n aming app02 -- curl 172.25.244.216

十二、恢复

$ 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 端口。

十三、创建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

十四、应用YAML

$ k apply -f allow-ns.yaml

十五、创建测试ns

$ k create ns test

十六、创建测试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

十七、查看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>

十八、给ns设置标签

$ k label ns test name=test

十九、查看ns label

$ k get ns --show-labels | grep test

test                   Active   118s   kubernetes.io/metadata.name=test,name=test

二十、测试,观察到只有命名空间 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

二十一、恢复

$ 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
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容