kubectl Installation and Usage Guide for Kubernetes Engine
kubectl, also known as Kube Control, is a command-line utility designed to interact with Kubernetes clusters. It enables administrators and developers to deploy, manage, and monitor applications within a Kubernetes environment. By using the Kubernetes API, kubectl communicates with the cluster’s control plane and executes management tasks. With kubectl, you can create, update, and remove resources such as pods, services, and deployments directly from the command line.
This tutorial outlines how to install and use kubectl to manage a Kubernetes Engine cluster.
Prerequisites
- A deployed Kubernetes Engine cluster.
Install kubectl
The kubectl utility is available on major operating systems, including Windows, Linux, and macOS. In this section, you will learn how to install kubectl to connect and manage Kubernetes clusters.
Note that kubectl is not available in the apt or dnf repositories for Debian/Ubuntu or CentOS/RHEL distributions. You can install kubectl either via the Snap package manager or by manually adding the latest binary to your system.
Install kubectl Using Snap
On Linux, install kubectl using the Snap package manager:
$ sudo snap install kubectl --classic
Install kubectl Binary on Linux
To manually install kubectl, download the latest binary with:
$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Grant execution permissions:
$ chmod +x kubectl
Move the binary to the /usr/local/bin directory to make kubectl available system-wide:
$ sudo cp kubectl /usr/local/bin/
Install kubectl on Windows
On Windows, install Chocolatey or the Windows Package Manager to obtain kubectl. Open the Start Menu, launch a new PowerShell session, and choose one of the following methods:
Using Chocolatey:
> choco install kubernetes-cli
Using Windows Package Manager:
> winget install -e --id CNCF.kubectl
Manual installation on Windows:
Download the latest kubectl binary:
> Invoke-WebRequest -Uri "http://dl.k8s.io/release/$(Invoke-WebRequest -Uri http://dl.k8s.io/release/stable.txt).txt/bin/windows/amd64/kubectl.exe" -OutFile kubectl.exe
Copy the file into a directory included in your system’s PATH:
> copy "C:\path\to\kubectl.exe" "C:\bin"
Install kubectl on macOS
On macOS, first install the Homebrew package manager, then use it to install kubectl:
% brew install kubectl
Manual installation steps on macOS:
Download the latest binary:
% curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
Grant execute permissions:
% chmod +x kubectl
Move the file to /usr/local/bin to enable system-wide access:
% sudo cp kubectl /usr/local/bin/
Verify the kubectl Installation
Once kubectl is available as a system-wide command on your management machine, confirm that it works correctly by checking the installed version:
$ kubectl version
The output should display the client version of your kubectl installation.
Connect to a KE Cluster Using kubectl
To connect kubectl with a KE cluster, you need to download the cluster configuration file and set kubectl to use it by following these steps:
- Log in to the customer portal and navigate to the Kubernetes section.
- Select the Kubernetes cluster you want to access.
- On the cluster dashboard, click Download Configuration to save the YAML configuration file to your computer.
Open a new terminal session and switch to the directory where you downloaded the configuration file, for example:
$ cd ~/Downloads
Create the .kube directory in your user home folder if it does not already exist:
$ mkdir ~/.kube
Back up the original configuration file, then copy your downloaded KE YAML file into the .kube directory and rename it to config:
$ cp ~/Downloads/ke.yaml ~/.kube/config
Check if kubectl connects properly by listing the nodes in your KE cluster:
$ kubectl get nodes
If successful, the command will return a list of your cluster nodes.
You can also display detailed cluster information, including control plane component endpoints, using the following command:
$ kubectl cluster-info
This will print detailed information about your Kubernetes Engine cluster.
Use kubectl to Manage a KE Cluster
With kubectl, your interactions with the KE cluster are secure, authenticated, and follow Kubernetes best practices. In this section, you will use common kubectl commands to manage your KE cluster. These commands help with retrieving information, managing resources, scaling applications, checking the status of resources, and reviewing cluster logs.
In Kubernetes, resources are the fundamental components used to run and manage containerized applications. They serve as the building blocks of workloads and applications. Below are kubectl commands that demonstrate how to manage these resources.
Create Resources in the Cluster
Create a new deployment with a Docker image:
$ kubectl create deployment <deployment-name> --image=<image-name>
Create a resource using a YAML manifest file:
$ kubectl create -f <filename.yaml>
Update the image of a container in a deployment:
$ kubectl set image deployment/<deployment-name> <container-name>=<new-image>
List Cluster Resources
Kubernetes resources include nodes, pods, services, deployments, configMaps, and secrets. The kubectl get command lists these resources.
List all cluster nodes:
$ kubectl get nodes
List pods in the default namespace:
$ kubectl get pods
List pods in a specific namespace:
$ kubectl get pods -n <namespace>
List pods across all namespaces:
$ kubectl get pods --all-namespaces
Get all deployments in the cluster:
$ kubectl get deployments
List services in the cluster:
$ kubectl get services
Describe Cluster Resources
Use the kubectl describe command to view detailed information about resources.
Inspect a specific pod:
$ kubectl describe pod <pod-name>
View details about a specific node:
$ kubectl describe node <node-name>
Get detailed information about a service:
$ kubectl describe service <service-name>
Delete Resources in the Cluster
Remove resources with the kubectl delete command:
Delete a pod:
$ kubectl delete pod <pod-name>
Delete a service:
$ kubectl delete svc <service-name>
Delete a deployment:
$ kubectl delete deployment <deployment-name>
Create and Manage Namespaces
Namespaces divide a Kubernetes cluster into virtual sub-clusters, allowing role-based access and permissions to be applied per namespace.
Create a namespace:
$ kubectl create namespace <namespace-name>
List namespaces:
$ kubectl get namespaces
Delete a namespace:
$ kubectl delete namespace <namespace-name>
ConfigMap and Secret Management
ConfigMaps and Secrets allow Kubernetes to inject configuration data and sensitive values into containers at startup.
Create a ConfigMap from a file:
$ kubectl create configmap <configmap-name> --from-file=<file-path>:
List ConfigMaps:
$ kubectl get configmaps
Delete a ConfigMap:
$ kubectl delete configmap <configmap-name>
Create a Secret from literal values:
$ kubectl create secret generic <secret-name> --from-literal=key=value
List Secrets:
$ kubectl get secrets
Delete a Secret:
$ kubectl delete secret <secret-name>
Scale Applications
Scale deployments with the kubectl scale command:
$ kubectl scale deployment <deployment-name> --replicas=<desired-replicas>
Edit an existing deployment:
$ kubectl edit deployment <deployment-name>
View Cluster Logs
Use kubectl logs to review pod logs.
View logs for a specific pod:
$ kubectl logs <pod-name>
View logs for a specific container inside a pod:
$ kubectl logs <pod-name> -c <container-name>
Stream logs in real time:
$ kubectl logs -f <pod-name>
View logs from previous instances of a pod:
$ kubectl logs --previous <pod-name>
View pod logs with timestamps:
$ kubectl logs --timestamps <pod-name>
Advanced kubectl Commands
This section covers advanced kubectl commands that provide greater control and flexibility when working with Kubernetes clusters.
Run Commands Within Pods
The kubectl exec command lets you check the status and contents of containers in your Kubernetes cluster. It is useful for inspecting and troubleshooting applications running inside pods.
Basic syntax for executing a command inside a pod:
$ kubectl exec -it <pod-name> -c <container-name> -- <command>
Explanation of the parameters:
- -it: Starts an interactive terminal shell.
- <pod-name>: The name of the pod.
- -c <container-name>: Specifies the container within the pod.
- — <command>: The command to run inside the container.
For example, to open an interactive shell in a container:
$ kubectl exec -it <pod-name> -c <container-name> -- /bin/sh
List files in a directory of the container:
$ kubectl exec -it <pod-name> -c <container-name> -- ls /app
Enable Port Forwarding for Local Access
Port forwarding allows access to Kubernetes applications from your local machine, making it useful for development and testing.
Basic syntax for port forwarding:
$ kubectl port-forward <service-name> <local-port>:<service-port>
For example, forward traffic from service my-service on port 8000 to port 8080 locally:
$ kubectl port-forward my-service 8080:8000
After running this command, you can access my-service at http://localhost:8080.
Manage Resource Quotas
Resource quotas control and allocate resources within namespaces in a Kubernetes cluster.
View resource quotas in a namespace:
$ kubectl get resourcequota -n <namespace>
View usage details of quotas in a namespace:
$ kubectl describe resourcequota -n <namespace>
Edit an existing resource quota:
$ kubectl edit resourcequota <resource-quota-name> -n <namespace>
Delete a resource quota in a namespace:
$ kubectl delete resourcequota <resource-quota-name> -n <namespace>
Drain Nodes for Maintenance
When performing updates, patches, or other maintenance on a node, kubectl allows you to safely drain it.
List cluster nodes:
$ kubectl get nodes
Mark the chosen node as unschedulable to prevent new workloads from being assigned to it:
$ kubectl cordon <node-name>
Drain the node to remove its running pods, which are rescheduled to other nodes:
$ kubectl drain <node-name>
Common flags for kubectl drain include:
--force: Forcefully drain the node.--timeout: Define a timeout period for the operation.--ignore-daemonsets: Evict DaemonSet pods during draining.
Mark the node schedulable again after completing maintenance:
$ kubectl uncordon <node-name>
Conclusion
In this guide, you learned how to install kubectl on your local system and configure it to connect with Kubernetes Engine. You also practiced managing resources, scaling applications, monitoring statuses, and viewing logs in a Kubernetes cluster. Mastering kubectl is a vital skill for system administrators working with Kubernetes environments.


