Docker三劍客之docker-machine

簡介

docker-machine就是docker公司官方提出的,用於在各種平臺上快速創建具有docker服務的虛擬機的技術,甚至可以通過指定driver來定製虛擬機的實現原理(一般是virtualbox)。

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

docker-machine可以在本地佈署相應環境的同時完成遠程docker主機相同環境的佈署,減少重複的操作。

docker-machine安裝

<code>$ sudo curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine
$ sudo chmod +x /usr/local/bin/docker-machine
/<code>

完成後,查看版本信息。

<code>$ docker-machine -v
docker-machine version 0.13.0, build 9ba6da9
/<code>

docker-machine命令

<code>docker-machine active
#顯示當前的活動主機

docker-machine config
#顯示連接主機的配置

docker-machine create
#創建一個主機

docker-machine env
#設置當前的環境與哪個主機通信

docker-machine inspect
#查看主機的詳細信息

docker-machine ip
#查看主機的IP 

docker-machine kill
#強制關閉一個主機

docker-machine ls 
#查看所有的主機信息

docker-machine provision
#重新配置現在主機

docker-machine regenerate-certs
#為主機重新生成證書

docker-machine restart
#重啟主機

docker-machine rm
#刪除主機

docker-machine ssh
#以SSH的方式連接到主機上

docker-machine scp
#遠程複製

docker-machine status
#查看主機的狀態

docker-machine stop
#停止一個正在運行的主機

docker-machine upgrade
#升級主機的docker服務到最新版本

docker-machine version
#查看docker-machine版本
/<code>

實例

兩臺服務器

  • 本地主機:192.168.124.129
  • 遠程主機:192.168.124.132

1、配置免密登錄

<code>$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:k6pTKvpG2AOIyktJfAV09tkeJ/zB6aYHtpxY7Q5jzzk root@node01
The key's randomart image is:
+---[RSA 2048]----+
|  .o.o           |
|    o.. + . .    |
|+   .  o = =     |
|+o .    ..B .    |
|++o     S= =     |
|o++   ..=.B      |
|.... o.. O o     |
| .o o.  . BE.    |
|.+....     =.    |
+----[SHA256]-----+

$ ssh-copy-id 192.168.124.132
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 192.168.124.132 (192.168.124.132)' can't be established.
ECDSA key fingerprint is SHA256:isURMZPfryhzac244IpxCfFGGkdWQ32IvV6MNIkk6L0.
ECDSA key fingerprint is MD5:01:d9:a4:16:79:0f:b9:dc:98:4b:c9:5d:9e:67:25:14.
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.124.132'"
and check to make sure that only the key(s) you wanted were added.

$ ssh 192.168.124.132
Last login: Wed Nov 14 11:18:51 2018 from 192.168.124.1

$ ip add |grep 192.168.124
    inet 192.168.124.132/24 brd 192.168.124.255 scope global ens32
/<code>

2、創建遠程主機

遠程主機需要安裝有docker環境

<code>$ docker-machine create -d generic --generic-ip-address=192.168.124.132 --generic-ssh-user=root --engine-registry-mirror https://registry.docker-cn.com   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

$ docker-machine ls
NAME         ACTIVE   DRIVER    STATE     URL                          SWARM   DOCKER     ERRORS
dockerhost   -        generic   Running   tcp://192.168.124.132:2376           v18.09.0   
/<code>

3、配置環境變量

<code>$ docker-machine env dockerhost
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.124.132: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)
$ eval $(docker-machine env dockerhost)
$ docker-machine ssh dockerhost
Last login: Thu Nov 15 05:51:29 2018 from manager
[root@dockerhost ~]# docker -v
Docker version 18.09.0, build 4d60db4
/<code>

4、運行一個容器

<code>$ docker run -d nginx:1.13
Unable to find image 'nginx:1.13' locally
1.13: Pulling from library/nginx
f2aa67a397c4: Pull complete 
3c091c23e29d: Pull complete 
4a99993b8636: Pull complete 
Digest: sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
Status: Downloaded newer image for nginx:1.13
7dfcf725aca0edce72e6b1288cfa803e6feadff8fba1216fc1811c97d83eb05d
$  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.13                ae513a47849c        6 months ago        109MB

$ docker-machine ssh dockerhost
Last login: Thu Nov 15 05:52:52 2018 from manager
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.13                ae513a47849c        6 months ago        109MB
/<code>

由此可以發現,利用docker-machine可以減少重複操作,便於環境的創建

參考地址鏈接:

  • 容器技術|Docker三劍客之docker-machine


分享到:


相關文章: