Docker容器學習與分享12

顧北清發表於2018-07-14

Docker多主機管理

之前在一臺Centos7上安裝了Docker,如果是在多臺主機上都安裝Docker,用手動安裝的方法不光效率低下,而且有可能出錯,所以可以使用Docker Machine進行多臺主機的Docker安裝和管理。
具體安裝過程可以參照官方文件https://docs.docker.com/machine/install-machine/.
首先準備兩臺Centos7的虛擬機器(一臺IP為192.168.41.133,安裝Docker Machine,另一臺IP為192.168.41.135),在其中一臺機器上安裝Docker,安裝過程可以參照分享01.
然後下載Docker Machine二進位制檔案並將其解壓縮到PATH。

[root@promote ~]# base=https://github.com/docker/machine/releases/download/v0.14.0 &&
>   curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
>   sudo install /tmp/docker-machine /usr/local/bin/docker-machine
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    583      0 --:--:--  0:00:01 --:--:--   584
100 26.7M  100 26.7M    0     0  1730k      0  0:00:15  0:00:15 --:--:-- 3488k

接著通過顯示機器版本來檢查安裝:

[root@promote ~]# docker-machine version
docker-machine version 0.14.0, build 89b8332

為了更好的體驗接著安裝bash完成指令碼,將指令碼儲存到/etc/bash_completion.d/usr/local/etc/bash_completion.d

[root@promote ~]# cd /etc/bash_completion.d
[root@promote bash_completion.d]# base=https://raw.githubusercontent.com/docker/machine/v0.14.0
[root@promote bash_completion.d]# 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
--2018-07-11 01:47:47--  https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker-                                                                         machine-prompt.bash
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1469 (1.4K) [text/plain]
Saving to: ‘/etc/bash_completion.d/docker-machine-prompt.bash’

100%[=======================================================================>] 1,469       --.-K/s   in 0s

2018-07-11 01:47:48 (192 MB/s) - ‘/etc/bash_completion.d/docker-machine-prompt.bash’ saved [1469/1469]

--2018-07-11 01:47:48--  https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker-                                                                         machine-wrapper.bash
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1525 (1.5K) [text/plain]
Saving to: ‘/etc/bash_completion.d/docker-machine-wrapper.bash’

100%[=======================================================================>] 1,525       --.-K/s   in 0s

2018-07-11 01:47:50 (160 MB/s) - ‘/etc/bash_completion.d/docker-machine-wrapper.bash’ saved [1525/1525]

--2018-07-11 01:47:50--  https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker-                                                                         machine.bash
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12205 (12K) [text/plain]
Saving to: ‘/etc/bash_completion.d/docker-machine.bash’

100%[=======================================================================>] 12,205      --.-K/s   in 0.02s

2018-07-11 01:47:51 (489 KB/s) - ‘/etc/bash_completion.d/docker-machine.bash’ saved [12205/12205]

這樣就下載好了指令碼。
接著申明環境變數。

[root@promote bash_completion.d]# source /etc/bash_completion.d/docker-machine-prompt.bash 

可以先使用docker-machine ls檢視一下當前的machine.

[root@promote ~]# docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS

因為還沒有安裝Docker所以使用這條命令什麼都沒有。
接著建立第一臺機器,就是另外一臺Centos虛擬機器(IP:192.168.41.155).
1.首先需要讓這兩臺Centos可以SSH免密登陸。

[root@promote ~]# 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:T6qcnNvYwepsMhGfdvG/Gk+BtzVe2eCLiF0aMAI6wqc root@promote.cache-dns.local
The key`s randomart image is:
+---[RSA 2048]----+
|    .            |
|.  . .           |
|..o.  . o     .  |
| .o..  ..o . . .o|
| E   o .So+ + +.o|
|    . +..*.* * + |
|     o .= *.+ o  |
|    o+.O . +.    |
|     =%.o ..o.   |
+----[SHA256]-----+
[root@promote ~]# ssh-copy-id 192.168.41.135
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host `192.168.41.135 (192.168.41.135)` can`t be established.
ECDSA key fingerprint is SHA256:JqAC8jcLCLobvRy0wzY9VGBNuZU3EydpO8n2fEtQ178.
ECDSA key fingerprint is MD5:5d:26:a1:60:c3:eb:02:e9:97:7a:bb:7a:49:8a:14:0b.
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@192.168.41.135`s password:

Number of key(s) added: 1

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

2.建立第一個機器
使用docker-machine create命令,因為是Linux,所以driver就用generic,目標地址選項用--generic-ip-address

[root@promote ~]# docker-machine create --driver generic --generic-ip-address=192.168.41.135 machine1
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
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...
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 machine1

現在到192.168.41.135上去驗證。

[root@promote ~]# docker version
Client:
Version:      18.05.0-ce
API version:  1.37
Go version:   go1.9.5
Git commit:   f150324
Built:        Wed May  9 22:14:54 2018
OS/Arch:      linux/amd64
Experimental: false
Orchestrator: swarm

Server:
Engine:
Version:      18.05.0-ce
API version:  1.37 (minimum version 1.12)
Go version:   go1.9.5
Git commit:   f150324
Built:        Wed May  9 22:18:36 2018
OS/Arch:      linux/amd64
Experimental: false

也可以在192.168.41.133上驗證。

[root@promote ~]# docker-machine ls
NAME       ACTIVE   DRIVER    STATE     URL                         SWARM   DOCKER        ERRORS
machine1   -        generic   Running   tcp://192.168.41.135:2376           v18.05.0-ce

發現安裝完成。
接著我通過192.168.41.133這臺主機來使用192.168.41.135主機上的Docker,首先檢視訪問machine所需要的環境變數。

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

可以看到倒數第二行:執行此命令來配置你的shell
也就是指執行eval $(docker-machine env machine1)命令配置上面的環境變數進入machine1.
執行之後發現並無變化,是因為一開始我們沒有配置shell提示.
接著啟用docker-machineshell提示,新增 $(__docker_machine_ps1)到您的PS1設定中~/.bashrc

[root@promote ~]# PS1=`[u@h W$(__docker_machine_ps1)]$ `
[root@promote ~ [machine1]]#

這時發現shell變了,在當前狀態下的所有docker操作都是執行在machine1上的,即192.168.41.135主機上。
如果要切回回來的環境使用unset命令,取消環境變數即可:

[root@promote ~ [machine1]]# unset export DOCKER_TLS_VERIFY
[root@promote ~ [machine1]]# unset DOCKER_HOST
[root@promote ~ [machine1]]# unset DOCKER_CERT_PATH
[root@promote ~ [machine1]]# unset DOCKER_MACHINE_NAME
[root@promote ~]#

發現變回來了。
docker-machine的其他用法可以使用docker-machine --help命令來檢視。

相關文章