共计 3665 个字符,预计需要花费 10 分钟才能阅读完成。
Docker swarm 是集群下 docker 容器编排工具,本文讲解了安装 swarm 的步骤和 swarm 界面管理工具 Portainer 的安装。
本文使用的操作是 CentOS7;三台主机,一台作为管理节点,另外两台作为工作节点。
1. 安装 swarm
编辑三台机器的 /etc/hosts 文件,内容修改为:192.168.1.130 manager.wisely.com manager
192.168.1.131 node1.wisely.com node1
192.168.1.132 node2.wisely.com node2
每台机器安装 docker,顺序执行下面命令 yum update -y
yum install docker
systemctl start docker
systemctl enable docker
每台机器开放下面防火墙端口,顺序执行下面命令:firewall-cmd –permanent –add-port=2376/tcp
firewall-cmd –permanent –add-port=2377/tcp
firewall-cmd –permanent –add-port=7946/tcp
firewall-cmd –permanent –add-port=7946/udp
firewall-cmd –permanent –add-port=4789/udp
firewall-cmd –permanent –add-port=80/tcp
firewall-cmd –reload
systemctl restart docker
在 manager 节点初始化集群:docker swarm init –advertise-addr 192.168.1.130
执行后,得到下面结果:
Swarm initialized: current node (b0389cuqtk1h2v5vhj3a3xurz) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
–token SWMTKN-1-2xinda2i8kc8is6eob7hhyqyntxal1688ixzep2dqpmkyktnio-4xub9o40ilq326yi6b0hnjx9f \
192.168.1.130:2377
To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
在另外两个工作节点执行:docker swarm join \
–token SWMTKN-1-2xinda2i8kc8is6eob7hhyqyntxal1688ixzep2dqpmkyktnio-4xub9o40ilq326yi6b0hnjx9f \
192.168.1.130:2377
输出为:
[root@localhost ~]# docker swarm join \
> –token SWMTKN-1-2xinda2i8kc8is6eob7hhyqyntxal1688ixzep2dqpmkyktnio-4xub9o40ilq326yi6b0hnjx9f \
> 192.168.1.130:2377
This node joined a swarm as a worker.
执行 docker node ls 查看集群信息: ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
0bd7q219yihhkrlbj2prhlmxu node1.wisely.com Ready Active
0qm0up69sjld7rwofso506oml node2.wisely.com Ready Active
b0389cuqtk1h2v5vhj3a3xurz * manager.wisely.com Ready Active Leader
使用阿里云 docker 镜像加速,按顺序执行,请去阿里云申请:sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://*.mirror.aliyuncs.com”]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
部署测试,在管理节点执行:docker service create -p 80:80 –name webserver –replicas 5 httpd
查看集群中的 servicedocker service ls:
ID NAME REPLICAS IMAGE COMMAND
7wk5kekwkyjd webserver 5/5 httpd
查看集群中的 webserver 服务 docker service ps webserver:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
5jq6c649y7kfdofhc8t4od7jc webserver.1 httpd node1.wisely.com Running Preparing 38 seconds ago
80ijfnq17p2h8dvipaabq3j4l webserver.2 httpd node1.wisely.com Running Preparing 38 seconds ago
94yrknng8y9e4h9l80ractm2r webserver.3 httpd manager.wisely.com Running Preparing 38 seconds ago
3ctowh4uctuh8q5u7lr930e9p webserver.4 httpd node2.wisely.com Running Preparing 38 seconds ago
du0eoe5nn9dpcvqik0z4m2ros webserver.5 httpd node2.wisely.com Running Preparing 38 seconds ago
访问 http://192.168.1.130、http://192.168.1.131 或 http://192.168.1.132,结果为:
2. 安装 Portainer
首先关闭 selinux,setenforce 0
在 manager 节点执行:
docker service create \
–name portainer \
–publish 9000:9000 \
–constraint ‘node.role == manager’ \
–mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
portainer/portainer \
-H unix:///var/run/docker.sock
访问 http://192.168.1.130:9000
更多 Docker 相关教程见以下内容:
Docker 安装应用 (CentOS 6.5_x64) https://www.linuxidc.com/Linux/2014-07/104595.htm
Ubuntu 16.04 服务器上配置使用 Docker https://www.linuxidc.com/Linux/2017-06/145176.htm
Ubuntu 15.04 下安装 Docker https://www.linuxidc.com/Linux/2015-07/120444.htm
Docker 安装实例 https://www.linuxidc.com/Linux/2017-04/142666.htm
Docker 创建基础镜像 https://www.linuxidc.com/Linux/2017-05/144112.htm
在 Ubuntu 15.04 上如何安装 Docker 及基本用法 https://www.linuxidc.com/Linux/2015-09/122885.htm
Ubuntu 16.04 上 Docker 使用手记 https://www.linuxidc.com/Linux/2016-12/138490.htm
使用 Docker 分分钟启动常用应用 https://www.linuxidc.com/Linux/2017-04/142649.htm
Ubuntu 16.04 下 Docker 修改配置文件不生效解决办法 https://www.linuxidc.com/Linux/2017-05/143862.htm
Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里