实验环境:CentOS 6.9
# 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环境
# 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
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
[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
$ docker-machine env aniu
$ 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
[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 官方三剑客项目之一,提供 Docker 容器集群服务,是 Docker 官方对容器云生态进行支持的核心方案。
Swarm 是使用 SwarmKit 构建的 Docker 引擎内置(原生)的集群管理和编排工具
运行 Docker 的主机可以主动初始化一个 Swarm 集群或者加入一个已存在的 Swarm 集群,这样这个运行 Docker 的主机就成为一个 Swarm 集群的节点 (node) 。 节点分为管理 (manager) 节点和工作 (worker) 节点。 管理节点用于 Swarm 集群的管理,docker swarm 命令基本只能在管理节点执行(节点退出集群命令 docker swarm leave 可以在工作节点执行)。一个 Swarm 集群可以有多个管理节点,但只有一个管理节点可以成为 leader,leader 通过 raft 协议实现。 工作节点是任务执行节点,管理节点将服务 (service) 下发至工作节点执行。管理节点默认也作为工作节点。也可以通过配置让服务只运行在管理节点。
任务 (Task)是 Swarm 中的最小的调度单位,目前来说就是一个单一的容器。 服务 (Services) 是指一组任务的集合,服务定义了任务的属性。服务有两种模式: replicated services 按照一定规则在各个工作节点上运行指定个数的任务。 global services 每个工作节点上运行一个任务 两种模式通过 docker service create 的 –mode 参数指定。 来自 Docker 官网的这张图片形象的展示了容器、任务、服务的关系。
由于切换了实验主机,需要重新安装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
[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集群中undefined
$ 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