您现在的位置是:首页 > 文章详情

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

日期:2019-12-22点击:473

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

前提条件

  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实例系统快照下载到本地
运维编排场景系列----批量更换系统盘
运维编排场景系列----自动创建多个区域同类型存储空间
运维编排场景系列---一键批量重置实例密码

原文链接:https://yq.aliyun.com/articles/740250
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章