通过编程(SH 脚本)检查 HA 代理将流量路由到哪个服务器

通过编程(SH 脚本)检查 HA 代理将流量路由到哪个服务器

我有 2 台 HA 代理服务器,并配置了 Keepalived,还有 2 台后端服务器,流量由 HA 代理路由到这些服务器,我正在编写一个 shell 脚本,该脚本将根据当前使用 osync 设置为活动的服务器在后端服务器之间同步目录(我不想要有关 HA 代理服务器的活动状态,而是后端服务器)(HA 配置为主动-被动,当前服务器发生故障时切换到可用服务器)。问题是我找不到任何资源或方法可以通过编程告诉我哪个服务器当前正在主动接受流量。

答案1

休息一段时间后我最近又恢复了这项工作并成功完成了。

Haproxy文件配置

为了实现这一点,我们首先需要在 haproxy 配置文件的全局部分中指定这些配置,以启用 haproxy 中的统计套接字级别日志。请参阅本指南,HAProxy 统计套接字,了解有关统计插座的更多信息。

global

 log /dev/log local0

 log /dev/log local1 notice

 stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin

 stats timeout 2m

确保没有策略阻碍 haproxy 从套接字访问日志,例如 selinux。请注意,haproxy.sock 文件在某些​​情况下可能位于不同的路径,因此您可能需要进行调查。

Shell 脚本

现在,进入激动人心的部分。启用统计套接字后,您可以查询 haproxy 以显示其正在执行的操作的当前统计信息,包括所有服务器和流量统计信息。

您可以使用以下命令查看它:

echo "show stat" | socat /var/run/haproxy.sock stdio

或者

echo "show info" | socat /var/run/haproxy.sock stdio

确保根据您的目录调整 haproxy.sock 的路径。这两个命令产生不同的输出,但就我们的目的而言,我们将重点关注“show stat”命令。

‘显示统计’的输出:

pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,agent_status,agent_code,agent_duration,check_desc,agent_desc,check_rise,check_fall,check_health,agent_rise,agent_fall,agent_health,addr,cookie,mode,algo,conn_rate,conn_rate_max,conn_tot,intercepted,dcon,dses,

stats,FRONTEND,,,2,2,2000,85,1262591,48945969,0,0,82,,,,,OPEN,,,,,,,,,1,2,0,,,,0,0,0,2,,,,0,2079,0,82,0,0,,0,3,2161,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,http,,0,2,85,2079,0,0,
stats,BACKEND,0,0,0,0,200,0,1262591,48945969,0,0,,0,0,0,0,UP,0,0,0,,0,5391,,,1,2,0,,0,,1,0,,0,,,,0,0,0,0,0,0,,,,0,0,0,0,0,0,0,1,,,0,0,0,2676,,,,,,,,,,,,,,http,roundrobin,,,,,,,
8080,FRONTEND,,,0,6,2000,14,426300,927680,0,0,0,,,,,OPEN,,,,,,,,,1,3,0,,,,0,0,0,3,,,,,,,,,,,0,0,0,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,tcp,,0,3,14,,0,0,
8082,FRONTEND,,,0,0,2000,0,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,4,0,,,,0,0,0,0,,,,,,,,,,,0,0,0,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,tcp,,0,0,0,,0,0,
8080,primary_8080,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,1,0,7,3,2667,2578,,1,5,1,,0,,2,0,,0,L4OK,,0,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Layer4 check passed,,2,3,4,,,,192.168.1.206:8080,,tcp,,,,,,,,
8080,backup_8080,0,0,0,6,,14,426300,927680,,0,,0,0,0,0,UP,1,0,1,0,0,5391,0,,1,5,2,,14,,2,0,,3,L4OK,,0,,,,,,,,,,,0,0,,,,,3770,,,0,0,0,6336,,,,Layer4 check passed,,2,3,4,,,,192.168.1.211:8080,,tcp,,,,,,,,
8080,BACKEND,0,0,0,6,200,14,426300,927680,0,0,,0,0,0,0,UP,1,1,1,,0,5391,0,,1,5,0,,14,,1,0,,3,,,,,,,,,,,,,,0,0,0,0,0,0,3770,,,0,0,0,6336,,,,,,,,,,,,,,tcp,roundrobin,,,,,,,
8082,primary_8082,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,1,0,2,0,5391,0,,1,6,1,,0,,2,0,,0,L4OK,,0,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Layer4 check passed,,2,3,4,,,,192.168.1.206:8082,,tcp,,,,,,,,
8082,backup_8082,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,0,1,0,0,5391,0,,1,6,2,,0,,2,0,,0,L4OK,,0,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Layer4 check passed,,2,3,4,,,,192.168.1.211:8082,,tcp,,,,,,,,
8082,BACKEND,0,0,0,0,200,0,0,0,0,0,,0,0,0,0,UP,1,1,1,,0,5391,0,,1,6,0,,0,,1,0,,0,,,,,,,,,,,,,,0,0,0,0,0,0,-1,,,0,0,0,0,,,,,,,,,,,,,,tcp,roundrobin,,,,,,,

在此输出中,您可以看到已设置的服务器的名称。对于每个特定的服务器,它都有自己的一组统计信息。在特定服务器的信息末尾,您会看到“up”或“down”字样。这是我们需要的信息。

获取所需详细信息的脚本

RESPONSE=$(echo "show stat" | socat unix-connect:$SOCKET stdio)

PRIMARY_8080_STATUS=$(echo "$RESPONSE" | awk -F',' '/primary_8080/ {print $18}')
PRIMARY_8082_STATUS=$(echo "$RESPONSE" | awk -F',' '/primary_8082/ {print $18}')

if [ "$PRIMARY_8080_STATUS" == "UP" ] && [ "$PRIMARY_8082_STATUS" == "UP" ]; then
    echo "All Traffic is currently routed to Primary Orchestrator" 
fi

结论

这就是我们通过编程检索有关哪个服务器处于运行状态或关闭状态的信息的方式。这里有无数种可能性,一切都取决于您想玩多少。

相关内容