docker(2) 三劍客 docker-machine安裝&管理swarm


ubuntu     系統類型

test                       docker管理操作用戶

192.168.1.73   swarm-node

192.168.1.80   swarm-master

修改使用國內鏡像

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh |sudo  sh -s http://abcd1234.m.daocloud.io

 

1、swarm-master 創建集群

1.1 切換切換活躍主機

test@bogon:~$ docker-machine ls
NAME   ACTIVE   DRIVER    STATE     URL                       SWARM   DOCKER        ERRORS
73     *        generic   Running   tcp://192.168.1.73:2376           v17.06.0-ce   
80     -        generic   Running   tcp://192.168.1.80:2376           v17.06.0-ce   
test@bogon:~$ docker-machine active 
73
test@bogon:~$ eval $(docker-machine env 80)
test@bogon:~$ docker-machine active 
80

1.2創建swarn鏡像

test@bogon:~$ docker run swarm create
Unable to find image 'swarm:latest' locally
latest: Pulling from library/swarm
248cc99c8148: Pull complete 
0aeceddbda61: Pull complete 
c90a34ed5817: Pull complete 
Digest: sha256:02040b8e8292aacb6e30be8babd613f1c253a3ec665d2fbc178d1745810fdd29
Status: Downloaded newer image for swarm:latest
Token based discovery is now deprecated and might be removed in the future.
It will be replaced by a default discovery backed by Docker Swarm Mode.
Other mechanisms such as consul and etcd will continue to work as expected.
3e2a3fa482c1994ad5ee684f120e27c4

#最后一行為CLUSTER_ID一定保留下好,下面增加節點都會用到

 1.3增加主控節點swarm-master
test@bogon:~$ docker-machine create --driver generic  --generic-ip-address=192.168.1.80   --generic-ssh-key ~/.ssh/id_rsa  --generic-ssh-user=test --swarm --swarm-master --swarm-discovery token://3e2a3fa482c1994ad5ee684f120e27c4 swarm-master
Running pre-create checks...
Creating machine...
(swarm-master) Importing SSH key...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env swarm-master
test@bogon:~$ docker-machine ls
NAME           ACTIVE   DRIVER    STATE     URL                       SWARM                   DOCKER        ERRORS
73             -        generic   Running   tcp://192.168.1.73:2376                           v17.06.0-ce   
80             *        generic   Running   tcp://192.168.1.80:2376                           v17.06.0-ce   
swarm-master   *        generic   Running   tcp://192.168.1.80:2376   swarm-master (master)   v17.06.0-ce 

1.4 增加swarm節點

#普通節點沒有--swarm-master選項

test@bogon:~$ docker-machine create --driver generic  --generic-ip-address=192.168.1.73   --generic-ssh-key ~/.ssh/id_rsa  --generic-ssh-user=test --swarm --swarm-discovery token://3e2a3fa482c1994ad5ee684f120e27c4 swarm-node1
Running pre-create checks...
Creating machine...
(swarm-node1) Importing SSH key...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env swarm-node1
test@bogon:~$ docker-machine ls
NAME           ACTIVE   DRIVER    STATE     URL                       SWARM                   DOCKER        ERRORS
swarm-master   *        generic   Running   tcp://192.168.1.80:2376   swarm-master (master)   v17.06.0-ce   
swarm-node1    -        generic   Running   tcp://192.168.1.73:2376   swarm-master            v17.06.0-ce   

1.5 查看swarm狀態

test@bogon:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
0jmswkqce9cqjvri9awdm37ty     swarm-node1         Ready               Active              
69nkaqu3ivfmty5isz0ug0oiw *   swarm-master        Ready               Active              Leader

2管理swarm

2.1退出集群

test@bogon:~$ docker swarm  leave --force
Node left the swarm.

#使用docker-machine切換環境分別在swarm-node1,swarn-master退出集群,以下僅涉及到切換環境不提供代碼參見1.1

2.2 swarm-master建立集群

test@bogon:~$ docker swarm  init
Swarm initialized: current node (69nkaqu3ivfmty5isz0ug0oiw) is now a manager.

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

    docker swarm join --token SWMTKN-1-1qj2mfe14jw2tzhg1fxpqiyhxpnm4bqhrhae0dtpp6ip9og4s3-btyj7t5304eyeuhrl4dw1vx1l 192.168.1.80:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

#同樣token很重要,不要丟失,docker代碼提示很明白,加入集群命令擺在那里

2.3 swarm-node1 加入集群

test@bogon:~$ docker swarm join --token SWMTKN-1-1qj2mfe14jw2tzhg1fxpqiyhxpnm4bqhrhae0dtpp6ip9og4s3-btyj7t5304eyeuhrl4dw1vx1l 192.168.1.80:2377
This node joined a swarm as a worker.
test@bogon:~$ eval $(docker-machine env swarm-master)
test@bogon:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
0jmswkqce9cqjvri9awdm37ty     swarm-node1         Ready               Active              
69nkaqu3ivfmty5isz0ug0oiw *   swarm-master        Ready               Active              Leader

 3 刪除節點

3.1管理節點刪除

test@swarm-master:~$ docker node ls
ID                          HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
0jmswkqce9cqjvri9awdm37ty     swarm-node1         Ready               Active              
69nkaqu3ivfmty5isz0ug0oiw *   swarm-master        Ready               Active              Leader
#刪除管理節點需要增加 --force
test@swarm-master:~$ docker swarm leave --force Node left the swarm.
#刪除完畢已經查不到swarm信息了 test@swarm
-master:~$ docker node ls Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

3.2 刪除普通節點

test@swarm-node1:~$ docker swarm leave --force
Node left the swarm.

 

 

 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM