Photo by Steve Johnson / Unsplash

How to use AI to Troubleshoot your Kubernetes Cluster

Prince Onyeanuna
Prince Onyeanuna

Table of Contents

With the increasing Kubernetes (K8s) adoption in modern software development, developers and DevOps teams face a critical challenge when deploying and maintaining applications. Their expertise is often diverted from core application development to analyzing and troubleshooting Kubernetes, affecting their development velocity.

To save you time on troubleshooting K8s, Alex Jones created K8sGPT. K8sGPT is an open source tool that leverages artificial intelligence (AI) to simplify the analysis of Kubernetes clusters. It translates complex cluster signals into clear and actionable suggestions. These suggestions give both experts and novice SRE superpowers.

This article will explore K8sGPT and its benefits. Also, You will learn how to install  and use it to analyze your Kubernetes cluster.

What is K8sGPT?

Built on the Open AI GPT language model, a powerful AI model that can generate human-like text. K8sGPT uses Open AI to scan Kubernetes clusters and create suggestions for troubleshooting and optimization.

Benefits of K8sGPT

K8sGPT is the definition of working smarter, not harder. The following are the benefits of utilizing K8sGPT for your Kubernetes clusters:

  • Simplify cluster analysis: Traditional cluster analysis methods can be inefficient, requiring manual inspection and extensive expertise, often leading to human error. However, K8sGPT can quickly translate complex cluster signals into clear and actionable suggestions. This feature allows you to focus on building and maintaining your applications.
  • Reduce troubleshooting time: Traditional troubleshooting procedures can range from a few minutes to hours, depending on the cluster size and SRE team. Using K8sGPT, you can scan and diagnose your cluster within a few seconds.
  • Improve cluster performance: K8sGPT can help you improve the performance of your cluster by providing suggestions on optimizing your cluster. These suggestions can help you improve the performance of your cluster.

How to install K8sGPT

Aside from being a powerful tool, K8sGPT is also very easy to install. K8sGPT has various installation steps for different operating systems.

You can find the installation steps appropriate for your operating system on the K8sGPT installation documentation page.

To install K8sGPT on Linux, run the following command:

For RPM-based Linux distributions (Fedora, CentOS, RHEL):

32 bit:

$ curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.2.1/k8sgpt_386.rpm
$ sudo rpm -ivh k8sgpt_386.rpm

64 bit:

$ curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.2.1/k8sgpt_amd64.rpm
$ sudo rpm -ivh -i k8sgpt_amd64.rpm

For DEB-based Linux distributions (Debian, Ubuntu):

32 bit:

$ curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.2.7/k8sgpt_386.deb
$ sudo dpkg -i k8sgpt_386.deb

64 bit:

$ curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.2.7/k8sgpt_amd64.deb
$ sudo dpkg -i k8sgpt_amd64.deb

For Alpine Linux:

32 bit:

$ curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.2.7/k8sgpt_386.apk
$ apk add k8sgpt_386.apk

64 bit:

$ curl -LO https://github.com/k8sgpt-ai/k8sgpt/releases/download/v0.2.1/k8sgpt_amd64.apk
$ apk add k8sgpt_amd64.apk

To verify that K8sGPT is installed, run the following command:

$ k8sgpt version

k8sgpt version 0.2.7

The K8sGPT installation is now complete. If you run into any issues in the process of installing this tool, you can check the K8sGPT installation documentation page for the resolution to common problems.

How to setup K8sGPT

After installing K8sGPT, you must set up the tool to analyze your Kubernetes cluster.
The setup process involves authenticating with a backend - the AI provider that will be used to analyze your cluster.

The goal of K8sGPT is to be able to support multiple AI providers. However, the only supported AI provider for now is OpenAI's ChatGPT.

Ensure you have created an OpenAI account for the authentication.

On your terminal, run the following command to generate a token from the backend:

$ k8sgpt generate
Figure 1 - K8sGPT generate

From the output, follow the URL to generate the token.

Then copy the token and run the following command to authenticate with the backend:

$ k8sgpt auth add --backend openai -m gpt-3.5-turbo

Enter openai key: <paste your key here>
key added

Paste the copied token and press enter.

The setup process is complete. You can now use K8sGPT to analyze your Kubernetes cluster.

How to analyze your Kubernetes cluster with K8sGPT

After installing and setting up K8sGPT, you can now use the tool to analyze your Kubernetes cluster.

You can't analyze a cluster without first creating a Kubernetes cluster. So, first, make sure you are connected to a Kubernetes cluster, such as KinD or MiniKube.

To analyze a Kubernetes cluster, follow the steps below:

Step 1: Ensure that you are connected to the correct Kubernetes cluster:

$ kubectl config current-context

Step 2: Create a broken pod with the file name broken-pod.yml. This YAML file will contain the following:

apiVersion: v1
  kind: Pod
  metadata:
    name: broken-pod
    namespace: default
  spec:
    containers:
      - name: broken-pod
        image: nginx:1.a.b.c
        livenessProbe:
          httpGet:
            path: /
            port: 81
          initialDelaySeconds: 3
          periodSeconds: 3

Note: This pod has the wrong image tag.

Step 3: Apply the file to your cluster:

$ kubectl apply -f broken-pod.yml

The code above will apply the broken-pod.yml file to your cluster.

Step 4: Analyze the cluster using the code below:

$ k8sgpt analyze

This command will generate a list of present issues in your cluster. In this example, the broken pod will be listed as an issue.

Figure 2 - K8sGPT analyze

The output shows that the broken pod has an error. The error is that the image tag is wrong.
For a better understanding of this issue, you can add the --explain flag to the command:

$ k8sgpt analyze --explain

Note: You will get an error with this command if you are not on a subscription plan with OpenAI.

With this, you have successfully analyzed your Kubernetes cluster with K8sGPT.

Aside from analyzing your cluster, K8sGPT has other options you can utilize. You can find these options by running the following command:

$ k8sgpt --help
Figure 3 - K8sGPT help

Conclusion

You can troubleshoot a Kubernetes cluster with K8sGPT, a powerful AI tool. It can help you simplify cluster analysis, reduce troubleshooting time, and improve cluster performance.

This article explored K8sGPT and its benefits. You learned how to install this tool and use it to analyze your Kubernetes cluster. It saves time when you use AI to analyze clusters rather than manually inspecting and troubleshooting them.

To learn more about K8sGPT, you can check out the K8sGPT documentation.

KubernetesAI

Prince Onyeanuna Twitter

Prince is a technical writer and DevOps engineer who believes in the power of showing up. He is passionate about helping others learn and grow through writing and coding.