Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Latest commit

 

History

History
279 lines (228 loc) · 9.58 KB

ec2-terminate-by-id.md

File metadata and controls

279 lines (228 loc) · 9.58 KB
id title sidebar_label
ec2-terminate-by-id
EC2 Terminate By ID Experiment Details
EC2 Terminate By ID

Experiment Metadata

Type Description Tested K8s Platform
Kube AWS Termination of an EC2 instance by id for a certain chaos duration EKS

WARNING

If the target EC2 instance is a part of a self-managed nodegroup:
Make sure to drain the target node if any application is running on it and also ensure to cordon the target node before running the experiment so that the experiment pods do not schedule on it. 

Prerequisites

  • Ensure that Kubernetes Version > 1.16
  • Ensure that the Litmus Chaos Operator is running by executing kubectl get pods in operator namespace (typically, litmus). If not, install from here
  • Ensure that the ec2-terminate-by-id experiment resource is available in the cluster by executing kubectl get chaosexperiments in the desired namespace If not, install from here
  • Ensure that you have sufficient AWS access to stop and start an ec2 instance.
  • Ensure to create a Kubernetes secret having the AWS access configuration(key) in the CHAOS_NAMESPACE. A sample secret file looks like:
apiVersion: v1
kind: Secret
metadata:
  name: cloud-secret
type: Opaque
stringData:
  cloud_config.yml: |-
    # Add the cloud AWS credentials respectively
    [default]
    aws_access_key_id = XXXXXXXXXXXXXXXXXXX
    aws_secret_access_key = XXXXXXXXXXXXXXX
  • If you change the secret key name (from cloud_config.yml) please also update the AWS_SHARED_CREDENTIALS_FILE ENV value on experiment.yamlwith the same name.

Entry-Criteria

  • EC2 instance is healthy before chaos injection.

Exit-Criteria

  • EC2 instance is healthy post chaos injection.

Details

  • Causes termination of an EC2 instance by instance ID or list of instance IDs before bringing it back to running state after the specified chaos duration.
  • It helps to check the performance of the application/process running on the ec2 instance.
  • When the MANAGED_NODEGROUP is enable then the experiment will not try to start the instance post chaos instead it will check of the addition of the new node instance to the cluster.

Integrations

  • EC2 Terminate can be effected using the chaos library: litmus, which makes use of aws sdk to start/stop an EC2 instance.
  • The desired chaoslib can be selected by setting the above options as value for the env variable LIB

Steps to Execute the Chaos Experiment

  • This Chaos Experiment can be triggered by creating a ChaosEngine resource on the cluster. To understand the values to provide in a ChaosEngine specification, refer Getting Started

  • Follow the steps in the sections below to create the chaosServiceAccount, prepare the ChaosEngine & execute the experiment.

Prepare chaosServiceAccount

  • Use this sample RBAC manifest to create a chaosServiceAccount in the desired (app) namespace. This example consists of the minimum necessary role permissions to execute the experiment.

Sample Rbac Manifest

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: ec2-terminate-by-id-sa
  namespace: default
  labels:
    name: ec2-terminate-by-id-sa
    app.kubernetes.io/part-of: litmus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: ec2-terminate-by-id-sa
  labels:
    name: ec2-terminate-by-id-sa
    app.kubernetes.io/part-of: litmus
rules:
- apiGroups: [""]
  resources: ["pods","events","secrets"]
  verbs: ["create","list","get","patch","update","delete","deletecollection"]
- apiGroups: [""]
  resources: ["pods/exec","pods/log"]
  verbs: ["create","list","get"]
- apiGroups: ["batch"]
  resources: ["jobs"]
  verbs: ["create","list","get","delete","deletecollection"]
- apiGroups: ["litmuschaos.io"]
  resources: ["chaosengines","chaosexperiments","chaosresults"]
  verbs: ["create","list","get","patch","update"]
