开发者社区> 巴梨> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

运维编排场景系列----给实例加到SLS机器组

简介: ### 场景简介 我们经常会有这样的运维场景,扩容一批机器需要配置SLS日志,对于已经配置好的SLS Logstore后,我们只需要将机器加到机器组里。 ### 解决方案 传统的解决方案是登录每台ecs实例并安装logtail,执行的命令为 ```shell wget http://logtail-release-{{ACS::RegionId}}.
+关注继续查看

场景简介

我们经常会有这样的运维场景,扩容一批机器需要配置SLS日志,对于已经配置好的SLS Logstore后,我们只需要将机器加到机器组里。

解决方案

传统的解决方案是登录每台ecs实例并安装logtail,执行的命令为

wget http://logtail-release-{{ACS::RegionId}}.oss-{{ACS::RegionId}}-internal.aliyuncs.com/linux64/logtail.sh
            -O logtail.sh; chmod 755 logtail.sh; ./logtail.sh install {{ACS::RegionId}};
echo {{ LogTailUserDefinedId }} > /etc/ilogtail/user_defined_id

分解下Task,需要以下几步:
1.检查实例是不是Running状态
2.调用云助手CreateCommand创建上述命令
3.调用InvokeCommand执行
4.等待执行成功
5.删除模板

再转换成OOS模板并创建命名为installSlsAgent

{
  "FormatVersion": "OOS-2019-06-01",
  "Description": "Install Logtail agent on the ECS Instance.",
  "Parameters": {
    "InstanceId": {
      "Type": "String",
      "Description": "the Instance Id to install ilogtail",
      "AllowedPattern": "i-[A-Za-z0-9]*",
      "MinLength": 1,
      "MaxLength": 30
    },
    "LogTailUserDefinedId": {
      "Type": "String",
      "Description": "the user defined Id write to /etc/ilogtail/user_defined_id",
      "AllowedPattern": "[A-Za-z0-9\\-_]*",
      "MinLength": 1,
      "MaxLength": 30
    },
    "OOSAssumeRole": {
      "Type": "String",
      "Description": "The RAM role to be assumed by OOS.",
      "Default": "OOSServiceRole"
    }
  },
  "RamRole": "{{OOSAssumeRole}}",
  "Tasks": [
    {
      "Name": "checkInstanceReady",
      "Action": "ACS::CheckFor",
      "Description": "describe instances with specified parameters, refer them here: https://help.aliyun.com/document_detail/63440.html",
      "Properties": {
        "API": "DescribeInstances",
        "Service": "ECS",
        "PropertySelector": "Instances.Instance[].Status",
        "DesiredValues": [
          "Running"
        ],
        "Parameters": {
          "InstanceIds": [
            "{{ InstanceId }}"
          ]
        }
      },
      "Outputs": {
        "InstanceIds": {
          "ValueSelector": "InstanceIdSets.InstanceIdSet[]",
          "Type": "List"
        }
      }
    },
    {
      "Name": "createCommand",
      "Action": "ACS::ExecuteApi",
      "Description": "create the command to install logtail agent.",
      "Properties": {
        "API": "CreateCommand",
        "Service": "ECS",
        "Parameters": {
          "CommandContent": {
            "Fn::Base64Encode": "wget http://logtail-release-{{ACS::RegionId}}.oss-{{ACS::RegionId}}-internal.aliyuncs.com/linux64/logtail.sh -O logtail.sh; chmod 755 logtail.sh; ./logtail.sh install {{ACS::RegionId}}; echo {{ LogTailUserDefinedId }} > /etc/ilogtail/user_defined_id"
          },
          "Name": "oos-{{ACS::TemplateName}}",
          "Type": "RunShellScript"
        }
      },
      "Outputs": {
        "CommandId": {
          "Type": "String",
          "ValueSelector": "CommandId"
        }
      }
    },
    {
      "Name": "invokeCommand",
      "Action": "ACS::ExecuteApi",
      "Description": "invoke the command to install ilogtail",
      "Properties": {
        "Service": "ECS",
        "API": "InvokeCommand",
        "Parameters": {
          "CommandId": "{{ createCommand.CommandId }}",
          "InstanceIds": [
            "{{ InstanceId }}"
          ]
        }
      },
      "Outputs": {
        "InvokeId": {
          "Type": "String",
          "ValueSelector": "InvokeId"
        }
      }
    },
    {
      "Name": "untilInvocationDone",
      "Action": "ACS::WaitFor",
      "Description": "until invocation ready",
      "MaxAttempts": 5,
      "Properties": {
        "Service": "ECS",
        "API": "DescribeInvocations",
        "Parameters": {
          "InvokeId": "{{  invokeCommand.InvokeId }}"
        },
        "DesiredValues": [
          "Finished"
        ],
        "PropertySelector": "Invocations.Invocation[].InvokeStatus"
      }
    },
    {
      "Name": "describeInvocationResult",
      "Action": "ACS::ExecuteApi",
      "Description": "get the command invocation result",
      "Properties": {
        "Service": "Ecs",
        "API": "DescribeInvocationResults",
        "Parameters": {
          "InvokeId": "{{ invokeCommand.InvokeId }}"
        }
      },
      "Outputs": {
        "InvocationResult": {
          "Type": "String",
          "ValueSelector": "Invocation.InvocationResults.InvocationResult[].Output"
        },
        "ExitCode": {
          "Type": "Number",
          "ValueSelector": "Invocation.InvocationResults.InvocationResult[].ExitCode"
        }
      }
    },
    {
      "Name": "deleteCommand",
      "Action": "ACS::ExecuteAPI",
      "Description": "clean up the install ilogtail command",
      "Properties": {
        "Service": "ECS",
        "Risk": "Normal",
        "API": "DeleteCommand",
        "Parameters": {
          "CommandId": "{{ createCommand.CommandId }}"
        }
      }
    }
  ],
  "Outputs": {
    "InvocationResult": {
      "Type": "String",
      "Value": {
        "Fn::Base64Decode": "{{ describeInvocationResult.InvocationResult }}"
      }
    },
    "ExitCode": {
      "Type": "String",
      "Value": "{{ describeInvocationResult.ExitCode }}"
    }
  }
}

