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
}
相关推荐
Saniffer_SH3 分钟前
【每日一题】讲讲PCIe链路训练和枚举的前后关系
运维·服务器·网络·数据库·驱动开发·fpga开发·硬件工程
2401_861786185 分钟前
linux修改ip地址(有详细步骤)kali
linux·运维·服务器
真正的醒悟14 分钟前
图解网络35
开发语言·网络·php
周杰伦_Jay27 分钟前
【 Vue前端技术详细解析】目录结构与数据传递
前端·javascript·vue.js
川贝枇杷膏cbppg36 分钟前
DmServiceDMSERVER.log是干嘛的
java·服务器·数据库
Trouvaille ~39 分钟前
【Linux】进程调度与环境变量:Linux内核的智慧
linux·运维·服务器·操作系统·进程·环境变量·调度算法
bleach-1 小时前
内网渗透之横向移动&持久化远程控制篇——利用ipc、sc、schtasks、AT,远程连接的winrm,wmic的使用和定时任务的创建
网络·windows·安全·web安全·网络安全·系统安全·安全威胁分析
Nerd Nirvana1 小时前
WSL——Windows Subsystem for Linux流程一览
linux·运维·服务器·windows·嵌入式·wsl·wsl2
A24207349301 小时前
JavaScript学习
前端·javascript·学习
奋斗吧程序媛1 小时前
动态组件驱动的标签页架构(简单来说:一个页面包含许多Tabs页面,这些Tabs页面渲染逻辑)
前端·javascript·vue.js