如何在 CentOS 7 上为 Docker Swarm 配置 Linux 防火墙

介绍

Docker Swarm是 Docker 的一项功能,可以轻松地在规模上运行 Docker 主机和容器。 Docker Swarm 或 Docker 集群由一个或多个 Docker 主机组成,这些主机作为 manager 节点,以及任何数量的 worker 节点。设置此类系统需要仔细操纵 Linux 防火墙。

Docker Swarm 需要正确运作的网络端口是:

  • TCP 端口 2376 用于安全的 Docker 客户端通信. 此端口需要 Docker Machine 才能工作. Docker Machine 用于组织 Docker 主机. * TCP 端口 2377. 此端口用于 Docker Swarm 或集群的节点之间的通信. 它只需要在管理节点上打开。 * TCP 和 UDP 端口 7946' 用于节点之间的通信(容器网络发现)。 * UDP 端口 4789' 用于覆盖网络流量(容器入网网络)。

注:除了这些端口之外,端口 `22 (对于 SSH 流量) 和任何其他需要在集群上运行特定服务的端口都必须打开。

在本文中,您将使用FirewallD和IPTables配置CentOS 7上的Linux防火墙FirewallD是CentOS 7上的默认防火墙应用程序,但IPTables也是可用的。

前提条件

在继续这篇文章之前,你应该:

您可以遵循教程(How To Provision and Manage Remote Docker Hosts with Docker Machine on CentOS 7)(https://andsky.com/tech/tutorials/how-to-provision-and-manage-remote-docker-hosts-with-docker-machine-on-centos-7))来设置这些。

注意:您会注意到,命令(以及本文中的所有命令)没有前缀为sudo,这是因为假设您使用docker-machine ssh命令登录到服务器后使用Docker Machine。

方法 1 — 使用防火墙D 打开 Docker Swarm 端口

FirewallD 是 CentOS 7 上的默认防火墙应用程序,但在新的 CentOS 7 服务器上,它被禁用了,所以让我们启用它并添加 Docker Swarm 所需的网络端口。

在开始之前,检查其状态:

1systemctl status firewalld

它不应该运行,所以开始它:

1systemctl start firewalld

然后启用它以便在 boot 上启动:

1systemctl enable firewalld

在将成为Swarm管理器的节点上,使用以下命令打开所需的端口:

1[environment second]
2firewall-cmd --add-port=2376/tcp --permanent
3firewall-cmd --add-port=2377/tcp --permanent
4firewall-cmd --add-port=7946/tcp --permanent
5firewall-cmd --add-port=7946/udp --permanent
6firewall-cmd --add-port=4789/udp --permanent

注意:如果您犯了一个错误,需要删除一个条目,请键入: firewall-cmd --remove-port=port-number/tcp -permanent

然后,重新加载防火墙:

1[environment second]
2firewall-cmd --reload

然后重新启动 Docker。

1[environment second]
2systemctl restart docker

然后,在将作为Swarm工人功能的每个节点上,执行以下命令:

1[environment third]
2firewall-cmd --add-port=2376/tcp --permanent
3firewall-cmd --add-port=7946/tcp --permanent
4firewall-cmd --add-port=7946/udp --permanent
5firewall-cmd --add-port=4789/udp --permanent

然后,重新加载防火墙:

1[environment third]
2firewall-cmd --reload

然后重新启动 Docker。

1[environment third]
2systemctl restart docker

您已成功使用 FirewallD 打开 Docker Swarm 所需的端口。

注意**:如果您要在集群中测试需要外部网络访问的应用程序,请确保打开所需的端口. 例如,如果您要测试需要在端口 80 上访问的 Web 应用程序,请在集群中的所有节点(管理员和工人)上使用以下命令添加授权访问该端口的规则:

1firewall-cmd --add-port=80/tcp --permanent

请记住,当您进行此更改时,请重新加载防火墙。

方法 2 — 使用 IPTables 打开 Docker Swarm 端口

要在任何 Linux 发行版上使用 IPTables,您必须先卸载任何其他防火墙实用程序。

1systemctl stop firewalld

然后阻止它。

1systemctl disable firewalld

然后安装iptables-services包,管理IPTables规则的自动加载:

1yum install iptables-services

接下来,开始IPTables:

1systemctl start iptables

然后启用它,以便它自动启动:

1systemctl enable iptables

在您开始将 Docker Swarm 特定的规则添加到 INPUT 链之前,让我们看看该链中的默认规则:

1iptables -L INPUT --line-numbers

输出应该是这样的:

1[secondary_label Output]
2Chain INPUT (policy ACCEPT)
3num target prot opt source destination         
41 ACCEPT all  --  anywhere anywhere state RELATED,ESTABLISHED
52 ACCEPT icmp --  anywhere anywhere            
63 ACCEPT all  --  anywhere anywhere            
74 ACCEPT tcp  --  anywhere anywhere state NEW tcp dpt:ssh
85 REJECT all  --  anywhere anywhere reject-with icmp-host-prohibited

结合起来,默认规则为服务器提供国家保护,拒绝所有输入流量,除了那些已经建立的。SSH流量是允许的. 注意规则第5号,上面提到,因为这是一个追踪拒绝规则. 为了您的Docker Swarm正常运作,您添加的规则需要添加到这个规则上。

现在你知道该怎么做,你可以通过使用iptables实用程序添加所需的规则,这个第一套命令应该在作为Swarm管理器的节点上执行。

1[environment second]
2iptables -I INPUT 5 -p tcp --dport 2376 -j ACCEPT
3iptables -I INPUT 6 -p tcp --dport 2377 -j ACCEPT
4iptables -I INPUT 7 -p tcp --dport 7946 -j ACCEPT
5iptables -I INPUT 8 -p udp --dport 7946 -j ACCEPT
6iptables -I INPUT 9 -p udp --dport 4789 -j ACCEPT

这些规则是运行时规则,如果系统重新启动,它们将丢失. 若要将当前的运行时规则保存到文件中,以便在重新启动后继续存在,请键入:

1[environment second]
2/usr/libexec/iptables/iptables.init save

现在,规则被保存到名为iptables的文件中,在/etc/sysconfig目录中。如果您使用iptables -L -line-number查看规则,您会看到所有规则都已插入到 catch-all reject规则上方:

 1[secondary_label Output]
 2Chain INPUT (policy ACCEPT)
 3num target prot opt source destination         
 41 ACCEPT all  --  anywhere anywhere state RELATED,ESTABLISHED
 52 ACCEPT icmp --  anywhere anywhere            
 63 ACCEPT all  --  anywhere anywhere            
 74 ACCEPT tcp  --  anywhere anywhere state NEW tcp dpt:ssh
 85 ACCEPT tcp  --  anywhere anywhere tcp dpt:2376
 96 ACCEPT tcp  --  anywhere anywhere tcp dpt:7946
107 ACCEPT udp  --  anywhere anywhere udp dpt:7946
118 ACCEPT udp  --  anywhere anywhere udp dpt:4789
129 ACCEPT tcp  --  anywhere anywhere tcp dpt:http
1310 REJECT all  --  anywhere anywhere reject-with icmp-host-prohibited

然后重新启动 Docker。

1[secondary_label Output]
2systemctl restart docker

在将作为Swarm工人的节点上,执行这些命令:

1[environment third]
2iptables -I INPUT 5 -p tcp --dport 2376 -j ACCEPT
3iptables -I INPUT 6 -p tcp --dport 7946 -j ACCEPT
4iptables -I INPUT 7 -p udp --dport 7946 -j ACCEPT
5iptables -I INPUT 8 -p udp --dport 4789 -j ACCEPT

将规则保存到磁盘:

1[environment third]
2/usr/libexec/iptables/iptables.init save

然后重新启动 Docker:

1[environment third]
2systemctl restart docker

您可以了解更多关于这些规则如何工作的教程 How the IPTables Firewall Works

注意**:如果您正在测试需要外部网络访问的群集上的应用程序,请确保打开所需的端口. 例如,如果您正在测试需要访问端口 80 的 Web 应用程序,请在群集中的所有节点(管理员和工人)上使用以下命令添加授权访问该端口的规则:

1iptables -I INPUT rule-number -p tcp --dport 80 -j ACCEPT

请务必在 catchall 拒绝规则上方插入规则。

结论

FirewallD 和 IPTables 是 Linux 世界中最流行的防火墙管理应用程序,您只需阅读如何使用它们来打开设置 Docker Swarm 所需的网络端口。

Published At
Categories with 技术
comments powered by Disqus