文档

使用系统运维管理 OOS(CloudOps Orchestration Service)创建和更新自定义镜像

更新时间:

背景

使用自定义镜像的客户,出于安全考虑,应该经常更新镜像,比如,需要定期安装最新的操作系统补丁,升级中间件软件,或者重新安装最新的第三方软件到镜像里。更新一个已有的镜像通常分为四步:先根据已有镜像创建一台ECS,然后登录这台ECS执行更新软件等操作,接着用更新后的ECS生成新镜像,最后释放掉ECS。如果采用人工操作的方式,费时费力,而且容易出错。

解决方案

系统运维管理 OOS(CloudOps Orchestration Service),专门为更新镜像的场景提供了更新镜像的公共模版,实现了一键自动化的镜像更新服务。您只需选择一个源镜像(待更新的镜像),补充更新镜像所需的Shell命令(如果是Linux实例)等必要参数,然后点击创建执行就可以一键生成新镜像。使用OOS更新镜像,您无需安装任何额外工具,无需关心ECS的创建,登录,修改,释放等步骤,无需设置环境变量和登录密钥。您还可以自定义OOS模板,实现定时或者批量的自动化更新镜像。

操作步骤

  1. 登录系统运维管理 OOS(CloudOps Orchestration Service)管理控制台。

  2. 单击公共模版

  3. 选择更新ECS镜像,对其单击创建执行

  4. 单击下一步,设置参数

  5. 输入以下的参数。

    • sourceImageId:您待更新的源镜像ID。

    • instanceType:更新镜像过程中,您允许创建临时ECS实例的规格。

    • securityGroupId:更新镜像过程中,您允许创建临时ECS实例使用的安全组ID。

    • vSwitchId:更新镜像过程中,您允许创建临时ECS实例使用的交换机ID。

    • commandContent:更新镜像过程中,您要对临时ECS实例执行的云助手命令,以此达到对镜像的更新,云助手命令详情请参见云助手概述。

    • commandType:请选择您将使用的云助手命令类型,云助手支持以下类型的命令:Linux实例适用的Shell脚本(RunShellScript)、Windows实例适用的Bat脚本(RunBatScript)、Windows实例适用的PowerShell脚本(RunPowerShellScript)。

    • targetImageName:您打算为新镜像设置的名称。

    • OOSAssumeRole:可选参数。OOS默认使用当前登录用户的权限执行动作;如果指定了RAM角色名称,OOS扮演该RAM角色执行动作。

  6. 单击下一步,确认,单击确认风险并执行

  7. 执行管理中可查看刚刚创建的执行,若创建执行成功,且执行状态处于运行中,则表示更新镜像正在进行中。

  8. 当执行状态转换为成功时,则表示镜像更新成功,可在执行详情中查看新镜像ID。

  9. 如需更多了解镜像更新过程,当创建执行成功后,您可单击该执行的详情,查看执行日志,实时了解执行的进度和状态。

附录1:公共模版和背后逻辑

ACS-ECS-UpdateImage公共模板内容如下:

FormatVersion: OOS-2019-06-01
Description: Updates an existing ECS image via ECS Cloud Assistant then creates a
  ECS image.
Parameters:
  sourceImageId:
    Description: The image ID for the ECS instances, centos_6_10_64_20G_alibase_20190326.vhd,
      for example.
    Type: String
    AllowedPattern: '[A-Za-z0-9_\-\.]*'
    MinLength: 1
    MaxLength: 40
  instanceType:
    Description: The instance type for the ECS instances, ecs.g5.large, for example.
    Type: String
    AllowedPattern: ecs\.[A-Za-z0-9\.\-]*
    MinLength: 1
    MaxLength: 30
  securityGroupId:
    Description: The security group ID for the ECS instances, sg-xxxxxxxxxxxxxxxxxxxx,
      for example.
    Type: String
    AllowedPattern: sg-[A-Za-z0-9]*
    MinLength: 1
    MaxLength: 30
  vSwitchId:
    Description: The virtual switch ID for the ECS instances, vsw-xxxxxxxxxxxxxxxxxxxx,
      for example.
    Type: String
    AllowedPattern: vsw-[A-Za-z0-9]*
    MinLength: 1
    MaxLength: 30
  commandContent:
    Description: The content of command.
    Type: String
  commandType:
    Description: The type of command to run in ECS instance.
    Type: String
    AllowedValues:
    - RunBatScript
    - RunPowerShellScript
    - RunShellScript
    MinLength: 1
    MaxLength: 30
  targetImageName:
    Description: The name of image.
    Type: String
    AllowedPattern: '[A-Za-z0-9\-_]*'
    MinLength: 1
    MaxLength: 30
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: checkNewImageName
  Action: ACS::CheckFor
  Description: Check image name is available.
  Properties:
    Service: ECS
    API: DescribeImages
    Parameters:
      ImageName: '{{ targetImageName }}'
    DesiredValues:
    - 0
    PropertySelector: TotalCount
