雲計算

基於OOS實現定時清理磁盤

OOS簡介

Operation Orchestration Service,簡稱OOS,是全面、免費的雲上自動化運維平臺,提供運維任務的管理和執行。典型使用場景包括:事件驅動運維,批量操作運維,定時運維任務,跨地域運維等,OOS為重要運維場景提供審批,通知等功能。OOS幫您實現標準化運維任務,從而實踐運維即代碼(Operations as Code)的先進理念。關於OOS更詳細的介紹請參見什麼是運維編排服務

操作步驟

1.登陸OOS控制檯,找到我的模板,點擊創建模板
image.png
2.選擇空白模版。
image.png
3.複製下文提供的模板到YAML欄中,輸入模板名稱(CleanUpDisks,點擊創建模板
image.png
模板內容:

FormatVersion: OOS-2019-06-01
Description:
en: Clean up disks.
zh-cn: 定時清理磁盤。
name-en: CleanUpDisks
name-zh-cn: 清理磁盤
Parameters:
regionId:
Type: String
Description:
  en: The id of region.
  zh-cn: 地域ID。
AssociationProperty: RegionId
Default: '{{ ACS::RegionId }}'

tagValues:

Description:
  en: The value list of tag.
  zh-cn: 標籤值列表。
Type: List

cleanUpInfos:

Description:
  en: 'The infos of disk to clean up.Format: 3d|/root/log/|*.log,3h|/root/log/|*.access,30m|C:\Users\Administrator\log\|*.log.'
  zh-cn: 磁盤清理信息。格式:3d|/root/log/|*.log,3h|/root/log/|*.access,30m|C:\Users\Administrator\log\|*.log
Type: String

threshold:

Description:
  en: The disk usage threshold.
  zh-cn: 磁盤使用率閾值。
Type: Number
MaxValue: 100

rateControl:

Description:
  en: Concurrency ratio of task execution.
  zh-cn: 任務執行的併發比率。
Type: Json
AssociationProperty: RateControl
Default:
  Mode: Concurrency
  MaxErrors: 0
  Concurrency: 10

OOSAssumeRole:

Description:
  en: The RAM role to be assumed by OOS.
  zh-cn: OOS扮演的RAM角色。
Type: String
Default: OOSServiceRole

RamRole: '{{ OOSAssumeRole }}'
Tasks:

Name: getInstance

Description:
  en: Views the ECS instances.
  zh-cn: 獲取ECS實例。
Action: 'ACS::ExecuteApi'
Properties:
  Service: ECS
  API: DescribeInstances
  Parameters:
    RegionId: '{{ regionId }}'
    Tags:

Key: appgroup
Value: "{{ ACS::TaskLoopItem }}"

Loop:
  RateControl: '{{ rateControl }}'
  Items: '{{ tagValues }}'
  Outputs:
    instanceIds:

AggregateType: Fn::ListJoin

AggregateField: instanceId

Outputs:
  instanceId:
    Type: List
    ValueSelector: 'Instances.Instance[].InstanceId'

Name: cleanUpDisk

Action: 'ACS::ECS::CleanUpDisk'
Description:
  en: Execute cloud assistant command to clean up disk.
  zh-cn: 執行雲助手命令清理磁盤。
Properties:
  regionId: '{{ regionId }}'
  instanceId: '{{ ACS::TaskLoopItem }}'
  threshold: '{{ threshold }}'
  cleanUpInfos: '{{ cleanUpInfos }}'
Loop:
  RateControl: '{{ rateControl }}'
  Items:
    'Fn::Jq':

First'[.[][]]''{{ getInstance.instanceIds }}'

執行模板

1.找到CleanUpDisks,點擊創建執行,點擊下一步:設置參數

image.png

2.根據提示設置參數,點擊下一步:確定

image.png
參數解釋:


  • regionId: 地域。

  • tagValues: 標籤值,List類型,每行表示不同的值。TagKey已固定為:appgroup。

  • cleanUpInfos: 磁盤清理信息,3d|/root/log/|.log表示:刪除/root/log/匹配.log的修改於3天前的所有文件,可輸入過個清理規則,各個規則間用","隔開。

  • threshold: 磁盤使用率閥值,磁盤使用率超過閥值後會清理磁盤。

  • rateControl: 任務執行併發速率,使用默認值即可。

3.確認參數無誤後,點擊創建。執行完成後,可以在執行詳情查看執行結果。

image.png

Leave a Reply

Your email address will not be published. Required fields are marked *