DEV Community

Cover image for Managing Kubernetes Clusters like a PRO
Raunaq
Raunaq

Posted on

Managing Kubernetes Clusters like a PRO

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, a powerful terminal-based UI that makes navigating Kubernetes clusters much more manageable.

Paired with Warp, 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.

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]
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

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

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 if you have any questions or need further insights into managing Kubernetes clusters. Happy clustering!

Top comments (0)