Connecting to a Cluster

Use the kubectl package to connect to a Kubernetes cluster created with the libvirt provider.

After creating a Kubernetes cluster, two Kubernetes configuration (kubeconfig) files are created to connect to the cluster using the kubectl command.

One file provides direct access to the true Kubernetes API server endpoint. This kubeconfig file is saved as $HOME/.kube/kubeconfig.cluster_name.local. You can use this file to access a cluster created on the localhost, or to access a cluster created on a remote libvirt host.

The second kubeconfig file provides access to a dedicated tunnel implemented with SLiRP to access the cluster on remote systems. This file is saved as $HOME/.kube/kubeconfig.cluster_name.vm. If the cluster is started on a remote system, and you want to log into that remote system to access the cluster, you need to copy this second kubeconfig to the remote system.

Install kubectl on the host on which you want to access the cluster, either the localhost, or the remote libvirt system.

These steps, unless explicitly mentioned, are to be performed on the host on which you want to access the cluster (either the localhost or a remote libvirt system).

  1. (Optional) Copy the kubeconfig file.

    If you created the cluster on a remote system, and you want to log in to the remote system to access the cluster, copy the kubeconfig file ending in .vm from the localhost to the remote system. The file is available on the localhost at:

    $HOME/.kube/kubeconfig.cluster_name.vm

    Replace cluster_name with the name you used to create the cluster. The default is ocne.

    Tip:

    If you copy the file to the remote system as $HOME/.kube/config, you don't need to set the $KUBECONFIG environment variable to access the cluster on the remote host.

  2. Install the kubectl package.
    sudo dnf install kubectl
  3. Set the kubeconfig file location using an environment variable.

    For a cluster running on the localhost, use:

    export KUBECONFIG=$HOME/.kube/kubeconfig.cluster_name.local
  4. (Optional) Persist the environment variable.

    Add the environment variable to the .bashrc file. For example:

    echo 'export KUBECONFIG=$HOME/.kube/kubeconfig.cluster_name'.local >> $HOME/.bashrc
  5. Verify that you can use kubectl to connect to the cluster.

    For example:

    kubectl get deployments --all-namespaces