目錄
一.consul概述
consul簡介
- consul是HashCorp公司推出的開源工具,用於實現分佈式系統的服務發現與配置。與其他分佈式服務註冊與發現的方案相比,consul的方案是“一站式”,內置了服務註冊與發現框架、分佈式一致性協議實現、健康檢查、K-V存儲、多數據中心方案,不需要依賴其他工具(比如ZooKeeper等)。
- consul使用go語言編寫,因此具有天然的可移植性(支持Linux、windows和Mac OS X等系統);安裝包僅僅包含一個二進制的可執行文件,方便部署,與Docker等輕量級容器可以無縫連接。
- 官網地址: https://www.consul.io/downloads.html
注:詳細介紹參考官方文檔
consul特性
- 基礎特性:服務註冊與發現、數據強一致性保證、多數據中心、健康檢查、Key/Value存儲
- 高級特性:HTTP API、ACL(對後端服務的訪問控制權限可以設置)
consul工作模式
- consul的架構如下:
- consul中包括3中不同的角色:client、server、server-leader,其實還有一個agent圖中沒有標識
- Client(客戶端)
轉發所有的請求給server
無狀態,不持久化數據
參與LAN Gossip的健康檢查
- server(服務器)
持久化數據
轉發請求給server-Leader
參數Server-Leader選舉
通過WAN Gossip(流算法協議,Gassndra和比特幣的底層算法一樣,符合弱一致性),與其他的數據中心交換數據
- Server-leader
響應RPC的請求
將服務列表數據同步給server
- Agent
agent是一個守護線程
跟隨consul應用啓動而啓動
負責檢查、維護節點同步
二.consul集羣部署
實驗環境
- 實驗設備部署
角色 | IP地址 | 需要安裝的軟件 |
consul、server-leader |
192.168.43.101/24 |
docker-ce、consul |
docker容器服務(nginx) | 192.168.43.102/24 | docker-ce、registrator |
具體部署步驟
- 在兩臺pc上安裝docker-ce
##安裝依賴包
yum install -y yum-utils device-mapper-persistent-data lvm2
##設置docker源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
##安裝docker
yum install -y docker-ce
##關閉安全性功能
systemctl stop firewalld
setenforce 0
##開啓docker服務
systemctl start docker.service
systemctl enable docker.service
##加速鏡像
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://dnntzrw4.mirror.aliyuncs.com"]
}
EOF
##重啓加載docker
systemctl daemon-reload
systemctl restart docker
- 在consul服務器上部署consul
1.解壓consul到/usr/bin
[root@consul consul]# unzip consul_0.9.2_linux_amd64.zip -d /usr/bin/
Archive: consul_0.9.2_linux_amd64.zip
inflating: /usr/bin/consul
[root@consul consul]#
##consul的安裝包可到官網下載
##進行consul設置
consul agent \ //代理功能
-server \ //服務功能
-bootstrap \ //參與選舉
-ui \ //提供web界面
-data-dir=/var/lib/consul-data \ //提供一個代理存儲數據目錄
-bind=192.168.43.101 \ //綁定本地地址
-client=0.0.0.0 \ //面對的客戶端地址,這些寫所有地址
-node=consul-server01 &> /var/log/consul.log & //定義節點名稱,日誌混合輸出到log,並且放到後臺運行
[root@consul consul]# consul members ##查看集羣信息
Node Address Status Type Build Protocol DC
consul-server01 192.168.43.101:8301 alive server 0.9.2 2 dc1
[root@consul consul]# consul info | grep leader ##查看管理信息
leader = true
leader_addr = 192.168.43.101:8300
[root@consul consul]#
//通過httpd api 獲取羣集信息
curl 127.0.0.1:8500/v1/status/peers #查看集羣server成員
curl 127.0.0.1:8500/v1/status/leader #集羣raf leader
curl 127.0.0.1:8500/v1/catalog/services #註冊的所有服務
curl 127.0.0.1:8500/v1/catalog/nginx #查看nginx的服務信息
curl 127.0.0.1:8500/v1/catalog/nodes #羣集節點的詳細信息
- 在docker-nginx服務器上連接consul,創建nginx容器服務
[root@nginx ~]# docker run -d \
> --name=registrator \ ##定義容器名稱
> --net=host \ ##定義網絡
> -v /var/run/docker.sock:/tmp/docker.sock \ ##指定數據卷,存儲信息
> --restart=always \
> gliderlabs/registrator:latest \ ##定義鏡像
> -ip=192.168.43.102 \ ##指定本地地址
> consul://192.168.43.101:8500 ##指定consul管理節點地址、端口
2c0dd09ee35139195c3de747fc87a445297c2afb39b0735afe6089a20b0272d3
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c0dd09ee351 gliderlabs/registrator:latest "/bin/registrator -i…" 3 seconds ago Up 2 seconds registrator
//創建容器服務
[root@nginx ~]# docker run -itd -p:81:80 --name test-01 -h test01 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
4bfafe4bf7bd0e84f12100b8d337effd9ed1100f723b7ccffedbf7c87941ec01
[root@nginx ~]# docker run -itd -p:82:80 --name test-02 -h test02 nginx
docker run -itd -p:84:80 --name test-04 -h test04 nginxd2281250cee5566d2d1c432b2b6a8019ab96c61156780a89393b613f755ba39f
[root@nginx ~]# docker run -itd -p:83:80 --name test-03 -h test03 nginx
973558821e126f5b2fa57cb8a8850b8671e387ff3c7bc53cfe069fc9e7cdabac
[root@nginx ~]# docker run -itd -p:84:80 --name test-04 -h test04 nginx
a740870c187964d6f4a7ede7c52ce16e2f1ae18912ba954e10d4691e1196c87a
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a740870c1879 nginx "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:84->80/tcp test-04
973558821e12 nginx "nginx -g 'daemon of…" 8 seconds ago Up 7 seconds 0.0.0.0:83->80/tcp test-03
d2281250cee5 nginx "nginx -g 'daemon of…" 8 seconds ago Up 7 seconds 0.0.0.0:82->80/tcp test-02
4bfafe4bf7bd nginx "nginx -g 'daemon of…" 17 seconds ago Up 16 seconds 0.0.0.0:81->80/tcp test-01
2c0dd09ee351 gliderlabs/registrator:latest "/bin/registrator -i…" 2 minutes ago Up 2 minutes registrator
[root@nginx ~]#
- 登錄本地瀏覽器,http://192.168.43.101:8500/ui/#/dc1/nodes/consul-server01,查看consul是否發現後端容器服務
到這裏我們實現了consul的服務發現功能,而且consul的UI界面中還有對ACL和K-V的配置,但是這些功能需要指定的
三.consul集羣上添加compose-temlate
compose-temlate簡介
- compose-template是一個守護進程,用於實時查詢consul集羣信息
- 並且運維人員可以在consul服務器上,定製任意的模板,生成配置文件
- compose可以查詢consul中的服務目錄、key、key-values等
- 這種強大的抽象功能和查詢語言模板使得consul-template特別適合動態的創建配置文件,也就是自動化增加服務
實驗架構圖分析
- 我們在consul服務上安裝一個nginx服務,用作負載均衡器,利用consul的服務發現功能和compose-template的動態生成配置文件的功能,就可以實現當我們在後端服務器上增加一個容器服務時,consul能夠及時發現,compose-template及時在nginx裏生成該服務的地址和端口號放入地址池裏面,這樣nginx作爲代理服務器,就可以調用了
具體安裝步驟
- 在consul服務端操作
//在consul目錄下操作,建立template nginx模板文件
cd /root/consul
vi nginx.ctmpl
upstream http_backend { ##聲明後端器服務池
{{range service "nginx"}}
server {{.Address}}:{{.Port}}; ##讀取nodes的地址和服務端口的變量,這是動態讀取
{{end}}
}
server {
listen 1216; ##監聽consul的端口,這是代理端口,consul是作爲一個代理服務,訪問後端的容器服務
server_name localhost 192.168.43.101; ##監聽本地地址,監聽代理端的地址
access_log /var/log/nginx/kgc.cn-access.log; ##日誌目錄,需要自行創建
index index.html index.php;
location / { ##反向代理的信息,代理的頭部信息
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend; ##跳轉到服務器池的地址和端口
}
}
//編譯安裝nginx ,在consul服務器上操作,安裝包可到官網下載
yum install -y gcc pcre-devel zlib-devel
tar xzvf nginx-1.12.2.tar.gz -C /opt
cd /opt/nginx-1.12.2/
./configure --prefix=/usr/local/nginx
make && make install
//配置nginx
vi /usr/local/nginx/conf/nginx.conf
http {
include mim.types;
include vhost/*.conf; ##添加虛擬主機的文件
default_type application/octet-stream;
}
//創建虛擬主機目錄
mkdir /usr/local/nginx/conf/vhost
//創建日誌文件目錄,前面指定的consul.conf中指定的
mkdir /var/log/nginx
//啓動nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
/usr/local/nginx/sbin/nginx
//配置並且啓動template
上傳consul-template_0.19.3_linux_amd64.zip到/root目錄下
//解壓
[root@consul consul]# unzip consul-template_0.19.3_linux_amd64.zip -d /usr/bin/
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: /usr/bin/consul-template
//啓用模板
// /root/consul/nginx.ctmpl,模板文件
// /usr/local/nginx/conf/vhost/kgc.conf,生成的配置文件
// /usr/local/nginx/sbin/nginx -s reload,重載服務
//-log-level=info,指定日誌級別
//進入監控狀態
consul-template -consul-addr 192.168.43.101:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
//另外打開一個終端查看利用nginx.ctmpl自動生成nginx虛擬主機的配置文件
[root@consul ~]# ls /usr/local/nginx/conf/vhost/
kgc.conf
[root@consul ~]# cat /usr/local/nginx/conf/vhost/kgc.conf
upstream http_backend {
server 192.168.43.102:81;
server 192.168.43.102:82;
server 192.168.43.102:83;
server 192.168.43.102:84;
}
server {
listen 1216;
server_name localhost 192.168.43.101;
access_log /var/log/nginx/kgc.cn-access.log;
index index.html index.php;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
[root@consul ~]#
- 使用 http://192.168.43.101:1216,訪問後端服務器
- 進入docker_nginx服務器中的查看容器服務的日誌
添加nginx節點驗證服務自動更新
- 在docker_nginx服務端操作
//在docker_nginx服務器上增加一個nginx的容器服務
[root@nginx ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
04692fb5ea88553f134cbf0d4663dea560d482ee4ac6ae6c8b3f3a0647d283b8
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04692fb5ea88 nginx "nginx -g 'daemon of…" 4 seconds ago Up 4 seconds 0.0.0.0:85->80/tcp test-05
a740870c1879 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:84->80/tcp test-04
973558821e12 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:83->80/tcp test-03
d2281250cee5 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:82->80/tcp test-02
4bfafe4bf7bd nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:81->80/tcp test-01
2c0dd09ee351 gliderlabs/registrator:latest "/bin/registrator -i…" About an hour ago Up About an hour registrator
[root@nginx ~]#
- 在consul服務器上查看虛擬主機文件的有沒有增加85號端口到服務器池
由此可以知道通過consul的一個實時學習新增服務,然後compose-template可以實現動態增加服務節點到nginx代理的配置文件,這樣就形成了服務的自動化增減