Support Online
Skip to main content

Connecting to a Kubernetes Cluster

Kubernetes includes a fully managed control plane, high availability, and auto-scaling.
Integrates with standard Kubernetes toolchains (kubectl, API & CLI).

Required Tools

To control your cluster from a local machine or a server that you manage remotely, you need the following tools:

  • kubectl — The official command line tool of Kubernetes. It is used to connect to your cluster and perform operations. With kubectl version you can check that your installation is working and compatible with the version of your cluster.

    oaicite:1

  • Kubeconfig management — You can manually edit the Kubeconfig file or set context settings with kubectl config commands.

Specify Authentication (Token or Certificate)

Once the cluster is created, you need to add an authentication token or a certificate to your kubectl configuration file.

oaicite:3

Version Requirements

The following Kubernetes version requirements apply to creating tokens:

oaicite:4

  • Kubernetes 1.16 and later are supported.
  • In older versions, it may be necessary to use a certificate for authentication.

If older versions of Doctl or older versions of Kubernetes are used, a certificate is issued instead of a token.

oaicite:5

The kubeconfig file is downloaded from the provider's panel. After copying the file into ~/.kube/config:

To import your cluster's kubeconfig file:

kubectl get nodes

This downloads your cluster's kubeconfig file, merges it with the existing ~/.kube/config file, and automatically manages the token or certificate required for authentication.

In the background it works like this:

  • Revocable OAuth token: If you meet the version requirements listed above, you will receive an OAuth token. You can view this token from the Applications & API section in the control panel and cancel it if necessary.
  • Automatic certificate renewal: In older versions of Kubernetes or doctl, a certificate valid for 7 days will be generated. This certificate is automatically renewed and cannot be revoked.

You can switch to using tokens instead of certificates by upgrading Kubernetes clusters to newer patches or minor versions.

Downloading the Config File from the Control Panel

You can also download the cluster configuration file manually from the control panel.

  1. Click on the name of your cluster to go to the Overview tab.
  2. Download the kubeconfig file by clicking Download Config File in the Configuration section.
  3. The downloaded file is named <clustername>-kubeconfig.yaml.
  4. Put this file in the ~/.kube directory and pass it to the kubectl command with the parameter --kubeconfig.

For example:

kubectl --kubeconfig=~/.kube/<clustername>-kubeconfig.yaml get nodes

The method used for authentication depends on your Kubernetes provider.

  • Most providers include a token in the configuration file.

  • In older versions, certificate-based authentication can be used.

Connecting to the Cluster

Once the cluster configuration file is ready, you can create and manage clusters and deploy applications using kubectl.
For more information about kubectl commands and options, you can check out the official kubectl documentation.

Contexts

In Kubernetes, a context is a named group used to easily manage access parameters.
Each cluster's configuration file has a context section that contains cluster-specific values. This structure usually looks like this:

Below is a Kubernetes context configuration example:

contexts:
- context:
cluster: example-cluster
user: example-cluster-admin
name: example-cluster
current-context: example-cluster

When you use kubectl, the commands you run take effect on the default context.
If you want to use a different context, you need to add the --context flag.

For example:

kubectl get nodes --context=example-cluster

To check the current default context, use this command:

kubectl config current-context

If you get error Current-context is not set, you need to set a default context.

To list all available contexts, use:

kubectl config get-contexts

Terminal output will look like this:

CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* example-cluster example-cluster example-cluster-admin

The default context is specified with an asterisk (*) in the CURRENT column in the output. To change the default context to a different context, use the following command:

kubectl config use-context example-cluster

Namespaces

In Kubernetes, namespace is a way to divide cluster resources among multiple users.
It is very useful in scenarios where many users are working on the same cluster.

  • You can create more than one namespace within a cluster.
  • Resources within a namespace are hidden from other namespaces.

For more information, you can check out the Kubernetes Namespaces Walkthrough documentation.