服務(wù)注冊與發(fā)現(xiàn)是微服務(wù)架構(gòu)中不可或缺的重要組件。起初服務(wù)都是單節(jié)點的,不保障高可用性,也不考慮服務(wù)的壓力承載,服務(wù)之間調(diào)用單純的通過接口訪問。直到后來出現(xiàn)了多個節(jié)點的分布式架構(gòu),起初的解決手段是在服務(wù)前端負載均衡,這樣前端必須要知道所有后端服務(wù)的網(wǎng)絡(luò)位置,并配置在配置文件中。這里就會有幾個問題:
既然有這些問題,那么服務(wù)注冊與發(fā)現(xiàn)就是解決這些問題的。后端服務(wù)A–N可以把當前自己的網(wǎng)絡(luò)位置注冊到服務(wù)發(fā)現(xiàn)模塊,服務(wù)發(fā)現(xiàn)就以K-v的方式記錄下來,K–般是服務(wù)名,v就是IP:PORT。服務(wù)發(fā)現(xiàn)模塊定時的進行健康檢查,輪詢查看這些后端服務(wù)能不能訪問的了。前端在調(diào)用后端服務(wù)A-N的時候,就跑去服務(wù)發(fā)現(xiàn)模塊問下它們的網(wǎng)絡(luò)位置,然后再調(diào)用它們的服務(wù)。這樣的方式就可以解決上面的問題了,前端完全不需要記錄這些后端服務(wù)的網(wǎng)絡(luò)位置,前端和后端完全解耦
服務(wù)注冊與發(fā)現(xiàn)
健康檢查:
Key/Value存儲:
環(huán)境準備
服務(wù)器類型 | 系統(tǒng) | IP地址 | 需要安裝的組件 |
---|---|---|---|
consul服務(wù)器 | CentOS7.4(64 位) | 192.168.80.10 | 運行consul服務(wù)、nginx服務(wù)、consul-template守護進程 |
registrator服務(wù)器 | CentOS7.4(64 位) | 192.168.80.20 | 運行registrator容器、nginx服務(wù) |
所有服務(wù)器關(guān)閉防火墻和SElinux
systemctl stop firewalld setenforce 0
mkdir /opt/consul/ cd /opt/consul/ rz -E #導(dǎo)入下面的壓縮包 consul_0.9.2_linux_amd64.zip unzip consul_0.9.2_linux_amd64.zip mv consul /usr/local/bin/
consul agent \ -server \ -bootstrap \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.80.10 \ -client=0.0.0.0 \ -node=consul-server01 &> /var/log/consul.log & netstat -napt | grep consul consul members consul info | grep leader
相關(guān)選項說明如下:
選項 | 說明 |
---|---|
-以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 | 指定用來在集群內(nèi)部的通訊地址,集群內(nèi)的所有節(jié)點到此地址都必須是可達的,默認是0.0.0.0。 |
-client | 指定consul 綁定在哪個client地址上,這個地址提供HTTP、DNS、RPC等服務(wù),默認是127.0.0.1。 |
-node | 節(jié)點在集群中的名稱,在一個集群中必須是唯一的, 默認是該節(jié)點的主機名。 |
-datacenter | 指定數(shù)據(jù)中心名稱,默認是dc1。 |
1)查看member狀態(tài)和查看集群狀態(tài)
查看集群server成員 curl 127.0.0.1:8500/v1/status/peers 集群Raf leader curl 127.0.0.1:8500/v1/status/leader 注冊的所有服務(wù) curl 127.0.0.1:8500/v1/catalog/services 查看nginx服務(wù)信息 curl 127.0.0.1:8500/v1/catalog/nginx 集群節(jié)點詳細信息 curl 127.0.0.1:8500/v1/catalog/nodes
docker run -d \ --name=registrator \ --net=host \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart=always \ gliderlabs/registrator:latest \ -ip=192.168.80.20 \ consul://192.168.80.10:8500
–net=host | 把運行的docker容器設(shè)定為host網(wǎng)絡(luò)模式。 |
---|---|
-v /var/run/docker.sock:/tmp/docker.sock | 把宿主機的Docker守護進程(Docker daemon)默認監(jiān)聽的Unix域套接字掛載到容器中。 |
–restart=always | 設(shè)置在容器退出時總是重啟容器。 |
–ip | 剛才把network指定了host模式,所以我們指定ip為宿主機的ip。 |
consul | 指定consul服務(wù)器的IP和端口。 |
docker run -itd -p:81:80 --name test-01 -h test01 nginx docker run -itd -p:82:80 --name test-02 -h test02 nginx docker run -itd -p:83:80 --name test-03 -h test03 httpd docker run -itd -p:84:80 --name test-04 -h test04 httpd
在consul服務(wù)器上進行操作
1)添加nginx.ctmpl配置文件
[root@consul consul]# pwd #當前在/opt/consul目錄下 /opt/consul [root@consul consul]# vim nginx.ctmpl upstream nginx_slb { {{range service "nginx"}} server {{.Address}}:{{.Port}}; {{end}} } server { listen 8000; server_name localhost 192.168.80.10; access_log /var/log/nginx/clj.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://nginx_slb; } }
2)傳入nginx安裝包并解壓
cd .. rz -E #上傳nginx壓縮包,如下: nginx-1.12.0.tar.gz tar zxvf nginx-1.12.0.tar.gz #解壓
3)創(chuàng)建nginx程序用戶并安裝依賴包
useradd -M -s /sbin/nologin nginx yum -y install gcc pcre-devel zlib-devel gcc-c++ make
4)編譯安裝后優(yōu)化路徑
cd nginx-1.12.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
5)修改nginx配置文件
cd /usr/local/nginx/conf/ vim nginx.conf http { include vhost/*.conf; #19行添加此項配置
6)創(chuàng)建nginx服務(wù)的vhost和日志目錄
mkdir vhost mkdir /var/log/nginx nginx #啟動nginx netstat -natp | grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 59892/nginx: master
7)傳入consul-template_0.19.3_linux_amd64.zip壓縮包并解壓
cd /opt/ rz -E #傳入template文件,如下 consul-template_0.19.3_linux_amd64.zip unzip consul-template_0.19.3_linux_amd64.zip mv consul-template /usr/local/bin/
8)前臺啟動consul-template(后臺也可以)
consul-template --consul-addr 192.168.80.10:8500 \ --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/clj.conf:/usr/local/nginx/sbin/nginx -s reload" \ --log-level=info
9)新開一個終端查看配置文件是否生成
cd /usr/local/nginx/conf/vhost/ vim clj.conf
10)在registrator服務(wù)器上添加端口為85的nginx容器
docker run -itd -p:85:80 --name test-05 -h test05 nginx
11)查看consul服務(wù)器運行template服務(wù)頁面發(fā)生了變化
12)再次查看一下配置文件(可以看到增加一個85端口,使用docker stop停止一個容器的話,配置文件也會相對應(yīng)改變)
13)修改容器nginx站點目錄中默認的html.index文件
14)在瀏覽器中進行訪問測試
1)先建立 consul 服務(wù)
rz -y #導(dǎo)入consul壓縮包 consul_0.9.2_linux_amd64.zip mv consul /usr/local/bin/
2)添加一臺已有docker環(huán)境的服務(wù)器加入到已有的集群中
consul agent \ -server \ --bootstrap \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.80.30 \ -client=0.0.0.0 \ -node=consul-server02 \ -enable-script-checks=true \ -datacenter=dc1 \ -join 192.168.80.10 &> /var/log/consul.log &
–enable-script-ckecks=true | 設(shè)置檢查服務(wù)為可用 |
---|---|
-datacenter | 數(shù)據(jù)中心名稱 |
-join | 加入到已有的集群中 |
–enable-script-ckecks=true 設(shè)置檢查服務(wù)為可用-datacenter數(shù)據(jù)中心名稱-join加入到已有的集群中
3)在consul服務(wù)器上查看
到此這篇關(guān)于Docker consul的容器服務(wù)更新與發(fā)現(xiàn)的文章就介紹到這了,更多相關(guān)Docker consul容器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
巨人網(wǎng)絡(luò)通訊聲明:本文標題《Docker consul的容器服務(wù)更新與發(fā)現(xiàn)的問題小結(jié)》,本文關(guān)鍵詞 Docker,consul,的,容器,服務(wù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。