目錄
?一、什么是服務注冊與發(fā)現(xiàn)?
二、什么是consul
三、consul 部署
3.1建立Consul服務
3.1.1查看集群狀態(tài)
3.1.2通過 http api 獲取集群信息
3.2registrator服務器
3.2.1安裝 Gliderlabs/Registrator
3.2.2測試服務發(fā)現(xiàn)功能是否正常
3.2.3驗證 http 和 nginx 服務是否注冊到 consul
3.3consul-template
3.3.1準備 template nginx 模板文件
3.3.2編譯安裝nginx
3.3.3配置 nginx
3.3.4配置并啟動 template
3.3.5訪問 template-nginx
3.3.6增加一個 nginx 容器節(jié)點
3.4consul 多節(jié)點
黃色背影為重點知識點
?一、什么是服務注冊與發(fā)現(xiàn)?
服務注冊與發(fā)現(xiàn)是微服務架構中不可或缺的重要組件。起初服務都是單節(jié)點的,不保障高可用性,也不考慮服務的壓力承載,服務之間調用單純的通過接口訪問。直到后來出現(xiàn)了多個節(jié)點的分布式架構,起初的解決手段是在服務前端負載均衡,這樣前端必須要知道所有后端服務的網(wǎng)絡位置,并配置在配置文件中。這里就會有幾個問題:
- 如果需要調用后端服務A-N,就需要配置N個服務的網(wǎng)絡位置,配置很麻煩
- 后端服務的網(wǎng)絡位置變化,都需要改變每個調用者的配置
既然有這些問題,那么服務注冊與發(fā)現(xiàn)就是解決這些問題的。后端服務A-N可以把當前自己的網(wǎng)絡位置注冊到服務發(fā)現(xiàn)模塊,服務發(fā)現(xiàn)就以K-V的方式記錄下來,K一般是服務名,V就是IP:PORT。服務發(fā)現(xiàn)模塊定時的進行健康檢查,輪詢查看這些后端服務能不能訪問的了。前端在調用后端服務A-N的時候,就跑去服務發(fā)現(xiàn)模塊問下它們的網(wǎng)絡位置,然后再調用它們的服務。這樣的方式就可以解決上面的問題了,前端完全不需要記錄這些后端服務的網(wǎng)絡位置,前端和后端完全解耦!
二、什么是consul
consul是google開源的一個使用go語言開發(fā)的服務管理軟件。支持多數(shù)據(jù)中心、分布式高可用的、服務發(fā)現(xiàn)和配置共享。采用Raft算法,用來保證服務的高可用。內置了服務注冊與發(fā)現(xiàn)框架、分布一致性協(xié)議實現(xiàn)、健康檢查、Key/Value存儲、多數(shù)據(jù)中心方案,不再需要依賴其他工具(比如ZooKeeper等)。服務部署簡單,只有一個可運行的二進制的包。每個節(jié)點都需要運行agent,他有兩種運行模式server 和 client。 每個數(shù)據(jù)中心官方建議需要3或5個server節(jié)點以保證數(shù)據(jù)安全,同時保證server-leader的選舉能夠正確的進行。
在client模式下,所有注冊到當前節(jié)點的服務會被轉發(fā)到server節(jié)點,本身是不持久化這些信息。 在server模式下,功能和client模式相似,唯一不同的是,它會把所有的信息持久化到本地,這樣遇到故障,信息是可以被保留的。
server-leader是所有server節(jié)點的老大,它和其它server節(jié)點不同的是,它需要負責同步注冊的信息給其它的server節(jié)點,同時也要負責各個節(jié)點的健康監(jiān)測。
consul提供的一些關鍵特性:
- 服務注冊與發(fā)現(xiàn):consul通過DNS或者HTTP接口使服務注冊和服務發(fā)現(xiàn)變的很容易,一些外部服務,例如saas提供的也可以一樣注冊。
- 健康檢查:健康檢測使consul可以快速的告警在集群中的操作。和服務發(fā)現(xiàn)的集成,可以防止服務轉發(fā)到故障的服務上面。
- Key/Value存儲:一個用來存儲動態(tài)配置的系統(tǒng)。提供簡單的HTTP接口,可以在任何地方操作。
- 多數(shù)據(jù)中心:無需復雜的配置,即可支持任意數(shù)量的區(qū)域。
安裝consul是用于服務注冊,也就是容器本身的一些信息注冊到consul里面,其他程序可以通過consul獲取注冊的相關服務信息,這就是服務注冊與發(fā)現(xiàn)。
三、consul 部署
3.1建立Consul服務
1. mkdir /opt/consul
cp consul_0.9.2_linux_amd64.zip /opt/consul
cd /opt/consul
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/local/bin/
//設置代理,在后臺啟動 consul 服務端
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.10.23 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
- -server: 以server身份啟動。默認是client。
- -bootstrap :用來控制一個server是否在bootstrap模式,在一個數(shù)據(jù)中心中只能有一個server處于bootstrap模式,當一個server處于 bootstrap模式時,可以自己選舉為 server-leader。
- -bootstrap-expect=2 :集群要求的最少server數(shù)量,當?shù)陀谶@個數(shù)量,集群即失效。
- -ui :指定開啟 UI 界面,這樣可以通過 http://localhost:8500/ui 這樣的地址訪問 consul 自帶的 web UI 界面。
- -data-dir :指定數(shù)據(jù)存儲目錄。
- -bind :指定用來在集群內部的通訊地址,集群內的所有節(jié)點到此地址都必須是可達的,默認是0.0.0.0。
- -client :指定 consul 綁定在哪個 client 地址上,這個地址提供 HTTP、DNS、RPC 等服務,默認是 127.0.0.1。 -node :節(jié)點在集群中的名稱,在一個集群中必須是唯一的,默認是該節(jié)點的主機名。
- -datacenter :指定數(shù)據(jù)中心名稱,默認是dc1。
netstat -natp | grep consul
啟動consul后默認會監(jiān)聽5個端口:
8300:replication、leader farwarding的端口
8301:lan cossip的端口
8302:wan gossip的端口
8500:web ui界面的端口
8600:使用dns協(xié)議查看節(jié)點信息的端口
3.1.1查看集群狀態(tài)
#查看members狀態(tài)
consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.10.23:8301 alive server 0.9.2 2 dc1
#查看集群狀態(tài)
consul operator raft list-peers
consul info | grep leader
leader = true
leader_addr = 192.168.10.23:8300
3.1.2通過 http api 獲取集群信息
curl 127.0.0.1:8500/v1/status/peers #查看集群server成員
curl 127.0.0.1:8500/v1/status/leader #集群 server-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 #集群節(jié)點詳細信息
3.2registrator服務器
3.2.1安裝 Gliderlabs/Registrator
Gliderlabs/Registrator 可檢查容器運行狀態(tài)自動注冊,還可注銷 docker 容器的服務到服務配置中心。目前支持 Consul、Etcd 和 SkyDNS2。
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
--ip=192.168.10.13 \
consul://192.168.10.23:8500
--net=host :把運行的docker容器設定為host網(wǎng)絡模式。
-v /var/run/docker.sock:/tmp/docker.sock :把宿主機的Docker守護進程
(Docker daemon)默認監(jiān)聽的Unix域套接字掛載到容器中。
--restart=always :設置在容器退出時總是重啟容器。
--ip :剛才把network指定了host模式,所以我們指定ip為宿主機的ip。
consul :指定consul服務器的IP和端口。
3.2.2測試服務發(fā)現(xiàn)功能是否正常
docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd httpd
#-h:設置容器主機名
3.2.3驗證 http 和 nginx 服務是否注冊到 consul
3.3consul-template
Consul-Template是基于Consul的自動替換配置文件的應用。Consul-Template是一個守護進程,用于實時查詢Consul集群信息,并更新文件系統(tǒng)上任意數(shù)量的指定模板,生成配置文件。更新完成以后,可以選擇運行 shell 命令執(zhí)行更新操作,重新加載 Nginx。
Consul-Template可以查詢Consul中的服務目錄、Key、Key-values 等。這種強大的抽象功能和查詢語言模板可以使 Consul-Template 特別適合動態(tài)的創(chuàng)建配置文件。例如:創(chuàng)建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。
3.3.1準備 template nginx 模板文件
//在consul服務器上操作
vim /opt/consul/nginx.ctmpl
#定義nginx upstream一個簡單模板
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
#定義一個server,監(jiān)聽8000端口,反向代理到upstream
server {
listen 8000;
server_name localhost 192.168.10.23;
access_log /var/log/nginx/kgc.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.3.2編譯安裝nginx
2. yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make -j && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
3.3.3配置 nginx
3. ......
http {
include mime.types;
include vhost/*.conf; #添加虛擬主機目錄
default_type application/octet-stream;
......
//創(chuàng)建虛擬主機目錄
mkdir /usr/local/nginx/conf/vhost
//創(chuàng)建日志文件目錄
mkdir /var/log/nginx
//啟動nginx
nginx
3.3.4配置并啟動 template
4. unzip consul-template_0.19.3_linux_amd64.zip -d /opt/
cd /opt/
mv consul-template /usr/local/bin/
//在前臺啟動 template 服務,啟動后不要按 ctrl+c 中止 consul-template 進程。
consul-template --consul-addr 192.168.10.23:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
//另外打開一個終端查看生成配置文件
upstream http_backend {
server 192.168.10.13:83;
server 192.168.10.13:84;
}
server {
listen 8000;
server_name 192.168.10.23;
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;
}
}
3.3.5訪問 template-nginx
5. docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f0dc08956f4 httpd "httpd-foreground" 1 hours ago Up 1 hours 0.0.0.0:89->80/tcp test-04
a0bde07299da httpd "httpd-foreground" 1 hours ago Up 1 hours 0.0.0.0:88->80/tcp test-03
4f74d2c38844 nginx "/docker-entrypoint.…" 1 hours ago Up 1 hours 0.0.0.0:84->80/tcp test-02
b73106db285b nginx "/docker-entrypoint.…" 1 hours ago Up 1 hours 0.0.0.0:83->80/tcp test-01
409331c16824 gliderlabs/registrator:latest "/bin/registrator -i…" 1 hours ago Up 1 hours registrator
docker exec -it 4f74d2c38844 bash
echo "this is test1 web" > /usr/share/nginx/html/index.html
docker exec -it b73106db285b bash
echo "this is test2 web" > /usr/share/nginx/html/index.html
瀏覽器訪問:http://192.168.10.23:8000/,并不斷刷新。
3.3.6增加一個 nginx 容器節(jié)點
(1)增加一個 nginx 容器節(jié)點,測試服務發(fā)現(xiàn)及配置更新功能。
docker run -itd -p:85:80 --name test-05 -h test05 nginx
//觀察 template 服務,會從模板更新/usr/local/nginx/conf/vhost/kgc.conf 文件內容,并且重載 nginx 服務
(2)查看/usr/local/nginx/conf/vhost/kgc.conf 文件內容 。
cat /usr/local/nginx/conf/vhost/kgc.conf
upstream http_backend {
server 192.168.10.23:83;:去!
server 192.168.10.23:84;
server 192.168.10.23:85;
server 192.168.10.23:86;
}
(3)查看三臺 nginx 容器日志,請求正常輪詢到各個容器節(jié)點上
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
docker logs -f test-06
3.4consul 多節(jié)點
添加一臺已有docker環(huán)境的服務器192.168.10.14/24加入已有的群集中
consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.10.14 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.10.23 &> /var/log/consul.log &
-enable-script-checks=true :設置檢查服務為可用
-datacenter : 數(shù)據(jù)中心名稱文章來源:http://www.zghlxwxcb.cn/news/detail-684571.html
-join :加入到已有的集群中文章來源地址http://www.zghlxwxcb.cn/news/detail-684571.html
consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.10.23:8301 alive server 0.9.2 2 dc1
consul-server02 192.168.10.14:8301 alive server 0.9.2 2 dc1
consul operator raft list-peers
Node ID Address State Voter RaftProtocol
Node ID Address State Voter RaftProtocol
consul-server01 192.168.10.23:8300 192.168.10.23:8300 leader true 2
consul-server02 192.168.10.14:8300 192.168.10.13:8300 follower true 2
到了這里,關于docker之 Consul(注冊與發(fā)現(xiàn))的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!