Managing Kubernetes Clusters like a PRO
Tags: kubernetes, docker, devops, programming
Published: 2024-07-12 09:36:26

Managing multiple Kubernetes clusters can be a complex task, but with the right tools and techniques, it becomes a seamless part of your workflow. 

As a Senior Machine Learning Engineer, I frequently work with various Kubernetes clusters, often needing to view logs or the state of multiple clusters at the same time. In this article, I'm going to show my workflow of how I achieve this using two of my favourite tools - k9s and Warp terminal.

## Tools of the Trade

To keep track of my Kubernetes clusters, I use [k9s](https://k9scli.io/), a powerful terminal-based UI that makes navigating Kubernetes clusters much more manageable. 

Paired with [Warp](https://www.warp.dev/), a fast and modern terminal app for MacOS, this combination provides a robust environment for managing multiple clusters. 

The visual interface of k9s allows me to monitor cluster resources, check logs, and troubleshoot issues without leaving the terminal.

Let me show you how I set up my cluster, and then you can use this tutorial as a guide to have a similar setup of your own.

### Dev Setup

To connect and interact with a Kubernetes cluster, you need to download and install [kubectl](https://kubernetes.io/docs/tasks/tools/). 

kubectl is the command-line tool for interacting with Kubernetes clusters, by communicating with the Kubernetes API server via RESTful API calls, handling all the interactions, communications, and requests between the users.

Once you have kubectl setup installed, you need to have the config of the Kubernetes Cluster stored in your `$HOME/.kube` directory. For example, if you're working with Google Kubernetes Engine, you can run the following command to fetch the details of the specific cluster.

```
gcloud container clusters get-credentials [CLUSTER_NAME] --zone [ZONE] --project [PROJECT_ID]
```

When you run this command, the details of the cluster are stored in a file `$HOME/.kube/config`, the contents of which will look like this : 

```
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: <ca-data-here>
    server: https://your-k8s-cluster.com
  name: <cluster-name>
contexts:
- context:
    cluster:  <cluster-name>
    user:  <cluster-name-user>
  name:  <cluster-name>
current-context:  <cluster-name>
kind: Config
preferences: {}
users:
- name:  <cluster-name-user>
  user:
    token: <secret-token-here>
```

This command updates your `kubeconfig` file with the credentials of the specified cluster. However, managing multiple clusters means your `kubeconfig` file can quickly become cluttered.

### Organizing Configurations

To handle multiple cluster configurations efficiently, I create separate configuration files for each cluster. This approach helps me keep things organized and avoids the complexity of managing a single, monolithic `kubeconfig` file. Here’s how you can do it:

1.	Fetch Credentials: Use the `gcloud container clusters get-credentials` command to fetch credentials for each cluster.
2.	Create Separate Config Files: Save each cluster’s configuration in a separate file, e.g., ~/.kube/config-cluster1, ~/.kube/config-cluster2, etc.
3.	Set Up Aliases: Create aliases in your shell configuration file (e.g., `.bashrc` or `.zshrc`) to switch between configurations easily.

Here’s an example of how to set up aliases:

![using aliases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsifxbbspd27qzi81qzo.png)

Once you've spent some time setting up cluster configurations, managing and interacting with them daily becomes quite seamless. 

Using Warp, I'm also able to split a single terminal window into 2 or more panes to view and interact with multiple clusters at the same time, which is very helpful, for example - when I want to view logs of multiple deployments at the same time.

![Viewing Logs of multiple pods at the same time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwda28afb3z2j8cjymse.png)

## Conclusion

Managing multiple Kubernetes clusters doesn’t have to be daunting. By leveraging powerful tools like k9s and Warp terminal, organizing your configurations effectively, and following best practices, you can handle multiple clusters like a pro. Whether you are fetching credentials, monitoring cluster health, or automating tasks, these strategies will help streamline your workflow and ensure smooth operations across all your Kubernetes environments.

Feel free to reach out to me on my socials [here](https://bento.me/raunaqness) if you have any questions or need further insights into managing Kubernetes clusters. Happy clustering!