使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本

 

说明:

昨天,写了一篇《Docker Swarm 集群创建+Portainer 图形化管理实验》博客,地址为:http://blog.csdn.net/csdn_duomaomao/article/details/73381277 主要是通过手工实现的,解决 Docker 初学者创建Swarm遇到的麻烦问题,但毕竟是手工的方式,今天父亲节,刚好在家有空,整了一篇自动创建的脚本,希望对大家学习Docker有帮助。

 

脚本:

# 初始设置M台管理节点,W台工作节点,其中manager1leader节点.

M=3

W=2

 

# 创建MDocker虚拟机,作为管理节点

for i in $(seq 1 $M)

do

docker-machine create \

--driver virtualbox \

--engine-registry-mirror=https://registry.docker-cn.com \

manager$i

done

 

#创建WDocker虚拟机,作为工作节点

for j in $(seq 1 $W)

do

docker-machine create \

--driver virtualbox \

--engine-registry-mirror=https://registry.docker-cn.com \

worker$j

done

 

#切换 manager1 ,创建 Swarm 集群

eval $(docker-machine env manager1)

docker swarm init --advertise-addr $(docker-machine ip manager1):2377

 

 

# 获取两个令牌变量

# 切换到主控节点

eval $(docker-machine env manager1)

 

#显示管理节点加入集群的信息,截取其中的令牌作为变量,便于后面使用

T1=`docker swarm join-token manager|grep token`

TM=${T1:12:85}

 

#显示工作节点加入集群的信息,截取其中的令牌作为变量,便于后面使用

T2=`docker swarm join-token worker|grep token`

TW=${T2:12:85}

# 两个令牌TMTW变量获取完毕

 

 

# 通过循环命令,把其他的控制节点都加入到集群中

for i in $(seq 2 $M)

do

eval $(docker-machine env manager$i)

docker swarm join \

--token $TM \

$(docker-machine ip manager1):2377

done

 

# 通过循环命令,把其他的工作节点都加入到集群中

for i in $(seq 1 $W)

do

eval $(docker-machine env worker$i)

docker swarm join \

--token $TW \

$(docker-machine ip manager1):2377

done

 

# 在主控节点上部署 Portainer 服务,图形化的 Docker 管理平台

eval $(docker-machine env manager1)

docker service create \

--name portainer \

--publish 9000:9000 \

--constraint 'node.role == manager' \

--mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \

portainer/portainer \

-H unix:///var/run/docker.sock

 

# 在主控节点上部署 visualizer 服务,图形化的方式查看集群运行情况

eval $(docker-machine env manager1)

docker service create \

--name=viz \

--publish=8088:8080/tcp \

--constraint=node.role==manager \

--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \

manomarks/visualizer

 

# 尝试在机器中部署一个测试服务 httpd ,检查集群是否能正常使用.

# 因为要从网络上下载httpd 镜像,可能要等1-几分钟,有加速器会很快,5分钟

eval $(docker-machine env manager1)

docker service create --name httpd --publish 80:80 --replicas=6 httpd

 

echo 访问 Portainer  的方法: http://$(docker-machine ip manager1):9000

echo 访问 Visualizer 的方法: http://$(docker-machine ip manager1):8088

echo 访问  测试网站  的方法: http://$(docker-machine ip manager1) 或者集群中的任何节点的IP地址

 

 

# 日期:2017618

# 电邮:[email protected]

# 环境:Win7 64+DockerToolbox(v17.05.0-ce)

# 结束

===============================================

 

 使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本_第1张图片使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本_第2张图片使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本_第3张图片使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本_第4张图片使用 Docker Toolbox 自动创建 Swarm 集群+Portainer 图形化管理的脚本_第5张图片

 

 

 

 

 

 

 

 

 

=============================

#以上脚本运行的屏幕显示信息如下:

 

Running pre-create checks...

Creating machine...

(manager1) Copying C:\Users\catty\.docker\machine\cache\boot2docker.iso to C:\Us

ers\catty\.docker\machine\machines\manager1\boot2docker.iso...

(manager1) Creating VirtualBox VM...

(manager1) Creating SSH key...

(manager1) Starting the VM...

(manager1) Check network to re-create if needed...

(manager1) Waiting for an IP...

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 boot2docker...

Copying certs to the local machine directory...

Copying certs to the remote machine...

