实战:k8s中网络策略实验(成功测试-博客输出)-20211005

image-20211005151133885

目录

写在前面

本文,我将带你实战演示k8s中网络策略实验。

我的博客主旨:我希望每一个人拿着我的博客都可以做出实验现象,先把实验做出来,然后再结合理论知识更深层次去理解技术点,这样学习起来才有乐趣和动力。并且,我的博客内容步骤是很完整的,也分享源码和实验用到的软件,希望能和大家一起共同进步!

各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人免费帮您解决问题:

  1. 个人微信二维码:x2675263825 (舍得), qq:2675263825。

    image-20211002091450217

  2. 个人博客地址:www.onlyonexl.cn

    image-20211002092057988

  3. 个人微信公众号:云原生架构师实战

    image-20211002141739664

  4. 个人csdn

    https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

    image-20211002092344616

基础知识

image-20211005151705245

image-20211005151717610

实验环境

实验环境:
1、win10,vmwrokstation虚机;
2、k8s集群:3台centos7.6 1810虚机,1个master节点,2个node节点
   k8s version:v1.21
   CONTAINER-RUNTIME:docker://20.10.7

image-20211002143602812

原课件内容

image-20210705074603979

网络策略:案例

案例1:拒绝其他命名空间Pod访问
案例2:同一个命名空间下应用之间限制访问
案例3:只允许指定命名空间中的应用访问

附:准备环境快捷命令
kubectl run busybox --image=busybox -n test -- sleep 12h
kubectl run web --image=nginx -n test
kubectl exec busybox -n test -- ping 10.244.169.135

1、案例1:拒绝其他命名空间Pod访问

image-20210705074814722

案例1:拒绝其他命名空间Pod访问

需求:test命名空间下所有pod可以互相访问,也可以访问其他命
名空间Pod,但其他命名空间不能访问test命名空间Pod。

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-namespaces 
  namespace: test
spec:
  podSelector: {} # 未配置,匹配本命名空间所有pod
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector: {} # 未配置,匹配本命名空间所有pod
    
#说明
一般情况,我们配置的ingress规则会多些,而出规则egress很少去配置的;

准备测试环境:

先创建下test命名空间:

[root@k8s-master ~]#kubectl create ns test
namespace/test created

再创建2个pod:

[root@k8s-master np]#kubectl run web --image=nginx -n test
pod/web created
[root@k8s-master ~]#kubectl run busybox --image=busybox -n test -- sleep 24h
pod/busybox created

#查看
[root@k8s-master ~]#kubectl get pod -n test
NAME                  READY   STATUS    RESTARTS   AGE
busybox               1/1     Running   0          9s
web-96d5df5c8-7r6w6   1/1     Running   0          3m11s
[root@k8s-master ~]#kubectl get pod -n test -o wide
NAME      READY   STATUS    RESTARTS   AGE     IP               NODE        NOMINATED NODE   READINESS GATES
busybox   1/1     Running   0          21h     10.244.169.148   k8s-node2   <none>           <none>
web       1/1     Running   0          7h26m   10.244.169.151   k8s-node2   <none>           <none>

#默认情况下,`Kubernetes 集群网络没任何网络限制,Pod 可以与任何其他 Pod 通信`
[root@k8s-master ~]#kubectl exec busybox -n test -- ping 10.244.169.151
PING 10.244.169.151 (10.244.169.151): 56 data bytes
64 bytes from 10.244.169.151: seq=0 ttl=63 time=0.283 ms
64 bytes from 10.244.169.151: seq=1 ttl=63 time=0.126 ms
^C
[root@k8s-master ~]#

我们再次在默认命名空间下创建一个pod,并测试不同命名空间下的pod是否可以进行通信:=>是可以通信的

[root@k8s-master ~]#kubectl run busybox --image=busybox -- sleep 24h

[root@k8s-master ~]#kubectl get pod -o wide
[root@k8s-master ~]#kubectl get pod -o wide -n test
[root@k8s-master ~]#kubectl exec busybox -- ping 10.244.169.157

image-20210706052628912

现在进行按题目需求进行限制:

拒绝其他命名空间Pod访问
需求:test命名空间下所有pod可以互相访问,也可以访问其他命名空间Pod,但其他命名空间不能访问test命名空间Pod。
创建np目录:

[root@k8s-master ~]#mkdir np
[root@k8s-master ~]#cd np/
[root@k8s-master np]#vim deny-all-namespaces.yaml #配置yaml,这就是所谓的白名单
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-namespaces 
  namespace: test
spec:
  podSelector: {} # 未配置,匹配本命名空间所有pod
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector: {} # 未配置,匹配本命名空间所有pod

apply并测试效果:=>完全符合预期效果。

 #apply下
 [root@k8s-master np]#kubectl apply -f deny-all-namespaces.yaml
networkpolicy.networking.k8s.io/deny-all-namespaces created

#验证效果
[root@k8s-master np]#kubectl exec busybox -- ping 10.244.169.157 #做了网络策略限制后,从默认命名空间下不能ping通test命名空间下的web pod了。
^C

