一、什么是Docker Machine
Docker Machine 是Docker官方编排项目之一,使用go语言编写的,使用不同引擎在多种平台上快速的安装Docker环境,开源地址:https://github.com/docker/machine。
Docker Machine 是一个工具,它允许你在虚拟宿主机上安装Docker,并使用docker-machine命令管理这个宿主机,可以使用Docker Machine在本地的MAC或者windows box、公司网络,数据中心或者AWS这样的云提供商上创建docker。
Docker Machine 是一个用于配置和管理你的宿主机(上面具有 Docker Engine 的主机)的工具。通常,你在你的本地系统上安装 Docker Machine。Docker Machine有自己的命令行客户端 docker-machine 和 Docker Engine 客户端 docker。你可以使用 Machine 在一个或多个虚拟系统上安装 Docker Engine。
使用docker-machine命令,可以启动、审查、停止、重启托管的docker 也可以升级Docker客户端和守护程序并配置docker客户端和宿主机通信。
Docker Machine 也可以集中管理所以得docker主机。
二 、安装
Docker Mechine 可以在多种平台上安装使用,包括Linux 、MacOS已经windows
Docker Mechine 安装非常的简单:GitHub地址:https://github.com/docker/machine/releases/ 里面有安装教程(在写这篇文章的时候最新版本是v0.16.2)
安装 Docker Mechin
[root@localhost ~]# curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
> chmod +x /tmp/docker-machine &&
> sudo cp /tmp/docker-machine /usr/local/bin/docker-machine #安装
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 100 617 0 0 268 0 0:00:02 0:00:02 --:--:-- 268
100 32.6M 100 32.6M 0 0 296k 0 0:01:52 0:01:52 --:--:-- 126k
[root@localhost ~]# docker-machine -v #查看版本确认是否安装成功
docker-machine version 0.16.2, build bd45ab13
安装自动补全功能
[root@operation ~]# yum -y install bash-completion
[root@operation ~]# scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do wget https://raw.githubusercontent.com/docker/machine/v0.16.2/contrib/completion/bash/${i} -P /etc/bash_completion.d; done
# 添加以下
[root@operation ~]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
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)]\$ '
# 使之生效
[root@operation ~]# source ~/.bashrc
docker-machine就安装完成了
三 参数
支持命令
命令 | 说明 |
active | 查看当前激活状态的Docker主机 |
config | 查看当前激活状态Docker主机的连接信息 |
creat | 创建Docker主机 |
env | 显示连接到某个主机需要的环境变量 |
inspect | 以json格式输出指定Docker的详细信息 |
ip | 获取指定Docker主机的地址 |
kill | 直接杀死指定的Docker主机 |
ls | 列出所有的管理主机 |
provision | 重新配置指定主机 |
regenerate-certs | 为某个主机重新生成TLS信息 |
restart | 重启指定的主机 |
rm | 删除某台Docker主机,对应的虚拟机也会被删除 |
ssh | 通过SSH连接到主机上,执行命令 |
scp | 在Docker主机之间以及Docker主机和本地主机之间通过scp远程复制数据 |
mount | 使用SSHFS从计算机装载或卸载目录 |
start | 启动一个指定的Docker主机,如果对象是个虚拟机,该虚拟机将被启动 |
status | 获取指定Docker主机的状态(包括:Running、Paused、Saved、Stopped、Stopping、Starting、Error)等 |
stop | 停止一个指定的Docker主机 |
upgrade | 将一个指定主机的Docker版本更新为最新 |
url | 获取指定Docker主机的监听URL |
version | 显示Docker Machine的版本或者主机Docker版本 |
help | 显示帮助信息 |
支持的平台及驱动引擎
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 |
|
说白了都是虚拟化平台和云平台的驱动文件
四、使用说明
docker-machine |
192.168.1.118 | 安装docker machine的机器 |
host1 |
192.168.1.119 | 被管理的机器 host1 |
host2 |
192.168.1.120 | 被管理的机器 host2 |
首先所有节点关闭防火墙,不然在创建的时候会报错如下:
Error creating machine: Error checking the host: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.1.119:2376": dial tcp 192.168.1.119:2376: connect: no route to host
创建步骤:
1、docker-machine上操作
先执行 docker-machine ls
查看一下当前的 machine:
(1)配置主机间的SSH免密(在192.168.1.118上面创建)
# 生成keys并配置可以免密登录主机(这个是必须要做的)
[root@machine ~]# systemctl stop firewalld 关闭防火墙
[root@machine ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@machine ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
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:
SHA256:inmguzQ0UnrTAeAWJl68emQc8zm7/3JpvYs3GXcdQf8 root@machine
The key's randomart image is:
+---[RSA 2048]----+
|o+o. .. |
|= o= ..|
| +o * . o|
|.o * = ..|
|o O o o S E|
| = = = . . . ..|
| = o + o + . |
| . o o . +.= |
| o. ..=o.o+ |
+----[SHA256]-----+
# 将keys拷贝到client1上去
[root@machine ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
# 测试是否可以免密登录
[root@machine ~]# ssh [email protected]
Last login: Thu Mar 5 14:58:53 2020 from 192.168.1.118
[root@host1 ~]#
(2)使用docker machine创建docker host
# 使用docker machine 创建
# 对于docker machine来将,术语Machine就是运行docker daemon的主机,创建machine就是在host上安装docker
# 执行docker-macine ls查看当前的machine
[root@machine ~] docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
[root@machine ~]#
# 当前还没有一个machine,接下来我们创建第一个machine:host1-192.168.1.119
[root@machine ~]docker-machine create --driver generic --generic-ip-address=192.168.1.119 host1
Running pre-create checks...
Creating machine...
(host1) 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 host1
[root@machine ~]
按照以上的方式添加host2
docker-machine create --driver generic --generic-ip-address=192.168.1.120 host2
# 创建成功执行ls查看
[root@machine ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
host1 - generic Running tcp://192.168.1.119:2376 v19.03.7
host2 - generic Running tcp://192.168.1.120:2376 v19.03.7
# 登录到host1或host2查看配置项
[root@machine ~]# ssh [email protected]
Last login: Thu Mar 5 16:33:47 2020 from 192.168.1.118
[root@host1 ~]# cat /etc/systemd/system/docker.service.d/10-machine.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver overlay2 --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=generic
Environment=
[root@host1 ~]#
注:-H tcp:
//0
.0.0.0:2376 使docker daemon接受远程连接
--tlscacert对远程连接启用安全认证和加密
注:大家可能会发现这里的主机名变成了host1 原因就是docker-machine创建的时候会把主机名也一起修改
# 查看host1的环境变量
[root@machine ~]# docker-machine env host1
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.1.119:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/host1"
export DOCKER_MACHINE_NAME="host1"
# Run this command to configure your shell:
# eval $(docker-machine env host1)
[root@machine ~]#
# 根据提示执行
[root@machine ~]# eval $(docker-machine env host1)
[root@machine ~ [host1]]#
可以看到,命令提示符变成了host1,其原因是我们之前在
/root/
.bashrc里面配置了 PS1=
'[\u@\h \W$(__docker_machine_ps1)]\$ '
,用于显示当前的docker host
注:如果我们输入
eval
$(docker-machine
env
docker1)没有显示出host1的命令提示符,我们可以重新输入一遍 PS1=
'[\u@\h \W$(__docker_machine_ps1)]\$ '
在此状态下执行的docker命令其效果都相当于在host1上执行
[root@machine ~ [host1]]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@machine ~ [host1]]#
[root@machine ~ [host1]]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@machine ~ [host1]]#
(3). 激活主机
通过docker-machine ls 发现有主机但是没激活(docker-machine active 输出No active host found)
# active命令
[root@machine ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
host1 - generic Running tcp://192.168.1.119:2376 v19.03.7
host2 - generic Running tcp://192.168.1.120:2376 v19.03.7
[root@machine ~]# docker-machine active
No active host found
[root@machine ~]#
#这里的host1是未激活状态 ACTIVE下面是 - 激活变为 *
[root@machine ~]# docker-machine env host1
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.1.119:2376" #激活主机
export DOCKER_CERT_PATH="/root/.docker/machine/machines/host1"
export DOCKER_MACHINE_NAME="host1"
# Run this command to configure your shell:
# eval $(docker-machine env host1)
[root@machine ~]# export DOCKER_HOST="tcp://192.168.1.119:2376" #用这个命令激活主机
[root@machine ~]# docker-machine active
host1
[root@machine ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
host1 * generic Running tcp://192.168.1.119:2376 v19.03.7
host2 - generic Running tcp://192.168.1.120:2376 v19.03.7
# host1 已经激活 ,host2没有激活 ,可以采用以上的方式激活
(4)其他命令
# 其他命令
# create 命令
选项包括:
·--driver,-d"none" 指定驱动类型;
·--engine-install-url"https://get.docker.com" 配置Dokcer主机时候的安装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集群服务。
实例:
docker-machine create -d virtualbox \
--engine-storage-driver overlay \
--engine-label name=testmachine \
--engine-label year=2018 \
--engine-opt dns=8.8.8.8 \
--engine-env HTTP_PROXY=http://proxy.com:3128 \
--engine-insecure-registry registry.private.com \
mydockermachine
#config命令
[root@machine ~]# docker-machine config host1
--tlsverify
--tlscacert="/root/.docker/machine/machines/host1/ca.pem"
--tlscert="/root/.docker/machine/machines/host1/cert.pem"
--tlskey="/root/.docker/machine/machines/host1/key.pem"
-H=tcp://192.168.1.119:2376
[root@machine ~]#
# inspect命令
# ssh命令
[root@machine ~]# docker-machine ssh host1 docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@machine ~]#
[root@machine ~]# docker-machine ssh host1
Last login: Thu Mar 5 17:48:52 2020 from 192.168.1.118
[root@host1 ~]#
# url命令
[root@machine ~]# docker-machine url host1
tcp://192.168.1.119:2376
# status命令
[root@machine ~]# docker-machine status host1
Running
# version命令
[root@machine ~]# docker-machine version host1
19.03.7
docker-machine upgrade
更新 machine 的 docker 到最新版本,可以批量执行:
docker-machine scp
可以在不同 machine 之间拷贝文件,比如:
docker-machine scp host1:/tmp/a host2:/tmp/b