- apiGroups: [""]
  resources: ["nodes"]
  verbs: ["patch","get","list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: ec2-terminate-by-id-sa
  labels:
    name: ec2-terminate-by-id-sa
    app.kubernetes.io/part-of: litmus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ec2-terminate-by-id-sa
subjects:
- kind: ServiceAccount
  name: ec2-terminate-by-id-sa
  namespace: default

Prepare ChaosEngine

  • Provide the application info in spec.appinfo. It is an optional parameter for infra level experiment.
  • Provide the auxiliary applications info (ns & labels) in spec.auxiliaryAppInfo
  • Override the experiment tunables if desired in experiments.spec.components.env
  • To understand the values to provided in a ChaosEngine specification, refer ChaosEngine Concepts

Supported Experiment Tunables

Variables Description Specify In ChaosEngine Notes
EC2_INSTANCE_ID Instance ID of the target ec2 instance. Multiple IDs can also be provided as a comma(,) separated values Mandatory Multiple IDs can be provided as `id1,id2`
TOTAL_CHAOS_DURATION The total time duration for chaos insertion (sec) Optional Defaults to 30s
CHAOS_INTERVAL The interval (in sec) between successive instance termination. Optional Defaults to 30s
MANAGED_NODEGROUP Set to enable if the target instance is the part of self-managed nodegroups Optional Defaults to disable
SEQUENCE It defines sequence of chaos execution for multiple instance Optional Default value: parallel. Supported: serial, parallel
RAMP_TIME Period to wait before and after injection of chaos in sec Optional
REGION The region name of the target instace Optional
INSTANCE_ID A user-defined string that holds metadata/info about current run/instance of chaos. Ex: 04-05-2020-9-00. This string is appended as suffix in the chaosresult CR name. Optional Ensure that the overall length of the chaosresult CR is still < 64 characters

Sample ChaosEngine Manifest

apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
  name: nginx-chaos
  namespace: default
spec:
  engineState: 'active'
  chaosServiceAccount: ec2-terminate-by-id-sa
  experiments:
    - name: ec2-terminate-by-id
      spec:
        components:
          env: 
            # set chaos duration (in sec) as desired
            - name: TOTAL_CHAOS_DURATION
              value: '30'

            # set interval duration (in sec) as desired
            - name: CHAOS_INTERVAL
              value: '30'

             # Instance ID of the target ec2 instance
             # Multiple IDs can also be provided as comma separated values ex: id1,id2
            - name: EC2_INSTANCE_ID
              value: ''
              
            # provide the region name of the instance
            - name: REGION
              value: ''

            # enable it if the target instance is a part of self-managed nodegroup.
            - name: MANAGED_NODEGROUP
              value: 'disable'              

Create the ChaosEngine Resource

  • Create the ChaosEngine manifest prepared in the previous step to trigger the Chaos.

    kubectl apply -f chaosengine.yml

  • If the chaos experiment is not executed, refer to the troubleshooting section to identify the root cause and fix the issues.

Watch Chaos progress

  • Monitor the ec2 state from AWS CLI.

    aws ec2 describe-instance-status --instance-ids <instance-id>

  • You can also use aws console to keep a watch over the instance state.

Abort/Restart the Chaos Experiment

  • To stop the ec2-terminate-by-id experiment immediately, either delete the ChaosEngine resource or execute the following command:

    kubectl patch chaosengine <chaosengine-name> -n <namespace> --type merge --patch '{"spec":{"engineState":"stop"}}'

  • To restart the experiment, either re-apply the ChaosEngine YAML or execute the following command:

    kubectl patch chaosengine <chaosengine-name> -n <namespace> --type merge --patch '{"spec":{"engineState":"active"}}'

Check Chaos Experiment Result

  • Check whether the application is resilient to the ec2-terminate-by-id, once the experiment (job) is completed. The ChaosResult resource name is derived like this: <ChaosEngine-Name>-<ChaosExperiment-Name>.

    kubectl describe chaosresult nginx-chaos-ec2-terminate-by-id -n <application-namespace>

EC2 Terminate Experiment Demo

  • A sample recording of this experiment execution will be added soon.