#但在test下不同pod是依然可以访问的
[root@k8s-master np]#kubectl exec busybox -n test -- ping 10.244.169.157
PING 10.244.169.157 (10.244.169.157): 56 data bytes
64 bytes from 10.244.169.157: seq=0 ttl=63 time=0.245 ms
64 bytes from 10.244.169.157: seq=1 ttl=63 time=0.307 ms
^C

#test命名空间下也是可以直接访问外网的;
[root@k8s-master np]#kubectl exec busybox -n test -- ping www.baidu.com
PING www.baidu.com (180.101.49.11): 56 data bytes
64 bytes from 180.101.49.11: seq=0 ttl=127 time=12.591 ms
64 bytes from 180.101.49.11: seq=1 ttl=127 time=9.736 ms
^C
[root@k8s-master np]#

#至此,案例1测试成功!

2、案例2:同一个命名空间下应用之间限制访问

image-20210706055231167

案例2:同一个命名空间下应用之间限制访问

需求:将test命名空间携带run=web标签的Pod隔离,只允许test
命名空间携带run=client1标签的Pod访问80端口。

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: app-to-app
  namespace: test
spec:
  podSelector:
    matchLabels:
      run: web
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          run: client1
    ports:
    - protocol: TCP
      port: 80

我们这边做测试时,先把上面的网络策略规则给删除掉:

[root@k8s-master ~]#cd np
[root@k8s-master np]#kubectl delete -f deny-all-namespaces.yaml
networkpolicy.networking.k8s.io "deny-all-namespaces" deleted
[root@k8s-master np]#

创建2个test命名空间下的不同label的测试pod:

[root@k8s-master np]#kubectl get pod -n test --show-labels
NAME      READY   STATUS    RESTARTS   AGE   LABELS
busybox   1/1     Running   1          22h   run=busybox
web       1/1     Running   1          8h    run=web

[root@k8s-master np]#kubectl run  client1 -l run=client1 --image=busybox -n test -- sleep 12h
pod/client1 created
[root@k8s-master np]#kubectl run  client2 -l run=client2 --image=busybox -n test -- sleep 12h
pod/client2 created

[root@k8s-master np]#kubectl get pod -n test --show-labels
NAME      READY   STATUS    RESTARTS   AGE   LABELS
busybox   1/1     Running   1          22h   run=busybox
client1   1/1     Running   0          33s   run=client1
client2   1/1     Running   0          20s   run=client2
web       1/1     Running   1          8h    run=web
[root@k8s-master np]#

配置前测试:默认同一个命名空间下的pod都是可以直接访问的:

image-20210706070201130

现在开始配置网络策略:

[root@k8s-master np]#vim app-to-app.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: app-to-app
  namespace: test
spec:
  podSelector:
    matchLabels:
      run: web
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          run: client1
    ports:
    - protocol: TCP
      port: 80
[root@k8s-master np]#kubectl apply -f app-to-app.yaml
networkpolicy.networking.k8s.io/app-to-app created

image-20210706072228019

测试失效效果:=>符合预期。

image-20210706071846377

至此,案例2实验完成。

3、案例3:只允许指定命名空间中的应用访问

image-20210706072539597

案例3:只允许指定命名空间中的应用访问

需求:只允许dev命名空间中的Pod访问test命名空间中的pod 80端口
命名空间打标签: kubectl label namespace dev name=dev

删除原来网络策略:

[root@k8s-master np]#kubectl delete -f app-to-app.yaml
networkpolicy.networking.k8s.io "app-to-app" deleted

创建dev命名空间:

[root@k8s-master np]#kubectl create ns dev
namespace/dev created

在dev命名空间里创建测试pod:

[root@k8s-master np]#kubectl run busybox --image=busybox -n dev -- sleep 12h
pod/busybox created
[root@k8s-master np]#

给命名空间打label:

[root@k8s-master np]#kubectl label namespaces dev name=dev
namespace/dev labeled
[root@k8s-master np]#kubectl get ns --show-labels
NAME                   STATUS   AGE     LABELS
default                Active   34d     <none>
dev                    Active   3m25s   name=dev
kube-node-lease        Active   34d     <none>
kube-public            Active   34d     <none>
kube-system            Active   34d     <none>
kubernetes-dashboard   Active   33d     <none>
test                   Active   23h     <none>
[root@k8s-master np]#

配置网络策略:

[root@k8s-master np]#vim allow-port-from-namespace.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-port-from-namespace
  namespace: test
spec:
  podSelector: {} 
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector: # 匹配命名空间标签
        matchLabels:
          name: dev
    ports:
    - protocol: TCP
      port: 80
      
[root@k8s-master np]#kubectl apply -f allow-port-from-namespace.yaml
networkpolicy.networking.k8s.io/allow-port-from-namespace created
[root@k8s-master np]#

测试效果:=>符合预期效果。

image-20210706075022934

总结

​ 好了,关于k8s中网络策略实验实验就到这里了,感谢大家阅读,最后贴上我的美圆photo一张,祝大家生活快乐,每天都过的有意义哦,我们下期见!

image-20211005150337391

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值