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 kubectlOr 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.kubectlOr manually: download the executable file from the releases page and add its path to the PATH environment variable.
Verifying the installation
kubectl version --clientExpected output:
Client Version: v1.xx.x
Kustomize Version: v5.x.xObtaining 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:
- Go to the dashboard of the created cluster
- In the “Information” tab, click ‘Connect’
- In the window that opens, click “Download kubeconfig”
- Save the file to your local computer
First Connection
Run the following command to check the connection:
kubectl --kubeconfig kubeconfig.yaml get nodesIf 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.xThe 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.yamlAfter that, kubectl will use the specified file automatically:
kubectl get nodesTo 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/configkubectl 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-contextsSwitch 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/configBasic 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-namespaceCreating 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-appLogs 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/shDeleting 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-namespaceUseful 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 -wCommand autocompletion
kubectl supports autocompletion for bash and zsh.
bash
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrczsh
echo 'source <(kubectl completion zsh)' >> ~/.zshrc
source ~/.zshrcAfter configuration, press “Tab” to autocomplete commands, resource names, and flags.
All articles in this section
- Kubernetes (K8s) – An Overview of the Managed Kubernetes Service
- Kubernetes Basics – Key Concepts: Cluster, Nodes, Pods, Services
- Creating and Configuring a Cluster – Master Node Configuration, Networking, and Worker Groups
- Connecting to the Cluster and Working with kubectl – You are here
- Cluster management – adding nodes, changing configuration, updating, and deleting
- Networking and load balancers – network model, external and internal load balancers
- 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.