一. 防火墙选择, CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤(原因见文末)。
1、关闭firewall:
// 停止firewall
systemctl stop firewalld.service
// 禁止firewall开机启动
systemctl disable firewalld.service
// 查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state
2. iptables
安装
yum install -y iptables
// 服务管理
// 查看iptables状态
service iptables status
// iptables服务重启
service iptables restart
// iptables服务禁用
service iptables stop
// 查看规则
iptables -L -n --line-number
// 删除规则 INPUT大写,2表示序号
iptables -D INPUT 2
开放端口
// TCP端口2376 用于安全的Docker客户端通信
iptables -I INPUT -p tcp --dport 2376 -j ACCEPT
// TCP端口2377 集群管理端口,只需要在管理器节点上打开
iptables -I INPUT -p tcp --dport 2377 -j ACCEPT
// TCP与UDP端口7946 节点之间通讯端口(容器网络发现)
iptables -I INPUT -p tcp --dport 7946 -j ACCEPT
iptables -I INPUT -p udp --dport 7946 -j ACCEPT
// UDP端口4789 overlay网络通讯端口(容器入口网络)
iptables -I INPUT -p udp --dport 4789 -j ACCEPT
// portainer 的endpoint端口
iptables -I INPUT -p tcp --dport 9001 -j ACCEPT
二. Swarm集群搭建
1. 修改集群用到的服务器docker配置:
// 修改文件 /lib/systemd/system/docker.service,允许使用 TCP 连接 Docker
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375
2. 创建集群
// 初始化集群
docker swarm init
// 显示manager节点的TOKEN
docker swarm join-token manager
// 加入到集群
docker swarm join --token <token> <manager ip>:2377
3. 服务创建、升级
// 创建自定义 Overlay 网络
docker network create --driver overlay swarm-network
// 创建服务
// --name 服务名称
docker service create
--with-registry-auth
--name api-service
--network swarm-network
-p 8000:8000
--replicas 2
api:test
// 滚动升级服务
// 参数含义解释:
// –-image 指定要更新的镜像
// -–update-parallelism 指定最大同步更新的任务数
// -–update-delay 指定更新间隔
docker service update
--with-registry-auth
--image api:test
--update-delay 2s
--update-parallelism 5 api-service
4. 常用命令
// 查看当前集群的信息。
docker info
// 查看集群节点
docker node ls
// 查看服务
docker service ls
// 删除服务
docker service rm <服务名称>
// 查看服务日志
docker service logs <服务名称>
// 扩展集群服务
docker service scale helloworld=2
// 查看服务运行在哪个节点上
docker service ps <服务名称>
// 查看服务部署的具体信息:
docker service inspect --pretty <服务名称>
问题整理:
1. 私有库创建服务必须加上: --with-registry-auth
docker service create
--with-registry-auth
--name shequn-api
-p 8000:8000
--replicas 2
--network swarm-network
cloud-lm720/api:test
2.负载均衡问题
node必须开放 4789 端口 参考: https://blog.csdn.net/weixin_33722405/article/details/94662896
3. 重写防火墙规则后导致当前节点的网络不可用
最佳方案: 防火墙换成 iptables 并且修改配置项:
IPTABLES_SAVE_ON_STOP="yes"
IPTABLES_SAVE_ON_RESTART="yes"
参考: https://blog.csdn.net/qq_14869093/article/details/102566291