日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

docker-Consul的概述及consul集群环境的搭建

發布時間:2024/2/28 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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集群環境

主機操作系統IP地址主要軟件及版本
consulCentos 7192.168.172.10Docker 、Consul、Consul-template
registratorCentos 7192.168.172.20Docker、registrator

1、安裝Consul

consul:192.168.172.10
編譯安裝consul

mkdir /root/consul cd /root/consul //放入安裝包 unzip consul_0.9.2_linux_amd64.zip mv consul /usr/binconsul agent \ -server \ -bootstrap \ -ui \ -data-dir=/var/lib/consul-data \ -bind=192.168.172.10 \ -client=0.0.0.0 \ -node=consul-server01 &> /var/log/consul.log &

查看集群信息

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/services

4、安裝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/nginx

7、啟動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服務端注冊

docker run -itd -p:83:80 --name test-05 -h test05 nginx

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


超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的docker-Consul的概述及consul集群环境的搭建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。