Get Kubeconfig File for your Virtual Cluster
To "use" a virtual cluster, as in connect to the virtual cluster Kubernetes API server, you can manually construct a kubeconfig or use the vCluster CLI to automatically create and update your kubeconfig.
Retrieve via vcluster CLI
The easiest way to retrieve a kubeconfig for your virtual cluster is by using the vCluster CLI.
You can update your current kubeconfig on your local machine by running the following command:
vcluster platform connect my-vcluster
If you want to print the kubeconfig file, you can append --print
to the command:
vcluster platform connect my-vcluster --print
If you have multiple virtual clusters with the same name, a sub-menu will be presented that allows you to specify the vcluster by project.
If you have direct cluster endpoints or ingress endpoints for virtual clusters enabled, the resulting kubeconfig will look different.
Create the Kubeconfig Without the CLI
You can also create a kubeconfig manually by creating an access key for your user first and then using the following template, with the following placeholders:
- $PROJECT: the project the virtual cluster is in
- $VIRTUAL_CLUSTER: the virtual cluster name to connect to
- $LOFT_HOST: the loft host you connect to
- $ACCESS_KEY: the access key to use
Then replace these placeholders in the following template and save it as my-kubeconfig.yaml
:
apiVersion: v1
kind: Config
clusters:
- cluster:
server: https://$LOFT_URL/kubernetes/project/$PROJECT/virtualcluster/$VIRTUAL_CLUSTER
# Optional: if the server uses an insecure certificate
# insecure-skip-tls-verify: true
name: loft
contexts:
- context:
cluster: loft
user: loft
name: loft
current-context: loft
users:
- name: loft
user:
token: $ACCESS_KEY
You can then access your virtual cluster by using the freshly created kubeconfig file:
KUBECONFIG=my-kubeconfig.yaml kubectl get namespaces