Swarm安裝部署
1 swarm的特點(diǎn)
對(duì)外以Docker API接口呈現(xiàn),這樣帶來(lái)的好處是,如果現(xiàn)有系統(tǒng)使用Docker Engine,則可以平滑將Docker Engine切到Swarm上,無(wú)需改動(dòng)現(xiàn)有系統(tǒng)。
?▲Swarm對(duì)用戶來(lái)說(shuō),之前使用Docker的經(jīng)驗(yàn)可以繼承過(guò)來(lái)。非常容易上手,學(xué)習(xí)成本和二次開(kāi)發(fā)成本都比較低。同時(shí)Swarm本身專注于Docker集群管理,非常輕量,占用資源也非常少。 *“Batteries included but swappable”,簡(jiǎn)單說(shuō),就是插件化機(jī)制,Swarm中的各個(gè)模塊都抽象出了API,可以根據(jù)自己一些特點(diǎn)進(jìn)行定制實(shí)現(xiàn)。
?▲Swarm自身對(duì)Docker命令參數(shù)支持的比較完善,Swarm目前與Docker是同步發(fā)布的。Docker的新功能,都會(huì)第一時(shí)間在Swarm中體現(xiàn)。
2 swarm的概念
1)Swarm
集群的管理和編排是使用嵌入到 docker 引擎的 SwarmKit,可以在 docker 初始化時(shí)啟動(dòng) swarm 模式或者加入已存在的 swarm
2)Node
一個(gè)節(jié)點(diǎn)(node)是已加入到 swarm 的 Docker 引擎的實(shí)例 當(dāng)部署應(yīng)用到集群,你將會(huì)提交服務(wù)定義到管理節(jié)點(diǎn),接著 Manager
管理節(jié)點(diǎn)調(diào)度任務(wù)到 worker 節(jié)點(diǎn),manager 節(jié)點(diǎn)還執(zhí)行維護(hù)集群的狀態(tài)的編排和群集管理功能,worker 節(jié)點(diǎn)接收并執(zhí)行來(lái)自
manager 節(jié)點(diǎn)的任務(wù)。通常,manager 節(jié)點(diǎn)也可以是 worker 節(jié)點(diǎn),worker 節(jié)點(diǎn)會(huì)報(bào)告當(dāng)前狀態(tài)給 manager 節(jié)點(diǎn)
3)服務(wù)(Service)
服務(wù)是要在 worker 節(jié)點(diǎn)上要執(zhí)行任務(wù)的定義,它在工作者節(jié)點(diǎn)上執(zhí)行,當(dāng)你創(chuàng)建服務(wù)的時(shí),你需要指定容器鏡像
4)任務(wù)(Task)
任務(wù)是在 docekr 容器中執(zhí)行的命令,Manager 節(jié)點(diǎn)根據(jù)指定數(shù)量的任務(wù)副本分配任務(wù)給 worker 節(jié)點(diǎn)
docker swarm:集群管理,子命令有 init, join, leave, update。(docker swarm –help 查看幫助)
docker service:服務(wù)創(chuàng)建,子命令有 create, inspect, update, remove, tasks。(docker service–help 查看幫助)
docker node:節(jié)點(diǎn)管理,子命令有 accept, promote, demote, inspect, update, tasks, ls, rm。(docker node –help 查看幫助)
node 是加入到 swarm 集群中的一個(gè) docker 引擎實(shí)體,可以在一臺(tái)物理機(jī)上運(yùn)行多個(gè) node,node 分為:
manager nodes,也就是管理節(jié)點(diǎn)
worker nodes,也就是工作節(jié)點(diǎn)
1)manager node 管理節(jié)點(diǎn):執(zhí)行集群的管理功能,維護(hù)集群的狀態(tài),選舉一個(gè) leader 節(jié)點(diǎn)去執(zhí)行調(diào)度任務(wù)。
2)worker node 工作節(jié)點(diǎn):接收和執(zhí)行任務(wù)。參與容器集群負(fù)載調(diào)度,僅用于承載 task。
3)service 服務(wù):一個(gè)服務(wù)是工作節(jié)點(diǎn)上執(zhí)行任務(wù)的定義。創(chuàng)建一個(gè)服務(wù),指定了容器所使用的鏡像和容器運(yùn)行的命令。
service 是運(yùn)行在 worker nodes 上的 task 的描述,service 的描述包括使用哪個(gè) docker 鏡像,以及在使用該鏡像的容器中執(zhí)行什么命令。
4)task 任務(wù):一個(gè)任務(wù)包含了一個(gè)容器及其運(yùn)行的命令。task 是 service 的執(zhí)行實(shí)體,task 啟動(dòng) docker 容器并在容器中執(zhí)行任務(wù)。
3 環(huán)境準(zhǔn)備
1 部署搭建docker完成(可以參照之前的博客安裝部署)
2 關(guān)閉selinux
vim /etc/selinux/config
將selinux=enforcing改為selinux=disabled,重啟機(jī)器才能生效
3 關(guān)閉防火墻
systemctl stop firewalld
systemctl disable firewalld
4 如果是集群需要修改hosts配置文件
修改每個(gè)節(jié)點(diǎn)的hostname,例如假如兩臺(tái)機(jī)器,修改內(nèi)容如下
Node1:
Vim /etc/hostname
docker1
vim /etc/hosts
192.168.248.172 docker1
192.168.248.173 docker2
Node2:
Vim /etc/hostname
Docker2
vim /etc/hosts
192.168.248.172 docker1
192.168.248.173 docker2
5 修改docker的監(jiān)聽(tīng)端口
Swarm通過(guò)監(jiān)聽(tīng)2375端口進(jìn)行通信,所以在使用swarm集群管理之前,需要設(shè)置一下2375端口的監(jiān)聽(tīng),所有節(jié)點(diǎn)docker開(kāi)啟2375監(jiān)聽(tīng),修改如下配置文件
vim /usr/lib/system/system/docker.service
在ExecStart加入
-H tcp://0.0.0.0:2375 –H unix:///var/run/docker.sock
詳細(xì)信息如下所示
[root@docker1 ~]# cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target
6 重啟docker服務(wù)
systemctl daemon-reload
systemctl restart docker
4 swarm安裝與集群部署
1 swarm鏡像的下載
在集群節(jié)點(diǎn)上都安裝swarm
docker pull swarm
2 初始化swarm
docker swarm init --advertise-addr 192.168.248.172
Swarm initialized: current node (iit9n9veuse9g4ctk525e661x) is now a manager.
To add a worker to this swarm, run the following command:
??? docker swarm join --token SWMTKN-1-03iw8hbuv2jzrd9f7r5qvhzr1xrlsysa2kavjao36170u7mlia-f54aml7vt3i6r3cj7an8oaj2e 192.168.248.172:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
上面命令執(zhí)行后,該機(jī)器自動(dòng)加入到swarm集群。這個(gè)會(huì)創(chuàng)建一個(gè)集群token,獲取全球唯一的 token,作為集群唯一標(biāo)識(shí)。后續(xù)將其他節(jié)點(diǎn)加入集群都會(huì)用到這個(gè)token值。
其中,--advertise-addr參數(shù)表示其它swarm中的worker節(jié)點(diǎn)使用此ip地址與manager聯(lián)系。命令的輸出包含了其它節(jié)點(diǎn)如何加入集群的命令
如果其他節(jié)點(diǎn)加入到swarm集群,可以采取如下步驟
?? docker swarm join --token SWMTKN-1-03iw8hbuv2jzrd9f7r5qvhzr1xrlsysa2kavjao36170u7mlia-f54aml7vt3i6r3cj7an8oaj2e 192.168.248.172:2377
3 查看swarm集群中的節(jié)點(diǎn)信息
docker node list
4 下載portainer鏡像
docker search portainer 查詢是否存在portainer鏡像
docker pull portainer/portainer
5 運(yùn)行portainer容器
1 單機(jī)版運(yùn)行
docker run -d -p 9000:9000 \
??? --restart=always \
??? -v /var/run/docker.sock:/var/run/docker.sock \
??? --name prtainer-test \
??? portainer/portainer
該語(yǔ)句用宿主機(jī)9000端口關(guān)聯(lián)容器中的9000端口,并給容器起名為portainer-test。啟動(dòng)成功后,使用該機(jī)器IP:PORT即可訪問(wèn)Portainer。
?
?