Connecting to a Kubernetes Cluster on Rancher with kubectl from Windows

Connecting to a Kubernetes Cluster on Rancher with kubectl from Windows

Introduction

For those managing Kubernetes clusters, Rancher is a fantastic tool that simplifies Kubernetes deployment and management. If you’re a Windows user, you might be wondering how to connect to a Rancher-managed Kubernetes cluster using kubectl, the command-line tool for Kubernetes.

This blog post will walk you through the process, ensuring you can manage your Kubernetes resources from your Windows environment effectively.

What You Need

Before starting, ensure you have the following:

  • Rancher: Access to a Kubernetes cluster managed by Rancher.
  • kubectl: kubectl installed on your Windows machine. (Refer to Installing kubectl on Windows for detailed installation instructions.)
  • Cluster Access Information: Credentials or a kubeconfig file for accessing the Kubernetes cluster.

Getting the kubeconfig File from Rancher

To connect to your Kubernetes cluster, you need the kubeconfig file. Rancher makes it easy to obtain this:

  1. Log into your Rancher dashboard.
  2. Navigate to the cluster you want to connect to.
  3. Click on the Kubeconfig File button usually found at the top of the cluster dashboard.
  4. Copy the contents to your clipboard.

My test cluster file looks like this: –

apiVersion: v1
kind: Config
clusters:
- name: "k8stest"
  cluster:
    server: "https://192.168.1.253/k8s/clusters/c-m-xxx5plwg"
    certificate-authority-data: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJ2akNDQ\
      VdPZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQkdNUnd3R2dZRFZRUUtFeE5rZVc1aGJXbGoKY........"

users:
- name: "k8stest"
  user:
    token: "kubeconfig-user-vb6qsgx6gx:9j9w299kg------------"


contexts:
- name: "k8stest"
  context:
    user: "k8stest"
    cluster: "k8stest"

current-context: "k8stest"

Note that I have removed some of the information in the certificate-authority-data and token tags because they are secret.

Setting Up kubeconfig on Windows

With the kubeconfig contents in hand, you need to set it up on your Windows machine:

  1. Create a directory named .kube in your user’s home directory (C:\Users\[Your User Name]\).
  2. Inside this directory, create a file named config (Note that there is no file type suffix).
  3. Paste the kubeconfig contents into this file.
  4. You can also set the KUBECONFIG environment variable pointing to this file for direct access.

Verifying the Connection

To ensure you’re connected:

  • Open a command prompt or PowerShell.
  • Run kubectl cluster-info.
  • If everything is set up correctly, you should see information about the cluster’s master and services.

My cluster looks like this: –

image-1024x89 Connecting to a Kubernetes Cluster on Rancher with kubectl from Windows

Using kubectl with Your Rancher Cluster

Now that you’re connected, you can use kubectl to manage your Kubernetes cluster:

  • View all nodes in the cluster: kubectl get nodes
  • Check running pods: kubectl get pods
  • Deploy applications, manage resources, and more.

Additional Tips

  • Multiple Clusters: If you’re managing multiple clusters, you can merge different kubeconfig files or switch between them using the KUBECONFIG environment variable.
  • Rancher CLI: For more advanced Rancher-specific operations, consider using the Rancher CLI.
  • Security: Always ensure that your kubeconfig file is stored securely, as it provides access to your Kubernetes cluster.

Conclusion

Connecting to a Rancher-managed Kubernetes cluster from a Windows machine using kubectl bridges the gap between your local development environment and your container orchestration platform.

This setup not only enhances your workflow but also provides the flexibility to manage and monitor your Kubernetes resources efficiently.

Whether you’re deploying new services, scaling existing ones, or just keeping an eye on your cluster’s health, kubectl in tandem with Rancher offers a robust solution for your Kubernetes management needs.

Happy Kubernetes administration!

I have other posts about Kubernetes here: – Install KubeCtl on Windows, Create a Kubernetes Cluster with Rancher, MetalLB – Kubernetes does need a load balancer.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.