Docker容器学习与分享12
Docker多主机管理
之前在一台Centos7上安装了Docker,如果是在多台主机上都安装Docker,用手动安装的方法不光效率低下,而且有可能出错,所以可以使用Docker Machine进行多台主机的Docker安装和管理。
具体安装过程可以参照官方文档https://docs.docker.com/machine/install-machine/.
首先准备两台Centos7的虚拟机(一台IP为192.168.41.133,安装Docker Machine,另一台IP为192.168.41.135),在其中一台机器上安装Docker,安装过程可以参照分享01.
然后下载Docker Machine二进制文件并将其解压缩到PATH。
[root@promote ~]# base=https://github.com/docker/machine/releases/download/v0.14.0 &&
> curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
> sudo install /tmp/docker-machine /usr/local/bin/docker-machine
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 583 0 --:--:-- 0:00:01 --:--:-- 584
100 26.7M 100 26.7M 0 0 1730k 0 0:00:15 0:00:15 --:--:-- 3488k
接着通过显示机器版本来检查安装:
[root@promote ~]# docker-machine version
docker-machine version 0.14.0, build 89b8332
为了更好的体验接着安装bash完成脚本,将脚本保存到/etc/bash_completion.d或 /usr/local/etc/bash_completion.d。
[root@promote ~]# cd /etc/bash_completion.d
[root@promote bash_completion.d]# base=https://raw.githubusercontent.com/docker/machine/v0.14.0
[root@promote bash_completion.d]# 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
--2018-07-11 01:47:47-- https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker- machine-prompt.bash
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1469 (1.4K) [text/plain]
Saving to: ‘/etc/bash_completion.d/docker-machine-prompt.bash’
100%[=======================================================================>] 1,469 --.-K/s in 0s
2018-07-11 01:47:48 (192 MB/s) - ‘/etc/bash_completion.d/docker-machine-prompt.bash’ saved [1469/1469]
--2018-07-11 01:47:48-- https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker- machine-wrapper.bash
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1525 (1.5K) [text/plain]
Saving to: ‘/etc/bash_completion.d/docker-machine-wrapper.bash’
100%[=======================================================================>] 1,525 --.-K/s in 0s
2018-07-11 01:47:50 (160 MB/s) - ‘/etc/bash_completion.d/docker-machine-wrapper.bash’ saved [1525/1525]
--2018-07-11 01:47:50-- https://raw.githubusercontent.com/docker/machine/v0.14.0/contrib/completion/bash/docker- machine.bash
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.40.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.40.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12205 (12K) [text/plain]
Saving to: ‘/etc/bash_completion.d/docker-machine.bash’
100%[=======================================================================>] 12,205 --.-K/s in 0.02s
2018-07-11 01:47:51 (489 KB/s) - ‘/etc/bash_completion.d/docker-machine.bash’ saved [12205/12205]
这样就下载好了脚本。
接着申明环境变量。
[root@promote bash_completion.d]# source /etc/bash_completion.d/docker-machine-prompt.bash
可以先使用docker-machine ls查看一下当前的machine.
[root@promote ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
因为还没有安装Docker所以使用这条命令什么都没有。
接着创建第一台机器,就是另外一台Centos虚拟机(IP:192.168.41.155).
1.首先需要让这两台Centos可以SSH免密登陆。
[root@promote ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:T6qcnNvYwepsMhGfdvG/Gk+BtzVe2eCLiF0aMAI6wqc root@promote.cache-dns.local
The key's randomart image is:
+---[RSA 2048]----+
| . |
|. . . |
|..o. . o . |
| .o.. ..o . . .o|
| E o .So+ + +.o|
| . +..*.* * + |
| o .= *.+ o |
| o+.O . +. |
| =%.o ..o. |
+----[SHA256]-----+
[root@promote ~]# ssh-copy-id 192.168.41.135
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.41.135 (192.168.41.135)' can't be established.
ECDSA key fingerprint is SHA256:JqAC8jcLCLobvRy0wzY9VGBNuZU3EydpO8n2fEtQ178.
ECDSA key fingerprint is MD5:5d:26:a1:60:c3:eb:02:e9:97:7a:bb:7a:49:8a:14:0b.
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.41.135's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.41.135'"
and check to make sure that only the key(s) you wanted were added.
2.创建第一个机器
使用docker-machine create命令,因为是Linux,所以driver就用generic,目标地址选项用--generic-ip-address。
[root@promote ~]# docker-machine create --driver generic --generic-ip-address=192.168.41.135 machine1
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(machine1) 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 machine1
现在到192.168.41.135上去验证。
[root@promote ~]# docker version
Client:
Version: 18.05.0-ce
API version: 1.37
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:14:54 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.05.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:18:36 2018
OS/Arch: linux/amd64
Experimental: false
也可以在192.168.41.133上验证。
[root@promote ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
machine1 - generic Running tcp://192.168.41.135:2376 v18.05.0-ce
发现安装完成。
接着我通过192.168.41.133这台主机来使用192.168.41.135主机上的Docker,首先查看访问machine所需要的环境变量。
[root@promote ~]# docker-machine env machine1
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.41.135:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/machine1"
export DOCKER_MACHINE_NAME="machine1"
# Run this command to configure your shell:
# eval $(docker-machine env machine1)
可以看到倒数第二行:运行此命令来配置你的shell
也就是指运行eval $(docker-machine env machine1)命令配置上面的环境变量进入machine1.
运行之后发现并无变化,是因为一开始我们没有配置shell提示.
接着启用docker-machineshell提示,添加 $(__docker_machine_ps1)到您的PS1设置中~/.bashrc。
[root@promote ~]# PS1='[\u@\h \W$(__docker_machine_ps1)]\$ '
[root@promote ~ [machine1]]#
这时发现shell变了,在当前状态下的所有docker操作都是运行在machine1上的,即192.168.41.135主机上。
如果要切回回来的环境使用unset命令,取消环境变量即可:
[root@promote ~ [machine1]]# unset export DOCKER_TLS_VERIFY
[root@promote ~ [machine1]]# unset DOCKER_HOST
[root@promote ~ [machine1]]# unset DOCKER_CERT_PATH
[root@promote ~ [machine1]]# unset DOCKER_MACHINE_NAME
[root@promote ~]#
发现变回来了。
docker-machine的其他用法可以使用docker-machine --help命令来查看。
Docker容器学习与分享12的更多相关文章
- Docker容器学习与分享07
Docker容器网络 在分享06中学完了bridge网络,接着学习none网络和host网络. Docker在安装时会在host上默认创建三个网络,分别是bridge.host.null. [root ...
- Docker容器学习与分享10
Docker容器向外提供服务 用分享04中的Nginx服务来试一下. 不过这次我直接用Nginx镜像创建容器,先下载Nginx镜像. [root@promote ~]# docker search n ...
- Docker容器学习与分享09
Docker容器之间的相互通信 先新建两个不同的网段,就用分享08里的两个网段作为新建的网段. [root@promote ~]# docker network ls NETWORK ID NAME ...
- Docker容器学习与分享04
Docker容器的基本操作(2) 基于docker分享03的centos容器,接着学习docker容器的基本操作. docker分享03中创建了一个centos镜像,如果想要查看容器的具体信息就要使用 ...
- Docker容器学习与分享03
Docker容器的基本操作 所有的docker命令都是以docker开头,也就是指调用docker程序.我学习的第一个命令就是docker run,运行一个容器.以Docker分享02中的容器为例: ...
- Docker容器学习与分享06
Docker容器网络 Docker有三种原生网络:none网络.host网络.bridge网络. 先来学习一下bridge网络. 首先使用ifconfig命令查看一下本机的网络设备: 从图中可以看见多 ...
- Docker容器学习与分享05
Docker镜像操作 学完了一些最基本的操作之后,我学习了一些关于docker镜像的基本操作. 首先来学习一下从docker hub上拉取镜像,以centos镜像为例,使用docker search命 ...
- Docker容器学习与分享02
1.docker容器的创建 首先运行一个centos容器,感受一下Docker容器的便捷 首先先看一下镜像仓库 发现仓库里没有镜像,也就是没有创建容器的模板,这时考虑从REPOSITORY中拉取镜像( ...
- Docker容器学习与分享01
1.什么是容器? 容器技术是一种虚拟化的方案,与传统的虚拟机不同,传统的虚拟机是通过中间层将一台或多台独立的机器虚拟运行于物理硬件之上,而容器是直接运行在操作系统内核之上的用户空间. 所以容器虚拟化又 ...
随机推荐
- java对文件的检索
先说下思路吧. 1.先把xls等文件转换为静态html文件:可以用很多插件,类似我的博客地址: http://www.cnblogs.com/Alandre/p/3221006.html 2.再对转的 ...
- mysql表分区实战
一,什么是数据库分区以mysql为例,mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面 (可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件 ...
- 探秘 Java 热部署
# 前言 在之前的 深入浅出 JVM ClassLoader 一文中,我们说可以通过修改默认的类加载器实现热部署,但在 Java 开发领域,热部署一直是一个难以解决的问题,目前的 Java 虚拟机只能 ...
- [转]Angular4 自制分页控件
本文转自:https://blog.csdn.net/Junyuan_123/article/details/79486276 过年后第一波,自制的分页控件,可能功能没有 PrimeNG 那么好,但是 ...
- 牛客网剑指offer 二维数组的查找
题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 解题思路 该题有很多种 ...
- 【实践练习一】Git以及Github的使用
以前经常在同学大神那听说过Github这神器,虽敬佩久已,奈何却无缘使用.好吧,我承认,主要还是不会用,一看网站全是英文的,想想还是不要为难自己了.然而现在还是要为难自己了,趁着早上刚学 ...
- js 如何移除一个匿名函数的绑定事件
大家都知道 addEventListener的用法 绑定事件 例如 element.addEventListener(type,handler,false); element是dom元素 type是事 ...
- blfs(systemv版本)学习笔记-编译安装配置dhcpcd
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! dhcpcd项目地址:http://www.linuxfromscratch.org/blfs/view/8.3/basicne ...
- linux学习笔记-解决google-chrome打开后弹出输入密码以解锁您的登录密钥环的提示
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.理论知识 1.密钥的作用 google-chrome存储了网站登录时使用的账号和密码信息,这个密钥是用来保护这些信息的 2. ...
- 泛化之美--C++11可变模版参数的妙用
1概述 C++11的新特性--可变模版参数(variadic templates)是C++11新增的最强大的特性之一,它对参数进行了高度泛化,它能表示0到任意个数.任意类型的参数.相比C++98/03 ...