Docker 再学习 (6) Docker machine

Submitted by Lizhe on Mon, 02/25/2019 - 07:58

我使用的环境是

docker1 172.16.166.135
docker2 172.16.166.136

1. 安装docker machine

base=https://github.com/docker/machine/releases/download/v0.16.0 &&
  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
  sudo install /tmp/docker-machine /usr/local/bin/docker-machine

 

安装bash completion script,这样bash可以通过tab来补全命令和参数:

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

 

[root@docker1 ~]# vi ~/.bashrc

添加 

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@docker1 ~]# source ~/.bashrc

 

2. 在本机上做一个免密登录

正常情况下即使你使用ssh登录本机也会要密码

 

[root@docker1 ~]# ssh root@172.16.166.135
root@172.16.166.135's password: 

所以即使在本机通过docker machine 创建 engine, 也需要对本机实现免密登录, 否则会出现如下状态

创建命令会一直卡在

ot@docker1 ~]# docker-machine create --driver generic --generic-ip-address=172.16.166.135 machine1
Running pre-create checks...
Creating machine...
(machine1) 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...
 

实际上在等待大约不到20分钟之后你会得到超时错误

20190225101106

然后使用ls命令会看见有错误

[root@docker1 ~]# docker-machine ls
NAME       ACTIVE   DRIVER    STATE     URL                         SWARM   DOCKER    ERRORS
machine1   -        generic   Running   tcp://172.16.166.135:2376           Unknown   Unable to query docker version: Cannot connect to the docker engine endpoint
[root@docker1 ~]# 
 

 

本机免密登录

[root@docker1 ~]# ssh-keygen
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:vLyzmHbKa0ncJaJUjzYnbcLRDMyQgm0HTFxwz6x8gJ4 root@docker1
The key's randomart image is:
+---[RSA 2048]----+
| *+++=.+         |
|. *oo+= o        |
| ..o.o+=         |
| . o.oO.* .      |
|  E.o+.OSo       |
|    ..o...       |
|     . .o        |
|     .+oo.       |
|     o*+oo       |
+----[SHA256]-----+
[root@docker1 ~]# 
[root@docker1 ~]# ssh-copy-id root@172.16.166.135
The authenticity of host '172.16.166.135 (172.16.166.135)' can't be established.
ECDSA key fingerprint is SHA256:dp1suzU6VbnRiyflVjev3HT2onSvJ5d5sUZIs/8MET8.
ECDSA key fingerprint is MD5:35:fd:91:a1:9c:37:b7:83:06:cc:09:01:7c:2c:9e:54.
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
root@172.16.166.135's password: 

Number of key(s) added: 1

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

[root@docker1 ~]# 
 

 

20190225053835

 

测试一下, 发现已经不用密码了

 

[root@docker1 ~]# ssh root@172.16.166.135
Last login: Mon Feb 25 17:24:27 2019
[root@docker1 ~]# 

20190225054954

 

使用命令添加机器吧

 

docker-machine create --driver generic --generic-ip-address=172.16.166.135 machine1

20190225094355

 

通过docker1 为 docker2 安装 docker

 

[root@docker1 ~]# ssh-copy-id root@172.16.166.136
The authenticity of host '172.16.166.136 (172.16.166.136)' can't be established.
ECDSA key fingerprint is SHA256:xwTmKsh//wPmhKTPetbIb9i5IPru9g6IHqGRtS5BQr4.
ECDSA key fingerprint is MD5:0b:ac:c7:4c:49:04:e7:78:1c:ec:6f:d7:1d:92:7d:4c.
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
root@172.16.166.136's password: 

Number of key(s) added: 1

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

[root@docker1 ~]# docker-machine create --driver generic --generic-ip-address=172.16.166.136 machine2
Running pre-create checks...
Creating machine...
(machine2) 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 machine2
[root@docker1 ~]# 
 

 

20190226120949

 

在docker2上查看可以看到 docker 已经安装了

20190226121231

 

20190226123116

 

重启之后也可以使用docker

20190226123738