Consul 服務注冊與發現
一: Consul 概述
1.1 什么是服務發現與注冊
服務注冊與發現是微服務架構中不可或缺的重要組件。
起初服務都是單節點的,不保障高可用性,也不考慮服務的壓力承載,服務之間調用單純的通過接口訪問。直到后來出現了多個節點的分布式架構,起初的解決手段是在服務前端負載均衡,這樣前端必須要知道所有后端服務的網絡位置,並配置在配置文件中。
這里就會有幾個問題:
- 如果需要調用后端服務A-N,就需要配置N個服務的網絡位置,配置很麻煩
- 后端服務的網絡位置變化,都需要改變每個調用者的配置
既然有這些問題,那么服務注冊與發現就是解決這些問題的。后端服務A-N可以把當前自己的網絡位置注冊到服務發現模塊,服務發現就以K-V的方式記錄下來, K一般是服務名, v就是IP: PORT,服務發現模塊定時的進行健康檢查,輪詢查看這些后端服務能不能訪問的了。前端在調用后端服務A-N的時候,就跑去服務發現模塊問下它們的網絡位置,然后再調用它們的服務。這樣的方式就可以解決上面的問題了,前端完全不需要記錄這些后端服務的網絡位置,前端和后端完全解耦!
1.2 什么是consul
什么是consul:
- consul是google開源的一個使用go語言開發的服務管理軟件。支持多數據中心、分布式高可用的、服務發現和配置共享。
- 采用Raft算法,用來保證服務的高可用。
- 內置了服務注冊與發現框架、分布一致性協議實現、健康檢查、Key/Value存儲、多數據中心方案,不再需要依賴其他工具(比如zookeeper等) 。
- 服務部署簡單,只有一個可運行的二進制的包。
- 每個節點都需要運行agent,他有兩種運行模式server和client。
- 每個數據中心官方建議需要3或5個server節點以保證數據安全,同時保證server-leader的選舉能夠正確的進行。
consul的的模式(client模式和sever模式):
- 在client模式下,所有注冊到當前節點的服務會被轉發到server節點,本身是不持久化這些信息。
- 在server模式下,功能和client模式相似,唯一不同的是,它會把所有的信息持久化到本地,這樣遇到故障,信息是可以被保留的。
- server-leader是所有server節點的老大,它和其它server節點不同的是,它需要負責同步注冊的信息給其它的server節點,同時也要負責各個節點的健康監測。
consul提供的一些關鍵特性:
- 服務注冊與發現: consul通過DNS或者HTTP接口使服務注冊和服務發現變的很容易,一些外部服務,例如saas提供的也可以一樣注冊。
- 健康檢查: 健康檢測使consu1可以快速的告警在集群中的操作。和服務發現的集成,可以防止服務轉發到故障的服務上面。
- Key/Value存儲:一個用來存儲動態配置的系統。提供簡單的HTTP接口,可以在任何地方操作。
- 多數據中心:無需復雜的配置,即可支持任意數量的區域。
安裝consul是用於服務注冊,也就是容器本身的一些信息注冊到consul里面,其他程序可以通過consul獲取注冊的相關服務信息,這就是服務注冊與發現。
二: consul 的部署
2.1 拓撲
節點 | 服務 |
---|---|
consul服務器: 192.168.23.103 | consul服務,nginx服務,consul-template守護進程 |
registraotor服務器:192.168.23.104 | 運行registrator容器,nginx容器 |
2.2 consul服務器建立consul服務
2.2.1 安裝並啟動服務
#解壓軟件包,安裝服務
[root@host103 ~]# mkdir /opt/consul
[root@host103 ~]# cp /opt/consul_0.9.2_linux_amd64.zip /opt/consul/
[root@host103 ~]# cd /opt/consul/
[root@host103 consul]# unzip consul_0.9.2_linux_amd64.zip
[root@host103 consul]# mv consul /usr/local/bin/
#設置代理,在后台啟動consul 服務端
[root@host103 consul]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.23.103 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
-server: 以server身份啟動。默認是client。
-bootstrap :用來控制一個server是否在bootstrap模式,在一個數據中心中只能有一個server處於bootstrap模式,當一個server處於bootstrap模式時,可以自己選舉為server-leader.
-bootstrap-expect=2 :集群要求的最少server數量,當低於這個數量,集群即失效。
-ui:指定開啟UI界面,這樣可以通過http://localhost:8500/ui這樣的地址訪問consul自帶的web UI界面。
-data-dir:指定數據存儲目錄。
-bind :指定用來在集群內部的通訊地址,集群內的所有節點到此地址都必須是可達的,默認是0.0.0.0
-client :指定consul綁定在哪個client地址上,這個地址提供HTTP, DNS,RPC等服務,默認是127.0.0.1.
-node:節點在集群中的名稱,在一個集群中必須是唯一的,默認是該節點的主機名。
-datacenter :指定數據中心名稱,默認是dc1.
2.2.2 consul監聽的5個端口
#查看consul 默認監聽的5個端口
[root@host103 docker]# netstat -natp | grep consul
tcp 0 0 192.168.23.103:8300 0.0.0.0:* LISTEN 48211/consul
tcp 0 0 192.168.23.103:8301 0.0.0.0:* LISTEN 48211/consul
tcp 0 0 192.168.23.103:8302 0.0.0.0:* LISTEN 48211/consul
tcp6 0 0 :::8500 :::* LISTEN 48211/consul
tcp6 0 0 :::8600 :::* LISTEN 48211/consul
2.3 集群的狀態信息查看
#memebers狀態
[root@host103 consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.23.103:8301 alive server 0.9.2 2 dc1
#查看集群狀態
[root@host103 consul]# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.23.103:8300 192.168.23.103:8300 leader true 2
[root@host103 consul]# consul info | grep leader
leader = true
leader_addr = 192.168.23.103:8300
2.2.4 通過http api獲取集群信息
#查看集群server成員
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/peers
["192.168.23.103:8300"]
#查看集群server-leader
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/leader
"192.168.23.103:8300"
#查看注冊的所有服務(當前還沒有)
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/services
#查看nginx的服務信息
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/nginx
#查看集群的節點詳細信息
[root@host103 consul]# curl 127.0.0.1:8500/v1/status/nodes
2.3 registrator 服務器配置
容器服務自動加入Nginx集群
2.3.1 安裝Gliderlabs/Registrator
Gliderlabs/Registrator可檢查容器運行狀態自動注冊,還可以注銷docker 容器的服務到服務配置中心。目前支持Consul,Etcd和SkyDNS2
[root@host104 ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> --ip=192.168.23.104 \
> consul://192.168.23.103:8500
--net=host :把運行的docker容器設定為host網絡模式。
-v /var/run/docker.sock:/tmp/docker.sock :把宿主機的Docker守護進程(Docker daemon)默認監聽的Unix域套接字掛載到容器中。
--restart=always :設置在容器退出時總是重啟容器。
--ip :剛才把network指定了host模式,所以我們指定ip為宿主機的ip。
consul :指定consul服務器的IP和端口。
2.3.2 consul 為什么可以實現自動發現
被監控的節點服務器上,nginx容器的服務啟動后,並做了端口映射后,會將映射的信息寫入到宿主機的docker.sock文件
registrator自動發現模塊會監控宿主機的docker.sock ,就會發現nginx服務。
registrator 會將信息寫入到consul的自動注冊模塊,通過8500 web ui 展示
2.3.3 測試服務發現功能
#--name 指定容器名,-h指定容器主機名
[root@host104 ~]# docker run -itd -p:83:80 --name web-01 -h test01 nginx
[root@host104 ~]# docker run -itd -p:84:80 --name web-02 -h test02 nginx
[root@host104 ~]# docker run -itd -p:88:80 --name web-03 -h test03 httpd
[root@host104 ~]# docker run -itd -p:89:80 --name web-04 -h test04 httpd
2.3.4 驗證http和nginx服務是否注冊到consul
瀏覽器中,輸入 http://192.168.23.103:8500,在 Web 頁面中“單擊 NODES”,然后單擊“consurl-server01”,會出現 5 個服務。
//在consul服務器使用curl測試連接服務器
curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}
三: consul-temple
Consul-Template是基於Consul的自動替換配置文件的應用。Consul-Template是一個守護進程,用於實時查詢Consul集群信息,並更新文件系統上任意數量的指定模板,生成配置文件。更新完成以后,可以選擇運行 shell 命令執行更新操作,重新加載 Nginx。
Consul-Template可以查詢Consul中的服務目錄、Key、Key-values 等。這種強大的抽象功能和查詢語言模板可以使 Consul-Template 特別適合動態的創建配置文件。例如:創建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。
3.1在 Consul 服務器上,准備template nginx模板文件
[root@host103 ~]# vim /opt/consul/nginx.ctmpl
#定義nginx的upstream 模板
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
#定義一個server,監聽8000端口,反向代理到upstream
server {
#監聽8000端口,訪問就使用端口8000
listen 8000;
server_name 192.168.23.103;
access_log /var/log/nginx/mynet.com-access.log; #定義首頁文件
index index.html index.php;
location / {
#記錄所有轉發路徑,使得后端的節點服務器可以獲取真正的客戶端地址,而不是去獲得代理端的ip地址。
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 進行7層轉發
proxy_pass http://http_backend;
}
}
3.2 編譯安裝nginx
[root@host103 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ make
[root@host103 ~]# useradd -M -s /sbin/nologin nginx
[root@host103 opt]# ls nginx-1.12.0.tar.gz
nginx-1.12.0.tar.gz
[root@host103 opt]#tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@host103 nginx-1.12.0]#cd /opt/nginx-1.12.0/
[root@host103 nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@host103 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
3.3 consul 服務端配置nginx
[root@host103 nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
http{
......
#配置添加虛擬主機目錄
19// include vhost/*.conf;
.....
}
#創建虛擬主機目錄
[root@host103 nginx-1.12.0]# mkdir /usr/local/nginx/conf/vhost
#創建日志文件目錄
[root@host103 nginx-1.12.0]# mkdir /var/log/nginx
#啟動nginx
[root@host103 sbin]# cd /usr/local/nginx/sbin/
[root@host103 sbin]# ./nginx
[root@host103 sbin]# netstat -natp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 52564/nginx: master
3.4 配置並啟動template
#上傳軟件包到 /opt目錄
[root@host103 ~]# cd /opt/
[root@host103 opt]# ls consul-template_0.19.3_linux_amd64.zip
consul-template_0.19.3_linux_amd64.zip
#解壓軟件包
[root@host103 opt]# unzip consul-template_0.19.3_linux_amd64.zip
[root@host103 opt]# mv consul-template /usr/local/bin/
#//在前台啟動 template 服務,啟動后不要按 ctrl+c 中止 consul-template 進程
#consul-addr 指定consul服務器的IP和端口
#--template指定模板文件:nginx配置文件: nginx重載配置文件命令
#--log-level=info :配置日志信息級別
[root@host103 opt]# consul-template --consul-addr 192.168.23.103:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/mynet.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
#重開終端,查看生成的配置文件
[root@host103 ~]# vim /usr/local/nginx/conf/vhost/mynet.conf
upstream http_backend {
server 192.168.23.104:83;
server 192.168.23.104:84;
}
server {
listen 8000;
server_name 192.168.23.103;
access_log /var/log/nginx/mynet.com-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;
}
}
3.5 訪問template-nginx
#為nginx容器寫入網頁文件(容器的nginx網頁目錄是/usr/share/nginx/html)
[root@host104 ~]# docker exec -it web-01 bash -c "echo 'this is web-01' > /usr/share/nginx/html/index.html"
[root@host104 ~]# docker exec -it web-02 bash -c "echo 'this is web-02' > /usr/share/nginx/html/index.html"
#訪問測試
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-02
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-01
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-02
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-01
3.6 增加一個nginx容器節點
3.6.1 加一個 nginx 容器節點,測試服務發現及配置更新功能
#增加一個nginx容器節點,測試服務發現及配置更新功能。
[root@host104 ~]# docker run -itd -p 85:80 --name web-05 -h test05 nginx
#為新的nginx容器寫入網站文件
[root@host104 ~]# docker exec -it web-05 bash -c "echo 'this is web-05' > /usr/share/nginx/html/index.html"
#訪問測試
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-01
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-02
[root@host104 ~]# curl http://192.168.23.103:8000
this is web-05
3.6.2在consul端 觀察 template 服務
#在consul端觀察 template 服務,會從模板更新/usr/local/nginx/conf/vhost/kgc.conf 文件內容,並且重載 nginx 服務。
[root@host103 ~]# cat /usr/local/nginx/conf/vhost/mynet.conf
upstream http_backend {
server 192.168.23.104:83;
server 192.168.23.104:84;
#新加入的節點
server 192.168.23.104:85;
}
.........
3.6.3 查看三台 nginx 容器日志,請求正常輪詢到各個容器節點上
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
四: consul多節點
4.1 添加一台已有docker環境的服務器加入已有的群集中
[root@host105 opt]# ls consul_0.9.2_linux_amd64.zip
consul_0.9.2_linux_amd64.zip
[root@host105 opt]# unzip consul_0.9.2_linux_amd64.zip
[root@host105 opt]# mv consul /usr/local/bin/
#將192.168.23.105/24加入已有的群集中
[root@host105 opt]# consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.23.105 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.23.103 &> /var/log/consul.log &
-enable-script-checks=true :設置檢查服務為可用
-datacenter : 數據中心名稱
-join :加入到已有的集群中
-node: 指定節點名(節點名唯一,集群里不可以重復)
4.2 查看集群狀態
[root@host105 opt]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.23.103:8301 alive server 0.9.2 2 dc1
consul-server02 192.168.23.105:8301 alive server 0.9.2 2 dc1
[root@host105 opt]# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.23.103:8300 192.168.23.103:8300 leader true 2
consul-server02 192.168.23.105:8300 192.168.23.105:8300 follower true 2