Setting Docker configuration on the remote daemon...

Checking connection to Docker...

Docker is up and running!

To see how to connect your Docker Client to the Docker Engine running on this vi

rtual machine, run: C:\DockerToolbox\docker-machine.exe env manager1

Running pre-create checks...

Creating machine...

(manager2) Copying C:\Users\catty\.docker\machine\cache\boot2docker.iso to C:\Us

ers\catty\.docker\machine\machines\manager2\boot2docker.iso...

(manager2) Creating VirtualBox VM...

(manager2) Creating SSH key...

(manager2) Starting the VM...

(manager2) Check network to re-create if needed...

(manager2) Waiting for an IP...

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 boot2docker...

Copying certs to the local machine directory...

Copying certs to the remote machine...

Setting Docker configuration on the remote daemon...

Checking connection to Docker...

Docker is up and running!

To see how to connect your Docker Client to the Docker Engine running on this vi

rtual machine, run: C:\DockerToolbox\docker-machine.exe env manager2

Running pre-create checks...

Creating machine...

(manager3) Copying C:\Users\catty\.docker\machine\cache\boot2docker.iso to C:\Us

ers\catty\.docker\machine\machines\manager3\boot2docker.iso...

(manager3) Creating VirtualBox VM...

(manager3) Creating SSH key...

(manager3) Starting the VM...

(manager3) Check network to re-create if needed...

(manager3) Waiting for an IP...

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 boot2docker...

Copying certs to the local machine directory...

Copying certs to the remote machine...

Setting Docker configuration on the remote daemon...

Checking connection to Docker...

Docker is up and running!

To see how to connect your Docker Client to the Docker Engine running on this vi

rtual machine, run: C:\DockerToolbox\docker-machine.exe env manager3

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ #创建WDocker虚拟机,作为工作节点

 

catty@15FD201 MINGW64 ~

$ for j in $(seq 1 $W)

> do

> docker-machine create \

> --driver virtualbox \

> --engine-registry-mirror=https://registry.docker-cn.com \

> worker$j

> done

Running pre-create checks...

Creating machine...

(worker1) Copying C:\Users\catty\.docker\machine\cache\boot2docker.iso to C:\Use

rs\catty\.docker\machine\machines\worker1\boot2docker.iso...

(worker1) Creating VirtualBox VM...

(worker1) Creating SSH key...

(worker1) Starting the VM...

(worker1) Check network to re-create if needed...

(worker1) Waiting for an IP...

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 boot2docker...

Copying certs to the local machine directory...

Copying certs to the remote machine...

Setting Docker configuration on the remote daemon...

Checking connection to Docker...

Docker is up and running!

To see how to connect your Docker Client to the Docker Engine running on this vi

rtual machine, run: C:\DockerToolbox\docker-machine.exe env worker1

Running pre-create checks...

Creating machine...

(worker2) Copying C:\Users\catty\.docker\machine\cache\boot2docker.iso to C:\Use

rs\catty\.docker\machine\machines\worker2\boot2docker.iso...

(worker2) Creating VirtualBox VM...

(worker2) Creating SSH key...

(worker2) Starting the VM...

(worker2) Check network to re-create if needed...

(worker2) Waiting for an IP...

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 boot2docker...

Copying certs to the local machine directory...

Copying certs to the remote machine...

Setting Docker configuration on the remote daemon...

Checking connection to Docker...

Docker is up and running!

To see how to connect your Docker Client to the Docker Engine running on this vi

rtual machine, run: C:\DockerToolbox\docker-machine.exe env worker2

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ #切换 manager1 ,创建 Swarm 集群

 

catty@15FD201 MINGW64 ~

$ eval $(docker-machine env manager1)

 

catty@15FD201 MINGW64 ~

$ docker swarm init --advertise-addr $(docker-machine ip manager1):2377

Swarm initialized: current node (pc80navfg4xpz231611tlerqo) is now a manager.

 

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

 

    docker swarm join \

    --token SWMTKN-1-4y3b6md725lsk4udkme70rrj64q1hn9uc9249lwzct2ltvq22w-bmreaixp

v5fuiczflplkpkffi \

    192.168.99.122:2377

 

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

 the instructions.

 

catty@15FD201 MINGW64 ~

$ # 获取两个令牌变量

 

catty@15FD201 MINGW64 ~

