0
1

More than 1 year has passed since last update.

k8sgptでChatGPTを使ったKubernetesの障害分析

Posted at

3/28にk8sgptなるものが公開された。
これはKubernetes向けChatGPTであり、kubernetesクラスタをスキャンし、問題を診断してくれるらしい。
早速試してみた。

準備

Installationのページに従ってコマンドをインストールする。Windows/Mac/Linuxそれぞれで利用可能なようだ。
今回はMac版で試していく。

brew tap k8sgpt-ai/k8sgpt
brew install k8sgpt

OpenAIを使っており、このAPIキーが必要となるので、アカウントを作成する。
Emailで登録し、電話番号を入力してVerifyする。ログインし、右上のPersonal->View API keysを選択し、Create secret keyからAPIキーを作成する。
1680148494428.png

作成後、k8sgpt authを実行して、APIキーを登録する。

$ k8sgpt auth
Enter openai Key: key added

分析

分析はk8sgpt analyzeで行う。
特にクラスタ上で問題が発生していない状態で分析してみる。

$ k8sgpt analyze
{ "status": "OK" }

問題のあるPodを作ってみる。

kubectl run --image nginx:hoge nginx-failure -n pvc-test
$ kubectl get pod -n pvc-test
NAME            READY   STATUS             RESTARTS   AGE
nginx-failure   0/1     ImagePullBackOff   0          73s

Namespaceを指定して分析する。

$ k8sgpt analyze -n pvc-test
   0% |                                                                                                                             | (0/1, 0 it/hr) [0s:0s]0 pvc-test/nginx-failure(nginx-failure): Back-off pulling image "nginx:hoge"

問題は検知出来たが、分析はされていない。次に--explainをつけて分析させてみる。

$ k8sgpt analyze -n pvc-test --explain
 100% |████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| (1/1, 8 it/min)
0 pvc-test/nginx-failure(nginx-failure): Back-off pulling image "nginx:hoge"
This error message is indicating that there was a problem pulling the "nginx:hoge" image. The Kubernetes system is retrying in a back-off fashion until the image is successfully pulled.

To solve the issue, one possible solution would be to check if the image name and tag are correctly spelled and exist within the specified repository. Additionally, it may be helpful to check network connectivity to the registry and ensure that the appropriate credentials are being used to access the image.

check if the image name and tag are correctly spelledとあるので一応あっているようだ。
なお、k8sgpt analyzeのオプションに--languageがあり、いくつかの言語に変換できる。

-l, --language string    Languages to use for AI (e.g. 'English', 'Spanish', 'French', 'German', 'Italian', 'Portuguese', 'Dutch', 'Russian', 'Chinese', 'Japanese', 'Korean') (default "english")

Japaneseもあるので--no-cacheで前回の結果を使わず分析してみる。

k8sgpt analyze -n pvc-test --explain --language 'Japanese' --no-cache
 100% |███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| (1/1, 4 it/min)
0 pvc-test/nginx-failure(nginx-failure): Back-off pulling image "nginx:hoge"
このKubernetesのエラーメッセージは、イメージ "nginx:hoge" の取得に失敗し続けていることを示しています。これは、Kubernetesが指定されたイメージを取得するのに問題があることを意味しています。この問題を解決するためには、接続エラー、権限エラー、またはDocker Hub内の問題などの原因を見つけ出して対処する必要があります。具体的な解決策については、DockerフォーラムやKubernetesコミュニティに投稿して、ガイダンスを求めることがお勧めです。

すごい。
エラー要因を変えてみて、DockerHubから拒否されるケースで試してみた。

$ kubectl get pod -n toomanyrequest
NAME            READY   STATUS             RESTARTS   AGE
nginx-failure   0/1     ImagePullBackOff   0          68s
$ kubectl describe pod -n toomanyrequest nginx-failure
:(省略)
  Warning  Failed     5s    kubelet            Failed to pull image "nginx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/nginx:latest": failed to copy: httpReadSeeker: failed open: unexpected status code https://registry-1.docker.io/v2/library/nginx/manifests/sha256:2ab30d6ac53580a6db8b657abf0f68d75360ff5cc1670a85acb5bd85ba1b19c0: 429 Too Many Requests - Server message: toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit

これを分析すると、以下の結果となった。

$ k8sgpt analyze -n toomanyrequest --explain --language 'Japanese' --no-cache
 100% |███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| (1/1, 4 it/min)
0 toomanyrequest/nginx-failure(nginx-failure): Back-off pulling image "nginx"
Kubernetes Error Message: Back-off pulling image "nginx"

Solution in Japanese: "nginx"のイメージをプルできていないため、バックオフが発生しています。この問題を解決するには、以下のことを確認してください:

1. Dockerイメージが正しい場所にあることを確認します。
2. イメージのダウンロード先がアクセス可能であることを確認してください。
3. ネットワークの問題が原因である可能性があるため、Kubernetesクラスターのネットワーク接続を確認してください。

問題が解決されない場合は、Dockerレジストリへリセットを行ってください。

提示してくれている要因の2に該当するので、これも参考には出来そうだ。

次にkind: Serviceにも試してみる。問題があったPodをクリーンアップした後、以下を実施する。
公開されていないPortを指定してみる。

kubectl run --image nginx nginx -n pvc-test
kubectl expose pod nginx --port 9999 -n pvc-test

分析結果は以下。

$ k8sgpt analyze -n pvc-test --explain --language 'Japanese' --no-cache
{ "status": "OK" }

流石にエラーとはならなかった。
次にPodを消してみる。

kubctl delete pod -n pvc-test nginx

分析してみた。

$ k8sgpt analyze -n pvc-test --explain --language 'Japanese' --no-cache
 100% |███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| (1/1, 5 it/min)
0 pvc-test/nginx(nginx): Service has no endpoints, expected label run=nginx
このKubernetesのエラーメッセージを簡略化し、日本語での解決策を提供してください:サービスにエンドポイントがありません。ラベルrun = nginxが必要です。

解決策:Podをデプロイして、そのPodに正しいラベル(run = nginx)を付ける必要があります。次に、サービスを作成し、そのサービスにラベル(run = nginx)を付ける必要があります。これにより、サービスが正しいエンドポイントを見つけることができます。

これは良い感じな回答。

まとめ

リリース直後の割にはかなりしっかり分析できているように見える。
特にKubernetes初心者にはデバッグ目的で利用してみてもいいのではないかと思う。

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