(四) docker-machine 集群伸缩

使用docker-machine ,可以启动、审查、停止、重新启动托管的宿主机、升级docker客户端和守护程序,并配置docker客户端与宿主机通信。

安装docker-machine

If you are running macOS:

1
2
3
$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine &&
chmod +x /usr/local/bin/docker-machine

If you are running Linux:

1
2
3
4
$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&
chmod +x /usr/local/bin/docker-machine

If you are running Windows with Git BASH:

1
2
3
4
$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
mkdir -p "$HOME/bin" &&
curl -L $base/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe" &&
chmod +x "$HOME/bin/docker-machine.exe"

参考: 官方文档

下载阿里云docker-machine驱动

下载docker-machine阿里云驱动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 下载
wget https://docker-machine-drivers.oss-cn-beijing.aliyuncs.com/docker-machine-driver-aliyunecs_linux-amd64.tgz

# 解压
tar zxvf docker-machine-driver-aliyunecs_linux-amd64.tgz

# 移动
mv ./bin/docker-machine-driver-aliyunecs.linux-amd64 /usr/local/bin/docker-machine-driver-aliyunecs

# 权限
chmod +x /usr/local/bin/docker-machine-driver-aliyunecs

# 创建
docker-machine create -d aliyunecs --aliyunecs-io-optimized=optimized --aliyunecs-description=aliyunecs-machine-driver --aliyunecs-instance-type=ecs.mn4.small --aliyunecs-access-key-id='<Your access key ID>' --aliyunecs-access-key-secret='<Your secret access key>' --aliyunecs-region=cn-hangzhou --aliyunecs-ssh-password=zaq1@wsx manager

--aliyunecs-io-optimized=optimized //磁盘io优化
--aliyunecs-description=aliyunecs-machine-driver //描述
--aliyunecs-instance-type=ecs.mn4.small //实例规格
--aliyunecs-access-key-id=LTxxxcxx // key
--aliyunecs-access-key-secret=Axxx //秘钥
--aliyunecs-region=cn-hangzhou //地区
--aliyunecs-ssh-password=zaq1@wsx //ssh登录密码
–aliyunecs-image-id=centos_7_04_64_20G_alibase_201701015.vhd //镜像实例
1
2
如果创建失败可以用下面命令删除
docker-machine rm bboysoul --force

参考: docker machine介绍和使用文档

想要创建一个阿里云虚拟化示例,需要满足几个条件

  1. 账户余额大于100,因为创建的实例为按量付费
  2. 设置accesskey,需要具备操作账户的权限

操作记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@izbp1bb2egi7vzn29c1dhvz ~]# base=https://github.com/docker/machine/releases/download/v0.16.0 &&
> curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
> sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&
> chmod +x /usr/local/bin/docker-machine
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 310 0 --:--:-- 0:00:01 --:--:-- 310
100 26.8M 100 26.8M 0 0 14897 0 0:31:30 0:31:30 --:--:-- 20899
[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine -v
docker-machine version 0.16.0, build 702c267f
[root@izbp1bb2egi7vzn29c1dhvz ~]# wget https://docker-machine-drivers.oss-cn-beijing.aliyuncs.com/docker-machine-driver-aliyunecs_linux-amd64.tgz
--2019-09-25 13:17:50-- https://docker-machine-drivers.oss-cn-beijing.aliyuncs.com/docker-machine-driver-aliyunecs_linux-amd64.tgz
正在解析主机 docker-machine-drivers.oss-cn-beijing.aliyuncs.com (docker-machine-drivers.oss-cn-beijing.aliyuncs.com)... 59.110.185.69
正在连接 docker-machine-drivers.oss-cn-beijing.aliyuncs.com (docker-machine-drivers.oss-cn-beijing.aliyuncs.com)|59.110.185.69|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:3852839 (3.7M) [application/gzip]
正在保存至: “docker-machine-driver-aliyunecs_linux-amd64.tgz”

100%[=================================================================================================================================================================================>] 3,852,839 14.3MB/s 用时 0.3s

2019-09-25 13:17:50 (14.3 MB/s) - 已保存 “docker-machine-driver-aliyunecs_linux-amd64.tgz” [3852839/3852839])

