Docker叢集管理之swarm
阿新 • • 發佈:2019-01-02
建立swarm叢集
[[email protected] mnt]# docker swarm init --advertise-addr 172.25.0.1
Swarm initialized: current node (odiaj2zgflianqfg7opd3aeef) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-1ixb41t6ml9xiivp8ernj8bxlr73o3imu3myzldmf2z8v3aosu-2g3lkezfq6e105n2pkk2bjnj7 \
172.25.0.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[ [email protected] mnt]# docker info|grep -i swarm
Swarm: active
WARNING: bridge-nf-call-ip6tables is disabled
只用這條命令就可以建立以172.25.0.1為manager的docker叢集,–advertise-addr不是必須的,當你的計算機上有多個網絡卡時候就要指定了。
docker swarm join --token ***** ip:2377 #別的機器想要加入這個docker叢集使用上面的命令就可以了。
[[email protected] mnt]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-1ixb41t6ml9xiivp8ernj8bxlr73o3imu3myzldmf2z8v3aosu-2g3lkezfq6e105n2pkk2bjnj7 \
172.25.0.1:2377
[ [email protected] mnt]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-1ixb41t6ml9xiivp8ernj8bxlr73o3imu3myzldmf2z8v3aosu-7yj6hf32ipm7pz28yzhvzfncu \
172.25.0.1:2377
[[email protected] mnt]# docker swarm join-token manager --quiet SWMTKN-1-1ixb41t6ml9xiivp8ernj8bxlr73o3imu3myzldmf2z8v3aosu-7yj6hf32ipm7pz28yzhvzfncu
部署單節點服務
這條命令讓我們在manager節點上部署了一個Nginx容器
[[email protected] mnt]# docker service create --name vm1 nginx:1.12
gl002m0j2r5aasw41s2cgeo07
通過REPLICAS專案可以看到叢集節點的啟動狀況,”/”左面是叢集中啟動的個數,”/”右面是叢集節點的總個數。
[[email protected] mnt]# docker service ls
ID NAME MODE REPLICAS IMAGE
gl002m0j2r5a vm1 replicated 0/1 nginx:1.12
docker service ps可以看到叢集節點具體執行在哪臺機器上,執行時間等。本次是單節點叢集,我們可以看到,容器就執行在本機上。
[[email protected] mnt]# docker service ps vm1
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
yx9boa4dyrha vm1.1 nginx:1.12 foundation1.ilt.example.com Running Running about a minute ago
部署多節點叢集
[[email protected] mnt]# docker service create --replicas 3 --name vm2 nginx:1.12
ejsy730nfry2jgq9kk88awpgz
#新增–replicas 3就是代表叢集的個數變為3。manager會將容器平均分配到三個節點上
[[email protected] mnt]# docker service ls
ID NAME MODE REPLICAS IMAGE
ejsy730nfry2 vm2 replicated 3/3 nginx:1.12
可以看到三個都啟動起來了
[[email protected] mnt]# docker service ps vm2
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
jznpdoc2mpep vm2.1 nginx:1.12 foundation1.ilt.example.com Running Running about a minute ago
5jty31mz5q0i vm2.2 nginx:1.12 server1.mo.com Running Running about a minute ago
sa6hd731rvne vm2.3 nginx:1.12 server2.mo.com Running Running about a minute ago