
Kubectl Add Cluster: A Comprehensive Guide
Learn how to use kubectl add cluster to manage multiple Kubernetes clusters efficiently, with step-by-step instructions and best practices for secure setup.
Developer workflows slow down when every new environment requires manually running kubectl add cluster
and digging up credentials. These manual steps don’t scale and quickly become a bottleneck as your Kubernetes footprint grows. In this guide, we’ll walk through standard kubectl
practices for multi-cluster management, highlight their shortcomings, and outline strategies for building a scalable setup that reduces manual configuration and keeps teams moving faster.
Unified Cloud Orchestration for Kubernetes
Manage Kubernetes at scale through a single, enterprise-ready platform.
Key takeaways:
- Treat kubeconfig as a critical security asset: Always restrict file permissions and avoid committing configurations to version control. For production environments, move away from static files and use authentication methods that provide short-lived, dynamically generated credentials to reduce risk.
- Recognize the limits of CLI-based cluster management: While
kubectl
contexts are effective for switching between a few clusters, relying on them at scale creates operational bottlenecks and security risks from manually distributing and managing configuration files across a team. - Centralize cluster access to improve security and efficiency: A unified platform like Plural abstracts away
kubeconfig
management by providing a single-pane-of-glass dashboard with SSO-integrated RBAC. This approach secures your fleet by removing the need to expose cluster APIs or distribute sensitive credentials.
What Is kubectl and Why Is It Essential?
kubectl
is the primary CLI for working with Kubernetes. It communicates directly with the Kubernetes API server, enabling developers to deploy workloads, manage resources, and debug issues. Whether you’re rolling out a simple app or troubleshooting a microservices stack, kubectl
gives you the control needed to operate Kubernetes effectively.
While it works well for a single cluster, scaling across multiple clusters introduces challenges. Managing and distributing kubeconfig files securely across a team creates operational overhead. This is where a centralized management plane becomes critical. Tools like Plural provide a unified dashboard for multi-cluster visibility and access control, removing the complexity of juggling kubeconfig files while still allowing developers to use kubectl
for direct interaction.
A Look at the kubectl Command-Line Tool
At its core, kubectl
translates your commands into API requests for the Kubernetes control plane. It’s the standard way to deploy apps, inspect resources, and check logs. For example, kubectl get pods
lists all pods in the current namespace, giving you a quick overview of running workloads. Mastering these commands is a baseline skill for any developer or operator working in cloud-native environments.
Core Components and Functionality
kubectl
commands revolve around managing Kubernetes objects such as Pods, Deployments, and Services. You can define desired state in manifests and apply them with kubectl apply -f <file>
, inspect details with kubectl describe <resource>
, and remove resources with kubectl delete
. This declarative workflow is central to Kubernetes operations, and kubectl
is the interface that makes it practical.
How kubectl Manages Clusters
kubectl
uses a configuration file called kubeconfig, typically stored at ~/.kube/config
, to know which cluster to connect to. This YAML file contains cluster endpoints, certificate authority data, and user credentials. It also defines contexts, which combine a cluster, user, and namespace to make switching between environments easy. While this system supports multi-cluster access from a single workstation, managing configurations at scale requires discipline—and often a centralized solution like Plural to simplify organization and access control.
What is a Kubeconfig File?
A kubeconfig file is a YAML configuration file that kubectl
and other Kubernetes clients use to connect to clusters. It stores the details needed to authenticate with the API server, organized into contexts that define a cluster, a user, and a namespace. Whenever you run a kubectl
command, the tool reads this file to decide which cluster to target and which credentials to use.
Although people often refer to a single kubeconfig file, configurations can be spread across multiple files or defined via environment variables. This flexibility helps in complex setups but also adds overhead, especially at scale. Distributing and securing kubeconfig files for multiple clusters and users quickly becomes a challenge. A centralized platform like Plural addresses this by providing SSO-integrated access and an embedded Kubernetes dashboard, removing the need to manage raw kubeconfig files manually.
Kubeconfig Structure and Purpose
Kubeconfig files are YAML-based and human-readable, designed to consolidate cluster connection details. The term “kubeconfig” doesn’t refer to a literal filename, but to any Kubernetes client configuration file. By defining multiple clusters, users, and contexts, you can easily switch between environments—for example, development, staging, and production—without rewriting configurations.
The Core Components: Clusters, Users, and Contexts
A kubeconfig file has three main sections:
- Clusters: Define the clusters you can connect to, including the API server URL (
server
) and certificate authority data (certificate-authority-data
). - Users: Store authentication details, which may be client certificates, bearer tokens, or external identity providers. Users can represent people or service accounts.
- Contexts: Bind a cluster and a user, with an optional default namespace. The
current-context
field determines which contextkubectl
uses by default.
Default Locations and Settings
By default, kubectl
looks for a kubeconfig at $HOME/.kube/config
. You can override this in several ways, in order of precedence:
- --kubeconfig flag: Specify a file path directly when running
kubectl
. - KUBECONFIG environment variable: Point to one or more config files, which
kubectl
merges into an effective configuration. - Default location: If no flag or environment variable is set,
kubectl
falls back to$HOME/.kube/config
.
This layered approach provides flexibility for local development, automation scripts, and multi-cluster setups.
How to Add a Cluster to kubectl: A Step-by-Step Guide
Adding a cluster to your local kubectl
configuration is a basic task for anyone working with Kubernetes. Knowing how to do it manually is important, but managing many kubeconfig files quickly becomes unmanageable and introduces security risks. Platforms like Plural streamline this by offering an embedded Kubernetes dashboard with secure authentication, so you don’t have to distribute or juggle individual kubeconfig files across a team.
For situations where you need to add a cluster manually, here’s a step-by-step guide.
Use the kubectl config set-cluster Command
The kubectl config set-cluster
command creates or updates a cluster entry in your kubeconfig file. Each entry specifies the API server address and authentication details. Think of it as adding a new record to your cluster “address book.” Once defined, this cluster can be tied to users and contexts for full access.
Define Parameters and Authentication Options
At a minimum, you’ll need to provide the cluster name and API server URL using the --server
flag. If the cluster name already exists, only the fields you specify will be updated. This makes it easy to change details like the server address or credentials without rebuilding the entire config. A full list of supported options is available in the Kubernetes documentation.
Set Up Certificates and Security
To connect securely, kubectl
must trust the API server’s certificate. Use the --certificate-authority
flag to point to the CA file for the cluster. This prevents man-in-the-middle attacks by ensuring the API server identity is verified. For testing or temporary setups, you can bypass this check with --insecure-skip-tls-verify=true
, but this should never be used in production.
Follow Provider-Specific Steps
Most managed Kubernetes services provide their own tools to simplify kubeconfig setup:
- Amazon EKS:
aws eks update-kubeconfig --name my-cluster
- Google GKE:
gcloud container clusters get-credentials my-cluster
- Azure AKS:
az aks get-credentials --resource-group my-rg --name my-cluster
These commands handle certificate retrieval, authentication, and context creation automatically, reducing the chance of manual errors.
Validate Your Cluster Connection
After adding the cluster, define a user and context, then test your setup. Running a simple command like kubectl get svc
should return the cluster’s services, including svc/kubernetes
. A successful response confirms that authentication and connectivity are working.
Avoid Common Configuration Mistakes
Be careful with kubeconfig files from untrusted sources. A malicious config could expose sensitive data or trick kubectl
into connecting to a compromised cluster with harmful plugins. Always review the contents of any kubeconfig file before using it, the same way you’d inspect a shell script. Following this practice helps prevent security breaches.
How to Manage Multiple Kubernetes Clusters
As infrastructure expands, managing Kubernetes usually shifts from handling one cluster to coordinating many. Development, staging, production, and regional clusters each require separate configurations and access controls. While kubectl
provides the tools to work across multiple clusters, managing them manually can be slow and error-prone. Constantly switching contexts, merging configuration files, and distributing kubeconfigs across teams introduces overhead and raises the risk of misconfiguration or security issues. Here’s how to use kubectl
for multi-cluster management—and where centralized tools like Plural can simplify the process.
Switch Between Clusters
kubectl
uses contexts to manage multiple clusters. A context pairs a cluster, a user, and a namespace. To view your contexts, run:
kubectl config get-contexts
Switching is as simple as:
kubectl config use-context dev-cluster
This ensures all subsequent kubectl
commands are directed to the correct environment, making it easy to move between dev, staging, and production clusters without reconfiguring.
Organize Cluster Access
The kubeconfig file organizes all clusters, users, and contexts in one place. As more clusters are added, a single kubeconfig can become complex and harder to manage securely. Distributing a monolithic kubeconfig across teams is risky—one misconfiguration can expose multiple clusters. Many teams adopt centralized solutions such as Plural, which abstracts away direct kubeconfig handling and provides secure, role-based access at scale.
Use Environment Variables
If you prefer separate files for different environments, the KUBECONFIG
environment variable lets you point kubectl
to multiple files. For example:
export KUBECONFIG=~/.kube/config:~/.kube/config-dev:~/.kube/config-prod
kubectl
merges these configs, giving you a combined view. This helps with compartmentalization but still requires disciplined management to avoid inconsistencies across machines.
Merge Kubeconfig Files
You can merge multiple kubeconfig files into one using kubectl config
commands. For example, kubectl config set-cluster
will add or update entries without overwriting other values. While merging creates a single source of truth, it also produces a large, sensitive file that’s difficult to maintain securely. Manual merges don’t scale well and can introduce drift or errors over time.
For teams managing many clusters, manual methods quickly hit limits. Centralized platforms like Plural offer a more scalable path, consolidating access and reducing the operational overhead of managing kubeconfig files directly.
How to Troubleshoot and Secure Your Setup
Even with a correct configuration, issues with connectivity, permissions, or security can arise. Troubleshooting involves systematically checking your connection, kubeconfig setup, and access rights. Securing your environment means treating kubeconfig files like credentials and applying best practices to reduce risk. A structured approach helps you quickly resolve problems while maintaining a strong security posture.
Resolve Common Connection Issues
Start by confirming basic connectivity. Run:
kubectl get nodes
kubectl get svc
If these commands return results, your cluster is reachable. If they hang or fail, check for:
- Incorrect API server URL in your kubeconfig
- Firewalls or VPN requirements blocking traffic
- Network policies that restrict access to the Kubernetes API server
Debug Configuration Problems
When networking isn’t the issue, inspect your kubeconfig. Use:
kubectl config view
to confirm cluster names, server addresses, and context details. To check which identity Kubernetes sees, run:
kubectl auth whoami
This helps pinpoint RBAC or credential issues. If you need to fix an entry, update only the specific field instead of rewriting the whole cluster definition to avoid introducing errors.
Manage Permissions and Access
Connectivity doesn’t guarantee authorization. Many errors are tied to insufficient RBAC or IAM permissions. For example, AWS users need the eks:DescribeCluster
action to generate kubeconfigs for EKS. To test access, run:
kubectl auth can-i <verb> <resource>
Managing RBAC across a large fleet is complex. Platforms like Plural simplify this by using Kubernetes Impersonation, mapping your console identity to cluster roles and streamlining access across environments.
Key Security Best Practices
- Treat kubeconfig files as sensitive credentials.
- Avoid storing tokens or passwords directly—use credential plugins that fetch them dynamically.
- Only source kubeconfigs from trusted origins; a malicious file could expose local files or inject harmful behavior.
- Apply least privilege by assigning limited roles per context instead of relying on
cluster-admin
.
Using a centralized control plane such as Plural reduces the need for developers to manage raw kubeconfig files, lowering the attack surface while keeping access secure and auditable.
Simplify Cluster Management with Plural
Managing kubeconfig files and contexts with kubectl
is effective for a few clusters, but this approach doesn't scale. As your fleet grows, manual configuration becomes prone to errors, security inconsistencies, and operational bottlenecks. Platform teams find themselves spending more time distributing credentials and debugging access issues than enabling developers. The lack of a central control plane makes it difficult to enforce standards, audit access, or get a clear picture of the health and security posture of the entire environment. This is where a dedicated fleet management platform becomes essential. Plural provides a unified control plane to streamline operations, enforce standards, and secure your entire Kubernetes environment at scale. It addresses the core challenges of multi-cluster management by automating deployment, access control, and security compliance across your infrastructure.
Get a Single-Pane-of-Glass View
Plural’s embedded dashboard offers a centralized view for managing all your Kubernetes clusters, removing the need to constantly switch contexts or handle individual kubeconfig files. This single-pane-of-glass interface provides secure, SSO-integrated access to any cluster, regardless of its location. Because all communication is proxied through the Plural agent, you can troubleshoot workloads in private or on-prem environments without configuring VPNs or complex network routes. This simplifies the operational overhead for platform teams and gives developers secure, ad-hoc access for debugging, all from one consistent UI.
Integrate Authentication and Access Control
Managing permissions across a fleet of clusters is a significant challenge. Plural centralizes Role-Based Access Control (RBAC) by connecting cluster permissions to your existing identity provider. You can define access policies once and apply them consistently across all clusters using a GlobalService
resource. This GitOps-based approach ensures that every cluster has the same baseline for RBAC, reducing configuration drift and strengthening your security posture. Instead of manually applying ClusterRoleBindings
on each cluster, you can manage permissions as code, making access control auditable, versioned, and scalable.
Automate Security and Compliance
Plural’s agent-based architecture is designed with security as a core principle. The agent, installed on each workload cluster, initiates all communication with the control plane, meaning traffic is unidirectional and egress-only. This allows your clusters to operate within secure, private networks without exposing any ingress points, a key requirement for many compliance standards. This model also eliminates the need to store cluster credentials in a central location, significantly reducing the attack surface. By automating secure connectivity and access, Plural removes the burden of manually managing kubeconfigs and network policies.
Run Multi-Cluster Operations with Ease
Plural is built to manage Kubernetes at scale, whether you have ten clusters or hundreds. Its API-driven, GitOps-based workflow for continuous deployment ensures that you can roll out applications and configuration changes consistently across your entire fleet. Plural integrates with native cluster APIs for providers like EKS, AKS, and GKE, as well as on-prem solutions, providing a uniform management layer for hybrid and multi-cloud environments. This allows platform teams to automate complex operations and provide developers with self-service capabilities, all while maintaining centralized control and visibility.
Related Articles
- Kubernetes Multi-Cluster Management: A Practical Guide
- What Is a Kubernetes Cluster? A Beginner's Guide
- Kubernetes Multi-Cluster: A Comprehensive Guide
Unified Cloud Orchestration for Kubernetes
Manage Kubernetes at scale through a single, enterprise-ready platform.
Frequently Asked Questions
Why can't I just use kubectl
for everything? When do I need a tool like Plural? kubectl
is excellent for direct, imperative interaction with a single Kubernetes cluster. It's the right tool for debugging a specific pod or manually inspecting a resource. However, its effectiveness diminishes as you scale to a fleet of clusters. Plural provides the declarative, GitOps-based management layer needed to apply configurations, enforce security policies, and manage access consistently across all your clusters. It solves the fleet management problem that kubectl
on its own was not designed to address.
What are the biggest security risks of managing kubeconfig
files manually? The primary risk is credential exposure. A kubeconfig
file contains sensitive credentials that grant direct access to a cluster's API server. If a developer's laptop is compromised or a file is accidentally committed to a Git repository, that access is compromised. Manually distributing and revoking these files is also error-prone and difficult to audit, making it hard to ensure that only authorized personnel have access, especially in large teams.
How does Plural provide access to clusters in private networks without a VPN? Plural uses an agent-based architecture. A lightweight agent installed on each managed cluster establishes a secure, unidirectional connection out to the Plural control plane. All communication, including dashboard access and proxied kubectl
commands, travels through this egress-only tunnel. This design means your cluster's API server never needs to be exposed to the public internet, and you don't need to configure or maintain VPNs for your team to gain access.
Can I still use kubectl
if my organization adopts Plural? Yes, absolutely. Plural complements kubectl
rather than replacing it. The Plural dashboard includes an embedded web terminal that provides a secure, authenticated kubectl
session for any managed cluster. This allows you to retain the power and familiarity of the command line for ad-hoc tasks without the security and operational burden of managing local kubeconfig
files.
How does Plural simplify managing RBAC policies across many clusters? Managing RBAC with kubectl
on a per-cluster basis often leads to inconsistent permissions and configuration drift. Plural solves this by enabling a GitOps workflow for access control. You can define your RBAC policies, such as ClusterRoleBindings
, in a central Git repository and use a Plural GlobalService
to ensure they are automatically and consistently applied across your entire fleet. This makes your access policies version-controlled, auditable, and scalable.
Newsletter
Join the newsletter to receive the latest updates in your inbox.