咱們常常會有這樣的運維場景,擴容一批機器須要配置SLS日誌,對於已經配置好的SLS Logstore後,咱們只須要將機器加到機器組裏。html
傳統的解決方案是登陸每臺ecs實例並安裝logtail,執行的命令爲linux
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模板並建立命名爲installSlsAgentoop
{ "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列表的spa
{ "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": {} }
建立一個執行日誌
執行後看詳細信息,發現執行已經成功而且能夠看到每一個loop子task的狀態code
因爲子task是一個嵌套執行,咱們點擊能夠看到嵌套的模板執行狀況orm
最後到機器組查看機器狀態OK說明已經執行成功htm
以上咱們介紹瞭如何使用資源編排OOS批量安裝SLS-Agent並加入到機器組中,更多場景待挖掘。目前運維編排(OOS)處於內測中,歡迎試用提意見blog
原文連接 本文爲雲棲社區原創內容,未經容許不得轉載。