docker入门教程七:三剑客之docker machine

简介与安装

Docker Machine项目基于 Go 语言实现,Docker 官方提供的一个工具,它可以帮助我们在远程的机器上安装 Docker,或者在虚拟机 host 上直接安装虚拟机并在虚拟机中安装 Docker。我们还可以通过 docker-machine命令来管理这些虚拟机和 Docker。使用的场景应该是比较少的在生产中。

安装方法详见 https://docs.docker.com/machine/install-machine/:

1
2
3
4
5
6
7
8
9
#安装
curl -L https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine
chmod +x /usr/local/bin/docker-machine
#命令补全
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

由于在虚拟机上面做的实验,在虚拟机上面再开启虚拟化,行不通,可以放弃。

如果有环境的话,由于docker machine要使用到virtualbox的驱动,所以必须先安装这个虚拟化工具。

1
2
3
4
5
6
7
yum update
yum install binutils qt gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms
wget http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc
rpm --import oracle_vbox.asc
cd /etc/yum.repos.d/
wget http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo -O /etc/yum.repos.d/virtualbox.repo
yum install VirtualBox-6.0.x86_64

安装完成之后需要reboot一下,然后使用/sbin/vboxconfig来配置。其他设置请参考:https://www.tecmint.com/install-virtualbox-on-redhat-centos-fedora/

完成之后,就可以使用 docker-machine create -d virtualbox test来创建本地环境的虚拟机,同时会自动安装上docker工具。

https://cloud.tencent.com/developer/article/1481575

在远程主机上面安装docker

部署密钥

由于需要在远程主机上面安装软件,必须要有相应的权限,所以需要配置免密码登陆,就是使用公私钥来登陆了。使用 ssh-keygen -t rsa 来生成密钥,再使用ssh-copy-id把公钥复制到另一台机器上,比如我这台是192.168.137.55,添加完密钥之后,直接ssh + ip查看是否有成功登陆了。

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
[root@localhost ~]# 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:
。。。
[root@localhost ~]# ll .ssh/ -ah
total 12K
drwx------. 2 root root 38 Oct 5 03:30 .
dr-xr-x---. 7 root root 4.0K Oct 5 03:30 ..
-rw-------. 1 root root 1.7K Oct 5 03:30 id_rsa
-rw-r--r--. 1 root root 408 Oct 5 03:30 id_rsa.pub
[root@localhost ~]# ssh-copy-id root@192.168.137.55
/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
root@192.168.137.55's password:
Permission denied, please try again.
root@192.168.137.55's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.137.55'"
and check to make sure that only the key(s) you wanted were added.

部署远端docker

完成上述操作之后,再运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# docker-machine create -d generic --generic-ip-address 192.168.137.55 remote
Running pre-create checks...
Creating machine...
(remote) 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 remote

注意,create 命令本是要创建虚拟主机并安装 Docker,因为本例中的目标主机已经存在,所以仅安装 Docker。

-d 是 —driver 的简写形式,主要用来指定使用什么驱动程序来创建目标主机。Docker Machine 支持在云服务器上创建主机,就是靠使用不同的驱动来实现了。本例中使用 generic 就可以了。

remote这个只是一个名字而也。

稍等几分钟,另一台机器上面就会完成docker工具的安装。我们可以通过 Docker Machine 的 ls 命令查看当前可管理的主机列表:

1
2
3
[root@localhost ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
remote - generic Running tcp://192.168.137.55:2376 v19.03.2

管理远端的docker

有2个方法。

docker-machine env

运行docker-machine env + name就会显示出一些指令,其实就是设置环境变量。但实际上并没有执行,只是显示而也,如果要执行,直接运行eval $(docker-machine env remote)即可。

1
2
3
4
5
6
7
[root@localhost ~]# docker-machine env remote
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.137.55:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/remote"
export DOCKER_MACHINE_NAME="remote"
# Run this command to configure your shell:
# eval $(docker-machine env remote)

docker-machine ssh

直接ssh即可。

1
2
3
4
[root@localhost ~]# docker-machine ssh remote
Last login: Sat Oct 5 03:56:47 2019 from 192.168.137.66
[root@remote ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE

2个方法,个人还是觉得ssh的方法会比较简单一些。

参考资料

Docker Machine 详解

  • 本文作者: wumingx
  • 本文链接: https://www.wumingx.com/k8s/docker-machine.html
  • 本文主题: docker入门教程七:三剑客之docker machine
  • 版权声明: 本博客所有文章除特别声明外,转载请注明出处!如有侵权,请联系我删除。
0%