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?

【Kubernetes】ノードをデバッグするスクリプト

Last updated at Posted at 2025-04-01

Kubernetesのノードをデバッグするスクリプトを書きました。
意外と便利なので共有です。

※ 中身は kubectl debug node/ ... を使ってデバッグ用のコンテナをノードにたてるだけです。
※ fzfとjqとkubectlが必要です。

node-debugger.sh

#!/bin/bash
set -euo pipefail

function usage {
cat <<EOF >&2
指定したNodeをDebugするPodを立ち上げるコマンド

Usage:
 $0 [Options]

Options:
 -h | --help
   ヘルプ
EOF
exit 1
}

function error {
 echo "[error] $1" >&2
 exit 1
}

function info {
 echo "[info] $1"
}

args=()
while [ "$#" != 0 ]; do
 case $1 in
   -h | --help ) usage ;;
   -* | --*    ) error "$1 : 不正なオプションです" ;;
   *           ) args+=("$1") ;;
 esac
 shift
done
[ "${#args[@]}" != 0 ] && usage

# ノードの選択
NODE="$(kubectl get node -o wide | sed '1d' | fzf --height 25% --header 'Select a node')"
NODE_NAME="$(echo $NODE | awk '{print $1}')"

# 起動するイメージの選択
IMAGE=$(
cat <<EOF | fzf --height 25% --header "Select a image"
nicolaka/netshoot
public.ecr.aws/aws-cli/aws-cli
ubuntu:24.04
EOF
)

# ノードデバッガーのPodを起動
echo kubectl debug node/${NODE_NAME} -ti --image=$IMAGE -- /bin/bash
kubectl debug node/${NODE_NAME} -ti --image=$IMAGE -- /bin/bash

# ノードデバッガーのPodを削除
for pod in $(kubectl get po -o json | jq -r ".items[].metadata.name" | grep -e "^node-debugger"); do
 kubectl delete po $pod
done

使ってみる

node-debugger.sh

まずはデバッグしたいノードを選択

image.png

続いて、起動するPodのイメージを選択

image.png

/host に ノードのファイルシステムがマウントされているので確認してみる

image.png

シェルからログアウトすると自動的にPodが削除されます。

exit
# pod "node-debugger-ip-10-32-1-221.ap-northeast-1.compute.internal-bst6b" deleted
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?