docker-machine
docker-machine
- 是一个简化docker安装的命令行工具。可以让主机一瞬间安装很多容器,并且可以远程进行管理
安装
复制官网的命令下载完即可完成安装
[root@bogon ~]# 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
0 0 0 0 0 0 0 0 --:--:-- 0:00:05 --:--:-- 0
100 26.8M 100 26.8M 0 0 46349 0 0:10:07 0:10:07 --:--:-- 108k
查看版本号
[root@bogon ~]# docker-machine -v
docker-machine version 0.16.0, build 702c267f
官方让将一下代码复制到脚本中
[root@bogon ~]# cd /etc/bash_completion.d/
[root@bogon bash_completion.d]# vi down.sh
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@bogon bash_completion.d]# source down.sh
上面是正常的顺序,但是需要连接谷歌
不连接谷歌这样做
https://github.com/docker/machine gitub这个网站
复制这个网站的三个脚本代码 https://github.com/docker/machine/tree/master/contrib/completion/bash
# 复制代码到这个目录里就可以了 [root@bogon bash_completion.d]# vi docker-machine-prompt.bash [root@bogon bash_completion.d]# vi docker-machine-wrapper.bash [root@bogon bash_completion.d]# vi docker-machine.bash [root@bogon bash_completion.d]# pwd /etc/bash_completion.d
设置docker-machine的环境提示
根据官网中的提示,要启用docker-machine外壳程序提示,$(__docker_machine_ps1)请PS1在中添加 设置~/.bashrc。
# 家目录 [root@bogon ~]# vi .bashrc # 最后添加 PS1='[\u@\h \W$(__docker_machine_ps1)]\$ '
[root@bogon ~]# cd - /etc/bash_completion.d [root@bogon bash_completion.d]# cp -p docker-machine.bash docker-machine [root@bogon ~]# docker-machine --version docker-machine version 0.16.0, build 702c267f
远程安装docker
- 192.168.100.212 对另外两台主机设置免密登录
192.168.100.212 docker-machine 192.168.100.202 host1 192.168.100.205 host2 三台主机都需要修改
# 注意: 三台主机都做 都做 [root@bogon ~]# vim /etc/ssh/sshd_config # 将以下取消注释,表示允许root权限登录操作 PermitRootLogin yes [root@bogon ~]# systemctl restart sshd [root@bogon ~]# vim /etc/sudoers # 将以下内容去掉!符号 # 提权的意思 保持权限连接ssh Defaults !visiblepw
生成秘钥
# 生成秘钥
[root@bogon ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:bGNIS9qQj3TO4TqW6v2CaEFpFk0NCCnVyCfhYy9cWNc root@bogon
The key's randomart image is:
+---[RSA 2048]----+
|o+**+ .. |
|o+++o+ E |
|. Bo= = |
| B = & = |
|+ o + O S |
| . . o o . |
| ...= |
|...+.. |
|..o .o. |
+----[SHA256]-----+
对202(host1)进行免密登录
# 对 204进行免密登录
[root@bogon ~]# ssh-copy-id -i root@192.168.100.202
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.100.202's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.204'"
and check to make sure that only the key(s) you wanted were added.
对205(host2)进行免密登录
# 对205进行免密登录
[root@bogon ~]# ssh-copy-id -i root@192.168.100.205
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.100.205's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.205'"
and check to make sure that only the key(s) you wanted were added.
- 212远程安装docker
远程安装需要指定驱动器 --driver
--generic-ip-address 是指定那台主机要安装docker - linux要使用 generic
- mac,windows 参考官网 https://docs.docker.com/machine/drivers/azure/
为202安装docker 起名为 host1
# 为202安装docker 起名为 host1 [root@localhost ~]# docker-machine create --driver generic --generic-ip-address 192.168.100.202 host1 Running pre-create checks... Creating machine... (host1) 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 host1
为205安装docker 起名为 host2
[root@localhost ~]# docker-machine create --driver generic --generic-ip-address 192.168.100.205 host2 Running pre-create checks... Creating machine... (host2) 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 host2
查看状态
[root@localhost ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS host1 - generic Running tcp://192.168.100.202:2376 v20.10.0 host2 - generic Running tcp://192.168.100.205:2376 v20.10.0
下面根据实验来使用docker-machine的命令
下载镜像
host1
[root@localhost ~]# docker-machine env host1 export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.100.202:2376" export DOCKER_CERT_PATH="/root/.docker/machine/machines/host1" export DOCKER_MACHINE_NAME="host1" # Run this command to configure your shell: # eval $(docker-machine env host1)
根据上面的提示运行eval $(docker-machine env host1) 可以进入host1环境中
# 先执行bash的原因是 # 如果exit就直接把当前主机的bash退出了 [root@localhost ~]# bash [root@localhost ~]# eval $(docker-machine env host1) # 下载镜像 [root@localhost ~ [host1]]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd 852e50cd189d: Pull complete 67d51c33d390: Pull complete b0ad2a3b9567: Pull complete 136f1f71f30c: Pull complete 01f8ace29294: Pull complete Digest: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456 Status: Downloaded newer image for httpd:latest docker.io/library/httpd:latest
host2
[root@localhost ~]# docker-machine env host2 export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.100.205:2376" export DOCKER_CERT_PATH="/root/.docker/machine/machines/host2" export DOCKER_MACHINE_NAME="host2" # Run this command to configure your shell: # eval $(docker-machine env host2) # 根据上面的提示运行eval $(docker-machine env host1) 可以进入host1环境中 # 先执行bash的原因是 # 如果exit就直接把当前主机的bash退出了 [root@localhost ~]# bash [root@localhost ~]# eval $(docker-machine env host2) # 下载镜像 [root@localhost ~ [host2]]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd 852e50cd189d: Pull complete 67d51c33d390: Pull complete b0ad2a3b9567: Pull complete 136f1f71f30c: Pull complete 01f8ace29294: Pull complete Digest: sha256:fddc534b7f6bb6197855be559244adb11907d569aae1283db8e6ce8bb8f6f456 Status: Downloaded newer image for httpd:latest docker.io/library/httpd:latest
启动和正常启动一样
映射挂载目录
将docker-machine主机上的目录映射到被管理端的容器中
安装支持docker-machine使用mount命令的工具
#安装支持docker-machine使用mount命令的工具 [root@localhost ~]# yum install -y epel-release [root@localhost ~]# yum -y install fuse-sshfs # docker-machine创建目录 [root@localhost ~ [host1]]# mkdir htdocs
在两个host中创建htdocs目录
# 在两个host中创建htdocs目录 # machine的目录必须为空,要不然挂不进去 [root@localhost ~]# docker-machine ssh host1 mkdir /htdocs [root@localhost ~]# docker-machine ssh host2 mkdir /htdocs
将machine的目录挂载到host1和host2
# [root@localhost ~]# docker-machine mount host1:/root/htdocs htdocs/ [root@localhost ~]# docker-machine mount host2:/root/htdocs htdocs/
启动两个容器
## #进入 host1启动httpd [root@localhost ~]# eval $(docker-machine env host1) [root@localhost ~ [host1]]# [root@localhost ~ [host1]]# docker run -itd --name httpd -p 80:80 --volume /root/htdocs:/usr/local/apache2/htdocs httpd 4014c56e36f6fda2800953d468efd634bb7c08b7672517427dc3aea63e10f56b #进入 host2启动httpd [root@localhost ~]# eval $(docker-machine env host2) [root@localhost ~ [host2]]# docker run -itd --name httpd -p 80:80 --volume /root/htdocs:/usr/local/apache2/htdocs httpd 06df25f57fa1b340cadc0429e6345247ef294b00c1a7042655c1b73a6006003b
在本地的 htdocs 写入文件
[root@localhost ~ [host1]]# echo '李明凯' > htdocs/index.html
访问
[root@localhost htdocs]# curl 192.168.100.205 李明凯 [root@localhost htdocs]# curl 192.168.100.202 李明凯
其他命令
查看主机配置
- docker-machine inspect host
查看被管理端ip[root@localhost ~]# docker-machine inspect host1 { "ConfigVersion": 3, "Driver": { "IPAddress": "192.168.100.202", # 列举一点
被管理端之间的文件传输[root@localhost ~]# docker-machine ip host1 host2 192.168.100.202 192.168.100.205
批量升级docker版本# host1主机内 [root@localhost ~]# echo "This file in the host1" > /tmp/host1 # docker-machine [root@localhost ~]# docker-machine scp host1:/tmp/host1 host2:/tmp/bb [root@localhost ~]# docker-machine ssh host2 cat /tmp/bb This file in the host1
远程主机切换远程主机[root@localhost ~]# docker-machine upgrade host1 host2
ssh连接远程主机执行命令[root@localhost ~ [host1]]# eval $(docker-machine env host2)
映射挂载目录# 查看了host1的网卡 [root@localhost ~]# docker-machine ssh host1 ifconfig docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:34ff:fef6:6c21 prefixlen 64 scopeid 0x20<link> ether 02:42:34:f6:6c:21 txqueuelen 0 (Ethernet) RX packets 12 bytes 1090 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 29 bytes 2902 (2.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.202 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::f056:ea91:f190:c311 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:e3:48:83 txqueuelen 1000 (Ethernet) RX packets 1033084 bytes 1166003951 (1.0 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 344264 bytes 37918295 (36.1 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 安装支持docker-machine使用mount命令的工具 [root@localhost ~]# yum -y install fuse-sshfs rpel-release # 创建要挂载的目录 [root@localhost ~]# mkdir /lmk # 在host1中创建挂载目录 [root@localhost ~]# docker-machine ssh host1 mkdir /kml # 将host1中的kml目录挂载到docker-machine主机的lmk目录 [root@localhost ~]# docker-machine mount host1:/kml /lmk # 在本机cyj目录创建index.html文件 [root@localhost ~]# echo "The page in the docker-machine" > /lmk/index.html # 查看在host1的挂载目录是否存在 [root@localhost ~]# docker-machine ssh host1 ls /kml index.html [root@localhost ~]# docker-machine ssh host1 cat /kml/index.html The page in the docker-machine
- 卸载 : docker-machine mount -u host1:/kml /lmk
远程重启
docker-machine restart host1
本博客所有文章是以学习为目的,如果有不对的地方可以一起交流沟通共同学习 邮箱:1248287831@qq.com!