Docker-machine

官网帮助文档: https://docs.docker.com/machine/overview/

一、docker-machine 介绍

1、定义
docker-machine就是docker公司官方提出的,用于在各种平台上快速创建具有docker服务的虚拟机的技术,甚至可以通过指定driver来定制虚拟机的实现原理(一般是virtualbox)。

2、Docker 与 Docker Machine 的区别
Docker 是一个 Client-Server 架构的应用,Docker是大家对Docker Engine简称。

3、Docker组成部分

① Docker daemon
② 一套与 Docker daemon 交互的 REST API
③ 一个命令行客户端

下图很清晰的展示了它们之间的关系:
Docker-machine_第1张图片

4、命令行工具
Docker Machine 是安装和管理 Docker 的工具。它有命令行工具:docker-machine

二、docker-machine 安装及常用命令

1、下载命令行工具

[root@localhost ~]# curl -L $base/docker-machine-$(uname -s)-$(uname -m) > /tmp/docker-machine 
[root@localhost ~]# chmod +x /tmp/docker-machine 
[root@localhost ~]# install /tmp/docker-machine /usr/local/bin/docker-machine
[root@localhost ~]# ll /usr/local/bin/docker-machine
-rwxr-xr-x. 1 root root 28034848 Oct 29 09:45 /usr/local/bin/docker-machine
[root@localhost ~]# docker-machine -v
docker-machine version 0.14.0, build 89b8332

2、docker-machine 常用命令

#查看帮助
[root@localhost ~]# docker-machine  --help

#显示当前的活动主机
[root@localhost ~]# docker-machine active

#显示连接主机的配置
[root@localhost ~]# docker-machine config

#创建一个主机
[root@localhost ~]# docker-machine create

#设置当前的环境与哪个主机通信
[root@localhost ~]# docker-machine env

#查看主机的详细信息
[root@localhost ~]# docker-machine inspect

#查看主机的IP 
[root@localhost ~]# docker-machine ip

#强制关闭一个主机
[root@localhost ~]# docker-machine kill

#查看所有的主机信息
[root@localhost ~]# docker-machine ls 

#重新配置现在主机
[root@localhost ~]# docker-machine provision

#为主机重新生成证书
[root@localhost ~]# docker-machine regenerate-certs

#重启主机
[root@localhost ~]# docker-machine restart

#删除主机
[root@localhost ~]# docker-machine rm

#以SSH的方式连接到主机上
[root@localhost ~]# docker-machine ssh

#远程复制
[root@localhost ~]# docker-machine scp

#查看主机的状态
[root@localhost ~]# docker-machine status

#停止一个正在运行的主机
[root@localhost ~]# docker-machine stop

#升级主机的docker服务到最新版本
[root@localhost ~]# docker-machine upgrade

#查看docker-machine版本
[root@localhost ~]# docker-machine version

3、实例操作

#下面两个命令的作用相同,创建一个名为testhost的主机,驱动方式是virtualbox
[root@localhost ~]# docker-machine create -d virtualbox testhost
[root@localhost ~]# docker-machine create --driver virtualbox testhost


#报错提示没有发现VBoxManage。因此,需要手工安装,具体安装操作如下。
[root@localhost ~]# docker-machine create -d virtualbox testhost
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"

三、安装Virtualbox驱动

1、配置yum源

[root@localhost ~]# vim  /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc

2、查看可安装的版本并安装驱动

#查找具体安装版本
[root@localhost ~]# yum search VirtualBox   
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
====================================================== N/S matched: VirtualBox ======================================================
VirtualBox-4.3.x86_64 : Oracle VM VirtualBox
VirtualBox-5.0.x86_64 : Oracle VM VirtualBox
VirtualBox-5.1.x86_64 : Oracle VM VirtualBox
VirtualBox-5.2.x86_64 : Oracle VM VirtualBox

#安装 VirtualBox 最新版本
[root@localhost ~]# yum install -y VirtualBox-5.2  

3、加载virtualbox

#重新加载virtualbox服务
[root@localhost ~]# /sbin/vboxconfig   
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-693.el7.x86_64
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-693.el7.x86_64

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.
#如果内核版本不一致,会出现上面的报错,需要安装相同的内核版本

4、安装对应的内核版本

[root@localhost ~]# rpm -ivh kernel-devel-3.10.0-693.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:kernel-devel-3.10.0-693.el7   ################################# [100%]
   
[root@localhost ~]# yum install gcc make perl -y

[root@localhost ~]# rpm -qa kernel\*
kernel-tools-3.10.0-693.el7.x86_64
kernel-devel-3.10.0-693.el7.x86_64
kernel-tools-libs-3.10.0-693.el7.x86_64
kernel-3.10.0-693.el7.x86_64
kernel-headers-3.10.0-862.3.2.el7.x86_64

