Dot-Net

自動部署到 F5 負載平衡環境

  • June 25, 2013

我們目前正在使用 TeamCity 進行 CI 建構,並且我們也在嘗試設置自動化部署。

我目前正在嘗試部署的項目是位於 F5 負載均衡器下的 Windows 服務。將來,我們還希望自動部署同樣位於 F5 下的 IIS 網站。

在 TeamCity 中,我們可以執行 PowerShell 腳本來解除安裝所需伺服器上的 Windows 服務,將我們的文件推送到它,然後重新安裝該服務。

但是,我無法弄清楚如何處理負載均衡器。我們希望一次禁用 1 個節點,觀察所有連接斷開,然後部署我們的程式碼並重新啟動節點。

這似乎是一個非常普遍的問題,但我發現關於如何做到這一點的資訊令人驚訝地少。

謝謝!

已回答

感謝 Jonathon Rossi 提供 iControl Powershell cmdlet!

為了其他使用者的利益,這裡有一個範例,關閉、監控連接斷開、推送程式碼,然後通過 powershell 腳本重新打開 F5 負載均衡器

要使這些腳本正常工作,您首先必須從以下答案中提供的連結安裝 F5 iControl cmdlet

#PULL IN OUR F5 UTILITY FUNCTIONS
. .\F5Functions.ps1


#DEFINE LOGIC TO DEPLOY CODE TO A NODE THAT HAS ALREADY BEEN REMOVED FROM THE LOAD BALANCER
function Deploy(
   [F5Node]$Node
)
{
   Write-Host "Deploying To: "$Node.Name
   #TODO: Remotely shut down services, push code, start back up services
}


#DEFINE NODES
$nodes = @()
$nodes += New-Object F5Node -ArgumentList @("TestNode1", "1.1.1.1")
$nodes += New-Object F5Node -ArgumentList @("TestNode2", "1.1.1.2")

#DEPLOY
DeployToNodes -Nodes $nodes -F5Host $F5Host -F5UserName $F5UserName -F5Password $F5Password

這是可重用的 F5Functions 腳本

#Load the F5 powershell iControl snapin
Add-PSSnapin iControlSnapin;

Write-Host "Imported F5 function!!!"

Add-Type @'
   public class F5Node
   {
       public F5Node(string name, string address){
           Address = address;
           Name = name;
       }
       public string Address {get;set;}
       public string Name {get;set;}
       public string QualifiedName {get{return "/Common/" + Name;}}
   }
'@

function DeployToNodes(
   [string]$F5Host = $(throw "Missing Required Parameter"),
   [string]$F5UserName = $(throw "Missing Required Parameter"),
   [string]$F5Password = $(throw "Missing Required Parameter"),
   [F5Node[]]$Nodes = $(throw "Missing Required Parameter"),    
   [int]$MaxWaitTime = 300 #seconds... defaults to 5 minutes
){
   Authenticate -F5Host $F5Host -F5UserName $F5UserName -F5Password $F5Password

   foreach($node in $Nodes){
       DisableNode -Node $node

       WaitForConnectionsToDrop -Node $node -MaxWaitTime $MaxWaitTime

       #Assume the Script that included this script defined a Deploy Method with a Node param
       Deploy -Node $node    

       EnableNode -Node $node
   }
}

function Authenticate(
   [string]$F5Host = $(throw "Missing Required Parameter"),
   [string]$F5UserName = $(throw "Missing Required Parameter"),
   [string]$F5Password = $(throw "Missing Required Parameter")
)
{
   Write-Host "Authenticating to F5..."
   Initialize-F5.iControl -HostName $F5Host -Username $F5UserName -Password $F5Password
   Write-Host "Authentication Success!!!"
}

function ParseStatistic(
       [iControl.CommonStatistic[]]$StatsCollection = $(throw "Missing Required Parameter"),
       [string]$StatName = $(throw "Missing Required Parameter")
   )
{
   for($i=0; $i -lt $StatsCollection.Count; $i++){   
       if($StatsCollection[$i].type.ToString() -eq $StatName){
           return $StatsCollection[$i].value.low  
           break
       }                      
   }
}

function GetStats(
       [F5Node]$Node = $(throw "Missing Required Parameter")
   )
{
   $arr = @($Node.QualifiedName)
   $nodeStats = (Get-F5.iControl).LocalLBNodeAddressV2.get_statistics($arr)
   return $nodeStats.statistics.statistics

   #foreach($memberStats in $poolStats.statistics){
   #    if($memberStats.member.address.ToString() -eq $Node -and $memberStats.member.port -eq $Port){
   #        return $memberStats.statistics
   #    }  
   #}
}

function GetStatistic(
       [F5Node]$Node = $(throw "Missing Required Parameter"),
       [string]$StatName = $(throw "Missing Required Parameter")
   )
{
   $stats = GetStats -Node $Node
   $stat = ParseStatistic -StatsCollection $stats -StatName $StatName

   return $stat
}

function DisableNode(
   [F5Node]$Node = $(throw "Missing Required Parameter")
)
{    
   Disable-F5.LTMNodeAddress -Node $Node.Address
   Write-Host "Disabled Node '$Node'"
}

function EnableNode(
   [F5Node]$Node = $(throw "Missing Required Parameter")
)
{
   Enable-F5.LTMNodeAddress -Node $Node.Address
   Write-Host "Enabled Node '$Node'"
}

function WaitForConnectionsToDrop(
   [F5Node]$Node = $(throw "Missing Required Parameter"),
   [int]$MaxWaitTime = 300
)
{
   $connections = GetCurrentConnections -Node $Node

   $elapsed = [System.Diagnostics.Stopwatch]::StartNew();
   while($connections -gt 0 -and $elapsed.ElapsedMilliseconds -lt ($MaxWaitTime * 1000)){        

       Start-Sleep -Seconds 10

       $connections = GetCurrentConnections -Node $Node
   }
}

function GetCurrentConnections(
   [F5Node]$Node = $(throw "Missing Required Parameter")
)
{
   $connections = GetStatistic -Node $Node -StatName "STATISTIC_SERVER_SIDE_CURRENT_CONNECTIONS"
   $name = $Node.Name + ":" + $Node.Address
   Write-Host "$connections connections remaining on '$name'"
   return $connections
}

我沒用過,但是你看過 F5 提供的F5 iControl Web 服務 API 和F5 iControl PowerShell cmdletPowerShell cmdlet 自2007 年以來一直存在,可以從F5 DevCentral下載。

看起來您可以使用一些 cmdlet Enable-MemberDisable-Member

引用自:https://stackoverflow.com/questions/17244218