0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Splunkの分散基盤をつくる【第2回】SSM Run CommandでSplunkの一括インストール

0
Posted at

はじめに

前回の記事では、CloudFormationを使ってSplunk Enterpriseの分散構成用にEC2を7台作成しました。

Splunkの分散基盤をつくる【第1回】CloudFormationによる基盤構築

今回は、AWS Systems Manager Run Commandを使い、この7台へSplunk Enterpriseを一括インストールします。

使用する手順

この記事では、公開リポジトリの次の手順とスクリプトを使用します。

  1. Splunkインストールの事前準備
  2. Splunkのインストール

前提

  • AWS CLIをインストールし、対象アカウントのプロファイルを設定済みである
  • 前回記事で作成した7台のEC2をSystems Managerから管理できる
  • 7台のEC2にCloudFormationのEnvironmentパラメーターと同じ値のEnvironmentタグが付いている
  • EC2のIAMロールから、配置先S3バケットとParameter Storeを読み取れる
  • Splunk Enterpriseのx86_64向けRPMをダウンロード済みである

この手順は、SplunkがまだインストールされていないクリーンなEC2を対象とします。

インストールの事前準備

1. 公開リポジトリを取得する

以降のコマンドは、リポジトリのルートディレクトリで実行します。

2. 実行に使用する値を設定する

<...>を利用する環境の値へ置き換え、ダウンロードしたRPMのパスを指定します。

AWS_PROFILE='<AWS_PROFILE>'
DEPLOYMENT_REGION='<DEPLOYMENT_REGION>'
DEPLOYMENT_BUCKET='<DEPLOYMENT_BUCKET>'
DEPLOYMENT_ENVIRONMENT='<DEPLOYMENT_ENVIRONMENT>'
ADMIN_PASSWORD_PARAMETER='<ADMIN_PASSWORD_PARAMETER>'
SPLUNK_RPM_PATH='<SPLUNK_RPM_PATH>'

SPLUNK_RPM=$(basename -- "${SPLUNK_RPM_PATH}")
SPLUNK_RPM_SHA256=$(sha256sum "${SPLUNK_RPM_PATH}" | cut -d ' ' -f 1)

取得したファイル名とSHA-256を確認します。

printf 'RPM: %s\nSHA-256: %s\n' \
  "${SPLUNK_RPM}" \
  "${SPLUNK_RPM_SHA256}"

3. admin初期パスワードを登録する

admin初期パスワードは、Run CommandのJSONへ記載せず、Parameter StoreのSecureStringへ保存し、Splunk インストール時に呼び出します。

read -rsp 'Splunk admin password: ' SPLUNK_ADMIN_PASSWORD
printf '\n'

aws ssm put-parameter \
  --name "${ADMIN_PASSWORD_PARAMETER}" \
  --description 'Initial Splunk administrator password' \
  --type SecureString \
  --value "${SPLUNK_ADMIN_PASSWORD}" \
  --profile "${AWS_PROFILE}" \
  --region "${DEPLOYMENT_REGION}"

unset SPLUNK_ADMIN_PASSWORD

Parameter Storeのバージョン番号が返れば登録完了です。

4. RPMとスクリプトをS3へ配置する

1つ目のコマンドでSplunkのRPMを、2つ目でインストールスクリプトを非公開S3バケットへアップロードします。Run Command実行時に、各EC2がこれらのファイルを取得します。

aws s3 cp "${SPLUNK_RPM_PATH}" \
  "s3://${DEPLOYMENT_BUCKET}/splunk/packages/${SPLUNK_RPM}" \
  --profile "${AWS_PROFILE}" \
  --region "${DEPLOYMENT_REGION}"

aws s3 cp splunk/procedures/02-splunk-installation/install-splunk.sh \
  "s3://${DEPLOYMENT_BUCKET}/splunk/scripts/install-splunk.sh" \
  --profile "${AWS_PROFILE}" \
  --region "${DEPLOYMENT_REGION}"

5. Run Commandの入力ファイルを作成する

send-command.example.jsonをコピーし、S3バケット、RPM、SHA-256、Parameter Store、リージョンを実行環境の値へ置き換えます。

SEND_COMMAND_JSON=splunk/procedures/02-splunk-installation/send-command.json

if [[ ! "${DEPLOYMENT_ENVIRONMENT}" =~ ^[A-Za-z0-9-]+$ ]]; then
  echo 'DEPLOYMENT_ENVIRONMENT must contain only letters, numbers, and hyphens.' >&2
  exit 1
fi

cp \
  splunk/procedures/02-splunk-installation/send-command.example.json \
  "${SEND_COMMAND_JSON}"

sed -i \
  -e "s|<DEPLOYMENT_BUCKET>|${DEPLOYMENT_BUCKET}|g" \
  -e "s|<SPLUNK_RPM>|${SPLUNK_RPM}|g" \
  -e "s|<SPLUNK_RPM_SHA256>|${SPLUNK_RPM_SHA256}|g" \
  -e "s|<ENVIRONMENT>|${DEPLOYMENT_ENVIRONMENT}|g" \
  -e "s|ADMIN_PASSWORD_PARAMETER=[^ ]*|ADMIN_PASSWORD_PARAMETER=${ADMIN_PASSWORD_PARAMETER}|g" \
  -e "s|DEPLOYMENT_REGION=[^ ]*|DEPLOYMENT_REGION=${DEPLOYMENT_REGION}|g" \
  -e "s|--region [a-z0-9-]*|--region ${DEPLOYMENT_REGION}|g" \
  "${SEND_COMMAND_JSON}"

if rg -n '<[A-Z0-9_]+>' "${SEND_COMMAND_JSON}"; then
  echo '未置換のプレースホルダーがあります。' >&2
  exit 1
fi

python3 -m json.tool "${SEND_COMMAND_JSON}" >/dev/null

Splunkを一括インストールする

Run Commandを送信し、Command IDを保存します。

COMMAND_ID=$(aws ssm send-command \
  --cli-input-json "file://${SEND_COMMAND_JSON}" \
  --query 'Command.CommandId' \
  --output text \
  --profile "${AWS_PROFILE}" \
  --region "${DEPLOYMENT_REGION}")

printf 'Command ID: %s\n' "${COMMAND_ID}"

入力ファイルでは、EnvironmentタグとSplunkRoleタグの両方に一致する7台を対象にしています。各EC2では、公開スクリプトがadmin初期認証情報を作成し、Splunkをsplunkユーザーで起動してsystemdへ登録します。

実行結果を確認する

aws ssm list-command-invocations \
  --command-id "${COMMAND_ID}" \
  --details \
  --query 'CommandInvocations[*].{InstanceId:InstanceId,Status:Status,ResponseCode:CommandPlugins[0].ResponseCode}' \
  --output table \
  --profile "${AWS_PROFILE}" \
  --region "${DEPLOYMENT_REGION}"

送信直後はPendingまたはInProgressと表示されます。時間を置いて再実行し、対象7台がすべてSuccessResponseCode0になれば完了です。

まとめ

公開リポジトリの準備手順とインストールスクリプトを使い、Systems Manager Run Commandから7台へSplunk Enterpriseを一括インストールしました。

次回は Splunk の分散構成 config を一括配布する手順をご説明します。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?