Docker Machine简介

Machine lets you create Docker hosts on your computer, on cloud providers, and inside your own data center. It creates servers, installs Docker on them, then configures the Docker client to talk to them.


安装 Docker Machine

安装 Docker Machine 前需要在本地先安装Docker

1
2
3
4
# curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine
# chmod +x /usr/local/bin/docker-machine
# docker-machine version
docker-machine version 0.12.2, build 9371605

在远程主机上安装 Docker

前提条件

创建machine要求能够无密码登录远程主机,所以需要先与远程主机做ssh的信任。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
80:90:63:7e:85:5c:f2:1a:e2:ec:bf:8d:09:8e:ba:d2 root@swarm-node1
The key's randomart image is:
+--[ RSA 2048]----+
| .o.o. |
| +.o+. |
| o..o.o |
| o...o . |
| o.. S |
| . |
| .o |
|.oEo + |
|*.. =.. |
+-----------------+
# ssh-copy-id 172.16.100.21

docker-machine创建过程:

  1. 通过指定的driver创建远程主机(此处已预先创建好主机,使用generic driver,可省略)
  2. 通过 ssh 登录到远程主机。
  3. 安装Docker Engine。
  4. 拷贝证书。
  5. 配置 Docker daemon。
  6. 启动 Docker daemon。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # docker-machine create -d generic --generic-ip-address=172.16.100.21 swarm-node1
    # docker-machine create -d generic --generic-ip-address=172.16.100.22 swarm-node2
    Running pre-create checks...
    Creating machine...
    (swarm-node2) No SSH key specified. Assuming an existing key at the default location.
    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 swarm-node2

创建machine需要通过"--driver"声明使用什么驱动程序来创建目标主机。
Docker Machine 支持在云服务器上创建主机,就是通过不同的驱动实现了。
以上由于主机已经创建完成,只需在主机上部署Docker服务,则使用generic的driver即可。

管理远程的Docker daemon

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
# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
swarm-node1 - generic Running tcp://172.16.100.21:2376 v17.06.0-ce
swarm-node2 - generic Running tcp://172.16.100.22:2376 v17.06.0-ce
# docker-machine env swarm-node2
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.16.100.21:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/swarm-node2"
export DOCKER_MACHINE_NAME="swarm-node2"
# Run this command to configure your shell:
# eval $(docker-machine env swarm-node2)

`执行以下命令即可使docker客户端连接到指定的远程主机的Docker daemon`
# eval $(docker-machine env swarm-node1)
# env |grep DOCKER
DOCKER_HOST=tcp://172.16.100.21:2376
DOCKER_MACHINE_NAME=swarm-node1
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/root/.docker/machine/machines/swarm-node1
# eval $(docker-machine env swarm-node2)
# env |grep DOCKER
DOCKER_HOST=tcp://172.16.100.22:2376
DOCKER_MACHINE_NAME=swarm-node2
DOCKER_TLS_VERIFY=1
DOCKER_CERT_PATH=/root/.docker/machine/machines/swarm-node2

# eval $(docker-machine env -u)
# env |grep DOCKER
坚持原创技术分享,您的支持将鼓励我继续创作!
0%