【小伟哥AI之路】安装使用Docker Machine

安装VirtualBox

1、增加epel源:

yum -y install epel-release

2、增加VirtualBox源

cd /etc/yum.repos.d/
wget https://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo

3、查看可安装版本,以及安装指定版本VirtualBox

yum search virtualbox
yum -y install VirtualBox-6.1

至此,VirtualBox安装完成。

安装Docker Machine

On OS X

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

On Linux

$ 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

On Windows with git bash

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

Otherwise, download one of the releases from the release page directly.

 

2.3 使用Docker Machine

使用Docker Machine创建一台docker虚拟机,非常的方便容易,使用如下命令即可:

[root@node2 ~]# docker-machine create --driver virtualbox default
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
(default) Image cache directory does not exist, creating it at /root/.docker/machine/cache...
(default) No default Boot2Docker ISO found locally, downloading the latest release...
(default) Latest release for github.com/boot2docker/boot2docker is v18.06.1-ce
(default) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.06.1-ce/boot2docker.iso...
(default) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(default) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet0"
(default) Waiting for an IP...
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 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 your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env default

从上面的过程可以看出,其实是使用了一个docker官方的一个轻量linux发行版本(boot2docker.iso)来创建了一个VM。
一些常用命令如下:
1、列出当前有多少docker vm。

 

[root@node2 ~]# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v18.06.1-ce

2、查看某一个docker vm主机的信息,例如刚才创建的default主机。

 

[root@node2 ~]# 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)

3、管理某个docker vm
前面我们提到过,Docker Machine可以直接管理远程的docker主机,不论是虚拟机还是云主机。那么它是怎么知道需要管理的主机的信息的呢?答案是设置相关的环境变量。结合上面的获取主机信息的命令,我们可以使用eval $(docker-machine env default)命令来设置当前的环境变量为某一个主机的信息。

 

[root@node2 ~]# eval $(docker-machine env default)

4、开始管理指定的主机

 

[root@node2 ~]# docker container run busybox echo hello world
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
90e01955edcd: Pull complete 
Digest: sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Status: Downloaded newer image for busybox:latest
hello world

就像之前的文章中,我们单独的在一台服务器上手动安装docker后一样,直接执行我们前面学到过的命令就可以操作这台default虚拟机。

Tips:我自己做到这里的时候,突然想到,这台名为node2的演示主机,我前面手动装好了docker,也进行了一系列的相关操作,启动过一些容器,那么现在我怎么知道我刚才执行的docker container run busybox命令到底是在操作node2本机,还是确实是对虚拟机的操作呢?
经过我的实验发现,当前窗口始终是操作default虚拟机,而我新开一个ssh连接到node2的时候,就是操作本机。后来仔细回想刚才docker machine的过程,发现其实就是当前环境变量决定了你当前操作的是谁。

5、切换环境变量,指定另一台主机
取消当前环境变量:eval $(docker-machine env -u)
设置一台新主机的环境变量:eval $(docker-machine env new-host)

6、常用的一些命令,需要时请自行添加--help参数查看。

 

docker-machine config
docker-machine env
docker-machine inspect
docker-machine ip        #这个命令也能让你知道你当前管理的主机IP
docker-machine kill
docker-machine provision
docker-machine regenerate-certs
docker-machine restart
docker-machine ssh        #远程ssh连接到docker虚拟机上
docker-machine start
docker-machine status
docker-machine stop        #暂停docker虚拟机
docker-machine upgrade
docker-machine url

在进行下一章之前,还有一些优化工作需要做一下,不然后面会有很多坑,主要是虚拟机里面默认使用的是docker hub的镜像仓库,拉取速度太慢或者卡死,这里使用如下脚本批量修改一下。

 

#!/bin/bash

# 本脚本用于优化虚拟机的docker镜像源,修改为与localhost一样的中科大源
for NODE in `seq 1 5`
do
  docker-machine scp /etc/docker/daemon.json node-${NODE}:/home/docker
  docker-machine ssh node-${NODE} 'sudo mv /home/docker/daemon.json /etc/docker/daemon.json && sudo chown root:root /etc/docker/daemon.json && sudo kill -SIGHUP $(pidof dockerd)'
done

下一章,我们就将使用Docker Machine创建多台docker虚拟机,并利用swarm集群化管理。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值