「Docker Machine」- 快速创建运行 Docker 服务的主机

  CREATED BY JENKINSBOT

问题描述

在 GitLab CI 中,我们发现有种 Executor 为 Docker Machine 类型。因此,我们开始了解 Docker Machine 相关的内容,学习它的功能及使用场景,思考它能否解决我们的问题。

该笔记将记录:在 Docker 中,使用 Dcoker Machine 的方法,以及常见问题处理。

解决方案

通过 Docker Machine 工具,能够快速创建运行 Docker 服务的主机。为了快速理解 Docker Machine 的作用,请查看如下示例:

第一步、安装 Docker Machine 命令

它是个工具,因此我们需要安装相关的命令(我们 Linux 环境):

# 下载命令
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

# 配置 Bash 补全
base=https://raw.githubusercontent.com/docker/machine/v0.16.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

第二步、使用 Docker Mainche 命令

// 在首次执行时,什么都没有
// 毕竟是第一次执行,我们还没有创建任何东西

# docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS

// 执行如下命令,将在本地的 Virtolbox 中创建虚拟机
// 该虚拟机实例中运行 Docker 服务

# docker-machine create --driver virtualbox default
Running pre-create checks...
Creating machine...
(staging) Copying /Users/ripley/.docker/machine/cache/boot2docker.iso to /Users/ripley/.docker/machine/machines/default/boot2docker.iso...
(staging) Creating VirtualBox VM...
(staging) Creating SSH key...
(staging) Starting the VM...
(staging) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
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 Docker to this machine, run: docker-machine env default

// 当我们再次查看时,将看到我们创建的实例

# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER   ERRORS
default   *        virtualbox   Running   tcp://192.168.99.187:2376           v1.9.1

// 为了访问我们新创建的实例,需要设置环境变量

# docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.16.62.130:2376"
export DOCKER_CERT_PATH="/Users/<yourusername>/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval "$(docker-machine env default)"

// 接下来,我们便可以直接使用 docker 命令,以在该实例中创建容器

# docker run busybox echo hello world
Unable to find image 'busybox' locally
Pulling repository busybox
e72ac664f4f0: Download complete
511136ea3c5a: Download complete
df7546f9f060: Download complete
e433a6c5b276: Download complete
hello world

Docker Machine 就是这么个工具。

当然,除了 VirtualBox 之外,选项 –driver virtualbox 能够替换为 –driver vmwarefusion / –driver aliyunecs 等等,参考 docker.github.io/AVAILABLE_DRIVER_PLUGINS.md 页面,以在不同的环境中快速部署创建运行 Docker 服务的云主机实例。

如下示例,使用已有主机

docker-machine --debug \
    create --driver generic \
    --generic-ip-address 172.31.253.10 \
    --generic-ssh-key ~/.ssh/id_rsa \
    --generic-ssh-port 22 \
    --generic-ssh-user root \
    b150m-d3v

我们开启 debug 选项,以查看整个安装过程执行的命令,以排查错误。

正如官方文档的描述,Docker Machine 的 Generic 驱动会完成如下动作:
1)自动安装 Docker 服务;
2)更新节点的包(apt-get update, yum update)
3)使用证书来安全 Docker 服务;
4)如果使用 systemd 则将创建 /etc/systemd/system/docker.service.d/10-machine.conf 配置;
5)重启 Docker 服务,所以正在运行的容器会停止;
6)更新主机名。在上面的示例中,主机名将被修改为 b150m-d3v;

常见问题处理

Unable to verify the Docker daemon is listening: Maximum number of retries (10) exceeded

问题描述:在使用 docker-machine create –driver generic 驱动时,最后产生该错误:

# docker-machine create --driver generic --generic-ip-address 172.31.253.10 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-port 22 --gener                                                                               ic-ssh-user root dm-b150m-d3v                                                                                                                                                                                                                  
Creating CA: /home/k4nz/.docker/machine/certs/ca.pem                                                                                                                                                                                           
Creating client certificate: /home/k4nz/.docker/machine/certs/cert.pem                                                                                                                                                                         
Running pre-create checks...                                                                                                                                                                                                                   
Creating machine...                                                                                                                                                                                                                         
(dm-b150m-d3v) 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...                                                                                                                                                                                           
Error creating machine: Error running provisioning: Unable to verify the Docker daemon is listening: Maximum number of retries (10) exceeded

# docker-machine ls                                                                                                                                                                                                                            
NAME           ACTIVE   DRIVER    STATE     URL                        SWARM   DOCKER    ERRORS                                                                                                                                                
dm-b150m-d3v   -        generic   Running   tcp://172.31.253.10:2376           Unknown   Unable to query docker version: Cannot connect to the docker engine end

原因分析:通过 –debug 选项,我们发现问题所在:docker-machine 在安装 Docker 服务之后,Docker 服务便启动。然后再写入 10-machine.conf 配置,并使用 systemctl -f start 来启动服务,但是这并不会生效。导致后面使用 ss 检测端口是否存在时失败。

解决方案:到主机中,手动重启 dokcer.service 服务。

补充说明

作为知识储备吧,目前我们还没有需要使用 Docker Machine 工具的场景。

在 GitLab CI 中,能够配置 Docker Machine 实现 GitLab Runner 节点的动态扩展。但是 Docker Machine 会操纵 Docker 服务,而非我们想象的单纯在远程主机中运行容器,因此,我们还无法将其运用在 GitLab 中。

参考文献

Docker Machine overview | Docker Documentation
docker-machine create | Docker Documentation
Install Docker Machine | Docker Documentation
docker-machine “generic” changes the server’s hostname · Issue #3696 · docker/machine
Generic | Docker Documentation