swarm的快速入门的中文资料(姿势正确的)很少。本文简单介绍swarm(伪)集群的搭建。使用docker-machine开一些虚拟机作为docker主机。
准备工作
首先怒开4个虚拟机,一个Manager,三个worker
docker-machine create manager (ip: 103)
docker-machine create worker1 (ip: 104)
docker-machine create worker2 (ip: 105)
docker-machine create worker3 (ip: 106)
docker-machine ls #查看虚拟机
创建manager节点
docker-machine ssh manager # 登录到manager
docker swarm init --advertise-addr 192.168.99.103:2377 # 集群manager
docker node list # 查看集群节点
下面登录worker机器并将它们加入集群
docker-machine ssh worker1
docker swarm join --token SWMTKN-1-1gcvu7m59jfo8jgmjeu44jypfl2pywwn7l8slscs4eix28x8
v7-1jm8o9wf8ghhcrit7hkkxvpd4 192.168.99.103:2377
回到103上看看集群节点情况。
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
afd3jflix35ykn3tju9853kxm * manager Ready Active Leader
aqb10743e0w278f99p8xg1njc worker3 Ready Active
ib651vgesr5aqre0z2bx2trq5 worker1 Ready Active
wxvwuuzou9q54ikez6gjjnoo6 worker2 Ready Active
在集群上创建服务,服务的工作就是ping manager机器
docker service create --name ping00 alpine ping 192.168.99.103
docker service ls # 查看集群中的服务
docker service ps ping00 # 查看服务的任务(容器)
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
72fk7w8j4sdd ping00.1 alpine:latest worker2 Running Running 47 seconds ago
我们看到这个task运行在了worker2机器上,登录到worker2运行docker ps可以看到这个运行的容器
路由转发
docker service create --name web --publish 80:80 nginx
我们启动了一个nginx服务,使用docker service ps web查看到其跑在了manager(103)上,但是,我们可以从103,104,105,106任何一台机器访问到这个服务。
事实上,这是因为集群上的任何节点在接到请求后如果处理不了就交给manager,manager会看哪一台机器可以处理并转发过去。
真正的集群
下面让我们的集群跑起10个Nginx
docker service update --replicas 10 web
docker service ls
ID NAME MODE REPLICAS IMAGE
mfljbz7x2w0u web replicated 10/10 nginx:latest
十个容器分布在了四台机器上
docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
du3vjyuwotu7 web.1 nginx:latest manager Running Running 6 minutes ago
wr6ncubcolhx web.2 nginx:latest worker1 Running Running 39 seconds ago
xizys7b4n0cf web.3 nginx:latest worker2 Running Running 39 seconds ago
sv7sk9f3gdwn web.4 nginx:latest worker3 Running Running 39 seconds ago
w8aeu4qk3rhb web.5 nginx:latest worker1 Running Running 39 seconds ago
inl9wxvv3051 web.6 nginx:latest manager Running Running 39 seconds ago
q5nczw8oyk3m web.7 nginx:latest worker3 Running Running 39 seconds ago
708m289ztdpp web.8 nginx:latest worker3 Running Running 39 seconds ago
qr2bx5n3x7gn web.9 nginx:latest manager Running Running 39 seconds ago
4p8gz2qg2z7s web.10 nginx:latest worker2 Running Running 39 seconds ago
下面我们关掉worker2,我们可以看到,worker2上的容器被跑到了其他节点上。
网友评论