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
}
相关推荐
网络空间小黑4 小时前
TCP/IP 知识体系
网络·网络协议·tcp/ip·计算机网络·5g·wireshark·信息与通信
Dotrust东信创智5 小时前
面向SDV的在环测试深度解析——仿真中间件SIL KIT应用篇
网络·中间件·汽车
厦门辰迈智慧科技有限公司5 小时前
城市排水管网流量监测系统解决方案
运维·服务器
leoufung6 小时前
ECPF 简介
linux·网络·kernel
水银嘻嘻6 小时前
12 web 自动化之基于关键字+数据驱动-反射自动化框架搭建
运维·前端·自动化
小嘟嚷ovo6 小时前
h5,原生html,echarts关系网实现
前端·html·echarts
十一吖i7 小时前
Vue3项目使用ElDrawer后select方法不生效
前端
只可远观7 小时前
Flutter目录结构介绍、入口、Widget、Center组件、Text组件、MaterialApp组件、Scaffold组件
前端·flutter
周胡杰7 小时前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
国际云,接待7 小时前
云服务器的运用自如
服务器·架构·云计算·腾讯云·量子计算