关闭

搭建Docker Swarm集群实战(一)(服务发现使用consul)

标签: docker集群
80人阅读 评论(0) 收藏 举报
分类:

注意:
docker在版本1.12之后,已经将swarm的部分集成在docker里面了,所以也就不需要依赖第三方的东西了。
所以如果不是为了有趣了解一下的话,请直接转到本系列的第三篇开始=》搭建Docker Swarm集群实战(三)(swarm mode)

个人建立的Docker爱好者交流QQ群:472149402,欢迎大家来此交流经验和问题,一起成长。

前言

Docker Swarm是docker的原生的集群解决方案。它可以让你使用一些docker的工具来统一管理一池子的docker host。

首先Docker Swarm依赖于一个Manager(其实就是Swarm Master)。Manager管理和调度所有集群上的container。而这些被管理的container就可以分布在集群的各个Docker Host中(我们称之为Swarm Nodes)

其次,集群中的Swarm Master和Swarm Nodes互相之间的节点发现,Docker公司提供了若干种实现方式,我们这里使用consul来实现。关于consul,大家可以去https://www.consul.io/了解详细。

最后顺便提一下,这里由于我只有一台电脑,所以为了模拟多台电脑,并且每一台电脑都有一个docker host,我使用了docker-machinel来创建不同的docker host的节点。关于docker-machine的详细这里就省略。

确认宿主的各个组件的版本

我使用的笔记本是mac book,所以有一些东西的安装可能根据自身的环境有所不同。

$ docker-machine version
docker-machine version 0.8.1, build 41b3b25

$ docker version
Client:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.7.1
 Git commit:   6f9534c
 Built:        Thu Sep  8 10:31:18 2016
 OS/Arch:      darwin/amd64

Server:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 17:52:38 2016
 OS/Arch:      linux/amd64

$ docker-compose version
docker-compose version 1.8.0, build f3628c7
docker-py version: 1.9.0
CPython version: 2.7.9
OpenSSL version: OpenSSL 1.0.2h  3 May 2016

正式开始

创建一个Docker Host来运行Consul服务

创建Docker Host 1号机 (起名consul-machine)

$ docker-machine create -d=virtualbox consul-machine
Running pre-create checks...
Creating machine...
(consul-machine) Copying /Users/jiandaojiao/.docker/machine/cache/boot2docker.iso to /Users/jiandaojiao/.docker/machine/machines/consul-machine/boot2docker.iso...
(consul-machine) Creating VirtualBox VM...
(consul-machine) Creating SSH key...
(consul-machine) Starting the VM...
(consul-machine) Check network to re-create if needed...
(consul-machine) 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 consul-machine

如果你没有事先安装virtualbox的话,会报错,使用brew cask install virtualbox来安装。

$ docker-machine ls
NAME             ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
consul-machine   -        virtualbox   Running   tcp://192.168.99.101:2376           v17.09.0-ce 

成功创建了consul-machine的docker host之后,可以看到它已经在running了。接下去我们需要在这个docker host上运行一个consul服务。

在1号机(consul-machine)上运行consul服务

$ eval $(docker-machine env consul-machine)
$ docker-machine ls
NAME             ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
consul-machine   *        virtualbox   Running   tcp://192.168.99.101:2376           v17.09.0-ce 
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES  

大家可以看到ACTIVE的地方变成了*号,所以现在docker客户端打docker命令的时候,实际上指令是发往consul-machine 1号机的。docker ps可以发现空空如也。接下去我们用docker-compose的方法来启动consul服务。

编辑一个docker-compose.yml文件如下:

version: '2'
services:
  myconsul:
    image: consul  ## 注意这里使用官方的consul镜像,所以不同的镜像对于command的处理也不一样
    restart: always
    hostname: consul
    ports:
      - 8500:8500
      - 8300:8300
      - 8301:8301
      - 8301:8301/udp
      - 8302:8302/udp
      - 8302:8302
      - 8400:8400
      - 53:53/udp
    command: " agent -server -bootstrap -ui -client 0.0.0.0 -advertise=10.0.2.15"  ## 注意这边的advertise的ip填写1号机的ip地址,否则会默认成容器的ip地址,因为其他号机器是通过1号机的ip来访问consul服务的。

