Docker三剑客

Docker集群管理工具

实验环境:CentOS 6.9

  • Docker Machine
  • Docker Compose
  • Docker Swarm

Docker Machine

  • 安装
# https://github.com/docker/machine/releases
$ sudo curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine
$ sudo chmod +x /usr/local/bin/docker-machine

# 查看版本
[root@db-slave ~]# docker-machine -v
docker-machine version 0.13.0, build 9ba6da9
  • 使用

Docker Machine 支持多种后端驱动,包括虚拟机、本地主机和云平台,使用 virtualbox 类型的驱动,创建一台 Docker 主机,命名为 aniu

[root@db-slave ~]# docker-machine create -d virtualbox aniu
Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"
# 注意这里报错提示需要安装VirtualBox环境
  • 配置VirtualBox源
# cat /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
You have new mail in /var/spool/mail/root
  • 安装VirtualBox
yum install -y VirtualBox-5.2 # 使用yum search VirtualBox 然后安装指定版本的VirtualBox
[root@db-slave ~]# sudo /sbin/vboxconfig # 重新加载VirtualBox服务
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
# 如果内核版本不一样可能会出错,需要:
[root@db-slave ~]# rpm -qa | grep kernel-lt
kernel-lt-4.4.103-1.el6.elrepo.x86_64
kernel-lt-devel-4.4.103-1.el6.elrepo.x86_64
# yum --enablerepo=elrepo-kernel install kernel-lt-devel kernel-lt-headers -y --skip-broken
  • 继续利用docker-machine创建docker主机
[root@db-slave ~]# docker-machine create -d virtualbox aniu
Running pre-create checks...
(aniu) Image cache directory does not exist, creating it at /root/.docker/machine/cache...
(aniu) No default Boot2Docker ISO found locally, downloading the latest release...
(aniu) Latest release for github.com/boot2docker/boot2docker is v17.11.0-ce
(aniu) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v17.11.0-ce/boot2docker.iso...
(aniu) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(aniu) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/aniu/boot2docker.iso...
(aniu) Creating VirtualBox VM...
(aniu) Creating SSH key...
(aniu) Starting the VM...
(aniu) Check network to re-create if needed...
(aniu) Found a new host-only adapter: "vboxnet0"
(aniu) Waiting for an IP...
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 boot2docker...
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 aniu
You have new mail in /var/spool/mail/root
  • 查看主机
# docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
aniu   -        virtualbox   Running   tcp://192.168.99.100:2376           v17.11.0-ce  
  • 创建主机成功后,可以通过 env 命令来让后续操作目标主机
$ docker-machine env aniu
  • 可以通过 SSH 登录到主机
$ docker-machine ssh aniu
这里写图片描述

官方支持的驱动列表

# 通过 -d 选项可以选择支持的驱动类型。
amazonec2
azure
digitalocean
exoscale
generic
google
none
openstack
rackspace
softlayer
virtualbox
vmwarevcloudair
vmwarevsphere
  • 常用操作命令
active 查看活跃的 Docker 主机
config 输出连接的配置信息
create 创建一个 Docker 主机
env 显示连接到某个主机需要的环境变量
inspect 输出主机更多信息
ip 获取主机地址
kill 停止某个主机
ls 列出所有管理的主机
provision 重新设置一个已存在的主机
regenerate-certs 为某个主机重新生成 TLS 认证信息
restart 重启主机
rm 删除某台主机
ssh SSH 到主机上执行命令
scp 在主机之间复制文件
mount 挂载主机目录到本地
start 启动一个主机
status 查看主机状态
stop 停止一个主机
upgrade 更新主机 Docker 版本为最新
url 获取主机的 URL
version 输出 docker-machine 版本信息
help 输出帮助信息

#每个命令,又带有不同的参数,可以通过
docker-machine COMMAND --help

Docker Compose

安装参考:http://blog.csdn.net/wh211212/article/details/78665052

[root@aniu-k8s ~]# docker-compose --help
Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)
  --verbose                   Show more output
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the name specified
                              in the client certificate (for example if your docker host
                              is an IP address)
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

Docker Swarm

Docker Swarm 是 Docker 官方三剑客项目之一,提供 Docker 容器集群服务,是 Docker 官方对容器云生态进行支持的核心方案。

  • Swarm mode

Docker 1.12 Swarm mode 已经内嵌入 Docker 引擎,成为了 docker 子命令 docker swarm。请注意与旧的 Docker Swarm 区分开来。

