docker集群swarm配置

简介:

 
Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源。Swarm和Kubernetes比较类似,但是更加轻,具有的功能也较kubernetes更少一些。

安装docker:
[root@server1 ~]# ls
docker-engine-17.03.1.ce-1.el7.centos.x86_64.rpm
docker-engine-selinux-17.03.1.ce-1.el7.centos.noarch.rpm
[root@server1 ~]# yum install *
tab键自动补齐的bash包(不大,只有几百k)
[root@server1 ~]# yum install bash*
[root@server1 ~]# source /usr/share/bash-completion/bash_completion
启动服务,创建集群
server1
[root@server1 ~]# systemctl start docker
[root@server1 ~]# docker swarm init
Swarm initialized: current node (ov1hn8g2kozu0jdbs91zhc6e0) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-64hep9thjsfkhyamcqa3y1zths9xg3pxsycu1ujsv7jp6a0xxl-6tp4az3k3h6mojwu0jozmh3r7 \
    172.25.5.1:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
复制中间的join到server2执行
server2
[root@server2 ~]# docker swarm join \
>     --token SWMTKN-1-64hep9thjsfkhyamcqa3y1zths9xg3pxsycu1ujsv7jp6a0xxl-6tp4az3k3h6mojwu0jozmh3r7 \
>     172.25.5.1:2377
This node joined a swarm as a worker.
server3
[root@server3 ~]# docker swarm join \
>     --token SWMTKN-1-64hep9thjsfkhyamcqa3y1zths9xg3pxsycu1ujsv7jp6a0xxl-6tp4az3k3h6mojwu0jozmh3r7 \
>     172.25.5.1:2377
This node joined a swarm as a worker.
集群创建成功:
[root@server1 ~]# docker node  ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
7uoqb7r9r32i3831bqusyt8vi    server2   Ready   Active        
7wcupczq4ys8nn0byqga4wh3d    server3   Ready   Active        
ov1hn8g2kozu0jdbs91zhc6e0 *  server1   Ready   Active        Leader
server导入镜像,server都需要导入,
[root@server1 ~]# docker load -i ./nginx.tar 
cdb3f9544e4c: Loading layer [==================================================>] 58.44 MB/58.44 MB
a8c4aeeaa045: Loading layer [==================================================>] 54.24 MB/54.24 MB
08d25fa0442e: Loading layer [==================================================>] 3.584 kB/3.584 kB
Loaded image: nginx:latest

还有一个visualizer监控镜像也要导入
这里写图片描述

创建集群:

[root@server1 ~]# docker service create --name nginx --publish 80:80 --replicas 3 nginx
unable to pin image nginx to digest: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on [::1]:53: read udp [::1]:46939->[::1]:53: read: connection refused
n1yamnhxjuyblbpy9vzgi1n02

[root@server1 ~]# docker service ps nginx
ID            NAME     IMAGE  NODE     DESIRED STATE  CURRENT STATE           ERROR  PORTS
m24rdh2k20r1  nginx.1  nginx  server1  Running        Running 11 seconds ago         
92ndp7kg8rny  nginx.2  nginx  server2  Running        Running 10 seconds ago         
630qidqhi64q  nginx.3  nginx  server3  Running        Running 10 seconds ago   


[root@server1 ~]# docker service scale nginx=6
[root@server1 ~]# docker service ps nginx 
ID            NAME     IMAGE  NODE     DESIRED STATE  CURRENT STATE               ERROR  PORTS
m24rdh2k20r1  nginx.1  nginx  server1  Running        Running about a minute ago         
92ndp7kg8rny  nginx.2  nginx  server2  Running        Running about a minute ago         
630qidqhi64q  nginx.3  nginx  server3  Running        Running about a minute ago         
8829mrpatv96  nginx.4  nginx  server3  Running        Running 4 seconds ago              
ctzgph33oasg  nginx.5  nginx  server1  Running        Running 4 seconds ago              
uh369f8kqq07  nginx.6  nginx  server2  Running        Running 4 seconds ago    

创建集群监控

[root@server1 ~]# docker service create --name=viz --publish=8080:8080/tcp --constraint=node.role==manager --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer
unable to pin image dockersamples/visualizer to digest: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on [::1]:53: read udp [::1]:49566->[::1]:53: read: connection refused
vqrnqxnva1s4dqfwzw2oeltlj

[root@server1 ~]# docker service ps viz 
ID            NAME   IMAGE                     NODE     DESIRED STATE  CURRENT STATE            ERROR  PORTS
2pim9nvafmkx  viz.1  dockersamples/visualizer  server1  Running        Starting 18 seconds ago  

测试:

docker集群swarm配置_第1张图片
docker集群swarm配置_第2张图片
docker集群swarm配置_第3张图片
docker集群swarm配置_第4张图片

这里写图片描述

docker集群swarm配置_第5张图片

批量更新镜像

[root@server1 ~]# docker load -i game2048.tar 
011b303988d2: Loading layer [==================================================>]  5.05 MB/5.05 MB
36e9226e74f8: Loading layer [==================================================>] 51.46 MB/51.46 MB
192e9fad2abc: Loading layer [==================================================>] 3.584 kB/3.584 kB
6d7504772167: Loading layer [==================================================>] 4.608 kB/4.608 kB
88fca8ae768a: Loading layer [==================================================>] 629.8 kB/629.8 kB
Loaded image: game2048:latest
[root@server1 ~]# 

这里写图片描述

这里写图片描述

[root@server1 ~]# docker service update --image game2048 --update-parallelism 5 --update-delay 10s nginx
unable to pin image game2048 to digest: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on [::1]:53: read udp [::1]:33494->[::1]:53: read: connection refused
nginx

docker集群swarm配置_第6张图片

docker集群swarm配置_第7张图片

你可能感兴趣的:(oo)