Docker Three Musketeers --Machine

Machine

We know that in more than one cluster services environment, installation management Docker containers to be used is Docker Swarm, while using Docker Swarm is carried out in the case of multiple server clusters already be in place Docker environment. If more than one service in the environment, and not installed Docker environment, you want to quickly build a Docker host cluster, it is necessary to use this Docker Machine.

By Basic introduction above, we know Docker Machine is used to doing the. That Docker Machine is the first step in Docker responsible for the use of, so that we can quickly install Docker environment on multiple platforms, but also supports a variety of platforms, so that users can build a Docker host cluster in a short time.

Docker Docker Machine is the official open source project, is responsible for implementing the management of Docker host itself, written by the Go language, open-source address https://github.com/docker/machine.

Machine can be specified by the user on the local host Machine Docker be managed, and its operations. Docker Machine location mainly "Docker created locally or cloud environment." Its basic features are:

  • Docker engine mounted on a specified node, which is configured to host Docker
  • Centralized management of all hosts Docker

In short Docker Machine is a tool that allows you to install Docker Engine on a virtual host, and use the command to manage these docker-machine host. You can use the Machine to create Docker host on your local Mac or Windows box, corporate networks, data centers or cloud providers like AWS or Digital Ocean.

Use docker-machine command, you can start, review, stop and restart the managed host, and upgrade Docker client daemon, and configure Docker client host to communicate with your sink.

In the contents of the above, we mentioned Docker Engine (engine), now referred to here, then we have to introduce Docker Engine.

Docker Engine

When people said "Docker", here usually refers to the Docker Engine. Docker Engine application is actually a Client-Server architecture. Main shown below:

3.png

From the figure above we can see Docker Engine includes the following sections:

  • Docker Daemon - daemon docker, belonging to the C / S in the server
  • Docker REST API - docker daemon outwardly exposed REST interface
  • Docker CLI - Command Line Interface docker exposed outward (Command Line API)

Engine docker docker receiving from CLI commands, e.g. docker RUN run a container, the container docker PS can run lists, docker image ls listed mirror, and the like.

Docker Engine与Docker Machine

Docker Machine Tools is a supply and management of Docker host (host of Docker Engine).

4.png

Typically, Docker Machine installed on the local system, Docker Machine will have its own command-line client docker-machine and Docker Engine client docker.

You can use the Docker Engine Machine installed on one or more virtual systems.

These virtual systems may be local (for example, when you use the Machine VirtualBox on Mac or Windows, install and run Docker engine) or remote (such as when you use the Machine Configuration Docker hosts on the cloud provider when). These are Docker by Machine of the host itself, sometimes referred to as the host ( "machines") to be managed.

installation

Docker Machine can be installed on multiple operating system platforms, including Linux, Mac os has Windows.

Before using Machine, first of all have installed Docker, if not installed, need to be installed in advance.

Installation from the command line:

On OS X

Code
1
2
$ curl -L https://github.com/docker/machine/releases/download/v0.16.1/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine && 
chmod +x /usr/local/bin/docker-machine

On Linux

Code
1
2
3
$ curl -L https://github.com/docker/machine/releases/download/v0.16.1/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

On Windows with git bash

Code
1
2
3
$ if [[ ! -d "$HOME/bin" ]]; then mkdir -p "$HOME/bin"; fi && 
curl -L https://github.com/docker/machine/releases/download/v0.16.1/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe" &&
chmod +x "$HOME/bin/docker-machine.exe"

也可以选择下载发行版

命令

Docker Machine中每个命令都有一系列的参数,可以通过下面的命令来查看具体用法:

Code
1
docker-machine <COMMAND> -h

Docker Machine命令列表如下:

