3个节点组docker集群
首先3个节点均安装docker-ce服务
参考下边文章
https://blog.csdn.net/zz_aiytag/article/details/103660090
然后把3个节点的防火墙都给关了
systemctl stop firewalld
systemctl disable firewalld
选择一个节点,执行以下指令,则该节点为manager节点
docker swarm init
执行后效果如下
[root@worker bin]# docker swarm init
Swarm initialized: current node (r1kz0bty9rwzt7p2slnhzcnbx) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1c9hl3te4bwn3ig0ev22m47xshdiaxywp37g8ddu7vcsa3yf7d-3kbudvji28g1hxf1yqulkmq5i XX.XX.XX.52:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
其它两个节点加入swarm集群
两个节点分别执行以下执令
docker swarm join --token SWMTKN-1-1c9hl3te4bwn3ig0ev22m47xshdiaxywp37g8ddu7vcsa3yf7d-3kbudvji28g1hxf1yqulkmq5i XX.XX.XX.52:2377
[root@master docker]# docker swarm join --token SWMTKN-1-1c9hl3te4bwn3ig0ev22m47xshdiaxywp37g8ddu7vcsa3yf7d-3kbudvji28g1hxf1yqulkmq5i XX.XX.XX.52:2377
This node joined a swarm as a worker.
两个worker节点加入集群之后,返回manager节点
查看集群节点信息
docker node ls
[root@worker bin]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
zk0e67y59b74ji70cufc0l850 manager.node Ready Active 18.03.0-ce
dxyb0i4gv7so2eoi0l54i8i3y master.node Ready Active 18.03.0-ce
r1kz0bty9rwzt7p2slnhzcnbx * worker.node Ready Active Leader 18.03.0-ce
某个节点执行docker swarm leave
离开集群
docker swarm leave
docker swarm集群搭建完成。
如果忘记了连接令牌的值,可在manager上执行下述指令
docker swarm join-token worker
简单运行nginx示例
查看集群上运行的服务
docker service ls
在没有service被创建的时候,上述指令显示列表应为空的
在manager节点拉取nginx镜像
docker pull nginx
创建nginx服务
将nginx的80端口映射到节点的8083端口,服务生成2个nginx容器
docker service create --name my-web --publish published=8083,target=80 --replicas=2 nginx
[root@worker bin]# docker service create --name my-web --publish published=8083,target=80 --replicas=2 nginx
image nginx:latest could not be accessed on a registry to record
its digest. Each node will access nginx:latest independently,
possibly leading to different nodes running different
versions of the image.
lfmx0y79dss7b0rgqyfnvmidj
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
再查看集群上运行的服务
docker service ls
[root@worker bin]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
lfmx0y79dss7 my-web replicated 2/2 nginx:latest *:8083->80/tcp
查看集群my-web服务的详细信息
不加--pretty
是显示的JSON格式
docker service inspect my-web
docker service inspect --pretty my-web
查看服务的分布节点信息
docker service ps my-web
[root@worker bin]# docker service ps my-web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
3qpm9umad3ew my-web.1 nginx:latest worker.node Running Running 6 minutes ago
ogyce9oqbrjs my-web.2 nginx:latest manager.node Running Running 3 minutes ago
2个nginx容器分别运行在两个节点
在浏览器上分别输入对应节点 的IP:8083
,均可以看到nginx的欢迎信息。
删除服务
docker service rm my-web
简单运行wordpress示例
创建一个overlay网络
目的是实现容器之间的跨节点通信
docker network create -d overlay d_net
查看所有的网络
docker network ls
[root@worker usr]# docker network ls
NETWORK ID NAME DRIVER SCOPE
88716f918239 bridge bridge local
p4rb8jyzxhnp d_net overlay swarm
ac228f80101a docker_gwbridge bridge local
46619ac752bd host host local
ql7zzydnbxnc ingress overlay swarm
d6d37480698f my-net bridge local
35ca88ad4fed none null local
d57ccb9606ff webapp_dev bridge local
932ff2fdda43 webapp_pro bridge local
创建一个mysql服务
docker service create --name d_mysql --env MYSQL_ROOT_PASSWORD=wordpress --env MYSQL_DATABASE=wordpress --network=d_net --mount type=volume,source=mysql-data,destination=/var/lib/mysql mysql:5.7
docker service create --name d_mysql \
--env MYSQL_ROOT_PASSWORD=wordpress \
--env MYSQL_DATABASE=wordpress \
--network=d_net \
--mount type=volume,source=mysql-data,destination=/var/lib/mysql \
mysql:5.7
其中,--env MYSQL_DATABASE=wordpress
是服务运行时创建的一个数据库
[root@worker usr]# docker service create --name d_mysql --env MYSQL_ROOT_PASSWORD=wordpress --env MYSQL_DATABASE=wordpress --network=d_net --mount type=volume,source=mysql-data,destination=/var/lib/mysql mysql:5.7
image mysql:5.7 could not be accessed on a registry to record
its digest. Each node will access mysql:5.7 independently,
possibly leading to different nodes running different
versions of the image.
xffqk26gnksud924u80tpog69
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
创建一个wordpress服务
docker service create --name d_wordpress -p 80:80 --env WORDPRESS_DB_PASSWORD=wordpress --env WORDPRESS_DB_HOST=d_mysql --network=d_net wordpress
docker service create --name d_wordpress \
-p 80:80 \
--env WORDPRESS_DB_PASSWORD=wordpress \
--env WORDPRESS_DB_HOST=d_mysql \
--network=d_net \
wordpress
其中wordpress默认的参数有--env WORDPRESS_DB_NAME=wordpress
[root@worker usr]# docker service create --name d_wordpress -p 80:80 --env WORDPRESS_DB_PASSWORD=wordpress --env WORDPRESS_DB_HOST=d_mysql --network=d_net wordpress
image wordpress:latest could not be accessed on a registry to record
its digest. Each node will access wordpress:latest independently,
possibly leading to different nodes running different
versions of the image.
w33qmq737vj3pyd326dfta0af
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
查看集群的服务
docker service ls
[root@worker usr]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
xffqk26gnksu d_mysql replicated 1/1 mysql:5.7
w33qmq737vj3 d_wordpress replicated 1/1 wordpress:latest *:80->80/tcp
查看wordpress服务运行的节点
docker service ps d_wordpress
[root@worker usr]# docker service ps d_wordpress
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
zbff8om4nk38 d_wordpress.1 wordpress:latest manager.node Running Running 20 seconds ago
在浏览器上输入对应节点的地址IP:80
可以正常访问wordpress服务
使用命令查看服务日志
docker service logs --tail 100 d_wordpress
遇到的问题
集群创建服务的时候,某个节点一直无法创建启动容器,报错:container ingress-sbox is already present in sandbox ingress sbox
,这个时候可能是因为docker的SELinux配置问题。
解决办法:编辑文件/etc/sysconfig/docker
,若不存在该文件,则添加文件。添加以下内容:
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
if [ -z "${DOCKER_CERT_PATH}" ]; then
DOCKER_CERT_PATH=/etc/docker
fi
保存退出之后最好重启一下docker服务
systemctl restart docker