[root@izbp1bb2egi7vzn29c1dhvz ~]# ls
dms-db-gateway dms-db-gateway.tar.gz docker-machine-driver-aliyunecs_linux-amd64.tgz php-7.0.1 php7.tar.gz walle-web.zip
[root@izbp1bb2egi7vzn29c1dhvz ~]# tar zxvf docker-machine-driver-aliyunecs_linux-amd64.tgz
bin/docker-machine-driver-aliyunecs.linux-amd64
[root@izbp1bb2egi7vzn29c1dhvz ~]# mv ./bin/docker-machine-driver-aliyunecs.linux-amd64 /usr/local/bin/docker-machine-driver-aliyunecs
[root@izbp1bb2egi7vzn29c1dhvz ~]# chmod +x /usr/local/bin/docker-machine-driver-aliyunecs
[root@izbp1bb2egi7vzn29c1dhvz ~]#

# 命令
docker-machine create -d aliyunecs \
--aliyunecs-access-key-id='<Your access key ID>' \
--aliyunecs-access-key-secret='<Your secret access key>' \
--aliyunecs-system-disk-size='40' \
--aliyunecs-image-id='centos_7_05_64_20G_alibase_20181210.vhd' \
--aliyunecs-io-optimized='true' \
--aliyunecs-instance-type='ecs.t5-lc2m1.nano' \
--aliyunecs-internet-max-bandwidth='1' \
--aliyunecs-region='cn-hangzhou' \
--aliyunecs-ssh-password='<Your ssh password>' \
--aliyunecs-zone='cn-hangzhou-i' \

