运维编排场景系列-----自动一键连通跳板机

本文涉及的产品
云服务器 ECS,每月免费额度280元 3个月
云服务器ECS,u1 2核4GB 1个月
简介: 跳板机是云盾提供的一个核心系统运维和安全审计管控平台。通过跳板机可以操作跳板机内连通的实例,并在跳板机内对实例进行免密连接操作。此文章构建的是一个简易的跳板机例文。 前提条件 实例状态为运行中(Running) 在控制台查看准备的秘钥对或创建秘钥对 保存秘钥对中的私钥部分 模版说明: 父.

跳板机是云盾提供的一个核心系统运维和安全审计管控平台。通过跳板机可以操作跳板机内连通的实例,并在跳板机内对实例进行免密连接操作。此文章构建的是一个简易的跳板机例文。

前提条件

  1. 实例状态为运行中Running
  2. 控制台查看准备的秘钥对或创建秘钥对
  3. 保存秘钥对中的私钥部分

模版说明:

父模版主要功能为:

  1. 获取实例
  2. 检查实例是否绑定了指定的公共秘钥否则绑定秘钥对
  3. 检查实例运行状态是否符合预期
  4. 检测选择执行公网Ip或者选择执行弹性公网Ip
  5. 将似钥存放进跳板机内并测试连接
  6. 检测选择是否执行测试连接弹性公网Ip
  7. 检测当前任务
FormatVersion: OOS-2019-06-01
Description:
  en: Connect the instance in the jumper instance.
  zh-cn: 连接跳板机内的实例。
Parameters:
  KeyPairName:
    Type: String
    Description:
      en: Key pair name.
      zh-cn: 秘钥对名称。
  SecretKeys:
    Type: String
    Description:
      en: Path and file name of the private key stored in the springboard.
      zh-cn: 跳板机内存放私钥的路径及文件名称。
  JumperInstanceId:
    Type: String
    Description:
      en: Jumper instance ID.
      zh-cn: 跳板机实例ID。
  Targets:
    Type: Json
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: 'ALIYUN::ECS::Instance'
  RateControl:
    Description:
      en: Concurrency ratio of task execution.
      zh-cn: 任务执行的并发比率。
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 100
      Concurrency: 10
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances.
      zh-cn: 获取ECS实例。
    Action: 'ACS::SelectTargets'
    Properties:
      ResourceType: 'ALIYUN::ECS::Instance'
      Filters:
        - '{{ Targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
  - Name: checkInstanceHasInstalledKeyPair
    Action: 'ACS::Template'
    Description:
      en: 'Check if the instance has the specified public Secret, if not, install it.'
      zh-cn: 检查实例是否安装了指定的公共秘要,若无则安装。
    Properties:
      TemplateName: CheckInstanceWetherInstallKeyPair
      Parameters:
        KeyPairName: '{{ KeyPairName }}'
        InstanceId: '{{ ACS::TaskLoopItem }}'
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ getInstance.instanceIds }}'
  - Name: checkInstanceReady
    Action: 'ACS::CheckFor'
    Description:
      en: Check the ECS instance is not Starting status..
      zh-cn: 检查实例的状态不是Starting。
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        InstanceIds:
          - '{{ ACS::TaskLoopItem }}'
      NotDesiredValues:
        - Starting
      PropertySelector: 'Instances.Instance[].Status'
    Outputs:
      publicIpAddress:
        Type: String
        ValueSelector: 'Instances.Instance[].PublicIpAddress.IpAddress[]'
      eipAddress:
        Type: String
        ValueSelector: 'Instances.Instance[].EipAddress.IpAddress'
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ getInstance.instanceIds }}'
      Outputs:
        publicIpAddresses:
          AggregateType: 'Fn::ListJoin'
          AggregateField: publicIpAddress
        eipAddresses:
          AggregateType: 'Fn::ListJoin'
          AggregateField: eipAddress
  - Name: chooseEipOrPublicIp
    Action: 'ACS::Choice'
    Properties:
      DefaultTask: runCommandPublicIp
      Choices:
        - When:
            'Fn::Equals':
              - - Null
              - '{{ checkInstanceReady.publicIpAddresses }}'
          NextTask: runCommandEIp
  - Name: runCommandPublicIp
    Action: 'ACS::ECS::RunCommand'
    Description:
      en: 'Try to connect the ECS instance in the jumper instance.'
      zh-cn: 测试连接跳板机内的实例。
    Properties:
      commandContent:
        'Fn::Join':
          - |+

          - - 'echo ''{{ SecretKeys }}'' > ~/jumperInstance.pem'
            - chmod 600 ~/jumperInstance.pem
            - 'ssh root@{{ ACS::TaskLoopItem }} -i ~/jumperInstance.pem'
            - ifconfig -a
      instanceId: '{{ JumperInstanceId }}'
      commandType: RunShellScript
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ checkInstanceReady.publicIpAddresses }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
  - Name: chooseEipOrEnd
    Action: 'ACS::Choice'
    Properties:
      DefaultTask: runCommandEIp
      Choices:
        - When:
            'Fn::Equals':
              - - Null
              - '{{ checkInstanceReady.eipAddresses }}'
          NextTask: listExecutions
  - Name: runCommandEIp
    Action: 'ACS::ECS::RunCommand'
    Description:
      en: 'Try to connect the ECS instance in the jumper instance.'
      zh-cn: 测试连接跳板机内的实例。
    Properties:
      commandContent:
        'Fn::Join':
          - |+

          - - 'echo ''{{ SecretKeys }}'' > ~/jumperInstance.pem'
            - chmod 600 ~/jumperInstance.pem
            - 'ssh root@{{ ACS::TaskLoopItem }} -i ~/jumperInstance.pem'
            - ifconfig -a
      instanceId: '{{ JumperInstanceId }}'
      commandType: RunShellScript
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
    Loop:
      RateControl: '{{ RateControl }}'
      Items: '{{ checkInstanceReady.eipAddresses }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
  - Name: listExecutions
    Action: 'ACS::ExecuteAPI'
    Description: 
      en: 'The execution springboard.'
      zh-cn: 执行跳板。
        Properties:
      Service: OOS
      API: ListExecutions
      Parameters:
        ExecutionId: '{{ ACS::ExecutionId }}'

