容器技术之Docker-machine
前文我们聊了下docker容器的资源限制,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13138725.html;今天我们来聊一聊docker machine;docker machine是docker 官方提供的工具,这个工具可以在不同主机/不同系统上快速安装、管理docker环境;docker machine 的实现原理就是通过不同的驱动来连接不同类型节点,来实现docker machine管理不同平台上的docker环境;
docker machine 安装
1、下载二进制程序文件到本地
[root@node1 ~]# base=https://github.com/docker/machine/releases/download/v0.16.0 && > curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine && > sudo mv /tmp/docker-machine /usr/local/bin/docker-machine && > chmod +x /usr/local/bin/docker-machine % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 638 100 638 0 0 590 0 0:00:01 0:00:01 --:--:-- 590 100 26.8M 100 26.8M 0 0 11911 0 0:39:24 0:39:24 --:--:-- 16907 [root@node1 ~]# ll /usr/local/bin/docker-machine -rwxr-xr-x 1 root root 28164576 Jun 18 11:28 /usr/local/bin/docker-machine [root@node1 ~]# docker-machine version docker-machine version 0.16.0, build 702c267f [root@node1 ~]#
提示:以上命令主要就做了三件事,下载对应系统的对应系统架构的docker-machine到本地/tmp/下,并保存为docker-machine;然后把/tmp/docker-machine移动至/usr/local/bin/下,然后给/usr/local/bin/docker-machine添加执行权限;如果下载完我们可以在终端运行docker-machine version 能够看到对应的版本信息,就表示docker-machine安装好了;docker-machine程序是安装好了,现在我们还不能直接使用;我们上面说过docker-machine本质就是通过不同的驱动去连接节点,连接节点实际上就是通过ssh链到节点服务器上,然后执行安装docker;所以为了能够很好的使用docker-machine 我们需要对管理的节点做免密登录;
2、管理节点对work节点做免密登录
[root@node1 ~]# ssh-keygen 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:4HrdVnoO+W/+J/ewP4A1m8HnneKWAKMKo3Ad2uExJ1k root@node1 The key's randomart image is: +---[RSA 2048]----+ | | | E | | o. . | | B... o = . | | = B. S o + B o| |. oo+. o . * = o.| |... + o . * + = | | . o . = +o+o| | +++=B| +----[SHA256]-----+ [root@node1 ~]# ssh-copy-id root@192.168.0.42 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.0.42 (192.168.0.42)' can't be established. ECDSA key fingerprint is SHA256:EG9nua4JJuUeofheXlgQeL9hX5H53JynOqf2vf53mII. ECDSA key fingerprint is MD5:57:83:e6:46:2c:4b:bb:33:13:56:17:f7:fd:76:71:cc. 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.0.42's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.0.42'" and check to make sure that only the key(s) you wanted were added. [root@node1 ~]# ssh-copy-id root@192.168.0.43 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.0.43 (192.168.0.43)' can't be established. ECDSA key fingerprint is SHA256:EG9nua4JJuUeofheXlgQeL9hX5H53JynOqf2vf53mII. ECDSA key fingerprint is MD5:57:83:e6:46:2c:4b:bb:33:13:56:17:f7:fd:76:71:cc. 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.0.43's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.0.43'" and check to make sure that only the key(s) you wanted were added. [root@node1 ~]
提示:有关免密登录的详细说明可以参考本人博客https://www.cnblogs.com/qiuhom-1874/p/11783371.html;接下来我们就可以使用docker-machine来对节点主机进行操作了;
对节点主机安装docker环境
提示:以上命令表示创建一个docker-machine主机,使用generic驱动,generic表示对linux主机,如果是windows需要用到--virtualbox;--generic-ip-address表示指定节点主机的ip地址;然后在给上一名称;从上面的信息可以看到,docker-node01这台machine已经启动,它告诉我们要查看怎么连接docker-node01这台虚拟主机,请运行docker-machine env docker-node01 查看;
查看怎么连接docker-node01这台虚拟主机
提示:它告诉我们运行下面的命令可以配置我们的shell
提示:从上面的信息可以看到我们运行 eval $(docker-machine env docker-node01)这条命令就表示把环境切换到docker-node01上;接下来的操作都会发送到docker node01上;
提示:从上面的信息可以看到,当我们使用eval $(docker-machine env docker-node01)把当前环境切换到docker-node01后,在当前终端运行的容器和下载的镜像,在退出当前终端重新登录后,本地的是没有nginx镜像的;这意味着我们切换环境后,运行容器的操作上发送给docker-node01上执行了;
测试:我们登录到docker-node01看看是否有nginx镜像和n1容器?
提示:用docker-machine创建虚拟主机来对节点主机管理时,我们给定虚拟主机的名称docker-machine会把该名称当作主机名,把节点主机的主机名更改为我们指定的名称;从上面的信息可以看到docker-node01这台主机上有nginx镜像和n1容器;这说明我们刚才的操作都是发送给docker-node01上了;从上面的演示可以看到,我们在docker-machine上切换环境,当前shell并不能反映我们是否切换到对应的环境了;这样一来在主机特别多的情况,很容易出错;接下来我们配置当前shell的PS1的环境变量;
下载docker-machine-wrapper.bash、docker-machine-prompt.bash和docker-machine.bash
[root@node01 ~]# cat /etc/bash_completion.d/down.bash 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@node01 ~]#
提示:以上脚本主要是循环下载上面说的三个脚本;执行该脚本直接有source命令即可;
提示:我们用source命令来执行上面的脚本,提示我们连接拒绝;这是因为没有解析到raw.githubusercontent.com的地址造成的;解决办法在/etc/hosts文件中介入raw.githubusercontent.com的解析地址即可;https://site.ip138.com/raw.githubusercontent.com/;这个网站可以查询到raw.githubusercontent.com的地址;
提示:更改/etc/hosts文件后,接下在用source命令执行上面的脚本就不会提示我们链接拒绝了;
提示:可以看到/etc/bash_completion.d/目录下有我们要的脚本了;接下来我们就需要配置当前用户的PS1环境的值;
提示:以上信息表示导入上面的三个脚本到当前登录用户的终端;配置好以上.bashrc后,我们在来切换环境,当前shell就不一样了;
提示:导入了docker-machine-wrapper.bash、docker-machine-prompt.bash和docker-machine.bash这三个脚本配合现在新定义的PS1变量,我们切换环境就很容易的辨识,我们操作的node节点主机是那一台;退出当前环境,直接使用exit即可;
到此docker-machine的环境就搭建好了;接下我们再来说说docker-machine的常用命令使用和说明
docker-machine active:查看当前激活状态的docker主机
[root@node01 ~]# docker-machine active docker-node01 [root@node01 ~]#
提示:所谓激活状态的docker主机就是指的当前的DOCKER_HOST环境变量所指向的主机;
docker-machine ls:列出所有管理的主机
[root@node01 ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS docker-node01 * generic Running tcp://192.168.0.42:2376 v19.03.11 docker-node02 - generic Running tcp://192.168.0.43:2376 v19.03.11 [root@node01 ~]#
docker-machine config:查看激活的docker主机的连接信息;
[root@node01 ~]# docker-machine config docker-node01 --tlsverify --tlscacert="/root/.docker/machine/machines/docker-node01/ca.pem" --tlscert="/root/.docker/machine/machines/docker-node01/cert.pem" --tlskey="/root/.docker/machine/machines/docker-node01/key.pem" -H=tcp://192.168.0.42:2376 [root@node01 ~]#
docker-machine inspect :以json格式输出指定docker主机的详细信息
[root@node01 ~]# docker-machine inspect docker-node01 { "ConfigVersion": 3, "Driver": { "IPAddress": "192.168.0.42", "MachineName": "docker-node01", "SSHUser": "root", "SSHPort": 22, "SSHKeyPath": "", "StorePath": "/root/.docker/machine", "SwarmMaster": false, "SwarmHost": "", "SwarmDiscovery": "", "EnginePort": 2376, "SSHKey": "" }, "DriverName": "generic", "HostOptions": { "Driver": "", "Memory": 0, "Disk": 0, "EngineOptions": { "ArbitraryFlags": [], "Dns": null, "GraphDir": "", "Env": [], "Ipv6": false, "InsecureRegistry": [], "Labels": [], "LogLevel": "", "StorageDriver": "", "SelinuxEnabled": false, "TlsVerify": true, "RegistryMirror": [], "InstallURL": "https://get.docker.com" }, "SwarmOptions": { "IsSwarm": false, "Address": "", "Discovery": "", "Agent": false, "Master": false, "Host": "tcp://0.0.0.0:3376", "Image": "swarm:latest", "Strategy": "spread", "Heartbeat": 0, "Overcommit": 0, "ArbitraryFlags": [], "ArbitraryJoinFlags": [], "Env": null, "IsExperimental": false }, "AuthOptions": { "CertDir": "/root/.docker/machine/certs", "CaCertPath": "/root/.docker/machine/certs/ca.pem", "CaPrivateKeyPath": "/root/.docker/machine/certs/ca-key.pem", "CaCertRemotePath": "", "ServerCertPath": "/root/.docker/machine/machines/docker-node01/server.pem", "ServerKeyPath": "/root/.docker/machine/machines/docker-node01/server-key.pem", "ClientKeyPath": "/root/.docker/machine/certs/key.pem", "ServerCertRemotePath": "", "ServerKeyRemotePath": "", "ClientCertPath": "/root/.docker/machine/certs/cert.pem", "ServerCertSANs": [], "StorePath": "/root/.docker/machine/machines/docker-node01" } }, "Name": "docker-node01" } [root@node01 ~]#
提示:以上命令也支持-f选项来指定格式,用法同docker image/container inspect 类似;
[root@node01 ~]# docker-machine inspect -f {{.HostOptions.AuthOptions.StorePath}} docker-node01 /root/.docker/machine/machines/docker-node01 [root@node01 ~]# docker-machine inspect -f {{.DriverName}} docker-node01 generic [root@node01 ~]#
docker-machine ip :获取指定docker主机的ip地址
[root@node01 ~]# docker-machine ip docker-node01 192.168.0.42 [root@node01 ~]# docker-machine ip docker-node02 192.168.0.43 [root@node01 ~]#
docker-machine ssh :连接指定docker执行命令
[root@node01 ~]# docker-machine ssh docker-node01 "ip a" 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:91:99:30 brd ff:ff:ff:ff:ff:ff inet 192.168.0.42/24 brd 192.168.0.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe91:9930/64 scope link valid_lft forever preferred_lft forever 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:0f:e1:e0:f7 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:fff:fee1:e0f7/64 scope link valid_lft forever preferred_lft forever [root@node01 ~]#
docker-machine scp:在docker主机间以及docker主机和本地之间通过scp命令来远程复制文件
[root@node01 ~]# echo "this is test file" > /tmp/test.txt [root@node01 ~]# cat /tmp/test.txt this is test file [root@node01 ~]# docker-machine scp /tmp/test.txt docker-node01:/root/ test.txt 100% 18 5.4KB/s 00:00 [root@node01 ~]# docker-machine ssh docker-node01 "ls -l /root/" total 4 -rw-r--r-- 1 root root 18 Jun 19 11:26 test.txt [root@node01 ~]# docker-machine ssh docker-node01 "cat /root/test.txt" this is test file [root@node01 ~]#
提示:同scp命令用法类似;
docker-machine rm:删除指定名称的docker主机对应的虚拟主机;
[root@node01 ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS docker-node01 * generic Running tcp://192.168.0.42:2376 v19.03.11 docker-node02 - generic Running tcp://192.168.0.43:2376 v19.03.11 [root@node01 ~]# docker-machine rm docker-node02 About to remove docker-node02 WARNING: This action will delete both local reference and remote instance. Are you sure? (y/n): y Successfully removed docker-node02 [root@node01 ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS docker-node01 * generic Running tcp://192.168.0.42:2376 v19.03.11 [root@node01 ~]#
提示:docker-machine rm 只是删除docker-machine上的虚拟主机,对于真正的物理节点上的docker环境并没有删除;实际上就切断对指定docker主机的管控;
docker-machine upgrade:将指定主机的docker版本更新为最新
[root@node01 ~]# docker-machine upgrade docker-node01 Waiting for SSH to be available... Detecting the provisioner... Upgrading docker... Restarting docker... [root@node01 ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS docker-node01 * generic Running tcp://192.168.0.42:2376 v19.03.11 [root@node01 ~]#
提示:如果指定docker主机的版本已经是最新的版本,那么它将不会再更新;
docker-machine url:获取指定主机监听URL
[root@node01 ~]# docker-machine url docker-node01 tcp://192.168.0.42:2376 [root@node01 ~]#
提示:通过docker-machine安装的docker环境,实际上就是把yum安装的docker环境,客户端和服务端分离了,各个节点就是各个服务端,而docker-machine就是同一的客户端,因为客户端和服务端不再同一主机,所以它会把docker监听在一个TCP端口上,方便客户端的来连接管理;