consul的常用api可以参考: https://my.oschina.net/guol/blog/353394
正常情况下, consul不会主动的去清除掉失效的服务,所以就需要手动的来清理, 但是往往一个consul上又有多个失效的服务,所以写了个脚本清理,脚本如下
这个脚本比较简单,适合在宿主机上搭建的服务,ip和端口都是固定的单点consul.
#!/bin/bash
# baiyongjie 2019-06-24
#consul的地址
CONSUL_ADDRESS="192.168.1.73:8500"
test -d logs || mkdir logs
echo "---------------" >> logs/`date +%Y%m%d`.log
# 获取当前consul中状态为critical的serviceID
CONSUL_CRITICAL=`curl -s -XGET http://${CONSUL_ADDRESS}/v1/health/state/critical | python -m json.tool | grep ServiceID | awk '{print $2}' |sed 's/"//g' | sed 's/,//g'`
for critical in ${CONSUL_CRITICAL}
do
echo "${critical} 已删除" >> logs/`date +%Y%m%d`.log
# 使用consul的API删除对于的serviceID
curl -XPUT http://${CONSUL_ADDRESS}/v1/agent/service/deregister/${critical}
done
但是呢, 我们这里的consul是集群方式运行在k8s中, 没一个namespace中都有一套consul集群, 所以上面的那个脚本就不能满足现状的需求了, 所以有了下面这个脚本
#!/bin/bash
# baiyongjie 2019-06-25
# 获取consul的地址
CONSUL_INGRESS=`kubectl get ingresses.extensions --all-namespaces | grep consul | awk '{print $3}'`
test -d logs || mkdir logs
# 获取consul的节点IP已经状态为critical的服务
for consul_address in ${CONSUL_INGRESS}
do
echo "------------------" >> logs/`date +%Y%m%d`.log
echo "当前consul为${consul_address}" >> logs/`date +%Y%m%d`.log
CONSUL_NODES=$(curl -s -XGET http://${consul_address}/v1/catalog/nodes | jq -r '.[].Address')
CONSUL_CRITICAL=$(curl -s -XGET http://${consul_address}/v1/health/state/critical | jq -r '.[].ServiceID')
for critical in ${CONSUL_CRITICAL}
do
echo "${critical} 已删除" >> logs/`date +%Y%m%d`.log
for consul_ip in ${CONSUL_NODES}
do
curl -s -XPUT http://${consul_ip}:8500/v1/agent/service/deregister/${critical} &> /dev/null
done
done
done
第二个脚本执行过程生成的日志
# sh delete-consul-critical.sh
# tail -f logs/20190525.log
------------------
当前consul为123123123.test.consul.baiyongjie.com
------------------
当前consul为214142421.dev.consul.baiyongjie.com
XXXXXX-sfa-9c1e0c30fbe6460f23c657bf55cc294a 已删除
XXXXXX-sfa-8cf8881c30dd715e3848c3ae18b67e81 已删除
XXXXXX-sfa-8fbbfc6f97130c346b9e479edae25281 已删除
------------------
当前consul为aaa-uae.dev.consul.baiyongjie.com
XXXXXX-XXXXXX-3b6b96bb76f4148d04ff5476a63924df 已删除
XXXXXX-XXXXXX-aa160001cc4c6ab52f56243d263c0318 已删除
XXXXXX-XXXXXX-b63e85cbc3351adef8da9522900f86fc 已删除
------------------
当前consul为aaa-uae.test.consul.baiyongjie.com
base-message-b2187152d7ba124eae68e4b3a65e626f 已删除
base-message-c33328d1bdc7aedad70f3684087fe935 已删除
base-message-12b75f0214131e4c96a0156f7ff1b3ca 已删除
base-message-39f61ee45b6891d7a1169a939506952b 已删除
base-message-65d9c7db268b223e49bfb28157c0c6a0 已删除
------------------
当前consul为bbb.dev.consul.baiyongjie.com
base-message-5aa3832f876c5f66c985b2e1fe573651 已删除
base-message-722e8876b7ef20cb60b2a610b2a67bf3 已删除
base-message-a4054a4293464861f85ab7f5c0005228 已删除
------------------
当前consul为ccc.test.consul.baiyongjie.com
------------------
当前consul为fff.test.consul.baiyongjie.com
XXXXXX-XXXXXX-0ae7fa0a87c8604bf1ffc58ada99f317 已删除
XXXXXX-XXXXXX-2716291857b4379537679b019670a283 已删除
XXXXXX-XXXXXX-27a2e782c41a867cf195d1a330c7f5bc 已删除
------------------
当前consul为xxxx.dev.consul.baiyongjie.com
XXXXXX-sfa-0b4fae2b082e8ffbccb73c542823e986 已删除
XXXXXX-sfa-45c0aa99c879cca759126826a4840a40 已删除
XXXXXX-sfa-581d85fe036a3032aeb16e40d541f816 已删除
XXXXXX-sfa-72051e8232d235274a8dde28587abfe2 已删除
------------------
当前consul为aaaqq.uat.consul.baiyongjie.com
------------------
当前consul为bbbcd.dev.consul.baiyongjie.com
------------------
当前consul为fffed.test.consul.baiyongjie.com
base-message-65d9c7db268b223e49bfb28157c0c6a0 已删除
base-message-97bfd24d6579e542c3b22f479bfba17c 已删除
网友评论