#重新加载virtualbox服务
[root@localhost ~]# /sbin/vboxconfig 
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.

四、创建虚拟机并启动

1、重新安装

#先安装默认的虚拟机
[root@localhost ~]#  docker-machine create --driver virtualbox default

#创建virtualbox虚拟机
[root@localhost ~]# docker-machine create --driver virtualbox testhost

#查看
[root@localhost ~]#  docker-machine ls

#状态
[root@localhost ~]# docker-machine status
Running

2、配置环境变量

#查看环境变量
[root@localhost ~]# docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env default)

#声明环境变量
[root@localhost ~]#  eval $(docker-machine env default)


#连接虚拟主机
[root@localhost ~]# docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.05.0-ce, build HEAD : b5d6989 - Thu May 10 16:35:28 UTC 2018
Docker version 18.05.0-ce, build f150324
docker@default:~$ docker --version
Docker version 18.05.0-ce, build f150324

五、实例

两台服务器
本地主机:192.168.22.177
远程主机:192.168.22.175

可能大家还是不太明白,这个docker-machine到底有什么用?docker容器的好处都知道了。而docker-machine可以在本地布署相应环境的同时完成远程docker主机相同环境的布署,减少重复的操作。

1、配置免密登录

[root@test001 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:7Ue0bc08pEs+PonJqy7/hyxUhNO4uEegVizX4EJC8J8 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| .oo ...o +      |
|  . o..= = o     |
|   . .=.o + .  . |
|    .oo. + o oo+ |
|    .E  S o ooo.+|
|       . + .o.. .|
|        o + =+.  |
|       . . B.+.  |
|        +++oo..  |
+----[SHA256]-----+


#将177服务器的公钥传给175服务器
[root@localhost ~]# ssh-copy-id 192.168.22.175
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.22.175 (192.168.22.175)' can't be established.
ECDSA key fingerprint is SHA256:p6+FPeTxTUx37cwJWJP8cUE9NhcUHSvAppVPyj4aj8c.
ECDSA key fingerprint is MD5:89:6d:f7:46:11:45:2e:fd:21:87:42:bd:62:06:fe:fd.
Are you sure you want to continue connecting (yes/no)? yes
/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 '192.168.22.175'"
and check to make sure that only the key(s) you wanted were added.

#连接175服务器
[root@test001 ~]# ssh 192.168.22.175
Last login: Tue Apr 24 06:51:06 2018 from 192.168.22.170
[root@test002 ~]# ip add |grep 192.168.22
    inet 192.168.22.175/24 brd 192.168.22.255 scope global ens32

2、创建远程主机

远程主机需要安装有docker环境

[root@test001 ~]# docker-machine create -d generic --generic-ip-address=192.168.22.175 --generic-ssh-user=root --engine-registry-mirror http://ef017c13.m.daocloud.io  dockerhost
Running pre-create checks...
Creating machine...
(dockerhost) 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 dockerhost


[root@test001 ~]# docker-machine ls
NAME         ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
dockerhost   -        generic      Running   tcp://192.168.22.175:2376           v1.13.1

3、配置环境变量

[root@localhost ~]# docker-machine env dockerhost
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.22.175:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/dockerhost"
export DOCKER_MACHINE_NAME="dockerhost"
# Run this command to configure your shell: 
# eval $(docker-machine env dockerhost)


[root@test001 ~]# eval $(docker-machine env dockerhost)
[root@test001 ~]# docker-machine ssh dockerhost
Last login: Thu Jun 14 02:32:42 2018 from 192.168.22.177


[root@dockerhost ~]# docker --version
Docker version 1.13.1, build 8633870/1.13.1

4、运行一个容器

[root@test001 ~]# docker run -d nginx:1.13
Unable to find image 'nginx:1.13' locally
Trying to pull repository docker.io/library/nginx ... 
sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35: Pulling from docker.io/library/nginx
f2aa67a397c4: Pull complete 
3c091c23e29d: Pull complete 
4a99993b8636: Pull complete 
Digest: sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
Status: Downloaded newer image for docker.io/nginx:1.13
72efb659ec38d263519c894bf0b5eb3d5ca35af1e3d0e9522abbcc19d8739403


[root@test001 ~]# docker image ls
REPOSITORY          TAG     IMAGE ID       CREATED       SIZE
docker.io/nginx     1.13    ae513a47849c   6 weeks ago   109 MB


[root@test001 ~]# docker-machine ssh dockerhost
Last login: Thu Jun 14 02:58:51 2018 from 192.168.22.177


[root@dockerhost ~]# docker image ls
REPOSITORY          TAG      IMAGE ID      CREATED       SIZE
docker.io/nginx     1.13     ae513a47849c  6 weeks ago   109 MB
#由此可以发现,利用docker-machine可以减少重复操作,便于环境的创建

你可能感兴趣的:(虚拟化)