Linux, 应用部署

docker swarm 集群部署

Docker Compose是一个在单个服务器或主机上创建多个容器的工具。

Docker swarm 是Docker公司推出的用来管理docker集群的平台。它是将一群Docker宿主机变成一个单一的虚拟主机,Swarm使用标准的Docker API接口作为其前端的访问入口。

swManager192.168.168.102
node1192.168.168.103
node2192.168.168.104
节点配置表

一、基础环境配置(所有节点)

1、各节点根据节点配置表修改自己的主机名,并且把所有节点的信息添加到hosts文件
2、设置ssh从sw_Manager上免密登录其它节点
3、做好各主机的时间同步
4、关闭防火墙、关闭selinux
5、安装docker引擎
yum -y install docker-ce
6、修改docker镜像仓库地址
cat <<EOF >/etc/docker/daemon.json
{
"insecure-registries": ["192.168.5.24:5000"],
"registry-mirrors": [
    "https://ccr.ccs.tencentyun.com",
    "https://docker.rainbond.cc",
    "https://elastic.m.daocloud.io",
    "https://elastic.m.daocloud.io",
    "https://docker.m.daocloud.io",
    "https://gcr.m.daocloud.io",
    "https://ghcr.m.daocloud.io",
    "https://k8s-gcr.m.daocloud.io",
    "https://k8s.m.daocloud.io",
    "https://mcr.m.daocloud.io",
    "https://nvcr.m.daocloud.io",
    "https://quay.m.daocloud.io"
  ]
}
7、设置docker引擎自启动
systemctl enable --now docker

二、配置集群

1、初始化swarm

[root@swManager ~]# docker swarm init --listen-addr 192.168.168.102:2377
Swarm initialized: current node (bukhx2jux7w2a9smr6l3b5arh) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-18g572b7yi84rm8vlu4nsz17amf7ncagmlvjegkdeofn2fb629-4kqmthlbw2dd2xkpxwitzp7qw 192.168.168.102:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

2、添加工作节点到swarm集群
[root@node1 ~]# docker swarm join --token SWMTKN-1-18g572b7yi84rm8vlu4nsz17amf7ncagmlvjegkdeofn2fb629-4kqmthlbw2dd2xkpxwitzp7qw 192.168.168.102:2377
This node joined a swarm as a worker.

[root@node2 ~]# docker swarm join --token SWMTKN-1-18g572b7yi84rm8vlu4nsz17amf7ncagmlvjegkdeofn2fb629-4kqmthlbw2dd2xkpxwitzp7qw 192.168.168.102:2377
This node joined a swarm as a worker.

3、查看集群节点列表

[root@swManager ~]# docker node ls
ID                            HOSTNAME    STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
cznaxs2o5spp5n0vheen4wh5z     node1       Ready     Active                          26.1.3
ii8xz274uhx1jza0w8t2a6c40     node2       Ready     Active                          26.1.3
bukhx2jux7w2a9smr6l3b5arh *   swManager   Ready     Active         Leader           26.1.3

4、图形化查看集群

[root@swManager ~]# docker run -itd -p 8888:8080 -e HOST=192.168.168.102 -e PORT=8080 -v /var/run/docker.sock:/var/run/docker.sock --name visua 192.168.5.24:5000/visualizer
5、服务测试
[root@swManager ~]# docker service create --replicas 2 --mount "type=bind,source=/html,target=/var/www/html" --publish 8080:80 --name httpservice 192.168.5.24:5000/centos-httpd:v1

服务已经运行在多个节点主机上。

6、docker stack
通过docker service已经可以把服务以多个容器副本的形式运行在不同的节点主机上,但是无法很好的一次性运行多个有关联的服务,而docker stack就可以很好的解决这个问题。

Leave a Reply