本帖子主要是实验iptables 的基本功能

 

示例1:

两台虚拟 ,通过iptables 控制,让k8s-01-102无法pingk8s-01-10。  服务器IP如下。

k8s-01-102:   192.168.2.102

k8s-01-103:   192.168.2.103

1、节点k8s-01-103,初始状态iptables 状态:

[root@k8s-node-2 ~]# iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination               

2、节点k8s-01-102,初始ping 状态,可以ping通:

[root@k8s-node-1 ~]# ping 192.168.2.103
PING 192.168.2.103 (192.168.2.103) 56(84) bytes of data.
64 bytes from 192.168.2.103: icmp_seq=16 ttl=64 time=0.357 ms
64 bytes from 192.168.2.103: icmp_seq=17 ttl=64 time=0.307 ms
64 bytes from 192.168.2.103: icmp_seq=18 ttl=64 time=0.284 ms
64 bytes from 192.168.2.103: icmp_seq=19 ttl=64 time=0.280 ms
64 bytes from 192.168.2.103: icmp_seq=20 ttl=64 time=0.313 ms
64 bytes from 192.168.2.103: icmp_seq=21 ttl=64 time=0.287 ms
64 bytes from 192.168.2.103: icmp_seq=22 ttl=64 time=0.270 ms

3、节点k8s-01-103,添加INPUT链,添加后查看INPUT链状态:

[root@k8s-node-2 ~]# iptables -I INPUT -p icmp -j DROP
[root@k8s-node-2 ~]# iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DROP       icmp --  anywhere             anywhere            

4、节点k8s-01-102,再次ping k8s-01-103节点:

[root@k8s-node-1 ~]# ping 192.168.2.103
PING 192.168.2.103 (192.168.2.103) 56(84) bytes of data.
^C
--- 192.168.2.103 ping statistics ---
15 packets transmitted, 0 received, 100% packet loss, time 14001ms

[root@k8s-node-1 ~]# 

5、删除k8s-01-103中新添加的链,注意删除链用的是行号,因此查看链时都加了 --line-numbers 参数:

[root@k8s-node-2 ~]# iptables -D INPUT 1
[root@k8s-node-2 ~]# iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination                  
[root@k8s-node-2 ~]# 

6、节点k8s-01-102,再次ping,可以ping通:

[root@k8s-node-1 ~]# ping 192.168.2.103
PING 192.168.2.103 (192.168.2.103) 56(84) bytes of data.
64 bytes from 192.168.2.103: icmp_seq=16 ttl=64 time=0.357 ms
64 bytes from 192.168.2.103: icmp_seq=17 ttl=64 time=0.307 ms
64 bytes from 192.168.2.103: icmp_seq=18 ttl=64 time=0.284 ms
64 bytes from 192.168.2.103: icmp_seq=19 ttl=64 time=0.280 ms
64 bytes from 192.168.2.103: icmp_seq=20 ttl=64 time=0.313 ms
64 bytes from 192.168.2.103: icmp_seq=21 ttl=64 time=0.287 ms
64 bytes from 192.168.2.103: icmp_seq=22 ttl=64 time=0.270 ms

7、原理解释:

ping 实际是采用了icmp 协议的一个工具。

在服务器k8s-01-103中,通过iptables 中在于内置INPUT链的最上边添加了icmp的drop规则。

收到的packet直接丢弃掉,不会有响应。也就是模拟了远端ping,但是不会收到相应包的情况。

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