Docker Machine 学习笔记

Docker Machine 学习笔记

仅以此文,记录我学习docker-machine的过程

环境:3台cenos7系统的linux虚拟机,分别为 trainoo-1、trainoo-2、trainoo-3
目的:在 trainoo-1 安装 docker-machine 环境,然后通过 docker-machine 在trainoo-2\3 中安装 docker

首先在 trainoo-1 上安装 docker-machine 环境

步骤不赘述,详情参见官方文档

创建machine之前,要保证几台主机之间ssh能够免密登录

1
2
# 首先现在几台机器上生成ssh-key,使用 ssh-keygen -t rsa 命令生成
# 使用 ssh-copy-id root@192.168.209.102 复制SSH密钥到目标主机,开启无密码SSH登录

创建machine,此步骤可能会非常的漫长…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@trainoo-1 ~]# docker-machine create --driver generic --generic-ip-address=192.168.209.102 trainoo-2
Running pre-create checks...
Creating machine...
(trainoo-2) 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 trainoo-2

此过程中,也许会遇到下面这个问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@trainoo-1 ~]# docker-machine create --driver generic --generic-ip-address=192.168.209.103 trainoo-3
Running pre-create checks...
Creating machine...
(trainoo-3) 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...
Error creating machine: Error checking the host: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.209.103:2376": dial tcp 192.168.209.103:2376: connect: no route to host

解决办法就是,重要的事情说三遍:记得关闭防火墙!记得关闭防火墙!记得关闭防火墙!或者开放端口(不如关闭防火墙来的利索)。

1
2
3
4
5
6
7
8
9
10
11
12
[root@trainoo-3 ~]# systemctl stop firewalld
[root@trainoo-3 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2019-07-07 08:03:50 CST; 5s ago
Docs: man:firewalld(1)
Process: 675 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 675 (code=exited, status=0/SUCCESS)

[root@trainoo-3 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

最后,查看machine是否安装成功

1
2
3
4
[root@trainoo-1 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
trainoo-2 - generic Running tcp://192.168.209.102:2376 v18.09.7
trainoo-3 - generic Running tcp://192.168.209.103:2376 v18.09.7

查看 machine 的环境变量

1
2
3
4
5
6
7
[root@trainoo-1 ~]# docker-machine env trainoo-2
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.209.102:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/trainoo-2"
export DOCKER_MACHINE_NAME="trainoo-2"
# Run this command to configure your shell:
# eval $(docker-machine env trainoo-2)

尝试连接到 trainoo-2 主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@trainoo-1 ~]# eval $(docker-machine env trainoo-2)
[root@trainoo-1 ~ [trainoo-2]]#

# 题外话,此处显示 [trainoo-2] 表示连接成功
# 之所以会显示是因为安装了docker-machine的bash脚本

# 安装方法如下,执行一下shell命令:
base=https://raw.githubusercontent.com/docker/machine/v0.14.0
for i in docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash
do
sudo wget "$base/contrib/completion/bash/${i}" -P /etc/bash_completion.d
done

# 之后在 ~/.bashrc 中添加如下内容:
source /etc/bash_completion.d/docker-machine-wrapper.bash
source /etc/bash_completion.d/docker-machine-prompt.bash
source /etc/bash_completion.d/docker-machine.bash
PS1='[\u@\h \W$(__docker_machine_ps1)]\$ '

# 执行 source ~/.bashrc 使之生效
# 注意,此时执行的 docker 命令就是在 trainoo-2 主机上执行的
# 需要退出的话,重启shell终端

至此,所有主机都安装好了 docker,你可以 docker 的进行统一管理了

1
2
3
4
5
6
7
8
# 批量更新 machine 上面的 docker 版本:
docker-machine upgrade trainoo-2 trainoo-3

# 在不同 machine 之间拷贝文件:
docker-machine scp trainoo-2:/path trainoo-3:/path

# 关于 stop/start/restart 命令:
# 这几个命令都是对host生效的,而不是针对host上面的docker daemon。
作者

Trainoo

发布于

2019-07-07

更新于

2020-06-02

许可协议