Skip to main content
Version: main 🚧

Check connection to a vCluster or host cluster

When working with Kubernetes clusters, you can determine if you're connected to a vCluster or host cluster using the following methods:

Check for vCluster node labels​

Retrieve the nodes and check for a vCluster node label:

kubectl get nodes --show-labels | grep vcluster

If this command returns results, you're likely connected to a vCluster. This method works in most cases but might not be entirely reliable.

Examine the current context​

For scripting purposes, you can check the current context:

kubectl config current-context

If the output contains "vcluster" (for example, vcluster_vcluster-xx5gk), you're connected to a vCluster.

Parse all contexts​

To get a more comprehensive view, you can list all contexts and search for "vcluster":

kubectl config get-contexts | grep vcluster

This shows all vCluster contexts available.

Implement check in a script​

Here's a simple bash script that combines these methods:

#!/bin/bash
# Check current context
current_context=$(kubectl config current-context)
if [[ $current_context == *"vcluster"* ]]; then
echo "Connected to vCluster: $current_context"
exit 0
fi

# Check for vCluster node labels
vcluster_nodes=$(kubectl get nodes --show-labels | grep vcluster)
if [ ! -z "$vcluster_nodes" ]; then
echo "Connected to vCluster (detected via node labels)"
exit 0
fi

echo "Connected to host cluster"
exit 1

This script first checks the current context name. If that doesn't indicate a vCluster, it then checks for vCluster node labels. If neither method detects a vCluster, it assumes you're connected to the host cluster.

Additional tools​

While not directly applicable for scripting, the following tools can help with manual context switching and management:

  • Install kubectl plugins using Krew
  • Use kubectx for easier context switching

These tools are particularly useful when working with multiple clusters or contexts in a development environment.