Docker高级教程之智能添加与修改防火墙规则

2020-06-17 06:44:44易采站长站整理

现在使用智能防火墙查看test1 的防火墙


[root@docker-test3 code]# python modify_docker_container_firewall.py test1 -l
This container:test1 is not firewall rule!
[root@docker-test3 code]# python modify_docker_container_firewall.py test2 -l
This container:test2 is not firewall rule!
[root@docker-test3 code]# python modify_docker_container_firewall.py test3 -l
This container:test3 info is not in etcd!

可以看到test1与test2都是没有规则的,而test3每一使用持久化固定ip,所以没办法查看数据

3、添加规则

先给test1添加内部模式规则-m internal,这样能使用docker宿主机的外网ip+port访问


[root@docker-test3 code]# python modify_docker_container_firewall.py test1 -a -m internal -s 1.1.1.1/24 -pm dynamic -dp 22
{'Mode': 'internal', 'Container_name': 'test1', 'Source_port': '40030', 'Port_mode': 'dynamic', 'Local_port': '22', 'Source_ip': '1.1.1.1/24', 'Id': 1, 'Container_ip': '172.16.1.2/24'}

这样是给与test1容器设置,允许1.1.1.1/24通过40030(使用动态模式自动生成访问端口),访问test1的22端口

其中test1的容器ip自动查看,不需要收到查找与手动输入

说明

Mode是运行的模式

Container_name是对应容器名

Source_port是来源端口

Port_mode是指端口模式为动态获取

Local_port是目的端口

Source_ip是来源ip

Id是指防火墙规则的id,后面可以通过指定id来删除规则

Container_ip是容器的ip

以上数据均是在使用持久化故障ip脚本生成容器的时候,程序自动把数据写入到etcd里,然后只能防火墙也通过etcd获取数据

在给test1添加一个使用手动模式设置来源端口


[root@docker-test3 code]# python modify_docker_container_firewall.py test1 -a -m internal -s 1.1.1.1/24 -pm manual -sp 400031 -dp 22
{'Mode': 'internal', 'Container_name': 'test1', 'Source_port': '40030', 'Source_ip': '1.1.1.1/24', 'Local_port': '22', 'Port_mode': 'dynamic', 'Id': 1, 'Container_ip': '172.16.1.2/24'}
{'Mode': 'internal', 'Container_name': 'test1', 'Source_port': '400031', 'Port_mode': 'manual', 'Local_port': '22', 'Source_ip': '1.1.1.1/24', 'Id': 2, 'Container_ip': '172.16.1.2/24'}

如果指定的来源端口相同,并且来源ip也相同会报错


[root@docker-test3 code]# python modify_docker_container_firewall.py test1 -a -m internal -s 1.1.1.1/24 -pm manual -sp 40031 -dp 22
This rule had exist!

现在通过-l参数来查看当前test1的规则


[root@docker-test3 code]# python modify_docker_container_firewall.py test1 -l
Follow is container:test1 firewall rule!
{'Mode': 'internal', 'Container_name': 'test1', 'Source_port': '40030', 'Port_mode': 'dynamic', 'Local_port': '22', 'Source_ip': '1.1.1.1/24', 'Id': 1, 'Container_ip': '172.16.1.2/24'}