在文章4蜂群中,通过docker-machine env命令操作docker-machine十分方便,当我们在真实环境中部署时,也需要实现这种效果。Docker可以对接多种云服务提供商,目前我用阿里云ECS主机较多,而Docker目前不支持,所以尝试使用Docker Cloud Agent。
1.docker可以配合多种云服务提供商
Amazon Web Services setup guide
Use the Docker Cloud Agent to bring your own host
Docker Cloud Agent,支持如下系统:
Ubuntu 14.04, 15.04, 15.10
Debian 8
Centos 7
Red Hat Enterprise Linux 7
Fedora 21, 22, 23
2.安装Docker Cloud Agent
Docker Cloud允许用任意linux主机作为节点,可以通过这个主机部署容器。
为了实现这样的功能,必须在linux主机安装Docker Cloud Agent,这样Docker Cloud就可以远程远程管理了
2.1 CentOS安装Docker
为了测试,使用阿里云ECS服务器,系统为centos7。
安装Docker CE一般用 SET UP THE REPOSITORY 方式安装,参考官网:
https://docs.docker.com/engine/installation/linux/docker-ce/centos/
a.删除已有或者旧版本docker
sudo yum remove docker \
docker-common \
docker-selinux \
docker-engine
b.安装yum-utils
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
c.设置资源库
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
d.注意服务器安装要查看版本:
yum list docker-ce --showduplicates | sort -r
e.根据查询结果以docker-ce-开头加版本号 (举例:docker-ce-17.09.0.ce)
sudo yum install <FULLY-QUALIFIED-PACKAGE-NAME>
f. 启动
sudo systemctl start docker
如果不启动,无法加入蜂群,报错如下
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
g.查看安装版本
docker -v
2.2 安装Docker Cloud
a.使用centos7主机 安装python3.0以上版本
pip install docker-cloud
https://docs.docker.com/docker-cloud/installing-cli/
b.查看安装版本
docker-cloud -v
2.3 用户名密码登录
docker login
2.4 执行以下命令,根据提示安装Agent
docker-cloud node byo
控制台输出
Docker Cloud lets you use your own servers as nodes to run containers. For this you have to install our agent.
Run the following command on your server:
curl -Ls https://get.cloud.docker.com/ | sudo -H sh -s e7a3abecb1ab478185d74d91620a6d10
2.5 将ECS服务器设置为管理机
不清楚如何设置ip时,可以直接初始化蜂群,根据提示操作
docker swarm init
控制台输出如下:
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.24.197.46 on eth0 and 47.95.20.117 on eth1) - specify one with --advertise-addr
其中(10.24.197.46 on eth0 and 47.95.20.117 on eth1)分别为阿里云的内网和外网IP
docker swarm init --advertise-addr 47.95.20.117
控制台输入如下:
Swarm initialized: current node (e2zsbci6r9m4guq7zwtp14ai5) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-58uxxw4k3sbcgzmuydg5tnj78orw3rj64oib29abdtmm0k7h0e-0rleqqyukk2mqyodq3r2vqdd3 47.95.20.117:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
3 使用
3.1 登录docker cloud,点swarms页面,Bring your own swarm
3.2根据提示,在管理机输入命令
如果不是管理机,执行语句会报错
控制台输出如下
Unable to find image 'dockercloud/registration:latest' locally
latest: Pulling from dockercloud/registration
90f4dba627d6: Pull complete
49ab6bb20a7f: Pull complete
d185660c194b: Pull complete
Digest: sha256:11dbb757fc07870824d22030cb1034c4376cf9670e272ef8b0f393e6232d315d
Status: Downloaded newer image for dockercloud/registration:latest
Error response from daemon: {"message":"This node is not a swarm manager. Use \"docker swarm init\" or \"docker swarm join\" to connect this node to swarm and try again."}
如果是管理机,会提示输入用户名及密码
控制台输出
Use your Docker ID credentials to authenticate:
Username: gaojingyuan
Password:
Available namespaces:
* gaojingyuan
Enter name for the new cluster [gaojingyuan/nsrlaw69zpcr0isiy802ucrx5]:
如果如数的名称不规范,会出现提示
The Swarm name you input is invalid, please follow the format: <namespace>/<name>
根据提示再次输入
gaojingyuan/alicluster
控制台输出
You can now access this cluster using the following command in any Docker Engine:
docker run --rm -ti -v /var/run/docker.sock:/var/run/docker.sock -e DOCKER_HOST dockercloud/client gaojingyuan/alicluster
3.3 可以看到docker cloud中已经增加了一个自己的swarm
点击Name,提示内容与之前控制台输出的内容一致
a.根据提示,从本机登录docker cloud,粘贴命令对远程机器进行操作
b.也可以通过客户端启动命令行,操作swarm
通过这两种方式,可以跟操作docker-machine一样 (本地操作docker-machine参考4.蜂群 输入docker-machine env myvm1 根据提示输入命令 eval $(docker-machine env myvm1))在本地操作ECS主机。
3.4 本地登录 docker login 然后执行
docker run --rm -ti -v /var/run/docker.sock:/var/run/docker.sock -e DOCKER_HOST dockercloud/client gaojingyuan/alicluster
控制台输出
Unable to find image 'dockercloud/client:latest' locally
latest: Pulling from dockercloud/client
90f4dba627d6: Pull complete
9ce3e514b7fa: Pull complete
ca5f3b3b0b8e: Pull complete
19aaa36bba7a: Pull complete
Digest: sha256:82aecd2129328607804855503ec5407c337aba33b0baa0a2f82adaae73eb1cc7
Status: Downloaded newer image for dockercloud/client:latest
Use your Docker ID credentials to authenticate:
Username: gaojingyuan
Password:
=> You can now start using the swarm gaojingyuan/alicluster by executing:
export DOCKER_HOST=tcp://127.0.0.1:32768
根据提示输入
export DOCKER_HOST=tcp://127.0.0.1:32768
成功控制管理机,验证一下:
a.本地执行
docker node ls
查看节点信息,与ECS服务器执行获得的结果相同
控制台输出
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
lhwr1d9595a9f3yiwgl7l03et iZ2ze0kyh2qh7f2u7kcv8bZ Down Active
uio0kku2d1bef0wm1sw58kdcj iZ2ze0kyh2qh7f2u7kcv8bZ Ready Active
ju3xf7xh7lo4ob3cc726iohfz * iZ2ze497rv9hjyh8mbf8abZ Ready Active Leader
b.本地执行
docker service ls
控制台输出
ID NAME MODE REPLICAS IMAGE PORTS
osvdh6h1b3as dockercloud-server-proxy global 1/1 dockercloud/server-proxy *:2376->2376/tcp
wongu4gn9jmi getstartedlab_redis replicated 0/1 redis:latest *:6379->6379/tcp
ukm0ih9vj0go getstartedlab_visualizer replicated 0/1 dockersamples/visualizer:stable *:8080->8080/tcp
883hdagic5m4 getstartedlab_web replicated 0/5 gaojingyuan/testrepo:v1 *:80->80/tcp
c.本地执行
docker stack ps getstartedlab
控制台输出
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
qzhj8qzapql6 getstartedlab_redis.1 redis:latest iZ2ze497rv9hjyh8mbf8abZ Running Preparing 13 minutes ago
2e6as2lv7c7z getstartedlab_visualizer.1 dockersamples/visualizer:stable iZ2ze497rv9hjyh8mbf8abZ Running Preparing 13 minutes ago
16pgwmb9m1j7 getstartedlab_web.1 gaojingyuan/testrepo:v1 iZ2ze497rv9hjyh8mbf8abZ Running Preparing 14 minutes ago
xvg1zxrhrmv1 getstartedlab_web.2 gaojingyuan/testrepo:v1 iZ2ze0kyh2qh7f2u7kcv8bZ Running Preparing 14 minutes ago
9focwx8lndq0 getstartedlab_web.3 gaojingyuan/testrepo:v1 iZ2ze497rv9hjyh8mbf8abZ Running Preparing 14 minutes ago
tetm6t2ffm53 getstartedlab_web.4 gaojingyuan/testrepo:v1 iZ2ze0kyh2qh7f2u7kcv8bZ Running Preparing 14 minutes ago
uu54r8t5bp1i getstartedlab_web.5 gaojingyuan/testrepo:v1 iZ2ze0kyh2qh7f2u7kcv8bZ Running Preparing 14 minutes ago