清理consul集群中失效的服务节点

 下述代码中,以清理k8s集群内起的consul集群为例,如果要清理已知集群,请把 CONSUL_HOSTS 改成自己的集群地址

#!/bin/bash
APPNAME="$1"
clear_consul(){
    CONSUL_HOSTS="consul.${NAMESPACE}.svc.cluster.local:8500"
    # 获取consul的节点IP已经状态为critical的服务
    CONSUL_NODES=$(curl -s -XGET http://${CONSUL_HOSTS}/v1/catalog/nodes | jq -r '.[].Address')
    # 获取consul所有的节点
    CONSUL_CRITICAL=$(curl -s -XGET http://${CONSUL_HOSTS}/v1/health/state/critical | jq -r '.[].ServiceID' | grep ${APPNAME})
    #CONSUL_CRITICAL=$(curl -s -XGET http://${CONSUL_HOSTS}/v1/health/state/critical | jq -r '.[].ServiceID')
    echo $CONSUL_CRITICAL
    for critical_id in ${CONSUL_CRITICAL}; do
        for node_ip in ${CONSUL_NODES}; do
            echo "将 consul 节点 ${node_ip} 中的 ${critical_id}"
            curl -s -XPUT http://${node_ip}:8500/v1/agent/service/deregister/${critical_id}
        done
    done
}

if [ -z $APPNAME ];then
    echo "$0 [service_name]"
else
    clear_consul
fi

 

你可能感兴趣的:(杂货店,consul)