windows 下通过脚本方式实现 类似 Linux keepalived IP 动态绑定效果

基本信息:

VIP: 10.50.1.170

node1: 10.50.1.171

node2: 10.50.1.172

实现原理:

  1. 脚本每2s 运行一次(注册为windows 服务, 开机同步启动)

  2. 监听VIP 是否可用, 可用则跳过;

  3. 如果不可用 则 判断本机 Nginx 服务是否可用

3.1 可用则执行绑定动作, 将VIP绑定到本机

3.2 不可用则跳过

脚本:

复制代码
while ($true) {
    # 1. 判断 http://10.50.1.170/health 返回是否是OK
    Write-Host "Checking Nginx health status via http://10.50.1.170/health..."
    try {
        $response = Invoke-WebRequest -Uri "http://10.50.1.170/health" -UseBasicParsing -Method HEAD -TimeoutSec 2 -ErrorAction Stop
        $status = if ($response.StatusCode -eq 200) { "OK" } else { "NOT OK" }
        Write-Host "Nginx health status: $status"
    }
    catch {
        $status = "ERROR"
        Write-Host "Error accessing Nginx health status: $($_.Exception.Message)"
    }

    # 2. 如果第1步返回的OK 则不做任何处理
    if ($status -eq "OK") {
        Write-Host "Nginx health status is OK, no operation needed."
    }
    else {
        # 3. 如果第一步返回的不是OK, 则执行解绑VIP的动作
        Write-Host "Nginx health status is not OK, starting to remove VIP binding..."
        $vip = "10.50.1.170"
        $interfaceName = "Ethernet0"
        $interfaceInfo = Get-NetIPAddress -InterfaceAlias $interfaceName -ErrorAction SilentlyContinue
        $vipExists = $interfaceInfo | Where-Object { $_.IPAddress -eq $vip }
        if ($vipExists) {
            try {
                Remove-NetIPAddress -IPAddress $vip -InterfaceAlias $interfaceName -ErrorAction Stop -Confirm:$false
                Write-Host "Removed VIP $vip binding from $interfaceName interface."
            }
            catch {
                Write-Host "Failed to remove VIP $vip binding. Error: $($_.Exception.Message)"
            }
        }
        else {
            Write-Host "VIP $vip is not bound on $interfaceName interface."
        }

        # 4. 然后判断本机Nginx (jg-nginx) 是否可用
        Write-Host "Checking if local Nginx (jg-nginx) is available..."
        $localNginxProcess = Get-Process -Name "jg-nginx" -ErrorAction SilentlyContinue
        if ($localNginxProcess) {
            Write-Host "Local Nginx (jg-nginx) is available."

            # 5. 如果可用则执行绑定VIP的动作
            Write-Host "Starting to bind VIP $vip to $interfaceName interface..."
            try {
                New-NetIPAddress -IPAddress $vip -PrefixLength 32 -InterfaceAlias $interfaceName -ErrorAction Stop -Confirm:$false
                Write-Host "VIP $vip bound to $interfaceName interface."
            }
            catch {
                Write-Host "Failed to bind VIP $vip. Error: $($_.Exception.Message)"
            }
        }
        else {
            Write-Host "Local Nginx (jg-nginx) is not available, skipping VIP binding."
        }
    }

    # 等待2秒后再次循环执行
    Start-Sleep -Seconds 2
}
相关推荐
lyz2468597 分钟前
vue3页面因参数未声明而报错的常犯错误
前端·javascript·vue.js
摸爬滚打的小李11 分钟前
流程图同步AI生成
前端
土星云SaturnCloud21 分钟前
边缘计算 + AI 视频存管一体机:快递末端网点分拣提效与安全管控实战方案
服务器·人工智能·ai·边缘计算
Hilaku22 分钟前
我重写了整个项目,但没人感谢我!
前端·javascript·程序员
制造业的搬运工25 分钟前
车载 PCB 打样周期与选型指南:从 IATF 16949 到 IPC-6012 的 5 个硬指标
大数据·网络·人工智能·制造·pcb工艺
bksczm29 分钟前
0基础面试2
网络·面试·职场和发展
wdfk_prog33 分钟前
Wi-Fi Direct教程 02:从 wpa_cli main() 到 P2P_FIND——用源码注释追踪 CLI 控制命令发送
运维·服务器·网络·网络协议·ubuntu·p2p·wifi-direct
DD云验证41 分钟前
DD云验证,免费网络验证系统
服务器·python·易语言·网络验证·卡密验证
zach1 小时前
Vue/React SPA 打包部署后,子路由刷新 401 未登录问题彻底解决
前端·nginx·next.js
旋生万物1 小时前
素数螺旋映射 $z_n=n^{1+i}$ 的角分布统计检验与零模型对比
大数据·前端·人工智能·算法·云原生·螺旋生成论·螺旋相位