欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker三剑客:docker-machine(认识docker-machine和实践)

程序员文章站 2022-07-01 11:24:46
...

一、认识docker-machine

  • docker machine是docker官方编排(orchestration)项目之一,负责在多种平台上快速安装docker环境。
  • docker machine支持在常规linux操作系统、虚拟化平台、openstack、共有云等不同环境下安装配置docker host。
  • docker machine项目基于go语言实现,目前github上的维护地址:https://github.com/docker/machine/
    docker三剑客:docker-machine(认识docker-machine和实践)

二、docker-machine实践

  1. docker-machine安装(下载docker-machine-Linux-x86_64)
mv docker-machine-Linux-x86_64 /usr/local/bin/docker-machine
chmod +x /usr/local/bin/docker-machine
docker-machine
  1. 在一台没有docker的主机上部署docker环境

2.1 做免密

[aaa@qq.com ~]# ssh-******
[aaa@qq.com ~]# ssh-copy-id server3

2.2. 自制yum共享仓库

[aaa@qq.com html]# pwd # apache发布目录
/var/www/html
[aaa@qq.com html]mkdir software
[aaa@qq.com html]
cp /home/kiosk/Desktop/container-selinux-2.77-1.el7.noarch.rpm software
[aaa@qq.com html]createrepo software  # 建立仓库

docker三剑客:docker-machine(认识docker-machine和实践)
2.4. docker-ce.repo文件

[aaa@qq.com software]# cat docker-ce.repo
[docker-ce]
name=docker-ce
baseurl=http://172.25.60.250/software  # 存放docker-cd的rpm文件的发布目录
gpgcheck=0

2.5. 下载get-docker.sh脚本并修改脚本

[aaa@qq.com software]# wget -O get-docker.sh https://get.docker.com/
[aaa@qq.com software]# vim get-docker.sh  # 写入docker-ce的yum源
398                         yum_repo="http://172.25.60.250/software/docker-ce.repo"

[aaa@qq.com ~]# vim /etc/os-release
3 ID="centos"

2.6. 给server3上安装docker

[aaa@qq.com ~]# docker-machine create --driver generic --engine-install-url "http://172.25.60.250/software/get-docker.sh" --generic-ip-address 172.25.60.3 server3
Running pre-create checks...
Creating machine...
(server3) 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 server3

测试:server3安装成功

[aaa@qq.com yum.repos.d]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

并且加密

[aaa@qq.com yum.repos.d]# 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=
  1. docker-machine管理

3.1. server3在docker-machine上的参数配置

[aaa@qq.com ~]# docker-machine config server3  # server3的配置参数
--tlsverify
--tlscacert="/root/.docker/machine/machines/server3/ca.pem"
--tlscert="/root/.docker/machine/machines/server3/cert.pem"
--tlskey="/root/.docker/machine/machines/server3/key.pem"
-H=tcp://172.25.60.3:2376

3.2. 远程控制server3

[aaa@qq.com ~]# docker `docker-machine config server3` ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[aaa@qq.com ~]# docker `docker-machine config server3` pull busybox

[aaa@qq.com ~]# docker `docker-machine config server3` images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              83aa35aa1c79        4 weeks ago         1.22MB
[aaa@qq.com ~]# docker `docker-machine config server3` run -it --rm busybox

[aaa@qq.com docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
46b574d0ebf7        busybox             "sh"                11 seconds ago      Up 9 seconds                            inspiring_mclean

[aaa@qq.com bin]# docker-machine env server3
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.25.60.3:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/server3"
export DOCKER_MACHINE_NAME="server3"
# Run this command to configure your shell: 
# eval $(docker-machine env server3)
[aaa@qq.com bin]# eval $(docker-machine env server3)  将server3的变量加到本地
  1. 让docker-machine管理server2
[aaa@qq.com yum.repos.d]# cat /etc/os-release
ID="centos"

做server2的免密

[aaa@qq.com ~]# ssh-copy-id server2

[aaa@qq.com ~]# docker-machine create --driver generic --engine-install-url "http://172.25.60.250/software/get-docker.sh" --generic-ip-address 172.25.60.2 server2

[aaa@qq.com ~]# eval $(docker-machine env server2)   转到server2
active的*表示正在使用server2
[aaa@qq.com ~]# docker-machine ls
NAME      ACTIVE   DRIVER    STATE     URL                      SWARM   DOCKER     ERRORS
server2   *        generic   Running   tcp://172.25.60.2:2376           v19.03.8   

server3   -        generic   Running   tcp://172.25.60.3:2376           v19.03.8  

并且当前的环境是server2

[aaa@qq.com ~]# env
DOCKER_HOST=tcp://172.25.60.2:2376
  1. 当转换管理的docker主机时,显示当前所在的docker主机
    下载三个脚本到docker-machine上
    docker三剑客:docker-machine(认识docker-machine和实践)
脚本存放位置
[aaa@qq.com ~]#cd /etc/bash_completion.d/
[aaa@qq.com /etc/bash_completion.d]#ls
docker-machine.bash         docker-machine-wrapper.bash  rct               rhn-migrate-classic-to-rhsm  rhsm-debug  subscription-manager
docker-machine-prompt.bash  iprutils                     redefine_filedir  rhsmcertd                    rhsm-icon   yum-utils.bash

[aaa@qq.com bash_completion.d]# source /etc/bash_completion.d/docker-machine-prompt.bash

[aaa@qq.com ~]#vim ~/.bashrc
PS1='[\aaa@qq.com\h \w$(__docker_machine_ps1)]\$'

[aaa@qq.com ~]#eval $(docker-machine env server2)
[aaa@qq.com ~ [server2]]#logout

  1. 安装指定版本docker
删除之前的安装
server1:
docker-machine rm server3

server3:
systemctl stop docker
yum remove docker-ce
yum remove docker-ce-cli
cd /etc/
rm -fr docker/
cd /etc/systemd/system/docker.service.d/
rm -f 10-machine.conf

指定安装版本
[aaa@qq.com software]# vim get-docker.sh
459                                 #if [ -n "$cli_pkg_version" ]; then
460                                 #       $sh_c "$pkg_manager install -y -q docker-ce-cli-19.03.8-3.el7"
461                                 #fi
462                                 $sh_c "$pkg_manager install -y -q docker-ce-19.03.8-3.el7 docker-ce-cli-19.03.8-3.el7"

[aaa@qq.com ~]#docker-machine create --driver generic --engine-install-url "http://172.25.60.250/software/get-docker.sh" --generic-ip-address 172.25.60.3 server3

三、总结

docker三剑客:docker-machine(认识docker-machine和实践)

相关标签: dcoker