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
}
相关推荐
oMcLin1 分钟前
如何在 SUSE Linux Enterprise Server 15 上部署并优化 K3s 集群,提升轻量级容器化应用的资源利用率?
linux·运维·服务器
wordbaby25 分钟前
TanStack Router 实战:如何优雅地实现后台管理系统的“多页签” (TabList) 功能
前端·react.js
凌览37 分钟前
2026年1月编程语言排行榜|C#拿下年度语言,Python稳居第一
前端·后端·程序员
Ghost Face...37 分钟前
深入解析YT6801驱动模块架构
linux·运维·服务器
线束线缆组件品替网39 分钟前
Amphenol LTW 防水线缆 IP67/IP68 结构解析
运维·网络·人工智能·汽车·硬件工程·材料工程
user861581857815441 分钟前
Element UI 表格 show-overflow-tooltip 长文本导致闪烁的根本原因与解法
前端
LaoZhangGong12342 分钟前
学习TCP/IP的第1步:ARP数据包
网络·stm32·学习·tcp/ip·以太网·arp·uip
不会写前端的小丁1 小时前
前端首屏渲染性能优化小技巧
前端
晴虹1 小时前
lecen:一个更好的开源可视化系统搭建项目--组件和功能按钮的权限控制--全低代码|所见即所得|利用可视化设计器构建你的应用系统-做一
前端·后端·低代码
爱分享的鱼鱼1 小时前
Pinia 深度解析:现代Vue应用状态管理最佳实践
前端·后端