- Name: runInstances
  Action: ACS::ECS::RunInstances
  Description: Create a ECS instance for the cloud assistant.
  Properties:
    imageId: '{{ sourceImageId }}'
    instanceType: '{{ instanceType }}'
    securityGroupId: '{{ securityGroupId }}'
    vSwitchId: '{{ vSwitchId }}'
  Outputs:
    instanceId:
      ValueSelector: instanceIds[0]
      Type: String
- Name: installCloudAssistant
  Action: ACS::ECS::InstallCloudAssistant
  Description: Install cloud assostant for ECS instance.
  OnError: deleteInstance
  Properties:
    instanceId: '{{ runInstances.instanceId }}'
- Name: runCommand
  Action: ACS::ECS::RunCommand
  Description: Run command on ECS instance.
  OnError: deleteInstance
  Properties:
    commandContent: '{{ commandContent }}'
    commandType: '{{ commandType }}'
    instanceId: '{{ runInstances.instanceId }}'
- Name: stopInstance
  Action: ACS::ECS::StopInstance
  Description: Stops the ECS instance by the instance ID.
  Properties:
    instanceId: '{{ runInstances.instanceId }}'
- Name: createImage
  Action: ACS::ECS::CreateImage
  Description: Create image with the specified image name and instance ID.
  OnError: deleteInstance
  Properties:
    imageName: '{{ targetImageName }}'
    instanceId: '{{ runInstances.instanceId }}'
  Outputs:
    imageId:
      ValueSelector: imageId
      Type: String
- Name: deleteInstance
  Action: ACS::ExecuteAPI
  Description: Deletes the ECS instance by the instance ID.
  Properties:
    Service: ECS
    API: DeleteInstance
    Risk: Normal
    Parameters:
      InstanceId: '{{ runInstances.instanceId }}'
      Force: true
Outputs:
  imageId:
    Type: String
    Value: '{{ createImage.imageId }}'        

该模板顺序执行以下任务:

  1. 检查您打算为新镜像设置的名称是否可用。

  2. 创建并运行一台临时ECS实例。该实例会根据您输入的参数进行创建,即实例将使用的镜像是您待更新的源镜像,实例属性将是您参数中允许的实例属性。

  3. 如果临时ECS实例上没有安装云助手客户端,则进行安装。

  4. 在临时ECS实例上通过云助手执行用来更新的命令,并等待执行成功更新命令成功。

  5. 当临时ECS实例执行更新命令成功后,停止临时ECS实例。

  6. 当临时ECS实例停止后,对该实例创建镜像,并等待镜像创建成功。

  7. 最后,删除临时ECS实例。

附录2:更新镜像的其他方式及对比

以下列出并对比了阿里云当前支持的镜像更新的各种方式,以供参考。

构建方式

工具与依赖

优点

缺点

使用系统运维管理 OOS(CloudOps Orchestration Service)创建和更新自定义镜像

开通即可,无其他依赖

  • 官方推荐,安全可靠

  • 在线使用,无需安装

  • 官方模板,无需编码

  • 无需提供登录密钥

  • 可自定义模版

  • 可视化执行过程

  • 可批量和定时操作

  • 使用快照创建自定义镜像

  • 使用实例创建自定义镜像

无其他依赖

ECS控制台操作,简单易用

  • 实例和快照需要手动创建

  • 操作步骤繁琐,容易出错,效率低

使用Packer创建自定义镜像

需要安装Packer

开源,多云的支持

  • 需要安装和维护

  • 需要自己编写脚本

  • 本页导读 (0)
文档反馈