다음을 통해 공유


Service Fabric 애플리케이션 업그레이드

이 샘플 스크립트는 실행 중인 Service Fabric 애플리케이션 인스턴스를 버전 1.3.0으로 업그레이드합니다. 이 스크립트는 새 애플리케이션 패키지를 클러스터 이미지 저장소에 복사하고, 애플리케이션 유형을 등록하고, 불필요한 애플리케이션 패키지를 제거합니다. 또한 모니터링되는 업그레이드를 시작하고 업그레이드가 완료되거나 롤백될 때까지 업그레이드 상태를 계속 확인합니다. 필요에 따라 매개 변수를 사용자 지정합니다.

필요한 경우 Service Fabric SDK를 사용하여 Service Fabric PowerShell 모듈을 설치합니다.

샘플 스크립트

## Variables
$ApplicationPackagePath = "C:\Users\sfuser\documents\visual studio 2017\Projects\Voting\Voting\pkg\Debug"
$ApplicationName = "fabric:/Voting"
$ApplicationTypeName = "VotingType"
$ApplicationTypeVersion = "1.3.0"
$imageStoreConnectionString = "fabric:ImageStore"
$CopyPackageTimeoutSec = 600
$CompressPackage = $false


## Check existence of the application
$oldApplication = Get-ServiceFabricApplication -ApplicationName $ApplicationName
        
if (!$oldApplication)
{
    $errMsg = "Application '$ApplicationName' doesn't exist in cluster."
    throw $errMsg
}
else
{
    ## Check upgrade status
    $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
    if ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
    {
        $errMsg = "An upgrade for the application '$ApplicationTypeName' is already in progress."
        throw $errMsg
    }

    $reg = Get-ServiceFabricApplicationType -ApplicationTypeName $ApplicationTypeName | Where-Object  { $_.ApplicationTypeVersion -eq $ApplicationTypeVersion }
    if ($reg)
    {
        Write-Host 'Application Type '$ApplicationTypeName' and Version '$ApplicationTypeVersion' was already registered with Cluster, unregistering it...'
        $reg | Unregister-ServiceFabricApplicationType -Force
    }

    ## Copy application package to image store
    $applicationPackagePathInImageStore = $ApplicationTypeName
    Write-Host "Copying application package to image store..."
    Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $ApplicationPackagePath -ImageStoreConnectionString $imageStoreConnectionString -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore -TimeOutSec $CopyPackageTimeoutSec -CompressPackage:$CompressPackage 
    if(!$?)
    {
        throw "Copying of application package to image store failed. Cannot continue with registering the application."
    }
    
    ## Register application type
    Write-Host "Registering application type..."
    Register-ServiceFabricApplicationType -ApplicationPathInImageStore $applicationPackagePathInImageStore
    if(!$?)
    {
        throw "Registration of application type failed."
    }

    # Remove the application package to free system resources.
    Remove-ServiceFabricApplicationPackage -ImageStoreConnectionString $imageStoreConnectionString -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore
    if(!$?)
    {
        Write-Host "Removing the application package failed."
    }
        
    ## Start monitored application upgrade
    try
    {
        Write-Host "Start upgrading application..." 
        Start-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName -ApplicationTypeVersion $ApplicationTypeVersion -HealthCheckStableDurationSec 60 -UpgradeDomainTimeoutSec 1200 -UpgradeTimeout 3000 -FailureAction Rollback -Monitored
    }
    catch
    {
        Write-Host ("Error starting upgrade. " + $_)

        Write-Host "Unregister application type '$ApplicationTypeName' and version '$ApplicationTypeVersion' ..."
        Unregister-ServiceFabricApplicationType -ApplicationTypeName $ApplicationTypeName -ApplicationTypeVersion $ApplicationTypeVersion -Force
        throw
    }

    do
    {
        Write-Host "Waiting for upgrade..."
        Start-Sleep -Seconds 3
        $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
    } while ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
    
    if($upgradeStatus.UpgradeState -eq "RollingForwardCompleted")
    {
        Write-Host "Upgrade completed successfully."
    }
    elseif($upgradeStatus.UpgradeState -eq "RollingBackCompleted")
    {
        Write-Error "Upgrade was Rolled back."
    }
    elseif($upgradeStatus.UpgradeState -eq "Failed")
    {
        Write-Error "Upgrade Failed."
    }
}

스크립트 설명

이 스크립트는 다음 명령을 사용합니다. 테이블에 있는 각 명령은 명령에 해당하는 문서에 연결됩니다.

명령 주의
Get-ServiceFabricApplication Service Fabric 클러스터의 모든 애플리케이션 또는 특정 애플리케이션을 가져옵니다.
Get-ServiceFabricApplicationUpgrade Service Fabric 애플리케이션 업그레이드의 상태를 가져옵니다.
Get-ServiceFabricApplicationType Service Fabric 클러스터에 등록된 Service Fabric 애플리케이션 유형을 가져옵니다.
Unregister-ServiceFabricApplicationType Service Fabric 애플리케이션 유형을 등록 취소합니다.
Copy-ServiceFabricApplicationPackage Service Fabric 애플리케이션 패키지를 이미지 저장소에 복사합니다.
Register-ServiceFabricApplicationType Service Fabric 애플리케이션 유형을 등록합니다.
Start-ServiceFabricApplicationUpgrade Service Fabric 애플리케이션을 지정된 애플리케이션 유형 버전으로 업그레이드합니다.
Remove-ServiceFabricApplicationPackage 이미지 저장소에서 Service Fabric 애플리케이션 패키지를 제거합니다.

다음 단계

Service Fabric PowerShell 모듈에 대한 자세한 내용은 Azure PowerShell 설명서를 참조하세요.

Azure Service Fabric에 대한 추가 PowerShell 샘플은 Azure PowerShell 샘플에서 확인할 수 있습니다.