1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS CLIでEKSクラスタのEC2インスタンスを起動・停止するシェル

1
Last updated at Posted at 2020-11-12

AWS CLIでEKSクラスタのEC2インスタンスを起動・停止するシェルのアイキャッチ

この記事でわかること

  • AWS CLIでEKSクラスタのEC2インスタンスを起動・停止するシェル で扱う作業の全体像
  • 手順を進める前に確認したい前提
  • 今の環境で試すときに気をつけたい点

はじめに

ローカルPCからAWS CLIを使って、EKSクラスタのワーカーノードであるEC2インスタンスを起動・停止します。
検証環境など、コスト削減のため、使わないときはEC2インスタンスを停止にすると思いますが、AWS CLIで変更します。

予めjqをインストールして奥必要があります。

スクリプト

1. EKSクラスタのワーカーノードであるEC2インスタンスを起動する

start_cluster_nodes.sh
#!/bin/sh

cd `dirname $0`

CLUSTER_NAME=[クラスタ名]

AUTO_SCALING_GROUP_NAME=$(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`eks:cluster-name`].Value, `'${CLUSTER_NAME}'`)].AutoScalingGroupName' | jq -r '. | tostring' | sed -e 's/\[//' -e 's/\]//' -e 's/\"//g' )
EC2_INSTANCE_IDS=($(aws ec2 describe-instances --filter "Name=tag:eks:cluster-name,Values=${CLUSTER_NAME}" --query "Reservations[].Instances[].InstanceId" | jq -r '. | tostring' | sed -e 's/\[//' -e 's/\]//' -e 's/\"//g' -e 's/,/ /g'))

for instance_id in ${EC2_INSTANCE_IDS[@]}; do
    aws ec2 start-instances --instance-ids ${instance_id}
    aws autoscaling attach-instances --instance-ids ${instance_id} \
    --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME}
done

2. EKSクラスタのワーカーノードであるEC2インスタンスを停止する

stop_cluster_nodes.sh
#!/bin/sh

cd `dirname $0`

CLUSTER_NAME=[クラスタ名]

AUTO_SCALING_GROUP_NAME=$(aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?contains(Tags[?Key==`eks:cluster-name`].Value, `'${CLUSTER_NAME}'`)].AutoScalingGroupName' | jq -r '. | tostring' | sed -e 's/\[//' -e 's/\]//' -e 's/\"//g' )
EC2_INSTANCE_IDS=($(aws ec2 describe-instances --filter "Name=tag:eks:cluster-name,Values=${CLUSTER_NAME}" --query "Reservations[].Instances[].InstanceId" | jq -r '. | tostring' | sed -e 's/\[//' -e 's/\]//' -e 's/\"//g' -e 's/,/ /g'))

for instance_id in ${EC2_INSTANCE_IDS[@]}; do
    aws autoscaling detach-instances --instance-ids ${instance_id} \
    --auto-scaling-group-name ${AUTO_SCALING_GROUP_NAME} --should-decrement-desired-capacity
    aws ec2 stop-instances --instance-ids ${instance_id}
done

実務で使うときの確認ポイント

  • Kubernetes、EKS、関連コントローラーのバージョン差分を確認する。
  • kubectl の接続先、namespace、RBAC権限を確認してから実行する。
  • 検証後は不要なリソース、Load Balancer、ログ出力先、権限設定を整理する。
  • 本番反映前に公式ドキュメント、リリースノート、既存クラスタの運用ルールを確認する。

おわりに

ここまで読んでいただきありがとうございました。
Wealthy Designでは、AWS・Webシステム開発・AI活用など、現場で使える技術を大切にしています。

会社の取り組みは、会社サイトにまとめています。

AWS・Web開発・AI活用のご相談や、採用・カジュアル面談については、Wealthy Design公式LINEでもお問い合わせいただけます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?