在 Service Fabric 中取消预配旧应用程序类型版本的策略

Strategy to unprovision old applicationtype versions in Service Fabric

有没有办法在集群上设置某种配置来删除服务结构应用程序类型版本?比如只保留最后 5 个版本之类的?

例如,我 CI/CD 将新版本的服务结构应用程序部署到我们的集群,它在集群中留下了一堆应用程序版本类型。有没有办法随着时间的推移自动取消配置它们或只保留一定数量的版本?

我想到了两个选项 -

  • 执行 Deploy-FabricApplication.ps1 时指定 UnregisterUnusedApplicationVersionsAfterUpgrade = $true。此参数指示是否注销升级完成后存在的任何未使用的应用程序版本。

  • 将自定义脚本添加到您的发布定义、部署脚本或您想要的任何位置,这将解析所有已部署的应用程序类型并取消配置那些您认为过时的应用程序类型。这是命令 您将需要使用 - Unregister-ServiceFabricApplicationType。这是取消注册除 运行 之外的所有应用程序类型的脚本的一些示例 -

    #resolve all app types
    $appTypes = Get-ServiceFabricApplicationType
    foreach($appType in $appTypes)
    {
       #try to find the match with any of installed applications
       $match = Get-ServiceFabricApplication -ApplicationTypeName $appType.ApplicationTypeName | Where-Object {$_.ApplicationTypeVersion -eq $appType.ApplicationTypeVersion}
       if(!$match)
       {
           Write-Host "Deleting $($appType.ApplicationTypeName) $($appType.ApplicationTypeVersion)"
           Unregister-ServiceFabricApplicationType -ApplicationTypeName $appType.ApplicationTypeName -ApplicationTypeVersion $appType.ApplicationTypeVersion -Force -Confirm
       }    
    }
    

我扩展了 Kyrl 的回答,将当前部署的版本 + 保留在历史记录中'

#resolve all app types
$appTypes = Get-ServiceFabricApplicationType
$deployedAppArray = @()
foreach($appType in $appTypes){    
    #try to find the match with any of installed applications
    $match = Get-ServiceFabricApplication -ApplicationTypeName $appType.ApplicationTypeName | Where-Object {$_.ApplicationTypeVersion -eq $appType.ApplicationTypeVersion}
    if(!$match)
    {
        $oldApp = new-object psobject -property @{
            ApplicationTypeName = $appType.ApplicationTypeName
            ApplicationTypeVersion  = $appType.ApplicationTypeVersion 
        }
        $deployedAppArray += $oldApp
    }    
}

$countToKeep = 2 # keeps this many in addition + currently deployed
$uniqueAppTypes = $deployedAppArray | Group-Object "ApplicationTypeName" | Where-Object { $_.Count -gt $countToKeep } | Select-Object -ExpandProperty Name

foreach($appType in $uniqueAppTypes){ 
    $versionsToRemove = $deployedAppArray | Where-Object {$_.ApplicationTypeName -eq $appType}
    $toRemoveCount =  $versionsToRemove.Length - $countToKeep
    $versionsToRemove = $versionsToRemove | Select-Object -First $toRemoveCount

    foreach($appToRemove in $versionsToRemove){
        Write-Host "Removing $($appToRemove.ApplicationTypeName) $($appToRemove.ApplicationTypeVersion)"
        Unregister-ServiceFabricApplicationType -ApplicationTypeName $appToRemove.ApplicationTypeName -ApplicationTypeVersion $appToRemove.ApplicationTypeVersion -Force
    }
}

您可以在集群设置的 Management 部分将 CleanupUnusedApplicationTypes 设置为 true 以启用自动清理。来自 docs:

This configuration if enabled, allows to automatically unregister unused application type versions skipping the latest three unused versions, thereby trimming the disk space occupied by image store. The automatic cleanup will be triggered at the end of successful provision for that specific app type and also runs periodically once a day for all the application types. Number of unused versions to skip is configurable using parameter "MaxUnusedAppTypeVersionsToKeep".

我认为这至少需要 Service Fabric 6.5。