Swarm mode 内置 kv 存储功能,提供了众多的新特性,比如:具有容错能力的去中心化设计、内置服务发现、负载均衡、路由网格、动态伸缩、滚动更新、安全传输等。使得 Docker 原生的 Swarm 集群具备与 Mesos、Kubernetes 竞争的实力。

  • 基本概念

Swarm 是使用 SwarmKit 构建的 Docker 引擎内置(原生)的集群管理和编排工具

  • 节点

运行 Docker 的主机可以主动初始化一个 Swarm 集群或者加入一个已存在的 Swarm 集群,这样这个运行 Docker 的主机就成为一个 Swarm 集群的节点 (node) 。

节点分为管理 (manager) 节点和工作 (worker) 节点。

管理节点用于 Swarm 集群的管理,docker swarm 命令基本只能在管理节点执行(节点退出集群命令 docker swarm leave 可以在工作节点执行)。一个 Swarm 集群可以有多个管理节点,但只有一个管理节点可以成为 leader,leader 通过 raft 协议实现。

工作节点是任务执行节点,管理节点将服务 (service) 下发至工作节点执行。管理节点默认也作为工作节点。也可以通过配置让服务只运行在管理节点。

[图片上传失败...(image-9d0e17-1512205797078)]

  • 服务和任务

任务 (Task)是 Swarm 中的最小的调度单位,目前来说就是一个单一的容器。

服务 (Services) 是指一组任务的集合,服务定义了任务的属性。服务有两种模式:

replicated services 按照一定规则在各个工作节点上运行指定个数的任务。

global services 每个工作节点上运行一个任务

两种模式通过 docker service create 的 --mode 参数指定。

来自 Docker 官网的这张图片形象的展示了容器、任务、服务的关系。

[图片上传失败...(image-2d133b-1512205797078)]

创建Swarm 集群

  • 初始化集群(实验环境:CentOS7 Docker CE)

由于切换了实验主机,需要重新安装VirtualBox,注意CentOS6/7安装VirtualBox貌似不同,参考:

# CentOS7 安装VirtualBox
cd /etc/yum.repos.d
wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo

yum --enablerepo=epel install dkms
yum groupinstall "Development Tools" -y && yum install kernel-devel -y
# 安装VirtualBox
yum install VirtualBox-5.2 -y

# 参考链接:https://wiki.centos.org/HowTos/Virtualization/VirtualBox
  • 使用 docker swarm init 在本机初始化一个 Swarm 集群
[root@aniu-saas-4 ~]# docker swarm init --advertise-addr 192.168.0.209
Swarm initialized: current node (s2ffwdkzjntux1rarf139vygb) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4uijqfvw990m8j065ulwdzuv14ec6rmzgpljm5lhyd3rmv358o-ersojr3gglsj7v4i2enmmvt3z 192.168.0.209:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

如果 Docker 主机有多个网卡,拥有多个 IP,必须使用 --advertise-addr 指定 IP

  • 增加工作节点

使用 Docker Machine 创建两个 Docker 主机,并加入到swarm集群中

$ docker-machine create -d virtualbox worker1
$ docker-machine ssh worker1

docker@worker1:~$ docker swarm join --token SWMTKN-1-4uijqfvw990m8j065ulwdzuv14ec6rmzgpljm5lhyd3rmv358o-ersojr3gglsj7v4i2enmmvt3z 192.168.0.209:2377

# Running pre-create checks...
Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"

# 注意安装运行dockers swarm必须支持VT-X/AMD-v
$ docker-machine create -d virtualbox worker2
docker@worker1:~$ docker swarm join --token SWMTKN-1-4uijqfvw990m8j065ulwdzuv14ec6rmzgpljm5lhyd3rmv358o-ersojr3gglsj7v4i2enmmvt3z 192.168.0.209:2377

细心的读者可能通过 docker-machine create --help 查看到 --swarm* 等一系列参数。该参数是用于旧的 Docker Swarm,与本章所讲的 Swarm mode 没有关系

  • 查看集群
$ docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
03g1y59jwfg7cf99w4lt0f662    worker2   Ready   Active
9j68exjopxe7wfl6yuxml7a7j    worker1   Ready   Active
dxn1zf6l61qsb1josjja83ngz *  manager1  Ready   Active        Leader

参考教程

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,873评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,483评论 1 306
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,525评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,595评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,018评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,958评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,118评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,873评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,643评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,813评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,293评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,615评论 3 262
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,306评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,170评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,968评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,107评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,894评论 2 278

推荐阅读更多精彩内容