docker-Consul的概述及consul集群环境的搭建
生活随笔
收集整理的這篇文章主要介紹了
docker-Consul的概述及consul集群环境的搭建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、概述
- Consul的作用
- 二、搭建consul集群環境
- 1、安裝Consul
- 2、配置容器服務自動加入nginx集群
- (1)安裝Gliderlabs/Registrator
- (2)測試服務
- 3、驗證http和nginx服務是否注冊到consul
- 4、安裝consul-template
- 5、配置template模板自動更新
- 6、部署nginx
- 7、啟動template
- 8、測試
一、概述
- 1、什么是consul
Consul是HashiCorp公司推出的開源工具,Consul由Go語言開發,部署起來非常容易,只需要極少的可執行程序和配置文件,具有綠色、輕量級的特點。
Consul是分布式的、高可用的、可橫向擴展的用于實現分布式系統的服務發現與配置。
Consul的作用
- 服務注冊與發現(主要功能),提供HTTP和DNS兩種發現方式
- 健康檢查,支持多種協議,HTTP、TCP等
- Key/Value存儲
- 支持多數據中心
- 基于Golong語言,可移植性強
- 支持ACL訪問控制
- 與Docker等輕量級容器可無縫配合
二、搭建consul集群環境
| consul | Centos 7 | 192.168.172.10 | Docker 、Consul、Consul-template |
| registrator | Centos 7 | 192.168.172.20 | Docker、registrator |
1、安裝Consul
consul:192.168.172.10
編譯安裝consul
查看集群信息
consul membersconsul info | grep leader 這里查詢到的8300端口用于集群內數據的讀寫和復制通過httpd api獲取集群信息
curl 127.0.0.1:8500/v1/status/peers //查看集群server成員 curl 127.0.0.1:8500/v1/status/leader //集群Raf 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 //集群節點詳細信息 netstat -natp |grep consul 這5個端口的作用: 8300:集群內數據的讀寫和復制 8301:單個數據中心gossip協議通訊 8302:跨數據中心gossip協議通訊 8500:提供獲取服務列表、注冊服務、注銷服務等HTTP接口;提供UI服務 8600:采用DNS協議提供服務發現功能2、配置容器服務自動加入nginx集群
registrator:192.168.172.20
(1)安裝Gliderlabs/Registrator
docker run -d \ --name=registrator \ --net=host \ -v /var/run/docker.sock:/tmp/docker.sock \ --restart=always \ gliderlabs/registrator:latest \ -ip=192.168.172.20 \ consul://192.168.172.10:8500(2)測試服務
測試發現功能是否正常
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:91:80 --name test-03 -h test03 httpd docker run -itd -p:92:80 --name test-04 -h test04 httpd
3、驗證http和nginx服務是否注冊到consul
宿主機
瀏覽器輸入http://192.168.172.10:8500,“單擊NODES”,然后單擊“consurl-server01”,會出現5個服務
consul:192.168.172.10
curl 127.0.0.1:8500/v1/catalog/services4、安裝consul-template
consul:192.168.172.10
unzip consul-template_0.19.3_linux_amd64.zip mv consul-template /usr/bin/5、配置template模板自動更新
consul:192.168.172.10
vim /root/consul/nginx.ctmplupstream http_backend {{{range service "nginx"}}server {{.Address}}:{{.Port}};{{end}} }server {listen 81;server_name localhost 192.168.172.10;access_log /var/log/nginx/nginx01-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、部署nginx
consul:192.168.172.10
yum -y install gcc pcre-devel zlib-devel rz nginx-1.12.0.tar.gz tar zxvf nginx-1.12.0.tar.gz -C /opt cd /opt/nginx-1.12.10./configure --prefix=/usr/local/nginxmake && make install
配置 nginx
vim /usr/local/nginx/conf/nginx.conf http {include mime.types;include vhost/*.conf; //添加虛擬主機目錄default_type application/octet-stream;//創建虛擬主機目錄 mkdir /usr/local/nginx/conf/vhost //創建日志文件目錄 mkdir /var/log/nginx//啟動nginx /usr/local/nginx/sbin/nginx7、啟動template
consul:192.168.172.10
consul-template -consul-addr 192.168.172.10:8500 \ -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/nginx88.conf:/usr/local/nginx/sbin/nginx -s reload" \ --log-level=info
重開一個consul終端查看
8、測試
增加一個nginx容器節點,測試服務發現及配置更新功能
registrator:192.168.172.20在registrator服務端注冊
consul:192.168.172.10
在consul服務器監控裝填會有提示自動更新
查看三臺nginx容器日志,請求正常輪詢到各個容器節點上
registrator:192.168.172.20
docker logs -f test-01 docker logs -f test-02 docker logs -f test-05
總結
以上是生活随笔為你收集整理的docker-Consul的概述及consul集群环境的搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存条选择攻略:8GB够用?32GB更香
- 下一篇: 焊接DDR3内存:三步轻松掌握,让你的电