工作流程:
當後面容器增加時,registrator發現並註冊容器中的服務—》通知consul server更新—》consul template模板進行更新,自動修改nginx.conf中的upstream引數
Registrator簡介:
Registrator監控新建的Docker容器,並且檢查判定這些容器提供的服務。從我們的目的出發,任何監聽在某個埠的程式都是服務。Registrator發現在容器內發現的任務服務,都將被新增到一個服務註冊端,比如Consul或etcd
實驗環境:
1.consul伺服器中安裝consul
[root@localhost ~]# mkdir /root/consul
上傳consul_0.9.2_linux_amd64.zip軟體到/root/consul中
[root@localhost ~]# cp consul_0.9.2_linux_amd64.zip consul
[root@localhost ~]# cd consul/
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip
[root@localhost consul]# mv consul /usr/bin/
[root@localhost consul]# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=14.0.0.20 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
#bootstrap:來自 Twitter,是目前最受歡迎的前端框架
#ui:通過網頁存取
[root@localhost consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 14.0.0.20:8301 alive server 0.9.2 2 dc1
[root@localhost consul]# consul info | grep leader
leader = true
leader_addr = 14.0.0.20:8300
2.通過httpd api獲取叢集資訊
[root@localhost consul]# curl 127.0.0.1:8500/v1/status/peers #檢視叢集server成員
["14.0.0.30:8300"]
[root@localhost consul]# curl 127.0.0.1:8500/v1/status/leader #叢集leader
"14.0.0.30:8300"
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services #註冊的所有服務
{"consul":[]}
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nginx #檢視nginx服務資訊
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/nodes #叢集節點詳細資訊
[{"ID":"3405c908-c58a-fe44-5cf8-f6b63fe80371","Node":"consul-server01","Address":"14.0.0.30","Datacenter":"dc1","TaggedAddresses":{"lan":"14.0.0.30","wan":"14.0.0.30"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}]
1.安裝gliderlabs/registrator
可檢查容器執行狀態自動註冊,還可登出docker容器的服務到服務設定中心
目前支援consul、etcd和SkyDNS2
在14.0.0.30伺服器,執行以下操作:
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=14.0.0.30 \
consul://14.0.0.20:8500
2.測試發現服務的功能是否正常
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
#回到consul節點檢視nginx是否被自動註冊
[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"nginx":[]}
3.驗證nginx服務是否註冊到consul,瀏覽器輸入http://14.0.0.20:8500
點選「NODES」,再點選「consul-server01」,會出現自動新增的兩個nginx服務
4.準備template nginx模板檔案,引數以變數形式寫入
在consul伺服器節點上操作
[root@localhost consul]# vim /root/consul/nginx.ctmpl
upstream http-server {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
server {
listen 83;
server_name localhost 14.0.0.20;
access_log /var/log/nginx/test-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-server;
}
}
5.Nginx編譯安裝
#安裝環境依賴包
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
#建立執行使用者、組
useradd -M -s /sbin/nologin nginx
#編譯安裝
tar zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
#以便管理員直接執行「nginx」命令就可以呼叫Nginx的主程式
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#測試語法
nginx -t
6.設定nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; #新增虛擬主機目錄
default_type application/octet-stream;
#建立虛擬主機目錄
[root@localhost ~]# mkdir /usr/local/nginx/conf/vhost
#建立紀錄檔檔案目錄
[root@localhost ~]# mkdir /var/log/nginx
#啟動nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
7.設定並啟動template
consul-template是一個守護行程,用於實時查詢consul叢集資訊,並更新檔案系統上任意數量的指定模板,生成組態檔。更新完成後,可以選擇執行shell命令執行更新操作,重新載入nginx。consul-template可以查詢consul中的服務目錄、key、key-values等。這種強大的抽象功能和查詢語言模板可以使consul-template特別適合動態的建立組態檔。例如:建立nginx反向代理。
上傳consul-template_0.19.3_linux_amd64.zip包到/root目錄下
[root@localhost ~]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@localhost ~]# mv consul-template /usr/bin/
[root@localhost ~]# consul-template -consul-addr 14.0.0.20:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
#指定模板檔案:/root/consul/nginx.ctmpl,對映到/usr/local/nginx/conf/vhost/test.conf,然後通過「/usr/local/nginx/sbin/nginx -s reload」進行過載
8.增加一個nginx容器節點,測試服務發現及設定更新功能
#在registrator伺服器端註冊
[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
#在consul伺服器監控會提示自動更新
2020/09/23 10:22:21.760332 [INFO] (runner) initiating run
2020/09/23 10:22:21.761907 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2020/09/23 10:22:21.761926 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2020/09/23 10:22:21.761986 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
#檢視/usr/local/nginx/conf/vhost/test.conf
#檢視三臺nginx容器紀錄檔,請求是否正常輪詢到各個容器節點上(開三個終端檢視輪詢效果)
[root@localhost ~]# docker logs -f test-01
[root@localhost ~]# docker logs -f test-02
[root@localhost ~]# docker logs -f test-05