下面具体介绍一下部分命令的用法:

  1. active

    格式: docker-machine active [OPTIONS] [arg…]

    查看当前激活状态的Docker主机。激活状态意味着当前的DOCKER_HOST环境变量指向该主机。

  2. config

    格式: docker-machine config [OPTIONS] [arg…]

    查看到激活Docker主机的连接信息。

  3. create

    格式: docker-machine create [OPTIONS] [arg…]

    创建一个Docker主机。
    选项包括:

    –dirve,-d “none”指定驱动类型;
    –engine-install-url “https://get.docker.com” 配置Docker主机时候的安装URL;
    –engine-opt option 以键值对格式指定所创建Docker引擎的参数;
    –engine-insecure-registry option 以键值对格式指定所创建Docker引擎允许访问的不支持认证的注册仓库服务;
    –engine-registry-mirror option 指定使用注册仓库镜像;
    –engine-label option 为所创建的Docker引擎添加标签;
    –engine-storage-driver 存储后端驱动类型;
    –engine-env option 指定环境变量;
    –swarm 指定使用Swarm;
    –swarm-image “swarm:latest”使用Swarm时候采用的镜像;
    –swarm-master 配置集群作为Swarm集群的master节点;
    –swarm-discovery Swarm 集群的服务发现机制参数;
    –swarm-strategy “spread” Swarm默认调度策略;
    –swarm-opt option 任意传递给Swarm的参数;
    –swarm-host “tcp://0.0.0.0:3376” 指定地址将监听Swarm master节点请求;
    –swarm-addr 从指定地址发送广播加入Swarm集群服务。

    比如创建一个命名为think的主机,ip地址为47.98.40.244,指定代理镜像地址和驱动类型,如下:

    Code
    1
    $ docker-machine create --driver generic --generic-ip-address=47.98.40.244 --engine-registry-mirror=https://registry.docker-cn.com think
  4. env

    格式: docker-machine env [OPTIONS] [arg…]

    显示连接到某个主机需要的环境变量。

    比如查看think主机的环境变量,docker-machine env think

  5. inspect

    格式: docker-machine inspect [OPTIONS] [arg…]

    以json格式输出指定Docker主机的详细信息。

    使用如下命令查看think主机的详细信息。

    Code
    1
    docker-machine insepct think
  6. ip

    获取指定Docker主机地址。查看think主机的ip地址。

    Code
    1
    docker-machine ip think
  7. kill

    直接杀死指定的Docker主机。 指定Docker主机会强行停止。

  8. ls

    格式: docker-machine ls [OPTIONS] [arg…]

    列出所有管理的主机。

    可以通过–fileter来输入某些Docker主机,支持过滤器包括名称正则表达式、驱动类型、Swarm管理节点名称、状态等。

    还支持–quiet,-q减少无关输出信息。

驱动

通过 -d 选项可以选择支持的驱动类型。

  • amazonec2
  • azure
  • digitalocean
  • exoscale
  • generic
  • google
  • hyperv
  • none
  • openstack
  • rackspace
  • softlayer
  • virtualbox
  • vmwarevcloudair
  • vmwarefusion
  • vmwarevsphere

第三方驱动请到 第三方驱动列表 查看

大专栏  Docker 三剑客--Machine="headerlink" title="实战">实战

