为 Azure 上的容器应用程序设置自动关闭

问题描述 投票:0回答:1

我有一个带有 Scale 的容器应用程序,我需要它在晚上自动关闭,比如从晚上 22 点到早上 7:00。如何才能做到这一点?因为我还没有找到任何相关信息,但我知道这是可以做到的。

我发现可以使用以下命令停止和启动 Conatiner:Stop-AzContainerApp -Name -ResourceGroupName 但是,我如何从我的 Linux 终端执行这句话呢?

azure azure-devops automation azure-functions containers
1个回答
0
投票

从 Azure 门户,您可以找到用于启动/停止容器应用程序的 Azure PowerShell 命令。

enter image description here

然后我们可以使用计划触发器在Azure DevOps管道的Azure PowerShell任务中运行命令。如果您之前没有使用过 Azure DevOps 管道,可以先浏览相关文档

这是 yaml 管道供您参考:

trigger: none

schedules:
- cron: "0 22 * * *"
  displayName: 'Shutdown Schedule'
  branches:
    include:
    - main
  always: true
- cron: "0 7 * * *"
  displayName: 'Startup Schedule'
  branches:
    include:
    - main
  always: true

jobs:
- job: Shutdown
  condition: and(succeeded(), eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'Shutdown Schedule'))
  pool:
    vmImage: 'windows-latest'
  steps:
  - task: AzurePowerShell@5
    inputs:
      azureSubscription: 'your service connection name'
      ScriptType: 'InlineScript'
      Inline: 'Stop-AzContainerApp -Name yourContainerAppname -ResourceGroupName yourResourceGroupname -SubscriptionId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
      azurePowerShellVersion: 'LatestVersion'

- job: Startup
  condition: and(succeeded(), eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'Startup Schedule'))
  pool:
    vmImage: 'windows-latest'
  steps:
  - task: AzurePowerShell@5
    inputs:
      azureSubscription: 'your service connection name'
      ScriptType: 'InlineScript'
      Inline: 'Start-AzContainerApp -Name yourContainerAppname -ResourceGroupName yourResourceGroupname -SubscriptionId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
      azurePowerShellVersion: 'LatestVersion'

管道将在下午 22 点运行停止作业,并在早上 7 点启动作业。

Scheduled pipeline runs

© www.soinside.com 2019 - 2024. All rights reserved.