执行docker-compose up -d, 下面贴一些启动log(不带-d)

$ docker-compose up 
Starting consul_myconsul_1
Attaching to consul_myconsul_1
myconsul_1  | ==> WARNING: Bootstrap mode enabled! Do not enable unless necessary
myconsul_1  | ==> Starting Consul agent...
myconsul_1  | ==> Consul agent running!
myconsul_1  |            Version: 'v0.9.3'
myconsul_1  |            Node ID: '3a298627-a294-cb0a-652e-7e675d831c87'
myconsul_1  |          Node name: 'consul'
myconsul_1  |         Datacenter: 'dc1' (Segment: '<all>')
myconsul_1  |             Server: true (Bootstrap: true)
myconsul_1  |        Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 8600)
myconsul_1  |       Cluster Addr: 10.0.2.15 (LAN: 8301, WAN: 8302)
myconsul_1  |            Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
myconsul_1  | 
myconsul_1  | ==> Log data will now stream in as it occurs:
myconsul_1  | 
myconsul_1  |     2017/10/06 05:27:09 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:10.0.2.15:8300 Address:10.0.2.15:8300}]
myconsul_1  |     2017/10/06 05:27:09 [INFO] raft: Node at 10.0.2.15:8300 [Follower] entering Follower state (Leader: "")
myconsul_1  |     2017/10/06 05:27:09 [INFO] serf: EventMemberJoin: consul.dc1 10.0.2.15
myconsul_1  |     2017/10/06 05:27:09 [INFO] serf: EventMemberJoin: consul 10.0.2.15
myconsul_1  |     2017/10/06 05:27:09 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp)
myconsul_1  |     2017/10/06 05:27:09 [WARN] serf: Failed to re-join any previously known node
myconsul_1  |     2017/10/06 05:27:09 [WARN] serf: Failed to re-join any previously known node
myconsul_1  |     2017/10/06 05:27:09 [INFO] consul: Adding LAN server consul (Addr: tcp/10.0.2.15:8300) (DC: dc1)
myconsul_1  |     2017/10/06 05:27:09 [INFO] consul: Handled member-join event for server "consul.dc1" in area "wan"
myconsul_1  |     2017/10/06 05:27:09 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp)
myconsul_1  |     2017/10/06 05:27:09 [INFO] agent: Started HTTP server on [::]:8500
myconsul_1  |     2017/10/06 05:27:16 [ERR] agent: failed to sync remote state: No cluster leader
myconsul_1  |     2017/10/06 05:27:17 [WARN] raft: Heartbeat timeout from "" reached, starting election
myconsul_1  |     2017/10/06 05:27:17 [INFO] raft: Node at 10.0.2.15:8300 [Candidate] entering Candidate state in term 3
myconsul_1  |     2017/10/06 05:27:17 [INFO] raft: Election won. Tally: 1
myconsul_1  |     2017/10/06 05:27:17 [INFO] raft: Node at 10.0.2.15:8300 [Leader] entering Leader state
myconsul_1  |     2017/10/06 05:27:17 [INFO] consul: cluster leadership acquired
myconsul_1  |     2017/10/06 05:27:17 [INFO] consul: New leader elected: consul
myconsul_1  |     2017/10/06 05:27:17 [INFO] agent: Synced node info

在1号机上用curl确认一下服务

$ curl 127.0.0.1:8500/v1/catalog/nodes
[{"ID":"3a298627-a294-cb0a-652e-7e675d831c87","Node":"consul","Address":"10.0.2.15","Datacenter":"dc1"}]
$ curl 10.0.2.15:8500/v1/catalog/nodes
[{"ID":"3a298627-a294-cb0a-652e-7e675d831c87","Node":"consul","Address":"10.0.2.15","Datacenter":"dc1"}]

下节预告

创建swarm的master和node机器环境

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1146次
    • 积分:153
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论