Operation and maintenance arrange scenes series ---- SLS was added to the group of machines Examples

Scenario Introduction

We often have this operation and maintenance scenarios, the expansion of machines that need to be configured SLS log, after already configured for SLS Logstore, we only need to machine the machine is added to the group.

solution

The traditional solution is to log on to each instance and install ecs logtail, the command is executed

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

Decomposition Task, requires the following steps:
1. Check the instance state is not Running
2. Call cloud assistant CreateCommand create the above command
3. Call InvokeCommand execution
4. Wait for the successful implementation
5. Remove Templates

OOS and converted to create a template named 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 }}"
    }
  }
}

Template above our good solution to a single machine to perform the task Install sls Agent, then for the implementation of multiple machines how to do it? OOS The Loop function can solve this problem. And OOS supports nested template execution, then we only need to build a list of incoming instance 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": {}
}

Create an execution

After you do look at the detailed information and found that the implementation has been successful and you can see the status of each task in the sub-loop

Since the sub-task is a nested execution, we can click on to see the implementation of a nested template

The last group to view the machine-to-machine status OK explained that it had executed successfully

to sum up

Above we describe how to use resources organized OOS batch installation SLS-Agent and added to the machine group, more scenes to be excavated. Currently choreography operation and maintenance (OOS) in the closed beta, welcomed the trial comments


Original link
This article Yunqi community original content may not be reproduced without permission.

Guess you like

Origin blog.csdn.net/yunqiinsight/article/details/95189366