子模版
需将子模版命名为:CheckInstanceWetherInstallKeyPair
子模版主要功能:

  • 检测是否绑定公共秘要
  • 若无则将公共秘要绑定到实例上
  • 重启实例
  • 检测是否绑定秘要
FormatVersion: OOS-2019-06-01
Description:
  en: Check if the instance bind the specified key pair.
  zh-cn: 检查实例是否绑定了指定的秘钥对。
Parameters:
  InstanceId:
    Type: String
    Description:
      en: The ECS instance ID in the jumper instance need to binding.
      zh-cn: 跳板机内需要绑定的ECS实例ID。
  KeyPairName:
    Type: String
    Description:
      en: The key pair name.
      zh-cn: 秘钥对的名称。
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: checkInstanceHasKeyPair
    Action: 'ACS::CheckFor'
    Description:
      zh-cn: 查询实例是否绑定了给定的秘钥对。
      en: Check if the instance bind the specified KeyPair.
    OnSuccess: checkInstanceHasKeyPairIsOnlyOne
    OnError: AttachKeyPair
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        KeyPairName: '{{ KeyPairName }}'
        InstanceIds:
          - '{{ InstanceId }}'
      NotDesiredValues:
        - 0
      PropertySelector: TotalCount
  - Name: AttachKeyPair
    Action: 'ACS::ExecuteAPI'
    Description:
      en: Bind the key pair to the instance.
      zh-cn: 将秘钥对绑定到实例上。
    Properties:
      Service: ECS
      API: AttachKeyPair
      Parameters:
        InstanceIds:
          - '{{ InstanceId }}'
        KeyPairName: '{{ KeyPairName }}'
  - Name: rebootInstance
    Action: 'ACS::ECS::RebootInstance'
    Description:
      en: Restarts the ECS instances.
      zh-cn: 重启实例。
    Properties:
      instanceId: '{{ InstanceId }}'
  - Name: checkInstanceHasKeyPairIsOnlyOne
    Action: 'ACS::CheckFor'
    Description:
      zh-cn: 查询实例是否绑定了给定的秘钥对。
      en: Check if the instance bind the specified KeyPair.
    Properties:
      Service: ECS
      API: DescribeInstances
      Parameters:
        KeyPairName: '{{ KeyPairName }}'
        InstanceIds:
          - '{{ InstanceId }}'
      DesiredValues:
        - 1
      PropertySelector: TotalCount

