一,什么是consul?
1,Consul 是 HashiCorp 公司推出的開源軟件,用於實現分布式系統的服務發現與配置。
Consul 是分布式的、高可用的、 可橫向擴展的
2,官方網站:
https://www.consul.io/
3,Consul 集群間使用了 Gossip 協議通信和 raft 一致性算法
4,Consul和Eureka的不同之處:
Eureka只需要在項目中加入服務端依賴,就可以作為服務端使用;
Consul需要從官網下載,並單獨安裝
5,本文演示用3台服務器組建一個consul的集群
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,下載consul軟件
1,下載地址:
https://www.consul.io/downloads
選擇64位linux版本下載
2,把consul的安裝文件解壓:
[root@localhost consul]# cd /usr/local/source/consul [root@localhost consul]# ls consul_1.8.4_linux_amd64.zip [root@localhost consul]# unzip consul_1.8.4_linux_amd64.zip Archive: consul_1.8.4_linux_amd64.zip inflating: consul [root@localhost consul]# ls consul consul_1.8.4_linux_amd64.zip
3,復制到到各服務器的/usr/local/soft目錄下
三,在第一台consul服務器上運行:
1,生成數據目錄:
[root@consul1 /]# mkdir /data/ [root@consul1 /]# mkdir /data/consul/ [root@consul1 /]# mkdir /data/consul/data [root@consul1 /]# chmod 777 /data/consul/data
2,運行consul
-server:以server身份啟動
-bootstrap-expect=2:集群要求的最少server數量
-bind:監聽的ip
-client:客戶端ip,0.0.0.0表示不限制客戶端ip
-data-dir:指定存放數據的目錄
-node:指定節點id,注意:同一集群內節點id不允許重復
[root@consul1 /]# nohup /usr/local/soft/consul agent -server -bind=172.17.0.2 -client=0.0.0.0 -bootstrap-expect=2 -data-dir=/data/consul/da -node=server-2 >/dev/null 2>&1 & [1] 549 [root@consul1 /]#
查看是否在運行中,這里我們選擇查看端口:
[root@consul1 /]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 4096 172.17.0.2:8300 0.0.0.0:* users:(("consul",pid=549,fd=6)) LISTEN 0 4096 172.17.0.2:8301 0.0.0.0:* users:(("consul",pid=549,fd=12)) LISTEN 0 4096 172.17.0.2:8302 0.0.0.0:* users:(("consul",pid=549,fd=8)) LISTEN 0 4096 *:8500 *:* users:(("consul",pid=549,fd=16)) LISTEN 0 4096 *:8600 *:* users:(("consul",pid=549,fd=15))
可以看到consul已經在守護端口中,而且consul啟用了多個端口
3,查看consul的版本:
[root@consul1 /]# /usr/local/soft/consul --version Consul v1.8.4 Revision 12b16df32 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
4,查看consul的集群成員數量
[root@consul1 /]# /usr/local/soft/consul members Node Address Status Type Build Protocol DC Segment server-2 172.17.0.2:8301 alive server 1.8.4 2 dc1 <all>
只有一台機器,正常
5,查看當前節點的信息:
[root@consul1 /]# /usr/local/soft/consul info agent: check_monitors = 0 check_ttls = 0 checks = 0 services = 0 build: prerelease = revision = 12b16df3 version = 1.8.4 consul: acl = disabled bootstrap = false known_datacenters = 1 leader = false leader_addr = server = true raft: applied_index = 0 commit_index = 0 fsm_pending = 0 last_contact = never last_log_index = 0 last_log_term = 0 last_snapshot_index = 0 last_snapshot_term = 0 latest_configuration = [] latest_configuration_index = 0 num_peers = 0 protocol_version = 3 protocol_version_max = 3 protocol_version_min = 0 snapshot_version_max = 1 snapshot_version_min = 0 state = Follower term = 0 runtime: arch = amd64 cpu_count = 2 goroutines = 79 max_procs = 2 os = linux version = go1.14.6 serf_lan: coordinate_resets = 0 encrypted = false event_queue = 0 event_time = 1 failed = 0 health_score = 0 intent_queue = 0 left = 0 member_time = 1 members = 1 query_queue = 0 query_time = 1 serf_wan: coordinate_resets = 0 encrypted = false event_queue = 0 event_time = 1 failed = 0 health_score = 0 intent_queue = 0 left = 0 member_time = 1 members = 1 query_queue = 0 query_time = 1
四,在第二台consul服務器上運行相同的操作,
啟動的命令需要修改ip和節點名:
[root@consul2 /]# nohup /usr/local/soft/consul agent -server -bind=172.17.0.3 -client=0.0.0.0 -bootstrap-expect=2 -data-dir=/data/consul/da -node=server-3 >/dev/null 2>&1 & [1] 371
查看集群成員:
[root@consul2 /]# /usr/local/soft/consul members Node Address Status Type Build Protocol DC Segment server-3 172.17.0.3:8301 alive server 1.8.4 2 dc1 <all>
把當前節點加入到第一台consul服務器的ip:
[root@consul2 /]# /usr/local/soft/consul join 172.17.0.2 Successfully joined cluster by contacting 1 nodes.
加入成功后再次查看節點:
[root@consul2 /]# /usr/local/soft/consul members Node Address Status Type Build Protocol DC Segment server-2 172.17.0.2:8301 alive server 1.8.4 2 dc1 <all> server-3 172.17.0.3:8301 alive server 1.8.4 2 dc1 <all>
五,在第三台consul服務器上運行相同的操作:
命令修改ip和節點名:並允許訪問ui
-ui:允許訪問web ui
[root@consul3 /]# nohup /usr/local/soft/consul agent -server -bind=172.17.0.4 -client=0.0.0.0 -bootstrap-expect=2 -data-dir=/data/consul/da -node=server-4 -ui >/dev/null 2>&1 & [1] 229 [root@consul3 /]#
查看成員
[root@consul3 /]# /usr/local/soft/consul members Node Address Status Type Build Protocol DC Segment server-4 172.17.0.4:8301 alive server 1.8.4 2 dc1 <all>
加入集群
[root@consul3 /]# /usr/local/soft/consul join 172.17.0.2 Successfully joined cluster by contacting 1 nodes.
成功后再次查看成員
[root@consul3 /]# /usr/local/soft/consul members Node Address Status Type Build Protocol DC Segment server-2 172.17.0.2:8301 alive server 1.8.4 2 dc1 <all> server-3 172.17.0.3:8301 alive server 1.8.4 2 dc1 <all> server-4 172.17.0.4:8301 alive server 1.8.4 2 dc1 <all>
六,訪問在第三台consul服務器上啟用的web ui:
訪問:
http://172.17.0.4:8500/
返回:
點擊后可以查看集群內3個實例:
如圖:
點擊每台機器可查看其狀態:
七,consul的退出:
leave指令觸發一個優雅的離開動作並關閉agent,節點離開后不會嘗試重新加入集群中
在第二台consul服務器上執行:
[root@consul2 /]# /usr/local/soft/consul leave
Graceful leave complete
查看端口:
[root@consul2 /]# ss -lntp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
可以看到守護進程已退出
在其他節點查看成員:
[root@consul3 /]# /usr/local/soft/consul members Node Address Status Type Build Protocol DC Segment server-2 172.17.0.2:8301 alive server 1.8.4 2 dc1 <all> server-3 172.17.0.3:8301 left server 1.8.4 2 dc1 <all> server-4 172.17.0.4:8301 alive server 1.8.4 2 dc1 <all>
可以看到server-3的狀態已變更為left
從web ui查看:
已看不到 server-3
八,查看linux的版本:
[root@localhost liuhongdi]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core)