以上模板我们很好的解决了单台机器执行Install sls Agent的任务,那么对于多台机器的执行怎么办呢?OOS的Loop功能可以很好的解决这个问题。并且OOS支持模板嵌套执行,那么我们只需要构建一个传入实例ID列表的

{
  "FormatVersion": "OOS-2019-06-01",
  "Parameters": {
    "InstanceIds":{
          "Type": "List",
           "Description": "the instance id list"
    },
    "LogTailUserDefinedId": {
      "Type": "String",
      "Description": "log tail user defined id",
      "MinLength": 1,
      "MaxLength": 30
    }
  },
  "Tasks": [
    {
      "Properties": {
        "TemplateName": "installSlsAgent",
        "Parameters": {
          "InstanceId": "{{ ACS::TaskLoopItem }}",
          "LogTailUserDefinedId": "{{ LogTailUserDefinedId }}"
        }
      },
      "Name": "installSLSAgent",
      "Action": "ACS::Template",
      "Outputs": {
        "ExitCode": {
          "ValueSelector": "ExitCode",
          "Type": "Number"
        }
      },
      "Loop": {
        "Items": "{{ InstanceIds }}",
        "MaxErrors": 100,
        "Concurrency": 10,
        "Outputs": {}
      }
    }
  ],
  "Outputs": {}
}

创建一个执行
1

执行后看详细信息,发现执行已经成功并且可以看到每个loop子task的状态
2

由于子task是一个嵌套执行,我们点击可以看到嵌套的模板执行情况
3

最后到机器组查看机器状态OK说明已经执行成功
4

总结

以上我们介绍了如何使用资源编排OOS批量安装SLS-Agent并加入到机器组中,更多场景待挖掘。目前运维编排(OOS)处于内测中,欢迎试用提意见

欢迎使用OOS

OOS管理控制台的链接: https://home.console.aliyun.com/redirect.htm?productId=ecs&path=automation/region/
OOS帮助文档的链接
OOS客户支持钉钉群:23330931

系列文章

主题文章

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

最佳实践

玩转运维编排服务的权限:Assume Role+Pass Role

场景系列

运维编排场景系列-----给ECS实例自动打TAG
运维编排场景系列----从实例中拷贝文件到OSS
运维编排场景系列----给实例加到SLS机器组

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
编写Java程序,使用Set实现不重复添加用户
编写Java程序,使用Set实现不重复添加用户
71 0
Servlet编程实例1
编程目的:使用JSP+servlet,来实现一个登陆页面,登陆成功则提示成功,登陆失败则提示失败。编程要求:登陆页面由login.jsp负责显示,登陆成功由success.jsp负责显示,登陆失败由error.jsp负责显示。
632 0
Struts2的拦截器----Dog实例
拦截器是一个类,这个类包含方法,用来解决DRY规则,即代码复用的问题。如果不调用拦截器,代码中需要显示通过代码调用目标方法,定义了拦截器,系统就会自动执行。大部分时候,拦截器方法都是通过代理的方式调用的。
734 0
Shell多线程编程的实例
#!/bin/bash # a_sub(){     sleep 5 } Multi_Thread(){     THREAD=5             # 此处定义线程数     TMP_FIFO="/tmp/$.
608 0
值得参考的 10 个 LESS CSS 实例
LESS, Sass 和其他 CSS 预处理器是一种超棒的方法用来扩展 CSS 功能,使之更适合程序员。你可以使用变量、函数、混合、继承等多种编程常用方法来编写 CSS,以更少的代码完成更多的样式。
887 0
+关注
巴梨
阿里云子账号和RAM系统开发
8
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载