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:
- Method 1: Check node labels - Examine node labels for vCluster-specific indicators
- Method 2: Examine the current context - Look for vCluster naming patterns in your kubectl context
- Method 3: List all contexts - Get a comprehensive view of available vCluster contexts
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:
These tools are particularly useful when working with multiple clusters or contexts in a development environment.