Connecting to the cluster and using kubectl

Once the cluster is created, you can connect to it using kubectl, a command-line tool for managing Kubernetes. With kubectl, you can create and delete resources, view the cluster status, read application logs, and perform other operations.

If you’re not familiar with the basics of Kubernetes, start with the article “Kubernetes Basics”.

Installing kubectl

Linux (Ubuntu and other distributions)

Installing kubectl on Ubuntu and other Linux distributions:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

macOS

Via Homebrew (package manager):

brew install kubectl

Or manually (Apple Silicon):

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

Windows

Install kubectl on Windows using winget (the package manager):

winget install Kubernetes.kubectl

Or manually: download the executable file from the releases page and add its path to the PATH environment variable.

Verifying the installation

kubectl version --client

Expected output:

Client Version: v1.xx.x
Kustomize Version: v5.x.x

Obtaining the kubeconfig

A kubeconfig is a configuration file that contains authentication credentials and the API server address (Application Programming Interface) for your Kubernetes cloud cluster. Without it, kubectl will not be able to connect to the cluster.

To download the kubeconfig:

  1. Go to the dashboard of the created cluster
  2. In the “Information” tab, click ‘Connect’
  3. In the window that opens, click “Download kubeconfig”
  4. Save the file to your local computer

First Connection

Run the following command to check the connection:

kubectl --kubeconfig kubeconfig.yaml get nodes

If the connection was successful, you will see a list of nodes (server groups) in your K8s cluster:

NAME                                    STATUS   ROLES    AGE   VERSION
dc5bca-client-c31fdf-xkd6w-jqpnc       Ready    <none>   2m    v1.xx.x
dc5bca-client-c31fdf-xkd6w-rq847       Ready    <none>   70s   v1.xx.x

The Ready status means that the nodes are operational and ready to handle traffic. If a node has a “NotReady” status, it most likely lacks sufficient resources. In this case, please contact technical support for troubleshooting.

Configuring kubectl for Convenience

The kubeconfig Environment Variable

To avoid having to specify --kubeconfig in every command, set the path to the file via an environment variable:

export KUBECONFIG=/path/to/kubeconfig.yaml

After that, kubectl will use the specified file automatically:

kubectl get nodes

To preserve this setting across sessions, add the line export KUBECONFIG=... to the ~/.bashrc (Linux) or ~/.zshrc (macOS) file.

Moving to the Default Location

An alternative method is to move the kubeconfig to the default kubectl directory:

mkdir -p ~/.kube
cp kubeconfig.yaml ~/.kube/config

kubectl automatically looks for the configuration in ~/.kube/config if the KUBECONFIG variable is not set.

Working with Multiple Clusters

If you have multiple clusters, you can combine their kubeconfig files and switch between them using contexts.

View available contexts:

kubectl config get-contexts

Switch to a different context:

kubectl config use-context <context-name>

To combine multiple kubeconfig files:

export KUBECONFIG=~/.kube/config:/path/to/second-kubeconfig.yaml
kubectl config view --merge --flatten > ~/.kube/merged-config
mv ~/.kube/merged-config ~/.kube/config

Basic kubectl Commands

Below are the main kubectl commands for everyday work with the cluster.

Viewing Resources

# List cluster nodes
kubectl get nodes

# List pods in all namespaces
kubectl get pods --all-namespaces

# List pods in a specific namespace
kubectl get pods -n my-namespace

# Detailed information about a pod
kubectl describe pod <name-pod>

# List services
kubectl get svc

# List all resources in a namespace
kubectl get all -n my-namespace

Creating and Applying Resources

# Apply a manifest from a file
kubectl apply -f manifest.yaml

# Apply all manifests from a directory
kubectl apply -f ./manifests/

# Create a namespace
kubectl create namespace my-app

Logs and Debugging

# Pod logs
kubectl logs <name-pod>

# Real-time logs
kubectl logs -f <name-pod>

# Logs for a specific container in a pod
kubectl logs <name-pod> -c <name-container>

# Run a command inside a pod
kubectl exec -it <name-pod> -- /bin/sh

Deleting resources

# Delete a resource using a manifest
kubectl delete -f manifest.yaml

# Delete a pod
kubectl delete pod <name-pod>

# Delete all pods in a namespace
kubectl delete pods --all -n my-namespace

Useful flags

  • -o wide – extended output with additional columns
  • -o yaml – output in YAML format (a language for storing information)
  • -o json – output in JSON format (a text-based data exchange format)
  • -w / --watch – monitor changes in real time
  • -n <namespace> – specify a namespace (default is default)

Example:

kubectl get pods -o wide -w

Command autocompletion

kubectl supports autocompletion for bash and zsh.

bash

echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc

zsh

echo 'source <(kubectl completion zsh)' >> ~/.zshrc
source ~/.zshrc

After configuration, press “Tab” to autocomplete commands, resource names, and flags.

All articles in this section

  1. Kubernetes (K8s) – An Overview of the Managed Kubernetes Service
  2. Kubernetes Basics – Key Concepts: Cluster, Nodes, Pods, Services
  3. Creating and Configuring a Cluster – Master Node Configuration, Networking, and Worker Groups
  4. Connecting to the Cluster and Working with kubectl – You are here
  5. Cluster management – adding nodes, changing configuration, updating, and deleting
  6. Networking and load balancers – network model, external and internal load balancers
  7. Limits, quotas, and constraints – platform constraints, what can and cannot be changed

If you have any questions, please submit a ticket via the account dashboard (under “Help and Support”). And if you’d like to discuss this article or our products, we’d love to see you in our Telegram community.