一、Consul介紹
1、什么是服務(wù)注冊與發(fā)現(xiàn)
- 服務(wù)注冊與發(fā)現(xiàn)是微服務(wù)架構(gòu)中不可或缺的重要組件。起初服務(wù)都是單節(jié)點的,不保障高可用性,也不考慮服務(wù)的壓力承載,服務(wù)之間調(diào)用單純的通過接口訪問。直到后來出現(xiàn)了多個節(jié)點的分布式架構(gòu),起初的解決手段是在服務(wù)前端負載均衡,這樣前端必須要知道所有后端服務(wù)的網(wǎng)絡(luò)位置,并配置在配置文件中。
2、服務(wù)注冊與發(fā)現(xiàn)中的問題
- 如果需要調(diào)用后端服務(wù)A-N,就需要配置N個服務(wù)的網(wǎng)絡(luò)位置,配置很麻煩
- 后端服務(wù)的網(wǎng)絡(luò)位置變化,都需要改變每個調(diào)用者的配置
- 既然有這些問題,那么服務(wù)注冊與發(fā)現(xiàn)就是解決這些問題的。后端服務(wù)A-N可以把當(dāng)前自己的網(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ò)位置,前端和后端完全解耦
3、什么是consul
- consul是google開源的一個使用go語言開發(fā)的服務(wù)管理軟件。支持多數(shù)據(jù)中心、分布式高可用的、服務(wù)發(fā)現(xiàn)和配置共享。采用Raft算法,用來保證服務(wù)的高可用。內(nèi)置了服務(wù)注冊與發(fā)現(xiàn)框架、分布一致性協(xié)議實現(xiàn)、健康檢查、Key/Value存儲、多數(shù)據(jù)中心方案,不再需要依賴其他工具(比如ZooKeeper等)。服務(wù)部署簡單,只有一個可運行的二進制的包。每個節(jié)點都需要運行agent,他有兩種運行模式server 和 client。 每個數(shù)據(jù)中心官方建議需要3或5個server節(jié)點以保證數(shù)據(jù)安全,同時保證server-leader的選舉能夠正確的進行。
- 在client模式下,所有注冊到當(dāng)前節(jié)點的服務(wù)會被轉(zhuǎn)發(fā)到server節(jié)點,本身是不持久化這些信息。
- 在server模式下,功能和client模式相似,唯一不同的是,它會把所有的信息持久化到本地,這樣遇到故障,信息是可以被保留的。
- server-leader是所有server節(jié)點的老大,它和其它server節(jié)點不同的是,它需要負責(zé)同步注冊的信息給其它的server節(jié)點,同時也要負責(zé)各個節(jié)點的健康監(jiān)測。
4、consul提供的一些關(guān)鍵特性
- 服務(wù)注冊與發(fā)現(xiàn):consul通過DNS或者HTTP接口使服務(wù)注冊和服務(wù)發(fā)現(xiàn)變的很容易,一些外部服務(wù),例如saas提供的也可以一樣注冊
- 健康檢查:健康檢測使consul可以快速的告警在集群中的操作。和服務(wù)發(fā)現(xiàn)的集成,可以防止服務(wù)轉(zhuǎn)發(fā)到故障的服務(wù)上面
- Key/Value存儲:一個用來存儲動態(tài)配置的系統(tǒng)。提供簡單的HTTP接口,可以在任何地方操作。
- 多數(shù)據(jù)中心:無需復(fù)雜的配置,即可支持任意數(shù)量的區(qū)域
- 安裝consul是用于服務(wù)注冊,也就是容器本身的一些信息注冊到consul里面,其他程序可以通過consul獲取注冊的相關(guān)服務(wù)信息,這就是服務(wù)注冊與發(fā)現(xiàn)。
二、consul 部署
1、實驗前準(zhǔn)備
consul服務(wù)器 192.168.247.132 運行consul服務(wù)、nginx服務(wù)、consul-template守護進程
registrator服務(wù)器 192.168.247.131 運行registrator容器、運行nginx容器
2、 建立 Consul 服務(wù)(consul服務(wù)器)
(1)準(zhǔn)備Consul 服務(wù)
[root@node02 ~]# systemctl stop firewalld.service
[root@node02 ~]# setenforce 0
setenforce: SELinux is disabled
[root@node02 ~]# mkdir /opt/consul
[root@node02 ~]# cd /opt/consul
[root@node02 consul]# unzip consul_0.9.2_linux_amd64.zip
Archive: consul_0.9.2_linux_amd64.zip
inflating: consul
[root@node02 consul]# mv consul /usr/local/bin/
(2)設(shè)置代理,在后臺啟動 consul 服務(wù)端
[root@node02 consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.247.132 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 1939
名稱 | 作用 |
---|---|
server | 以server身份啟動,默認是client |
bootstrap | 用來控制一個server是否在bootstrap模式,在一個數(shù)據(jù)中心中只能有一個server處于bootstrap模式,當(dāng)一個server處于 bootstrap模式時,可以自己選舉為 server-leader |
bootstrap-expect=2 | 集群要求的最少server數(shù)量,當(dāng)?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 |
(3)查看端口號
[root@node02 consul]# netstat -natp | grep consul
tcp 0 0 192.168.247.132:8300 0.0.0.0:* LISTEN 1939/consul
tcp 0 0 192.168.247.132:8301 0.0.0.0:* LISTEN 1939/consul
tcp 0 0 192.168.247.132:8302 0.0.0.0:* LISTEN 1939/consul
tcp 0 0 192.168.247.132:52015 192.168.247.132:8300 ESTABLISHED 1939/consul
tcp 0 0 192.168.247.132:8300 192.168.247.132:52015 ESTABLISHED 1939/consul
tcp6 0 0 :::8500 :::* LISTEN 1939/consul
tcp6 0 0 :::8600 :::* LISTEN 1939/consul
- 默認會監(jiān)聽的端口:
端口號 | 解釋 |
---|---|
8300 | replication、leader farwarding的端口 |
8301 | lan cossip的端口 |
8302 | wan gossip的端口 |
8500 | web ui界面的端口 |
8600 | 使用dns協(xié)議查看節(jié)點信息的端口 |
3、 查看集群信息
(1)查看members狀態(tài)
[root@node02 consul]# consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.247.132:8301 alive server 0.9.2 2 dc1
(2)查看集群狀態(tài)
[root@node02 consul]# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.247.132:8300 192.168.247.132:8300 leader true 2
[root@node02 consul]# consul info | grep leader
leader = true
leader_addr = 192.168.247.132:8300
4、通過 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 #注冊的所有服務(wù)
curl 127.0.0.1:8500/v1/catalog/nginx #查看 nginx 服務(wù)信息
curl 127.0.0.1:8500/v1/catalog/nodes #集群節(jié)點詳細信息
三、registrator服務(wù)器
1、容器服務(wù)自動加入 Nginx 集群
(1)安裝 Gliderlabs/Registrator
- Gliderlabs/Registrator 可檢查容器運行狀態(tài)自動注冊,還可注銷 docker 容器的服務(wù)到服務(wù)配置中心。目前支持 Consul、Etcd 和 SkyDNS2
[root@lion ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> --ip=192.168.247.131 \
> consul://192.168.247.132:8500
560c5ce43950da1e8fb86c1059d355042b33c176b69a7a2631e2013d23910811
字段 | 作用 |
---|---|
–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 |
2、 測試服務(wù)發(fā)現(xiàn)功能是否正常
[root@lion ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
2295f309fff88f72b5c6b19b2e688739ae8e7ab60ad97fb8a332138699e17861
[root@lion ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
55757a713bbdbe575ddf40b93f72e87a1b5bc4eea1d2909d8d980f669a4d323a
[root@lion ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a2abf6c4d29d: Already exists
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
641ed2409f2e1168084894dc4dc5a1df8b0f6e37c986fb2abc24683a3d846a92
[root@lion ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
675e7af3911689326e20b4d22e5cfd24802f509b8a9d9fab287e21c9a6aa27ce
-
#-h:設(shè)置容器主機名
-
出現(xiàn)以下報錯使用:systemctl restart docker
docker: Error response from daemon: driver failed programming external connectivity on endpoint test-02 (e5164ff8e230c5f3e3c52457132e5945ce8e106b18b586538853adeed47c29ce): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 84 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
3、驗證 http 和 nginx 服務(wù)是否注冊到 consul
- 瀏覽器中,輸入 http://192.168.247.132:8500,在 Web 頁面中“單擊 NODES”,然后單擊“consurl-server01”,會出現(xiàn) 5 個服務(wù)
4、在consul服務(wù)器使用curl測試連接服務(wù)器
[root@node02 consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"harbor-log":[],"httpd":[],"nginx":[]}[
四、 consul-template
1、consul-template 介紹
- Consul-Template是基于Consul的自動替換配置文件的應(yīng)用。Consul-Template是一個守護進程,用于實時查詢Consul集群信息,并更新文件系統(tǒng)上任意數(shù)量的指定模板,生成配置文件。更新完成以后,可以選擇運行 shell 命令執(zhí)行更新操作,重新加載 Nginx
- Consul-Template可以查詢Consul中的服務(wù)目錄、Key、Key-values 等。這種強大的抽象功能和查詢語言模板可以使 Consul-Template 特別適合動態(tài)的創(chuàng)建配置文件。例如:創(chuàng)建Apache/Nginx Proxy Balancers 、 Haproxy Backends等
2、準(zhǔn)備 template nginx 模板文件
(1)定義nginx upstream一個簡單模板(在consul服務(wù)器上操作)
vim /opt/consul/nginx.ctmpl
upstream http_backend {
{{range service "nginx"}}
server {{.Address}}:{{.Port}};
{{end}}
}
(2)定義一個server,監(jiān)聽8000端口,反向代理到upstream
server {
listen 8000;
server_name localhost 192.168.247.132;
access_log /var/log/nginx/group.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、編譯安裝nginx
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 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
4、配置 nginx
(1)修改配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
include mime.types;
include vhost/*.conf; #添加虛擬主機目錄
default_type application/octet-stream;
......
(2)創(chuàng)建虛擬主機目錄
[root@node02 nginx-1.24.0]# mkdir /usr/local/nginx/conf/vhost
(3)創(chuàng)建日志文件目錄
[root@node02 nginx-1.24.0]# mkdir /var/log/nginx
(4)啟動nginx
[root@node02 nginx-1.24.0]# nginx
[root@node02 nginx-1.24.0]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2023-07-26 17:56:19 CST; 50min ago
Process: 1170 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 1183 (nginx)
CGroup: /system.slice/nginx.service
├─1183 nginx: master process /usr/local/nginx/sbin/nginx
└─1189 nginx: worker process
7月 26 17:56:19 node02 systemd[1]: Starting nginx...
7月 26 17:56:19 node02 systemd[1]: Started nginx.
5、template配置并啟動
(1)配置template
[root@node02 opt]# unzip consul-template_0.19.3_linux_amd64.zip
Archive: consul-template_0.19.3_linux_amd64.zip
inflating: consul-template
[root@node02 opt]# cd /opt/
[root@node02 opt]# mv consul-template /usr/local/bin/
(2)在前臺啟動 template 服務(wù),啟動后不要按 ctrl+c 中止 consul-template 進程
[root@node02 opt]# consul-template --consul-addr 192.168.247.132:8500 \
> --template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/group.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
2023/07/26 11:26:49.018918 [INFO] consul-template v0.19.3 (ebf2d3d)
(3)另外打開一個終端查看生成配置文件
[root@node02 sbin]# cd /usr/local/nginx/conf/vhost
[root@node02 vhost]# ls
group.conf
[root@node02 vhost]# cat group.conf
upstream http_backend {
server 192.168.247.131:83;
server 192.168.247.131:84;
}
server {
listen 8000;
server_name localhost 192.168.247.132;
access_log /var/log/nginx/group.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;
}
}
6、 訪問 template-nginx
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 55757a713bbd bash
echo "this is test1 web" > /usr/share/nginx/html/index.html
docker exec -it 2295f309fff8bash
echo "this is test2 web" > /usr/share/nginx/html/index.html
- 瀏覽器訪問:http://192.168.247.132:8000/ ,并不斷刷新
7、增加一個 nginx 容器節(jié)點
(1)增加一個 nginx 容器節(jié)點,測試服務(wù)發(fā)現(xiàn)及配置更新功能
docker run -itd -p:85:80 --name test-05 -h test05 nginx
- 觀察 template 服務(wù),會從模板新/usr/local/nginx/conf/vhost/group.conf 文件內(nèi)容,并且重載 nginx 服務(wù)
(2)查看/usr/local/nginx/conf/vhost/group.conf 文件內(nèi)容文章來源:http://www.zghlxwxcb.cn/news/detail-611804.html
cat /usr/local/nginx/conf/vhost/group.conf
upstream http_backend {
server 192.168.247.131:83;
server 192.168.247.131:84;
server 192.168.247.131:85;
}
(3)查看三臺 nginx 容器日志,請求正常輪詢到各個容器節(jié)點上文章來源地址http://www.zghlxwxcb.cn/news/detail-611804.html
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
五、consul 多節(jié)點
1、添加一臺已有docker環(huán)境的服務(wù)器192.168.80.12/24加入已有的群集中
consul agent \
-server \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.80.12 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.247.132 &> /var/log/consul.log &
-enable-script-checks=true :設(shè)置檢查服務(wù)為可用
-datacenter : 數(shù)據(jù)中心名稱
-join :加入到已有的集群中
consul members
Node Address Status Type Build Protocol DC
consul-server01 192.168.247.132:8301 alive server 0.9.2 2 dc1
consul-server02 192.168.80.12:8301 alive server 0.9.2 2 dc1
consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-server01 192.168.247.132:8300 192.168.247.132:8300 leader true 2
consul-server02 192.168.80.12:8300 192.168.80.12:8300 follower true 2
到了這里,關(guān)于Docker 之 Consul容器服務(wù)更新與發(fā)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!