一、docker machine说明

Docker Machine 使你能够部署多个远程docker 主机在不同linux。


二、安装docker machine

部署简说

192.168.2.120 docker-machine

192.168.2.121 linux 

192.168.2.122 linux


2.1.安装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 install /tmp/docker-machine /usr/local/bin/docker-machine

 

# docker-machine version
docker-machine version 0.16.0, build 702c267f


2.2.安装bash completion脚本

# 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
# source /etc/bash_completion.d/docker-machine-prompt.bash


查看命令:

# docker-machine 
active            create            help              ip                ls                provision         restart           scp               start             stop              url               version
config            env               inspect           kill              mount             regenerate-certs  rm                ssh               status            upgrade           use


三、创建docker machine

3.1.当前无docker machine

# docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS


3.2.Generic

3.2.1.在121上创建machine

# docker-machine create \
>   --driver generic \
>   --generic-ip-address=192.168.2.121 \
>   dm1
Running pre-create checks...
Creating machine...
(dm1) 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... #等待ssh响应
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... #配置daemon
Checking connection to Docker...
Docker is up and running! #启动docker
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env dm1

3.2.2.在122上创建machine

# docker-machine create   --driver generic   --generic-ip-address=192.168.2.122 dm2  
Running pre-create checks...
Creating machine...
(dm2) 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 dm2

3.3.查看创建结果

# docker-machine ls
NAME   ACTIVE   DRIVER    STATE     URL                        SWARM   DOCKER     ERRORS
dm1    -        generic   Running   tcp://192.168.2.121:2376           v18.09.0   
dm2    -        generic   Running   tcp://192.168.2.122:2376           v18.09.0

查看2.121远程docker配置

# cat /etc/systemd/system/docker.service.d/10-machine.conf 
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --storage-driver overlay2 --tlsverify --tlscacert /etc/docker/ca.pem --tlscert /etc/docker/server.pem --tlskey /etc/docker/server-key.pem --label provider=generic 
Environment=

四、管理machine

4.1.查看machine的env

# docker-machine env dm1
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.2.121:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/dm1"
export DOCKER_MACHINE_NAME="dm1"
# Run this command to configure your shell: 
# eval $(docker-machine env dm1)

4.2.获取主机IP

# docker-machine ip dm1
192.168.2.121
# docker-machine ip dm2
192.168.2.122

4.3.在dm1上运行nginx容器

# docker run -d -p 8000:80 nginx

4.4.在docker-machine 主机访问dm1的nginx

# curl $(docker-machine ip dm1):8000    
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4.5.拷贝文件

# docker-machine scp ckl.txt dm1:/root
ckl.txt                 100%   32     8.7KB/s   00:00

在dm1查看:

# ls
anaconda-ks.cfg  ckl.txt

4.5.获取dm1详情

# docker-machine inspect dm1
{
    "ConfigVersion": 3,
    "Driver": {
        "IPAddress": "192.168.2.121",
        "MachineName": "dm1",
        "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/dm1/server.pem",
            "ServerKeyPath": "/root/.docker/machine/machines/dm1/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/dm1"
        }
    },
    "Name": "dm1"
}

4.6.获取dm1配置

# docker-machine config dm1
--tlsverify
--tlscacert="/root/.docker/machine/machines/dm1/ca.pem"
--tlscert="/root/.docker/machine/machines/dm1/cert.pem"
--tlskey="/root/.docker/machine/machines/dm1/key.pem"
-H=tcp://192.168.2.121:2376

官网:https://docs.docker.com/machine/drivers/generic/#sudo-privileges