我们使用物理机或者云主机,通过应用把所有服务器连接到一起供多个服务使用,这样既达到了资源的合理利用又能保证系统的稳定性,这就是集群。集群的好处是能够根据流量的增加与减少进行机器灵活伸缩,灵活简单实用。但是这里说的是Docker的集群,在Docker 1.12以及以上版本中,Docker官方推出了Swarm容器集群服务,简单的理解为把多个主机上的Docker组成一个Docker集群,使容器跨网组成跨主机的子网网络。
swarm简介
swarm是管理集群工具,主要作用就是把若干台Docker主机抽象的组成一个整体,可以通过一个统一入口管理这些主机上的docker资源。swarm和kubernetes类似,但是更轻,具有的功能比kubernetes更少一些。
安装docker
我使用的是yum安装,可以参考https://www.wulaoer.org/?p=260 只执行到[6]就可以了。
swarm集群环境配置
docker安装之后不能直接pull swarm的。swarm需要每台安装docker的服务器上开启基于HTTP的DockerAPI,在集群中有一台是swarmManager的管理者,用来管理整个集群中的容器资源。管理者不是针对服务,而是针对集群的管理,例如:在一台服务器上的docker关闭了,swarm会把这台服务器上运行的服务迁移到另外一台正常的服务器上。默认环境下docker是没有开启API,所以需要手动开启。
[root@wulaoer.org ~]# vim /lib/systemd/system/docker.service [Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network-online.target firewalld.service Wants=network-online.target [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
也可以直接使用sed替换
sed -i 's/ExecStart.*/& -H tcp:\/\/0.0.0.0:2375 -H unix:\/\/\/var\/run\/docker.sock/g' /lib/systemd/system/docker.service
docker配置了API之后才可以启动docker,这个端口是随意的,也可以换成另外一个不使用的端口。下面重启docker服务,查看一下是否开启了API。
[root@wulaoer.org ~]# systemctl daemon-reload [root@wulaoer.org ~]# systemctl restart docker [root@wulaoer.org ~]# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1463/master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 981/sshd tcp6 0 0 ::1:25 :::* LISTEN 1463/master tcp6 0 0 :::8090 :::* LISTEN 4544/java tcp6 0 0 :::8091 :::* LISTEN 17883/java tcp6 0 0 127.0.0.1:8000 :::* LISTEN 4544/java tcp6 0 0 :::2375 :::* LISTEN 18413/dockerd tcp6 0 0 :::3306 :::* LISTEN 1405/mysqld tcp6 0 0 :::22 :::* LISTEN 981/sshd
swarm开启了API,我们就可以pull一下swam了,
[root@wulaoer.org ~]# docker pull swarm Using default tag: latest latest: Pulling from library/swarm d85c18077b82: Pull complete 1e6bb16f8cb1: Pull complete 85bac13497d7: Pull complete Digest: sha256:b866583a3b8791bcd705b7bc0fd94c66b695a1a2dbaeb5f59ed29940e5015dc8 Status: Downloaded newer image for swarm:latest
注意,如果pull swarm下载不下来,可以尝试修改一下hosts文件,利用dig工具查看,先安装dig
[root@wulaoer.org ~]# yum install bind-utils [root@wulaoer.org ~]# dig @114.114.114.114 registry-1.docker.io ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> @114.114.114.114 registry-1.docker.io ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19575 ;; flags: qr rd ra; QUERY: 1, ANSWER: 8, AUTHORITY: 4, ADDITIONAL: 8 ;; QUESTION SECTION: ;registry-1.docker.io. IN A ;; ANSWER SECTION: registry-1.docker.io. 600 IN A 34.199.77.19 registry-1.docker.io. 600 IN A 52.201.142.14 registry-1.docker.io. 600 IN A 34.197.189.129 registry-1.docker.io. 600 IN A 34.205.88.205 registry-1.docker.io. 600 IN A 34.201.196.144 registry-1.docker.io. 600 IN A 34.228.211.243 registry-1.docker.io. 600 IN A 34.232.31.24 registry-1.docker.io. 600 IN A 34.199.40.84 ;; AUTHORITY SECTION: docker.io. 55871 IN NS ns-421.awsdns-52.com. docker.io. 55871 IN NS ns-1168.awsdns-18.org. docker.io. 55871 IN NS ns-513.awsdns-00.net. docker.io. 55871 IN NS ns-1827.awsdns-36.co.uk. ;; ADDITIONAL SECTION: ns-421.awsdns-52.com. 55442 IN A 205.251.193.165 ns-421.awsdns-52.com. 55442 IN AAAA 2600:9000:5301:a500::1 ns-513.awsdns-00.net. 86382 IN A 205.251.194.1 ns-513.awsdns-00.net. 86382 IN AAAA 2600:9000:5302:100::1 ns-1168.awsdns-18.org. 86382 IN A 205.251.196.144 ns-1168.awsdns-18.org. 86382 IN AAAA 2600:9000:5304:9000::1 ns-1827.awsdns-36.co.uk. 86382 IN A 205.251.199.35 ns-1827.awsdns-36.co.uk. 86382 IN AAAA 2600:9000:5307:2300::1 ;; Query time: 2 msec ;; SERVER: 114.114.114.114#53(114.114.114.114) ;; WHEN: Fri Feb 14 14:56:02 CST 2020 ;; MSG SIZE rcvd: 482 [root@wulaoer.org ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 34.199.77.19 registry-1.docker.io
创建swarm集群
swarm镜像已经下载完成,先创建swarm集群便于区分。我们可以在三个节点上都部署swarm集群交叉一下,相当于三个集群中都有三个节点。
[root@wulaoer.org ~]# docker swarm init --advertise-addr 10.211.55.145 #这个IP是本机的IP Swarm initialized: current node (nenu012qektax3uptdrzxbiab) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-1eazlzz2qooqy4fggrte1mdllq0e9qpx87veydi4r99uy008t5-50l1mwq2osibjjdq55xql1lqc 10.211.55.145:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
创建集群后会生成一个集群token,方便客户端加入集群,下面根据集群的token加入到集群中。
[root@server09 ~]# docker swarm join --token SWMTKN-1-1eazlzz2qooqy4fggrte1mdllq0e9qpx87veydi4r99uy008t5-50l1mwq2osibjjdq55xql1lqc 10.211.55.145:2377 This node joined a swarm as a worker. [root@server08 ~]# docker swarm join --token SWMTKN-1-1eazlzz2qooqy4fggrte1mdllq0e9qpx87veydi4r99uy008t5-50l1mwq2osibjjdq55xql1lqc 10.211.55.145:2377 This node joined a swarm as a worker. [root@wulaoer.org ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS nenu012qektax3uptdrzxbiab * wulaoer.org Ready Active Leader k5zby5vidu5jnkjptz4h7sxbu server08 Ready Active fn8v8ztfi3rq0b4h51g02dpzj server09 Ready Active
swarm集群创建完成,节点也加入到了集群中,这里看到在一个集群中只有一个leader,leader是负载另外两个节点的状态,可以根据需要进行扩容或缩减应用。下个章节说swarm的基本使用
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