在文章4蜂群中蘑拯,通過(guò)docker-machine env命令操作docker-machine十分方便,當(dāng)我們?cè)谡鎸?shí)環(huán)境中部署時(shí),也需要實(shí)現(xiàn)這種效果。Docker可以對(duì)接多種云服務(wù)提供商柠掂,目前我用阿里云ECS主機(jī)較多,而Docker目前不支持依沮,所以嘗試使用Docker Cloud Agent涯贞。
1.docker可以配合多種云服務(wù)提供商
Amazon Web Services setup guide
Use the Docker Cloud Agent to bring your own host
Docker Cloud Agent,支持如下系統(tǒng):
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主機(jī)作為節(jié)點(diǎn)危喉,可以通過(guò)這個(gè)主機(jī)部署容器宋渔。
為了實(shí)現(xiàn)這樣的功能,必須在linux主機(jī)安裝Docker Cloud Agent姥饰,這樣Docker Cloud就可以遠(yuǎn)程遠(yuǎn)程管理了
2.1 CentOS安裝Docker
為了測(cè)試傻谁,使用阿里云ECS服務(wù)器,系統(tǒng)為centos7列粪。
安裝Docker CE一般用 SET UP THE REPOSITORY 方式安裝审磁,參考官網(wǎng):
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.設(shè)置資源庫(kù)
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
d.注意服務(wù)器安裝要查看版本:
yum list docker-ce --showduplicates | sort -r
e.根據(jù)查詢結(jié)果以docker-ce-開(kāi)頭加版本號(hào) (舉例:docker-ce-17.09.0.ce)
sudo yum install <FULLY-QUALIFIED-PACKAGE-NAME>
f. 啟動(dòng)
sudo systemctl start docker
如果不啟動(dòng),無(wú)法加入蜂群岂座,報(bào)錯(cuò)如下
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主機(jī) 安裝python3.0以上版本
pip install docker-cloud
https://docs.docker.com/docker-cloud/installing-cli/
b.查看安裝版本
docker-cloud -v
2.3 用戶名密碼登錄
docker login
2.4 執(zhí)行以下命令态蒂,根據(jù)提示安裝Agent
docker-cloud node byo
控制臺(tái)輸出
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服務(wù)器設(shè)置為管理機(jī)
不清楚如何設(shè)置ip時(shí),可以直接初始化蜂群费什,根據(jù)提示操作
docker swarm init
控制臺(tái)輸出如下:
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)分別為阿里云的內(nèi)網(wǎng)和外網(wǎng)IP
docker swarm init --advertise-addr 47.95.20.117
控制臺(tái)輸入如下:
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钾恢,點(diǎn)swarms頁(yè)面,Bring your own swarm
3.2根據(jù)提示鸳址,在管理機(jī)輸入命令
如果不是管理機(jī)瘩蚪,執(zhí)行語(yǔ)句會(huì)報(bào)錯(cuò)
控制臺(tái)輸出如下
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."}
如果是管理機(jī),會(huì)提示輸入用戶名及密碼
控制臺(tái)輸出
Use your Docker ID credentials to authenticate:
Username: gaojingyuan
Password:
Available namespaces:
* gaojingyuan
Enter name for the new cluster [gaojingyuan/nsrlaw69zpcr0isiy802ucrx5]:
如果如數(shù)的名稱不規(guī)范稿黍,會(huì)出現(xiàn)提示
The Swarm name you input is invalid, please follow the format: <namespace>/<name>
根據(jù)提示再次輸入
gaojingyuan/alicluster
控制臺(tái)輸出
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中已經(jīng)增加了一個(gè)自己的swarm
點(diǎn)擊Name疹瘦,提示內(nèi)容與之前控制臺(tái)輸出的內(nèi)容一致
a.根據(jù)提示,從本機(jī)登錄docker cloud巡球,粘貼命令對(duì)遠(yuǎn)程機(jī)器進(jìn)行操作
b.也可以通過(guò)客戶端啟動(dòng)命令行言沐,操作swarm
通過(guò)這兩種方式邓嘹,可以跟操作docker-machine一樣 (本地操作docker-machine參考4.蜂群 輸入docker-machine env myvm1 根據(jù)提示輸入命令 eval $(docker-machine env myvm1))在本地操作ECS主機(jī)。
3.4 本地登錄 docker login 然后執(zhí)行
docker run --rm -ti -v /var/run/docker.sock:/var/run/docker.sock -e DOCKER_HOST dockercloud/client gaojingyuan/alicluster
控制臺(tái)輸出
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
根據(jù)提示輸入
export DOCKER_HOST=tcp://127.0.0.1:32768
成功控制管理機(jī)险胰,驗(yàn)證一下:
a.本地執(zhí)行
docker node ls
查看節(jié)點(diǎn)信息汹押,與ECS服務(wù)器執(zhí)行獲得的結(jié)果相同
控制臺(tái)輸出
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
lhwr1d9595a9f3yiwgl7l03et iZ2ze0kyh2qh7f2u7kcv8bZ Down Active
uio0kku2d1bef0wm1sw58kdcj iZ2ze0kyh2qh7f2u7kcv8bZ Ready Active
ju3xf7xh7lo4ob3cc726iohfz * iZ2ze497rv9hjyh8mbf8abZ Ready Active Leader
b.本地執(zhí)行
docker service ls
控制臺(tái)輸出
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.本地執(zhí)行
docker stack ps getstartedlab
控制臺(tái)輸出
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