Kubernetes Management with AI using Tools4AI
Tags: kubernetes, java, ai, llm
Published: 2024-04-03 12:17:23

Kubernetes is a great platform for orchestrating containerized applications. However, the intricate details of its API can be daunting for many. This is where the Tools4AI project steps in, offering a novel solution that allows for managing Kubernetes clusters through natural language, thus making the technology accessible to a wider audience.

> Code for this article is [here](https://github.com/vishalmysore/KuberAI)

**Introducing Tools4AI**
[Tools4AI is an open-source](https://github.com/vishalmysore/Tools4AI) project that integrates Large Language Models (LLMs) with Java, enabling the execution of complex tasks through simple prompts. A remarkable feature of this project is its capability to interpret natural language commands for Kubernetes cluster management. This feature bridges the technical complexity of Kubernetes with the intuitive ease of natural language, opening up Kubernetes management to non-experts.

**Operational Mechanics**
The feature operates by dynamically fetching and parsing the Kubernetes [OpenAPI (Swagger) specification](https://www.linkedin.com/pulse/http-endpoints-large-action-model-complete-ai-vishal-mysore-vhhmc?trackingId=5HSO4ixpSsegOSdo78uSOg%3D%3D&lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_recent_activity_content_view%3BX9eVsKrnT4mEfNjIHJakBg%3D%3D&). By configuring the API's URL, base URL for the API server, authentication headers, and an identifier, Tools4AI generates a comprehensive list of actionable commands from the Kubernetes API documentation.

**Configuration Example**
This is how you can configure the kubernetes endpoints ,I have deployed the sample mock [here](https://huggingface.co/spaces/VishalMysore/mock?logs=container)

```java 
{
  "endpoints": [
    {
      "swaggerurl": "https://huggingface.co/spaces/VishalMysore/mock/raw/main/kubernetes.json",
      "baseurl": "https://vishalmysore-mock.hf.space",
      "id": "openshift",
      "headers": [
        {
          "key": "Authorization",
          "value": "apiKey=samplekey"
        }
      ]
    }
  ]
}
```
This configuration directs Tools4AI to the Kubernetes OpenAPI specification and includes authentication details necessary for API interactions.

**Endpoint Analysis and Action Generation**

Upon retrieving the OpenAPI specification, Tools4AI parses it to map out available operations. For instance, the tool identifies operations like creating, listing, updating, and deleting Kubernetes resources such as ClusterRoleBindings and Roles. Each operation is translated into an actionable command that can be initiated through natural language prompts.

**Examples of HTTP Endpoints**

GET Request: Fetch all Cluster Role Bindings or a specific Role Binding in a namespace. (you can provide specific names and they will be passed as arguments to the http get or post) - Look at the details [here
](https://www.linkedin.com/pulse/http-endpoints-large-action-model-complete-ai-vishal-mysore-vhhmc?trackingId=5HSO4ixpSsegOSdo78uSOg%3D%3D&lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_recent_activity_content_view%3BX9eVsKrnT4mEfNjIHJakBg%3D%3D&)

```
GET https://vishalmysore-mock.hf.space/apis/rbac.authorization.k8s.io/v1/clusterrolebindings
```
POST Request: Create a new Cluster Role or Role Binding within a specified namespace.

```
POST https://vishalmysore-mock.hf.space/apis/rbac.authorization.k8s.io/v1/clusterroles
```
DELETE Request: Remove a particular ClusterRole or its binding

```
DELETE https://vishalmysore-mock.hf.space/apis/rbac.authorization.k8s.io/v1/clusterroles/voluptas
```
**Impact on Kubernetes Management**

The Tools4AI project significantly lowers the entry barrier to Kubernetes, enabling:

**Broader Accessibility:** By simplifying Kubernetes management, Tools4AI makes the technology approachable for users without deep technical knowledge.

**Enhanced Productivity:** Users can manage Kubernetes resources more efficiently through natural language commands, saving time and reducing complexity.

**Educational Advantage: **It serves as an innovative tool for teaching Kubernetes, allowing learners to interact with clusters in a more intuitive manner.

**Advanced Example**
In an effort to delve deeper into the practical applications of Tools4AI for Kubernetes management, let's explore complex scenarios where multiple endpoints are leveraged in sequence to accomplish broader tasks. These examples will illustrate how natural language prompts can initiate [complex workflows](https://www.linkedin.com/pulse/building-autonomous-ai-agent-java-action-scripts-vishal-mysore-p3mbf?trackingId=5HSO4ixpSsegOSdo78uSOg%3D%3D&lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_recent_activity_content_view%3BX9eVsKrnT4mEfNjIHJakBg%3D%3D&), showcasing the power and flexibility of Tools4AI in managing cloud-native environments.

**Scenario 1: Upgrading a Deployment**
Objective: Upgrade all pods in a deployment to a new version of the application.
Natural Language Prompt: "Upgrade the payment service deployment to version 2.0."
Underlying Actions:
Fetch the Current Deployment:
GET /apis/apps/v1/namespaces/default/deployments/payment-service
This retrieves the current deployment configuration for the "payment-service".
Update the Image Version:
PATCH /apis/apps/v1/namespaces/default/deployments/payment-service
The request body contains the new image version, initiating the rolling update process.

**Scenario 2: Rolling Back a Faulty Deployment**

Objective: Roll back a deployment to its previous stable version after detecting an issue with the current release.
Natural Language Prompt: "Roll back the inventory service deployment to the previous version."
Underlying Actions:
List Deployment Revisions:
GET /apis/apps/v1/namespaces/default/deployments/inventory-service/revisions
This action fetches the history of revisions for the "inventory-service" deployment.
Identify the Previous Revision Number:
Determine the revision number just before the latest (this step may involve parsing the list and selecting the second-latest entry).
Roll Back to the Previous Revision:
PATCH /apis/apps/v1/namespaces/default/deployments/inventory-service/rollback
The request body specifies the identified revision number for rollback

**Scenario 3: Configuring Auto-Scaling for a Deployment**

Objective: Set up Horizontal Pod Autoscaler (HPA) for a deployment based on CPU usage, enabling automatic scaling of pods in response to load.
Natural Language Prompt: "Enable auto-scaling for the frontend deployment based on CPU usage, with a minimum of 3 pods and a maximum of 10 pods."
Underlying Actions:
Create or Update HPA:
POST /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers
If an HPA already exists for the "frontend" deployment, this might instead be a PATCH action to update the existing HPA configuration.
The request body specifies the target deployment, minimum and maximum number of pods, and the CPU utilization threshold for scaling.

**Conclusion**

Tools4AI's integration of natural language processing with Kubernetes management represents a forward leap in making cloud-native technologies more user-friendly. This project not only demonstrates the potential of AI in simplifying complex systems but also sets a precedent for future innovations in the technological space, making advanced technologies more accessible and manageable for a broader audience.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d37cn7lb1mrs997z7nwz.jpg)