[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine create -d aliyunecs --aliyunecs-access-key-id='<Your access key ID>' --aliyunecs-access-key-secret='<Your secret access key>' --aliyunecs-system-disk-size='40' --aliyunecs-image-id='centos_7_05_64_20G_alibase_20181210.vhd' --aliyunecs-io-optimized='true' --aliyunecs-instance-type='ecs.t5-lc2m1.nano' --aliyunecs-internet-max-bandwidth='1' --aliyunecs-region='cn-hangzhou' --aliyunecs-ssh-password='<Your ssh password>' --aliyunecs-zone='cn-hangzhou-i' manager
Running pre-create checks...
Creating machine...
(manager) manager | Creating key pair for instance ...
(manager) manager | Configuring security groups instance ...
(manager) manager | Creating instance with image centos_7_05_64_20G_alibase_20181210.vhd ...
(manager) manager | Create instance i-bp18so0vhjjsk6xyynep successfully
(manager) manager | Allocate publice IP address 47.99.236.50 for instance i-bp18so0vhjjsk6xyynep successfully
(manager) manager | Starting instance i-bp18so0vhjjsk6xyynep ...
(manager) manager | Start instance i-bp18so0vhjjsk6xyynep successfully
(manager) manager | Waiting SSH service 47.99.236.50:22 is ready to connect ...
(manager) manager | Uploading SSH keypair to 47.99.236.50:22 ...
(manager) manager | Created instance i-bp18so0vhjjsk6xyynep successfully with public IP address 47.99.236.50 and private IP address 172.16.142.104
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 centos...
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 virtual machine, run: docker-machine env manager

ECS实例图示

补充

杭州镜像创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
docker-machine create -d aliyunecs \
--aliyunecs-access-key-id='<Your access key ID>' \
--aliyunecs-access-key-secret='<Your secret access key>' \
--aliyunecs-system-disk-size='40' \
--aliyunecs-image-id='centos_7_05_64_20G_alibase_20181210.vhd' \
--aliyunecs-io-optimized='true' \
--aliyunecs-instance-type='ecs.t5-lc2m1.nano' \
--aliyunecs-internet-max-bandwidth='1' \
--aliyunecs-region='cn-hangzhou' \
--aliyunecs-ssh-password='<Your ssh password>' \
--aliyunecs-zone='cn-hangzhou-i' \
worker1

--aliyunecs-zone ECS_ZONE - 可用区id,这个我没有找到,后来是用阿里云openapi explorer查出来的


https://api.aliyun.com/?spm=a2c4g.11186623.2.8.2a6f7f49RdvJPV#/?product=Ecs&api=DescribeZones&params={%22RegionId%22:%22cn-hangzhou%22}&tab=DEMO&lang=JAVA

--aliyunecs-image-id ECS_IMAGE_ID - ecs镜像id,我找这个找了很久,因为找的是公共镜像,公共镜像id在控制台,云服务器ECS,镜像,公共镜像里有

删除服务器

1
2
3
4
5
6
7
8
9
10
11
[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager - aliyunecs Running tcp://139.224.56.70:2376 v1.12.6
worker1 - aliyunecs Running tcp://121.41.65.234:2376 v19.03.5
[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine rm manager
About to remove manager
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
(manager) manager | Remove instance i-uf6afru7j8ieppzodrls ...
(manager) manager | Deleting instance: i-uf6afru7j8ieppzodrls
Successfully removed manager

常用操作

链接服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine env manager
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://139.224.56.70:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/manager"
export DOCKER_MACHINE_NAME="manager"
# Run this command to configure your shell:
# eval $(docker-machine env manager)

[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine ssh manager
Last login: Mon Nov 25 14:11:18 CST 2019 from 47.96.181.251 on ssh

Welcome to Alibaba Cloud Elastic Compute Service !

manager ~ #

查看主机配置信息

1
2
3
docker-machine config [options]

docker-machine config worker1

挂载共享目录

1
2
3
4
5
yum -y install fuse-sshfs

[machine:][path] [mountpoint]

docker-machine mount worker1:/root/test test

sshfs可以把远程主机的文件系统映射到本地主机上,透过ssh把远程文件系统挂载到本机上,这样可以不必使用scp工具就可以做到直接复制及删除远程主机的文件了。就像操作本地磁盘一样方便。
注意如果挂载的文件不为空,会提示让你设置参数,但是docker-machine mount 是不支持的

下面这种方式可以,但是会将现有文件的内容清空,谨慎使用
sshfs root@121.xxx.xxx.187:/root/worker1 worker1 -o nonempty

docker-machine操作步骤

1
2
3
4
5
6
7
8
9
10
[root@izbp1bb2egi7vzn29c1dhvz ~]# yum install fuse-sshfs -y

[root@izbp1bb2egi7vzn29c1dhvz ~]# mkdir data
[root@izbp1bb2egi7vzn29c1dhvz ~]# pwd
/root
[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine mount manager:/data ./data
[root@izbp1bb2egi7vzn29c1dhvz ~]# cd data/
[root@izbp1bb2egi7vzn29c1dhvz data]# touch remark.md
[root@izbp1bb2egi7vzn29c1dhvz data]# cat remark.md
挂载成功

docker-manager操作步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@izbp1bb2egi7vzn29c1dhvz ~]# docker-machine ssh manager
Last login: Mon Nov 25 16:10:53 2019 from 47.96.181.251

Welcome to Alibaba Cloud Elastic Compute Service !

[root@manager ~]# ll
total 0
[root@manager ~]# mkdir /data
[root@manager ~]# vim /data/remark.md
挂载成功

[root@manager ~]# cat /data/remark.md
挂载成功

取消挂载 -u
docker-machine mount -u manager:/data ./data
一个目录只能被挂载到一台机器上

图示

拷贝文件 SCP

1
2
3
4
docker-machine scp -r host1:/tmp/a(源地址) host2:/tmp/b(目标地址)

-r 递归目录拷贝
-d 大文件增量拷贝