接下来我们就进入Docker Machine的实战部分,我们知道Docker Machine通过多种后端驱动来管理不同的资源,包括虚拟机、本地主机和云平台等,通过–driver可以选择支持的驱动类型。在这里我们主要分两种情况来实际应用。

  1. 本地主机

    这种情况驱动适合主机操作系统和SSH服务都已经安装好,需要部署Docker环境的要求。
    在实验环境中,我们有三个运行Ubuntu系统的主机,其中有一个安装好了Docker环境,同时安装了Docker Machine,

    我们知道对于 Docker Machine 来说,术语 Machine 就是运行 docker daemon 的主机。“创建 Machine” 指的就是在主机上安装和部署 docker。

    首先我们使用docker-machine ls查看一下当前的machine,可以看到这里并没有machine。接下来我们创建第一个 machine:think - 47.98.40.244

    创建 machine 要求能够无密码登录远程主机,所以需要先通过如下命令将 ssh key 拷贝到 39.108.119.87:

    Code
    1
    ssh-copy-id 47.98.40.244

    如果在执行过程中报错没有.pub的文件的ID,则可能是没有公共的id_rsa.pub文件。执行如下命令,然后一路回车。

    Code
    1
    ssh-keygen

    然后就会在~/.ssh目录下就会出现id_rsa.pub文件,接着重新执行上面的命令,输入服务器登录密码,就可以完成key添加的配置。

    在上面的准备工作完成后,就可以创建我们的machine,创建一个命名为think的machine。执行命令如下:

    Code
    1
    docker-machine create --driver generic --generic-ip-address=47.98.40.244 --engine-registry-mirror=https://registry.docker-cn.com think

    这里是在Linux环境中部署,直接使用Generic驱动,如果在其他环境中部署docker环境,可以使用其他驱动,详情文档参考https://docs.docker.com/machine/drivers/。

    –driver generic 为指定驱动类型,–generic-ip-address 为指定目标系统的IP,–engine-registry-mirror 为指定镜像源地址(这里更改为国内镜像源,不然后面安装容器可能无法pull镜像下来),并命名为 think。

    Docker Machine执行操作的步骤:

    1. 通过SSH登录到远程主机上。
    2. 负责在远处主机上执行安装docker步骤。
    3. 拷贝证书。
    4. 配置 docker daemon。
    5. 启动 docker 服务。

    再次执行 docker-machine ls ,可以查看到具体的相关服务

    我们可以发现创建的machine。

    同时登陆到39.108.x.x服务器,我们可以查看到docker的运行环境以及安装完毕。

    在/etc/systemd/system/docker.service.d目录下我们可以查看到具体的daemon配置文件,编辑配置文件

    在配置信息中:

    • -H tcp://0.0.0.0:2376 是使 docker daemon 接受远程连接的地址。
    • –tls* 对远程连接启用安全认证和加密的配置。

    同时我们也可以查看到hostname也已经设置为think。

    使用同样的方法,我们给120.79.x.x服务器进行安装machine。执行如下命令:

    Code
    1
    docker-machine create --driver generic --generic-ip-address=120.79.x.x  --engine-registry-mirror=https://registry.docker-cn.com dev

    在使用时,需要将ip地址更换为自己实践的服务主机地址即可。

    创建成功后使用docker-machine ls 可以查看已经部署好的环境,可以看到think、dev主机已经准备就绪了。

    目前为止两台服务器都已经部署好了Docker的环境。

    在上面我们通过machine部署好了Docker环境,接下来我们就使用docker-machine的命令对集群的docker环境进行管理。

    我们知道docker连接指定目标主机服务器,可以通过-H或者指定DOCKER_HOST来实现。比如连接39.108.119.87服务器,可以通过指DOCKER_HOST=tcp://39.108.119.87:2376,也可以-H tcp://39.108.119.87:2376 来实现访问。

    在machine中就比较方便了,使用docker-machine env think,就可以查看到think服务的配置环境。

    然后我们要切换到think服务器进行操作,根据上面的提示,执行eval $(docker-machine env think)

    我们再次执行docker-machine ls,可以发现think主机active被激活了,同时使用docker ps -a可以看到think主机上暂时并没有容器的。

    然后我们所有的操作就可以像在think主机上进行操作是一样的,比如我们要运行一个nginx容器。

    Code
    1
    docker run --name think-nginx -d -p 8080:80 nginx

    可以看到我们在think主机上运行了一个nginx容器。接着同样我们可以切换主机环境进入dev主机环境操作。

    可以看到dev主机上并没有运行什么容器。

    除此之外Docker Machine还有很多其他方便管理集群使用的命令,这里大致介绍一下。

    查看某个docker daemon的配置信息:

    Code
    1
    docker-machine config think

    还可以通过ssh命令进行连接,进入具体的主机中。比如进入think主机服务器当中,就可以使用docker-machine ssh think命令来连接。

    可以查看到我们在think主机上运行的nginx容器。

    使用docker-machine scp 我们还可以在不同 machine 之间拷贝文件,比如:

    Code
    1
    docker-machine scp [machine1:][path] [machine2:[path]

    比如我们将think主机下tmp目录下的a.txt文件拷贝到dev主机下的tmp目录下。

    比如我们将think主机下tmp目录下的a.txt文件拷贝到dev主机下的tmp目录下。

    Code
    1
    docker-machine scp think:/tmp/a.txt dev:/tmp

    可以看到我们执行了scp命令,再使用ssh连接dev主机,到tmp目录下可以发现a.txt 文件。

    docker-machine还有stop/start/restart 命令,通过stop/start/restart 我们可以控制docker主机的运行状态。

    Machine还支持更新操作,通过docker-machine upgrade 更新 machine 的 docker 到最新版本,同时支持批量执行。

    Code
    1
    docker-machine upgrade think dev

    通过上面的一些列操作,在多主机环境下 Docker Machine 可以极大地提升我们的工作效率。接下来我们在讲解一下虚拟化平台使用Docker Machine。

  2. 虚拟化平台

    可以通过virtualbox驱动支持本地(需要已安装virtualbox)启动一个虚拟机并配置为Docker主机:

    Code
    1
    docker-machine create --driver virtualbox --engine-registry-mirror=https://registry.docker-cn.com vbox

    If you do not install virtualbox will complain, install virtualbox: reference wiki or tutorial .

    Then we re-create the Machine command execution. Not surprisingly, will launch a new virtual machine and install Docker engine.

    Because the server is Ali cloud ESC does not support virtual machines in a virtual machine, so the author does not perform this step verified.

    In addition to virtualbox drive outside, docker also support other virtualization platforms.

    Management operations back to the local host consistent with the previous, not repeat here.

Use the cloud Ali

Reference: https://blog.csdn.net/bwlab/article/details/79515045

reference

  1. Docker Three Musketeers Machine Project
  2. "Intermediate articles" under linux / mac by Docker-Machine for use on Ali cloud (11)

Guess you like

Origin www.cnblogs.com/lijianming180/p/12375798.html