consul集群搭建 与 aws 负载均衡器配合使用

consul集群搭建 与 aws 负载均衡器配合使用

20180803 陈信
参考:
https://blog.csdn.net/chenchong08/article/details/77885989 集群搭建
https://www.hi-linux.com/posts/28048.html 参数说明

规划
3台主机构成集群:
10.10.0.50
10.10.0.51
10.10.0.52

放置路径:
/home/xbzj/data-service/consul/
文件:
consul consul.json data startConsul.sh

配置与脚本
10.10.0.50
[xbzj@ip-10-10-0-50 consul]$ cat consul.json
{
"ports": {
"http": 8500,
"dns": -1,
"serf_lan": 8301,
"serf_wan": 8302,
"server": 8300
}
}
[xbzj@ip-10-10-0-50 consul]$ cat startConsul.sh
nohup ./consul agent -server -bootstrap-expect 3 -data-dir ./data -config-file=./consul.json -node 10.10.0.50 -bind=0.0.0.0 -client 10.10.0.50 -enable-script-checks=true -datacenter dc1 >./nohup.out 2>&1 &

nohup ./consul agent -server -bootstrap-expect 1 -data-dir ./data -config-file=./consul.json -node=cn1 -bind=127.0.0.1 -client 127.0.0.1 -enable-script-checks=true -datacenter=dc1 >./nohup.out 2>&1 & #单节点非集群模式

10.10.0.51
[xbzj@ip-10-10-0-51 consul]$ cat consul.json
{
"ports": {
"http": 8500,
"dns": -1,
"serf_lan": 8301,
"serf_wan": 8302,
"server": 8300
}
}
[xbzj@ip-10-10-0-51 consul]$ cat startConsul.sh
nohup ./consul agent -server -bootstrap-expect 3 -data-dir ./data -config-file=./consul.json -join 10.10.0.50 -node 10.10.0.51 -bind=0.0.0.0 -client 10.10.0.51 -enable-script-checks=true -datacenter dc1 >./nohup.out 2>&1 &

10.10.0.52
[xbzj@ip-10-10-0-52 consul]$ cat consul.json
{
"ports": {
"http": 8500,
"dns": -1,
"serf_lan": 8301,
"serf_wan": 8302,
"server": 8300
}
}
[xbzj@ip-10-10-0-52 consul]$ cat startConsul.sh
nohup ./consul agent -server -bootstrap-expect 3 -data-dir ./data -config-file=./consul.json -join 10.10.0.50 -node 10.10.0.52 -bind=0.0.0.0 -client 10.10.0.52 -enable-script-checks=true -datacenter dc1 >./nohup.out 2>&1 &

检查集群
./consul operator raft list-peers -http-addr=10.10.0.52:8500

Node ID Address State Voter RaftProtocol
10.10.0.50 a637f472-ede6-88d8-3a0f-740974b59d71 10.10.0.50:8300 leader true 3
10.10.0.51 8ee26690-0f2e-e8e6-5987-d3c11ca36b31 10.10.0.51:8300 follower true 3
10.10.0.52 09ec5c57-10d1-d5f5-d4b7-deda30dd4731 10.10.0.52:8300 follower true 3

若kill掉50,会有剩下的2台中1台变成leader.剩余2台仍能正常提供服务.
若kill掉2台,只保留1台,则无法正常提供服务.
若kill掉3台,再次启动3个consul后,leader可能会发生变化,比如51成为leader了.

检查节点间数据复制
10.10.0.50 set/get参数
./consul kv put -http-addr=10.0.0.50:8500 key value
Success! Data written to: key
./consul kv get -http-addr=10.0.0.50:8500 key
value
以上说明在10.10.0.50 可以正常设置key的值为value,并能正常查回来.
同样分别在51,52上put和set其他key-value,效果相同.说明3个服务器可以相互复制信息.

负载均衡器ELB
aws可以添加NLB
./consul kv put -http-addr=consul-test-127db72a1b523d25.elb.ap-southeast-1.amazonaws.com:8500 key3 value3
./consul kv get -http-addr=consul-test-127db72a1b523d25.elb.ap-southeast-1.amazonaws.com:8500 key3
注意事项:
如果是在50,51,52这3台机器上通过NLB访问自己的服务,经常会报错:
Error querying Consul agent: Get http://consul-test-127db7...naws.com:8500/v1/kv/key3: dial tcp 10.0.1.113:8500: i/o timeout
因为这是从consul上通过NLB再访问consul,可能造成一些死循环,故报了io的timeout.
正常情况应该是从其他机器(非consul)来访问NLB.

数据说明
删除数据的话,只需要清空data文件夹下的东西即可.
日常kill掉consul进程,不会影响数据的保持.

指令说明
./consul --help
./consul kv --help
./consul kv put --help

ports This is a nested object that allows setting the bind ports for the following keys:

dns - The DNS server, -1 to disable. Default 8600.
http - The HTTP API, -1 to disable. Default 8500.
https - The HTTPS API, -1 to disable. Default -1 (disabled).
serf_lan - The Serf LAN port. Default 8301.
serf_wan - The Serf WAN port. Default 8302. Set to -1 to disable. Note: this will disable WAN federation which is not recommended. Various catalog and WAN related endpoints will return errors or empty results.
server - Server RPC address. Default 8300.
proxy_min_port - Minimum port number to use for automatically assigned managed Connect proxies. If Connect is disabled, managed proxies are unused, or ports are always specified, then this value is unused. Defaults to 20000.
proxy_max_port - Maximum port number to use for automatically assigned managed Connect proxies. See proxy_min_port for more information. Defaults to 20255.

posted @ 2020-04-21 14:58  ChanixChen  阅读(411)  评论(0编辑  收藏  举报