模版参数说明:

参数 说明 示例
KeyPairName 创建的秘钥对名称 test-oos-jump
SecretKeys 秘钥对中的私钥 usr/test.pem
JumperInstanceId 跳板机实例Id
Targets 根据实例标签(tags)或实例ID(InstanceId)选定需要的实例。 i-11111111111
RateControl 固定参数。
- 速率控制类型:并发控制
- 并发速率:1
- 最大错误次数:0
OOSAssumeRole 可选参数。
- (默认设置)当前账号的已有权限:执行您使用的账号的权限动作。请确保您拥有创建自定义镜像涉及的所有ECS API调用权限。
- 指定RAM角色,使用该角色的权限:如果指定了RAM角色名称,OOS扮演该RAM角色执行运维任务。
当前账号的已有权限

操作步骤

  1. 请访问运维编排OOS的控制1575967687928_2a2bdc63_7a88_4a10_a561_178b68c70790
  2. 单击我的模版,单击创建模板按钮,选取空白模板1575969102712_7959cd3f_5f5c_4d6c_ad97_6e4f7beeaed1
  3. 先将上述提供的模板粘贴到yaml栏,输入名称CheckInstanceWetherInstallKeyPair,点击创建模板。然后将父模版粘贴到yaml栏,输入名称ConnectJumperInstance,点击创建模版。1576811830198_c4ce539d_ca54_45dd_a3f6_c3d2cad3c0a5
  4. 找到创建的父模版,单击创建执行1576811915209_f663aa88_b490_44a4_944e_0e68dfac9c99
  5. 保持基本信息的默认设置,单击下一步:设置参数
  6. 完成参数设置填写,单击下一步:确认1576812032493_5b7ae212_9d57_4658_aebd_ad08acc0e209
  7. 查看参数设置是否无误,确认无误后单击创建执行1576812146046_d882f3d6_5967_43c9_be37_24d677cfad67
  8. 执行管理中查看创建的运维任务。1576812308765_ed7b3546_286c_4046_8c2e_a85c1ba1c758
  9. 执行结果
    登陆跳板机,此时在跳板机内输入ssh root@11.xx.xx.xx -i ~/xxxx.pem就可以实现免密连接其下任意实例。
    1576812562482_1f4c2020_308f_49b2_9fe0_97481077bc38

系列文章

主题文章

阿里云重磅发布云上自动化利器——运维编排OOS

最佳实践

玩转运维编排服务的权限:Assume Role+Pass Role
阿里云运维编排新功能:一键批量克隆ECS
批量在多台ECS内执行命令

场景系列

运维编排场景系列----更新ECS镜像
运维编排场景系列----给ECS实例自动打TAG
运维编排场景系列----从实例中拷贝文件到OSS
运维编排场景系列----给实例加到SLS机器组
运维编排场景系列----检测MFA功能状态
运维编排场景系列----每日统计多Region实例的运行状态
运维编排场景系列----如何使用jq
运维编排场景系列----分批到机器上运行命令
运维编排场景系列----更新镜像后自动更新伸缩配置镜像
运维编排场景系列----向Linux实例上传文件
运维编排场景系列----运行远端shell脚本
运维编排场景系列----在ECS实例上运行Ansible-playbook
运维编排场景系列----下载JVM堆栈到OSS
运维编排系列场景----将实例的固定公网IP转换为其它新EIP
运维编排场景系列----自动定时升级临时带宽
运维编排场景系列----批量更新云助手客户端
运维编排系列场景----批量释放实例
运维编排场景系列----ECS实例系统快照下载到本地
运维编排场景系列----批量更换系统盘
运维编排场景系列----自动创建多个区域同类型存储空间
运维编排场景系列---一键批量重置实例密码

