Docker三劍客

Docker集群管理工具

實驗環(huán)境: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 支持多種后端驅(qū)動,包括虛擬機、本地主機和云平臺,使用 virtualbox 類型的驅(qū)動,創(chuàng)建一臺 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環(huán)境
  • 配置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服務(wù)
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
# 如果內(nèi)核版本不一樣可能會出錯,需要:
[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
  • 繼續(xù)利用docker-machine創(chuàng)建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  
  • 創(chuàng)建主機成功后,可以通過 env 命令來讓后續(xù)操作目標(biāo)主機
$ docker-machine env aniu
  • 可以通過 SSH 登錄到主機
$ docker-machine ssh aniu
這里寫圖片描述

官方支持的驅(qū)動列表

# 通過 -d 選項可以選擇支持的驅(qū)動類型。
amazonec2
azure
digitalocean
exoscale
generic
google
none
openstack
rackspace
softlayer
virtualbox
vmwarevcloudair
vmwarevsphere
  • 常用操作命令
active 查看活躍的 Docker 主機
config 輸出連接的配置信息
create 創(chuàng)建一個 Docker 主機
env 顯示連接到某個主機需要的環(huán)境變量
inspect 輸出主機更多信息
ip 獲取主機地址
kill 停止某個主機
ls 列出所有管理的主機
provision 重新設(shè)置一個已存在的主機
regenerate-certs 為某個主機重新生成 TLS 認(rèn)證信息
restart 重啟主機
rm 刪除某臺主機
ssh SSH 到主機上執(zhí)行命令
scp 在主機之間復(fù)制文件
mount 掛載主機目錄到本地
start 啟動一個主機
status 查看主機狀態(tài)
stop 停止一個主機
upgrade 更新主機 Docker 版本為最新
url 獲取主機的 URL
version 輸出 docker-machine 版本信息
help 輸出幫助信息

#每個命令,又帶有不同的參數(shù),可以通過
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 容器集群服務(wù),是 Docker 官方對容器云生態(tài)進行支持的核心方案。

  • Swarm mode

Docker 1.12 Swarm mode 已經(jīng)內(nèi)嵌入 Docker 引擎,成為了 docker 子命令 docker swarm。請注意與舊的 Docker Swarm 區(qū)分開來。

Swarm mode 內(nèi)置 kv 存儲功能,提供了眾多的新特性,比如:具有容錯能力的去中心化設(shè)計、內(nèi)置服務(wù)發(fā)現(xiàn)、負(fù)載均衡、路由網(wǎng)格、動態(tài)伸縮、滾動更新、安全傳輸?shù)?。使?Docker 原生的 Swarm 集群具備與 Mesos、Kubernetes 競爭的實力。

  • 基本概念

Swarm 是使用 SwarmKit 構(gòu)建的 Docker 引擎內(nèi)置(原生)的集群管理和編排工具

  • 節(jié)點

運行 Docker 的主機可以主動初始化一個 Swarm 集群或者加入一個已存在的 Swarm 集群,這樣這個運行 Docker 的主機就成為一個 Swarm 集群的節(jié)點 (node) 。

節(jié)點分為管理 (manager) 節(jié)點和工作 (worker) 節(jié)點。

管理節(jié)點用于 Swarm 集群的管理,docker swarm 命令基本只能在管理節(jié)點執(zhí)行(節(jié)點退出集群命令 docker swarm leave 可以在工作節(jié)點執(zhí)行)。一個 Swarm 集群可以有多個管理節(jié)點,但只有一個管理節(jié)點可以成為 leader,leader 通過 raft 協(xié)議實現(xiàn)。

工作節(jié)點是任務(wù)執(zhí)行節(jié)點,管理節(jié)點將服務(wù) (service) 下發(fā)至工作節(jié)點執(zhí)行。管理節(jié)點默認(rèn)也作為工作節(jié)點。也可以通過配置讓服務(wù)只運行在管理節(jié)點。

[圖片上傳失敗...(image-9d0e17-1512205797078)]

  • 服務(wù)和任務(wù)

任務(wù) (Task)是 Swarm 中的最小的調(diào)度單位,目前來說就是一個單一的容器。

服務(wù) (Services) 是指一組任務(wù)的集合,服務(wù)定義了任務(wù)的屬性。服務(wù)有兩種模式:

replicated services 按照一定規(guī)則在各個工作節(jié)點上運行指定個數(shù)的任務(wù)。

global services 每個工作節(jié)點上運行一個任務(wù)

兩種模式通過 docker service create 的 --mode 參數(shù)指定。

來自 Docker 官網(wǎng)的這張圖片形象的展示了容器、任務(wù)、服務(wù)的關(guān)系。

[圖片上傳失敗...(image-2d133b-1512205797078)]

創(chuàng)建Swarm 集群

  • 初始化集群(實驗環(huán)境: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 主機有多個網(wǎng)卡,擁有多個 IP,必須使用 --advertise-addr 指定 IP

  • 增加工作節(jié)點

使用 Docker Machine 創(chuàng)建兩個 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

細(xì)心的讀者可能通過 docker-machine create --help 查看到 --swarm* 等一系列參數(shù)。該參數(shù)是用于舊的 Docker Swarm,與本章所講的 Swarm mode 沒有關(guān)系

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

參考教程

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容