ECMP(等价路由)到多台Nginx 备忘
有若干台Nginx 做Web 负载均衡,Nginx接入到华为 S5720-36C-EI
交换机(管理业务分离),交换机配置多条业务IP路由至Nginx,并将ECMP的算法更改为二元组(srcip dstip,即同一源IP落到同一台Nginx上面)
Huawei> ecmp load-balance sip dip
此时的Nginx LB对此逻辑已经可以称之为"集群",然而集群的概念需要将故障的机器自动剔除,比如这里采用简单原始的操作,将NGINX LB的业务网卡设置为Down即可。
配置摘要
systemd--虚拟IP配置
vipManager.sh
#!/bin/bash
# 文件名: vipManager.sh
# 功 能: 虚拟IP 管理脚本
# Copyright © 2017 vqiu
declare -A ips=(
[1]="182.150.0 64 127"
[2]="182.150.0 192 255"
[3]="118.112.248 0 255"
)
if [[ $UID != 0 ]]; then
echo "Please use root role to run this script."
exit 1
fi
function vip() {
if [[ $# == 1 ]]; then
for pre in ${!ips[*]}; do
ip_pre=$(echo ${ips[$pre]} | awk '{print $1}')
min_host=$(echo ${ips[$pre]} | awk '{print $2}')
max_host=$(echo ${ips[$pre]} | awk '{print $3}')
for host in $(seq ${min_host} ${max_host}); do
ip addr $1 ${ip_pre}.${host}/32 dev lo
done
done
fi
if [[ $# == 2 ]]; then
ip addr $1 ${2}/32 dev lo >/dev/null 2>&1
if [[ $? = 0 ]]; then
echo "[+] IP: ${2}/32 handling success."
else
echo "[-] IP: ${2}/32 handling failed"
fi
fi
}
case $1 in
up)
vip add
;;
down)
vip del
;;
-a)
vip add $2
;;
-d)
vip del $2
;;
*)
echo "Usage: $0 up | down | -a address | -d address."
exit 1
esac
/usr/lib/systemd/system/vip.service
[Unit]
Description=Virtual IP Control Service
ConditionFileIsExecutable=/data/script/vipManager.sh
[Service]
Type=oneshot
ExecStart=/data/script/vipManager.sh start
ExecStop=/data/script/vipManager.sh stop
TimeoutStopSec=5
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
systemd--虚拟IP配置
/etc/sysconfig/nginx.env
引入环境变量,不同节点若业务网卡名称不一致时,直接改文件内容即可
# 业务网卡名称
SIP_INT_NAME=eth2
/usr/lib/systemd/system/nginx.service
nginx 二制文件目录按对应的更改
[Unit]
Description=The nginx HTTP and reverse proxy server
Requires=vip.service
Before=vip.service
After=network.target remote-fs.target nss-lookup.target
ConditionFileNotEmpty=/etc/sysconfig/nginx.env
[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/nginx.env
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStartPost=/usr/sbin/ip link set $SIP_INT_NAME up
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStopPost=/usr/sbin/ip link set $SIP_INT_NAME down
ExecStopPost=-/usr/bin/systemctl stop vip.service
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
/usr/lib/systemd/system/nginx.service.d/nofile_limit.conf
[Service]
LimitNPROC=infinity
LimitNOFILE=204800
测试
启用nginx
服务时:
- vip.service 会随nginx服务启动
- 业务网卡会nginx服务正常up
停止nginx
服务时:
- 业务网卡会配置为
down
(重要) - vip.service 会随nginx服务停止
后论
这里仅使用nginx.service 服务去控制业务网卡的启禁,更严谨的做法应该是结合端口,模拟客户端请求是否返回内容去做判断。
同时也要注意,有些板载网卡即使你在系统层将其配置为 Down 状态,交换机侧的状态依然是 Up。而这时候,你就不能使用这种方法了,不过你可以使用OSPF或者VRRP来浪,甚至你还可以在交换机上启用联动静态路由(结合ping或者HTTP)。
https://unix.stackexchange.com/questions/452976/disable-power-from-ethernet