####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。
1[root@promote ~]# base=https://github.com/docker/machine/releases/download/v0.14.0 && 2> curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine && 3> sudo install /tmp/docker-machine /usr/local/bin/docker-machine 4% Total % Received % Xferd Average Speed Time Time Time Current 5 Dload Upload Total Spent Left Speed 6100 617 0 617 0 0 583 0 --:--:-- 0:00:01 --:--:-- 584 7100 26.7M 100 26.7M 0 0 1730k 0 0:00:15 0:00:15 --:--:-- 3488k
接着通过显示机器版本来检查安装:
1[root@promote ~]# docker-machine version 2docker-machine version 0.14.0, build 89b8332
为了更好的体验接着安装bash完成脚本,将脚本保存到/etc/bash_completion.d或 /usr/local/etc/bash_completion.d。
1[root@promote ~]# cd /etc/bash_completion.d 2[root@promote bash_completion.d]# base=https://raw.githubusercontent.com/docker/machine/v0.14.0 3[root@promote bash_completion.d]# for i in docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine. bash 4> do 5> sudo wget "$base/contrib/completion/bash/${i}" -P /etc/bash_completion.d 6> done 7--2018-07-11 01:47:47-- https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker- machine-prompt.bash 8Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133 9Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected. 10HTTP request sent, awaiting response... 200 OK 11Length: 1469 (1.4K) [text/plain] 12Saving to: ‘/etc/bash_completion.d/docker-machine-prompt.bash’ 13 14100%[=======================================================================>] 1,469 --.-K/s in 0s 15 162018-07-11 01:47:48 (192 MB/s) - ‘/etc/bash_completion.d/docker-machine-prompt.bash’ saved [1469/1469] 17 18--2018-07-11 01:47:48-- https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker- machine-wrapper.bash 19Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133 20Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected. 21HTTP request sent, awaiting response... 200 OK 22Length: 1525 (1.5K) [text/plain] 23Saving to: ‘/etc/bash_completion.d/docker-machine-wrapper.bash’ 24 25100%[=======================================================================>] 1,525 --.-K/s in 0s 26 272018-07-11 01:47:50 (160 MB/s) - ‘/etc/bash_completion.d/docker-machine-wrapper.bash’ saved [1525/1525] 28 29--2018-07-11 01:47:50-- https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker- machine.bash 30Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133 31Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected. 32HTTP request sent, awaiting response... 200 OK 33Length: 12205 (12K) [text/plain] 34Saving to: ‘/etc/bash_completion.d/docker-machine.bash’ 35 36100%[=======================================================================>] 12,205 --.-K/s in 0.02s 37 382018-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.
1[root@promote ~]# docker-machine ls 2NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
因为还没有安装Docker所以使用这条命令什么都没有。 接着创建第一台机器,就是另外一台Centos虚拟机(IP:192.168.41.155). 1.首先需要让这两台Centos可以SSH免密登陆。
1[root@promote ~]# ssh-keygen 2Generating public/private rsa key pair. 3Enter file in which to save the key (/root/.ssh/id_rsa): 4Created directory '/root/.ssh'. 5Enter passphrase (empty for no passphrase): 6Enter same passphrase again: 7Your identification has been saved in /root/.ssh/id_rsa. 8Your public key has been saved in /root/.ssh/id_rsa.pub. 9The key fingerprint is: 10SHA256:T6qcnNvYwepsMhGfdvG/Gk+BtzVe2eCLiF0aMAI6wqc root@promote.cache-dns.local 11The key's randomart image is: 12+---[RSA 2048]----+ 13| . | 14|. . . | 15|..o. . o . | 16| .o.. ..o . . .o| 17| E o .So+ + +.o| 18| . +..*.* * + | 19| o .= *.+ o | 20| o+.O . +. | 21| =%.o ..o. | 22+----[SHA256]-----+ 23[root@promote ~]# ssh-copy-id 192.168.41.135 24/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" 25The authenticity of host '192.168.41.135 (192.168.41.135)' can't be established. 26ECDSA key fingerprint is SHA256:JqAC8jcLCLobvRy0wzY9VGBNuZU3EydpO8n2fEtQ178. 27ECDSA key fingerprint is MD5:5d:26:a1:60:c3:eb:02:e9:97:7a:bb:7a:49:8a:14:0b. 28Are you sure you want to continue connecting (yes/no)? yes 29/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed 30/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 31root@192.168.41.135's password: 32 33Number of key(s) added: 1 34 35Now try logging into the machine, with: "ssh '192.168.41.135'" 36and check to make sure that only the key(s) you wanted were added.
2.创建第一个机器 使用docker-machine create命令,因为是Linux,所以driver就用generic,目标地址选项用--generic-ip-address。
1[root@promote ~]# docker-machine create --driver generic --generic-ip-address=192.168.41.135 machine1 2Creating CA: /root/.docker/machine/certs/ca.pem 3Creating client certificate: /root/.docker/machine/certs/cert.pem 4Running pre-create checks... 5Creating machine... 6(machine1) No SSH key specified. Assuming an existing key at the default location. 7Waiting for machine to be running, this may take a few minutes... 8Detecting operating system of created instance... 9Waiting for SSH to be available... 10Detecting the provisioner... 11Provisioning with centos... 12Copying certs to the local machine directory... 13Copying certs to the remote machine... 14Setting Docker configuration on the remote daemon... 15Checking connection to Docker... 16Docker is up and running! 17To 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上去验证。
1[root@promote ~]# docker version 2Client: 3Version: 18.05.0-ce 4API version: 1.37 5Go version: go1.9.5 6Git commit: f150324 7Built: Wed May 9 22:14:54 2018 8OS/Arch: linux/amd64 9Experimental: false 10Orchestrator: swarm 11 12Server: 13Engine: 14Version: 18.05.0-ce 15API version: 1.37 (minimum version 1.12) 16Go version: go1.9.5 17Git commit: f150324 18Built: Wed May 9 22:18:36 2018 19OS/Arch: linux/amd64 20Experimental: false
也可以在192.168.41.133上验证。
1[root@promote ~]# docker-machine ls 2NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS 3machine1 - generic Running tcp://192.168.41.135:2376 v18.05.0-ce
发现安装完成。 接着我通过192.168.41.133这台主机来使用192.168.41.135主机上的Docker,首先查看访问machine所需要的环境变量。
1[root@promote ~]# docker-machine env machine1 2export DOCKER_TLS_VERIFY="1" 3export DOCKER_HOST="tcp://192.168.41.135:2376" 4export DOCKER_CERT_PATH="/root/.docker/machine/machines/machine1" 5export DOCKER_MACHINE_NAME="machine1" 6# Run this command to configure your shell: 7# eval $(docker-machine env machine1)
可以看到倒数第二行:运行此命令来配置你的shell 也就是指运行eval $(docker-machine env machine1)命令配置上面的环境变量进入machine1. 运行之后发现并无变化,是因为一开始我们没有配置shell提示. 接着启用docker-machineshell提示,添加 $(__docker_machine_ps1)到您的PS1设置中~/.bashrc。
1[root@promote ~]# PS1='[\u@\h \W$(__docker_machine_ps1)]\$ ' 2[root@promote ~ [machine1]]#
这时发现shell变了,在当前状态下的所有docker操作都是运行在machine1上的,即192.168.41.135主机上。 如果要切回回来的环境使用unset命令,取消环境变量即可:
1[root@promote ~ [machine1]]# unset export DOCKER_TLS_VERIFY 2[root@promote ~ [machine1]]# unset DOCKER_HOST 3[root@promote ~ [machine1]]# unset DOCKER_CERT_PATH 4[root@promote ~ [machine1]]# unset DOCKER_MACHINE_NAME 5[root@promote ~]#
发现变回来了。 docker-machine的其他用法可以使用docker-machine --help命令来查看。