$ # 切换到主控节点

 

catty@15FD201 MINGW64 ~

$ eval $(docker-machine env manager1)

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ #显示管理节点加入集群的信息,截取其中的令牌作为变量,便于后面使用

 

catty@15FD201 MINGW64 ~

$ T1=`docker swarm join-token manager|grep token`

 

catty@15FD201 MINGW64 ~

$ TM=${T1:12:85}

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ #显示工作节点加入集群的信息,截取其中的令牌作为变量,便于后面使用

 

catty@15FD201 MINGW64 ~

$ T2=`docker swarm join-token worker|grep token`

 

catty@15FD201 MINGW64 ~

$ TW=${T2:12:85}

 

catty@15FD201 MINGW64 ~

$ # 两个令牌变量获取完毕

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ # 通过循环命令,把其他的控制节点都加入到集群中

 

catty@15FD201 MINGW64 ~

$ for i in $(seq 2 $M)

> do

> eval $(docker-machine env manager$i)

> docker swarm join \

> --token $TM \

> $(docker-machine ip manager1):2377

> done

This node joined a swarm as a manager.

This node joined a swarm as a manager.

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ # 通过循环命令,把其他的工作节点都加入到集群中

 

catty@15FD201 MINGW64 ~

$ for i in $(seq 1 $W)

> do

> eval $(docker-machine env worker$i)

> docker swarm join \

> --token $TW \

> $(docker-machine ip manager1):2377

> done

This node joined a swarm as a worker.

This node joined a swarm as a worker.

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ # 在主控节点上部署 Portainer 服务,图形化的 Docker 管理平台

 

catty@15FD201 MINGW64 ~

$ eval $(docker-machine env manager1)

 

catty@15FD201 MINGW64 ~

$ docker service create \

> --name portainer \

> --publish 9000:9000 \

> --constraint 'node.role == manager' \

> --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \

> portainer/portainer \

> -H unix:///var/run/docker.sock

s31los8s1ohbprodq8ayil0gq

Since --detach=false was not specified, tasks will be created in the background.

In a future release, --detach=false will become the default.

 

catty@15FD201 MINGW64 ~

$ # 在主控节点上部署 visualizer 服务,图形化的方式查看集群运行情况

 

catty@15FD201 MINGW64 ~

$ eval $(docker-machine env manager1)

 

catty@15FD201 MINGW64 ~

$ docker service create \

> --name=viz \

> --publish=8088:8080/tcp \

> --constraint=node.role==manager \

> --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \

> manomarks/visualizer

mios135cu8elr778mfa4b45z2

Since --detach=false was not specified, tasks will be created in the background.

In a future release, --detach=false will become the default.

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ # 尝试在机器中部署一个测试服务 httpd ,检查机器是否能正常使用.

 

catty@15FD201 MINGW64 ~

$ # 因为要从网络上下载httpd 镜像,可能要等1-几分钟,有加速器会很快,5分钟

 

catty@15FD201 MINGW64 ~

$ eval $(docker-machine env manager1)

 

catty@15FD201 MINGW64 ~

$ docker service create --name httpd --publish 80:80 --replicas=6 httpd

cztglh24t739in4exb5mbv72q

Since --detach=false was not specified, tasks will be created in the background.

In a future release, --detach=false will become the default.

 

catty@15FD201 MINGW64 ~

$

 

catty@15FD201 MINGW64 ~

$ echo 访问 Portainer  的方法: http://$(docker-machine ip manager1):9000

访问 Portainer 的方法: http://192.168.99.122:9000

 

catty@15FD201 MINGW64 ~

$ echo 访问 Visualizer 的方法: http://$(docker-machine ip manager1):8088

访问 Visualizer 的方法: http://192.168.99.122:8088

 

catty@15FD201 MINGW64 ~

$ echo 访问  测试网站  的方法: http://$(docker-machine ip manager1) 或者集群中

的任何节点的IP地址

访问 测试网站 的方法: http://192.168.99.122 或者集群中的任何节点的IP地址

 

catty@15FD201 MINGW64 ~

$ # 结束

 

catty@15FD201 MINGW64 ~

$ # 2017618

 

catty@15FD201 MINGW64 ~

$ # [email protected]

 

 

 

 

你可能感兴趣的:(Docker管理,Docker入门,Docker集群,软件编程)