こんにちは、虎の穴ラボのY.N.です。
昨今、AIを活用した技術が目まぐるしい発展をしています。
KubernetesでもAIを活用したツールがあります。
今回は、Kubernetesのトラブルシューティングに役立つAIツール k8sgpt
を紹介します。
前提
本記事では下記環境を使用することを前提として記載します。
- 開発環境:Mac
- コンテナ:Docker
- Kubernetes:kind
- AIプロバイダ:OpenAI
k8sgpt
何ができるの?
k8sgptはAIを活用してKubernetes環境における問題の診断を行ってくれるツールです。
また、問題の自動修復も行え、Kubernetesのトラブルシューティングを円滑に進められます。
インストール
k8sgptをbrewでインストールします
$ brew tap k8sgpt-ai/k8sgpt $ brew install k8sgpt
インストールが完了したら、OpenAIのAPIキーを登録しましょう
$ k8sgpt generate
以下のメッセージが表示され、OpenAIの「API keys」ページが開くので、APIキーを作成してください
Opening: https://platform.openai.com/api-keys to generate a key for openai Please copy the generated key and run `k8sgpt auth add` to add it to your config file
作成したAPIキーを登録します
$ k8sgpt auth add
登録したら下記のコマンドでopenaiがActiveになっていることを確認します
$ k8sgpt auth list
これで使う準備が完了しました
試してみる
まずは、kindでclusterを作成します
$ kind create cluster --name k8sgpt-sandbox
次に、正常に動作するPodを作成します
- normal-pod.yml
apiVersion: v1 kind: Pod metadata: name: normal-pod spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
$ kubectl apply -f normal-pod.yml
Podの状態はRunningとなっています
$ kubectl get pods NAME READY STATUS RESTARTS AGE normal-pod 1/1 Running 0 2m3s
この状態でk8sgptを使うと、問題なしの応答が返されます
$ k8sgpt analyse -e -b openai AI Provider: openai No problems detected
では、次に問題のあるPodを立ててみます
- error-pod.yml
apiVersion: v1 kind: Pod metadata: name: error-pod spec: containers: - name: nginx image: error-ni-naruyo:1.0.1 ports: - containerPort: 80
この状態でerror-podのステータスを確認すると ImagePullBackOff
となります
kubectl get pods NAME READY STATUS RESTARTS AGE error-pod 0/1 ImagePullBackOff 0 68s normal-pod 1/1 Running 0 6m1s
では、この状態でk8sgptに分析をお願いしましょう
$ k8sgpt analyse -e -b openai AI Provider: openai 0: Pod default/error-pod() - Error: failed to pull and unpack image "docker.io/library/error-ni-naruyo:1.0.1": failed to resolve reference "docker.io/library/error-ni-naruyo:1.0.1": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed Error: The image "docker.io/library/error-ni-naruyo:1.0.1" cannot be pulled because access is denied or the repository doesn't exist. Solution: 1. Verify the image name and tag. 2. Check if the repository exists. 3. Ensure you have the correct permissions. 4. Log in to Docker with `docker login`.
k8sgptが、 error-ni-naruyo:1.0.1
のリポジトリがないまたは権限がないことを教えてくれました。
更に、解決方法を提案してくれるため、次のアクションが取りやすくなっていることがわかります。
analyseでよく使いそうなオプション
-b --backend
: 使用するAIを指定でき、指定がない場合はk8sgpt.ymlでDefaultに指定されているものが使用されます-e --explain
: これがあるとSolutionを提示してくれます。なければエラーの原因を表示してくれるだけになるので、簡潔にエラー原因を知りたい場合はつけなくても良さそうです-n --namespace
: namespaceを指定して解析ができます-l --language
: 言語を指定できます。日本語にも対応しており、Japaneseを指定できます。ただし、 -bでopenaiを指定した場合 では英語になってしまっていました-o --output
: 出力形式を指定できます。text or jsonが選択可能です。kubectlにも同様のオプションがあるので馴染みやすいです-c --no-cache
: k8sgptにはキャッシュ機能があるため、結果が期待通りに変わらない場合はこのオプションを使用すると良いでしょう
kubectl describeとの違い
前述のエラー原因については、従来のトラブルシューティングで使われる kubectl describe
でも同様のことを出力できます。
error-podに対して kubectl describeで原因を調べてみましょう。
$ kubectl describe pod error-pod
その結果がこちら
Name: error-pod Namespace: default Priority: 0 Service Account: default Node: k8sgpt-sandbox-control-plane/172.22.0.2 Start Time: Tue, 25 Mar 2025 08:52:14 +0900 Labels: <none> Annotations: <none> Status: Pending IP: 10.244.0.2 IPs: IP: 10.244.0.2 Containers: nginx: Container ID: Image: error-ni-naruyo:1.0.1 Image ID: Port: 80/TCP Host Port: 0/TCP State: Waiting Reason: ImagePullBackOff ...中略... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal BackOff 11s (x60761 over 28d) kubelet Back-off pulling image "error-ni-naruyo:1.0.1" Warning Failed 11s (x60761 over 28d) kubelet Error: ImagePullBackOff
色々情報が出てきますが、この場合だと、一番下部に出力されるEventsの内容が重要な項目です。
内容としては、 kubelet Back-off pulling image "error-ni-naruyo:1.0.1"
と表示されているため、
error-ni-naruyo:1.0.1
イメージの取得に失敗していると判断できますが、
イメージの取得の失敗理由については自分で考える必要がありました。
このように、kubectlと比べ、k8sgptは必要な情報のみを抽出してくれ非常に分かりやすいです。
また、解決策を提案してくれるため、次のアクションを考える手間が省け、作業効率が向上します。
まとめ
今回はk8sgptを試してみました。
Kubernetes環境管理者は、原因がわかりにくい問題に直面することが多く、
kubectl describeで日々調査する必要性がありました。
しかし、k8sgptを使うことで調査の手間を減らせることがわかりました。
k8sgptを活用してKubernetesの問題を解決していきましょう!
採用情報
虎の穴ラボでは一緒に働く仲間を募集中です!
この記事を読んで、興味を持っていただけた方はぜひ弊社の採用情報をご覧ください。
toranoana-lab.co.jp