相关文章
|
10月前
|
弹性计算 负载均衡 测试技术
运维编排系列场景--跨可用区批量克隆ECS实例
背景可用区(Availability Zone,简称 AZ)是指在同一地域内,电力和网络互相独立的物理区域。例如,华北1(青岛)地域支持2个可用区,包括青岛 可用区B和青岛 可用区C。同一可用区内实例之间的网络延时更小,其用户访问速度更快。将应用部署在多个可用区可以提高应用的可用性,降低故障风险。然而,在实际应用中,有时需要将实例从一个可用区迁移到另一个可用区,以实现故障切换、负载均衡、数据备份与
559 0
运维编排系列场景--跨可用区批量克隆ECS实例
|
10月前
|
弹性计算 运维 数据中心
运维编排系列场景--跨账号跨地域实例操作系统补丁修复
运维编排(OOS) 简介什么是OOSOperation Orchestration Service,简称OOS,是全面、免费的云上自动化运维平台,提供运维任务的管理和执行。典型使用场景包括:事件驱动运维,批量操作运维,定时运维任务,跨地域运维等,OOS为重要运维场景提供审批,通知等功能。OOS帮您实现标准化运维任务,从而实践运维即代码(Operations as Code)的先进理念。关于OOS更
393 0
|
10月前
|
弹性计算 运维 监控
运维编排系列场景--通过告警触发自动重启CPU使用率高的ECS实例
运维编排(OOS) 简介什么是OOSOperation Orchestration Service,简称OOS,是全面、免费的云上自动化运维平台,提供运维任务的管理和执行。典型使用场景包括:事件驱动运维,批量操作运维,定时运维任务,跨地域运维等,OOS为重要运维场景提供审批,通知等功能。OOS帮您实现标准化运维任务,从而实践运维即代码(Operations as Code)的先进理念。关于OOS更
184 0
|
弹性计算 运维 监控
运维编排系列场景--使用定时运维进行状态逼近
### 面向终态的运维 阿里云运维编排服务(Operation Orchestration Service,简称OOS),是阿里云提供的云上自动化运维服务,能够自动化管理和执行任务。OOS支持定时运维功能,可以周期性重复执行指定的运维任务。基于定时运维,OOS能够对云资源的状态进行管理,实现面向终态的运维。 本文以[ilogtail](https://help.aliyun.com/d
3530 0
运维编排系列场景--使用定时运维进行状态逼近
|
弹性计算 运维 网络安全
运维编排系列场景--批量管理自定义的软件包
运维编排(OOS) 简介 什么是OOS Operation Orchestration Service,简称OOS,是全面、免费的云上自动化运维平台,提供运维任务的管理和执行。典型使用场景包括:事件驱动运维,批量操作运维,定时运维任务,跨地域运维等,OOS为重要运维场景提供审批,通知等功能。OOS帮您实现标准化运维任务,从而实践运维即代码(Operations as Code)的先进理念。关于
1459 0
运维编排系列场景--批量管理自定义的软件包
|
弹性计算 运维 监控
运维编排系列场景-----在磁盘使用率超限时自动清理临时文件
### 场景介绍 有些应用在运行过程中会不断产生文件,比如下载的文件、备份文件、日志文件,我们需要在磁盘使用率过高后删除不再使用的或者过期的文件。这里我们以清理/tmp下的临时文件为例,说明如何通过运维编排(OOS)提供的公共模板来实现此类运维操作。 ### 解决方案 我们准备一台ECS实例,登陆进系统后,查看它的磁盘占用率: ![磁盘占用2.png](https:
661 0
|
弹性计算 运维
【技术战疫】运维编排灵活审批场景解决方案
运维编排(OOS) 简介 什么是OOS Operation Orchestration Service,简称OOS,是全面、免费的云上自动化运维平台,提供运维任务的管理和执行。典型使用场景包括:事件驱动运维,批量操作运维,定时运维任务,跨地域运维等,OOS为重要运维场景提供审批,通知等功能。OOS帮您实现标准化运维任务,从而实践运维即代码(Operations as Code)的先进理念。关于
904 0
|
弹性计算 运维 Linux
运维编排场景系列---Linux数据盘扩展分区和文件系统
本文讲述如何利用OOS实现扩容linux数据盘后扩展其分区和文件系统。目前支持仅有一块数据盘,扩展已有MBR分区或扩容裸设备文件系统,具体流程请参考帮助文档扩展分区和文件系统_Linux数据盘。
1888 0