Docker Machine 是Docker官方三劍客項目之一。
官網地址:https://docs.docker.com/machine/
負責使用docker容器的第一步:在多種平台上快速安裝和維護docker運行環境。讓用户可以在很短時間內在本地或雲環境中搭建一套docker主機集羣
Machine基本功能包括:
1:在指定節點或平台上安裝Docker引擎,配置其為可使用的Dokcer環境
2:集中管理(包括啓動,查看等)所安裝的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 mv /tmp/docker-machine /usr/local/bin/docker-machine &&
chmod +x /usr/local/bin/docker-machine
驗證安裝是否成功,查看版本號
[root@server01 ~]# docker-machine -v
docker-machine version 0.16.0, build 702c267f
安裝命令自動補全腳本
運行以下腳本
#!/bin/ba/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
會將下載好的腳本保存在/etc/bash_completion.d路徑下
最後運行:source /etc/bash_completion.d/docker-machine-prompt.bash 使之生效
使用Machine
Machine連接不同類型的操作平台是通過對應驅動來實現的,目前已經集成了包括AWS,IBM,Google,以及OpenStack,VirtualBox,Vsphere等多種雲平台的支持。
通過-d選項可以選擇支持的驅動類型
實驗一:本地主機
這種驅動適合主機操作系統和ssh服務都已經安裝好了,需要對其安裝doker引擎。
首先確保本地主機(安裝了docker-machine)可以免密登錄到目標主機。
過ssh-keygen命令生成密鑰對,密鑰類型為RSA
[root@server01 ~]# ssh-keygen -t rsa
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:WGQJ1FeUzxmiZmjljcYQ5pBNEvZ7NBogykgYYgLkoto root@server01
The key's randomart image is:
+---[RSA 2048]----+
|O= ..OB*o oo. |
|Bo . o.X= o o . |
|o.o =Bo+ + o |
|o oo=O.. + |
|. ..S+. |
|.. . |
|. E |
| |
| |
+----[SHA256]-----+
將公鑰傳給目標主機:
[root@server01 ~]# ssh-copy-id root@192.168.1.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
驗證免密登錄
[root@server01 ~]# ssh root@192.168.1.7
Last login: Sun Mar 15 10:28:08 2020 from 192.168.1.103
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.7 netmask 255.255.255.0 broadcast 192.168.1.255
generic類型的驅動,註冊一台docker主機,命名為wg01
確保192.168.1.7等訪問外網!
[root@server01 ~]# docker-machine create -d generic --generic-ip-address=192.168.1.7 --generic-ssh-user=root wg01
Running pre-create checks...
Creating machine...
(wg01) 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 wg01
列出所有管理的docker主機
[root@server01 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
wg01 - generic Running tcp://192.168.1.7:2376 v19.03.8
[root@server01 ~]#
Machine命令
1 active 查看活動的Docker主機
激活狀態意味着當前的DOCKER_HOST環境變量指向該主機
[root@server01 ~]# export DOCKER_HOST="tcp://192.168.1.7:2376"
[root@server01 ~]# echo $DOCKER_HOST
tcp://192.168.1.7:2376
[root@server01 ~]# docker-machine active
wg01
2 config 輸出連接的配置信息
ca證書,公鑰,私鑰 docker監聽的地址
[root@server01 ~]# docker-machine config wg01
--tlsverify
--tlscacert="/root/.docker/machine/machines/wg01/ca.pem"
--tlscert="/root/.docker/machine/machines/wg01/cert.pem"
--tlskey="/root/.docker/machine/machines/wg01/key.pem"
-H=tcp://192.168.1.7:2376
3 create 創建一個docker主機
docker-machine create -d generic --generic-ip-address=192.168.1.7 --generic-ssh-user=root wg01
-d:驅動類型
--generic-ip-address:目標主機ip地址
--generic-ssh-user:目標主機授權登錄用户
4 env 顯示連接到某個主機需要的環境變量
[root@server01 ~]# docker-machine env wg01
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.1.7:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/wg01"
export DOCKER_MACHINE_NAME="wg01"
# Run this command to configure your shell:
# eval $(docker-machine env wg01)
5 inspect 輸出主機的詳細信息
[root@server01 ~]# docker-machine inspect -f {{.Driver.IPAddress}} wg01
192.168.1.7
6 獲取docker主機ip地址
[root@server01 ~]# docker-machine ip wg01
192.168.1.7
7 ls 列出所有管理的docker主機
192.168.1.7
[root@server01 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
wg01 * generic Running tcp://192.168.1.7:2376 v19.03.8
8 url 獲取主機的url
[root@server01 ~]# docker-machine url wg01
tcp://192.168.1.7:2376
9 rm 刪除docker主機 (不演試了!)