1. 程式人生 > >搭建Docker Swarm叢集實戰(一)(服務發現使用consul)

搭建Docker Swarm叢集實戰(一)(服務發現使用consul)

注意:
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: 23
cf638 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機器環境