kubectl Cheatsheet | Kubernetes

ยท 1710 words ยท 9 minute read

kubectl ๐Ÿ”—

kubectl is a command-line interface for running commands against Kubernetes clusters.

List information about a resource with more details:

kubectl get pod|service|deployment|ingress|... -o wide

Update specified pod with the label ‘unhealthy’ and the value ’true’:

kubectl label pods name unhealthy=true

List all resources with different types:

kubectl get all

Display resource (CPU/Memory/Storage) usage of nodes or pods:

kubectl top pod|node

Print the address of the master and cluster services:

kubectl cluster-info

Display an explanation of a specific field:

kubectl explain pods.spec.containers

Print the logs for a container in a pod or specified resource:

kubectl logs pod_name

Run command in an existing pod:

kubectl exec pod_name -- ls /

kubectl run ๐Ÿ”—

Run pods in Kubernetes. Specifies pod generator to avoid deprecation error in some K8S versions.

Run an nginx pod and expose port 80:

kubectl run nginx-dev --image=nginx --port 80

Run an nginx pod, setting the TEST_VAR environment variable:

kubectl run nginx-dev --image=nginx --env="TEST_VAR=testing"

Show API calls that would be made to create an nginx container:

kubectl run nginx-dev --image=nginx --dry-run=none|server|client

Run an Ubuntu pod interactively, never restart it, and remove it when it exits:

kubectl run temp-ubuntu --image=ubuntu:22.04 --restart=Never --rm -- /bin/bash

Run an Ubuntu pod, overriding the default command with echo, and specifying custom arguments:

kubectl run temp-ubuntu --image=ubuntu:22.04 --command -- echo argument1 argument2 ...

kubectl taint ๐Ÿ”—

Update the taints on nodes.

Apply taint to a node:

kubectl taint nodes node_name label_key=label_value:effect

Remove taint from a node:

kubectl taint nodes node_name label_key:effect-

Remove all taints from a node:

kubectl taint nodes node_name label_key-

kubectl logs ๐Ÿ”—

Show logs for containers in a pod.

Show logs for a single-container pod:

kubectl logs pod_name

Show logs for a specified container in a pod:

kubectl logs --container container_name pod_name

Show logs for all containers in a pod:

kubectl logs --all-containers=true pod_name

Stream pod logs:

kubectl logs --follow pod_name

Stream logs for a specified container in a pod:

kubectl logs --follow --container container_name pod_name

Show pod logs newer than a relative time like 10s, 5m, or 1h:

kubectl logs --since=relative_time pod_name

Show the 10 most recent logs in a pod:

kubectl logs --tail=10 pod_name

kubectl get ๐Ÿ”—

Get Kubernetes objects and resources.

Get all namespaces in the current cluster:

kubectl get namespaces

Get nodes in a specified [n]amespace:

kubectl get nodes --namespace namespace

Get pods in a specified [n]amespace:

kubectl get pods --namespace namespace

Get deployments in a specified [n]amespace:

kubectl get deployments --namespace namespace

Get services in a specified [n]amespace:

kubectl get services --namespace namespace

Get all resources in a specified [n]amespace:

kubectl get all --namespace namespace

Get Kubernetes objects defined in a YAML manifest [f]ile:

kubectl get --file path/to/manifest.yaml

kubectl expose ๐Ÿ”—

Expose a resource as a new Kubernetes service.

Create a service for a resource, which will be served from container port to node port:

kubectl expose resource_type resource_name --port=node_port --target-port=container_port

Create a service for a resource identified by a file:

kubectl expose -f path/to/file.yml --port=node_port --target-port=container_port

Create a service with a name, to serve to a node port which will be same for container port:

kubectl expose resource_type resource_name --port=node_port --name=service_name

kubectl rollout ๐Ÿ”—

Manage the rollout of a Kubernetes resource (deployments, daemonsets, and statefulsets).

Start a rolling restart of a resource:

kubectl rollout restart resource_type/resource_name

Watch the rolling update status of a resource:

kubectl rollout status resource_type/resource_name

Roll back a resource to the previous revision:

kubectl rollout undo resource_type/resource_name

View the rollout history of a resource:

kubectl rollout history resource_type/resource_name

kubectl create ๐Ÿ”—

Create a resource from a file or from stdin.

Create a resource using the resource definition file:

kubectl create -f path/to/file.yml

Create a resource from stdin:

kubectl create -f -

Create a deployment:

kubectl create deployment deployment_name --image=image

Create a deployment with replicas:

kubectl create deployment deployment_name --image=image --replicas=number_of_replicas

Create a service:

kubectl create service service_type service_name --tcp=port:target_port

Create a namespace:

kubectl create namespace namespace_name

kubectl edit ๐Ÿ”—

Edit Kubernetes resources.

Edit a pod:

kubectl edit pod/pod_name

Edit a deployment:

kubectl edit deployment/deployment_name

Edit a service:

kubectl edit svc/service_name

Edit a resource using a specific editor:

KUBE_EDITOR=nano kubectl edit resource/resource_name

Edit a resource in JSON format:

kubectl edit resource/resource_name --output json

kubectl label ๐Ÿ”—

Label Kubernetes resources.

Label a pod:

kubectl label pod pod_name key=value

Update a pod label by overwriting the existing value:

kubectl label --overwrite pod_name key=value

Label all pods in the namespace:

kubectl label pods --all key=value

Label pod identified by pod definition file:

kubectl label -f pod_defination_file key=value

Remove the label from a pod:

kubectl label pod pod_name key-

kubectl describe ๐Ÿ”—

Show details of Kubernetes objects and resources.

Show details of pods in a [n]amespace:

kubectl describe pods --namespace namespace

Show details of nodes in a [n]amespace:

kubectl describe nodes --namespace namespace

Show the details of a specific pod in a [n]amespace:

kubectl describe pods pod_name --namespace namespace

Show the details of a specific node in a [n]amespace:

kubectl describe nodes node_name --namespace namespace

Show details of Kubernetes objects defined in a YAML manifest [f]ile:

kubectl describe --file path/to/manifest.yaml

kubectl scale ๐Ÿ”—

Set a new size for a deployment, replica set, replication controller, or stateful set.

Scale a replica set:

kubectl scale --replicas=number_of_replicas rs/replica_name

Scale a resource identified by a file:

kubectl scale --replicas=number_of_replicas -f path/to/file.yml

Scale a deployment based on current number of replicas:

kubectl scale --current-replicas=current_replicas --replicas=number_of_replicas deployment/deployment_name

kubectl apply ๐Ÿ”—

Manages applications through files defining Kubernetes resources. It creates and updates resources in a cluster.

Apply a configuration to a resource by file name or stdin:

kubectl apply -f resource_filename

Edit the latest last-applied-configuration annotations of resources from the default editor:

kubectl apply edit-last-applied -f resource_filename

Set the latest last-applied-configuration annotations by setting it to match the contents of a file:

kubectl apply set-last-applied -f resource_filename

View the latest last-applied-configuration annotations by type/name or file:

kubectl apply view-last-applied -f resource_filename

kubectl replace ๐Ÿ”—

Replace a resource by file or stdin.

Replace the resource using the resource definition file:

kubectl replace -f path/to/file.yml

Replace the resource using the input passed into stdin:

kubectl replace -f -

Force replace, delete and then re-create the resource:

kubectl replace --force -f path/to/file.yml

kubectl delete ๐Ÿ”—

Delete Kubernetes resources.

Delete a specific pod:

kubectl delete pod pod_name

Delete a specific deployment:

kubectl delete deployment deployment_name

Delete a specific node:

kubectl delete node node_name

Delete all pods in a specified namespace:

kubectl delete pods --all --namespace namespace

Delete all deployments and services in a specified namespace:

kubectl delete deployments,services --all --namespace namespace

Delete all nodes:

kubectl delete nodes --all

Delete resources defined in a YAML manifest:

kubectl delete --filename path/to/manifest.yaml

kubetail ๐Ÿ”—

kubetail is an opensource utility to tail multiple Kubernetes pod logs at the same time.

Tail the logs of multiple pods (whose name starts with “my_app”) in one go:

kubetail my_app

Tail only a specific container from multiple pods:

kubetail my_app -c my_container

To tail multiple containers from multiple pods:

kubetail my_app -c my_container_1 -c my_container_2

To tail multiple applications at the same time separate them by comma:

kubetail my_app_1,my_app_2

kube-fzf ๐Ÿ”—

It is an opensource utility.

Shell commands for command-line fuzzy searching of Kubernetes Pods.

Get pod details (from current namespace):

findpod

Get pod details (from all namespaces):

findpod -a

Describe a pod:

describepod

Tail pod logs:

tailpod

Exec into a pod’s container:

execpod shell_command

Port-forward a pod:

pfpod port_number

minikube ๐Ÿ”—

Run Kubernetes locally.

Start the cluster:

minikube start

Get the IP address of the cluster:

minikube ip

Access a service named my_service exposed via a node port and get the URL:

minikube service my_service --url

Open the Kubernetes dashboard in a browser:

minikube dashboard

Stop the running cluster:

minikube stop

Delete the cluster:

minikube delete

Connect to LoadBalancer services:

minikube tunnel

kubeadm ๐Ÿ”—

Command-line interface for creating and managing Kubernetes clusters.

Create a Kubernetes master node:

kubeadm init

Bootstrap a Kubernetes worker node and join it to a cluster:

kubeadm join --token token

Create a new bootstrap token with a TTL of 12 hours:

kubeadm token create --ttl 12h0m0s

Check if the Kubernetes cluster is upgradeable and which versions are available:

kubeadm upgrade plan

Upgrade Kubernetes cluster to a specified version:

kubeadm upgrade apply version

View the kubeadm ConfigMap containing the cluster’s configuration:

kubeadm config view

Revert changes made to the host by ‘kubeadm init’ or ‘kubeadm join’:

kubeadm reset

kube-capacity ๐Ÿ”—

kube-capacity is an opensource CLI tool.

Provide an overview of resource requests, limits, and utilization in a Kubernetes cluster.

Combine the best parts of kubectl top and kubectl describe into a CLI focused on cluster resources.

List nodes including the total CPU and Memory resource requests and limits:

kube-capacity

Include pods:

kube-capacity -p

Include utilization:

kube-capacity -u

kubectx ๐Ÿ”—

kubectx is an opensource utility to manage and switch between kubectl contexts.

List the contexts:

kubectx

Switch to a named context:

kubectx name

Switch to the previous context:

kubectx -

Delete a named context:

kubectx -d name

kubens ๐Ÿ”—

kubens is an opensource utility to switch between Kubernetes namespaces.

List the namespaces:

kubens

Change the active namespace:

kubens name

Switch to the previous namespace:

kubens -

doctl kubernetes cluster ๐Ÿ”—

Manage Kubernetes clusters and view configuration options relating to clusters.

Create a Kubernetes cluster:

doctl kubernetes cluster create --count 3 --region nyc1 --size s-1vcpu-2gb --version latest cluster_name

List all Kubernetes clusters:

doctl kubernetes cluster list

Fetch and save the kubeconfig:

doctl kubernetes cluster kubeconfig save cluster_name

Check for available upgrades:

doctl kubernetes cluster get-upgrades cluster_name

Upgrade a cluster to a new Kubernetes version:

doctl kubernetes cluster upgrade cluster_name

Delete a cluster:

doctl kubernetes cluster delete cluster_name

doctl kubernetes options ๐Ÿ”—

Provides values available for use with doctl’s Kubernetes commands.

List regions that support Kubernetes clusters:

doctl kubernetes options regions

List machine sizes that can be used in a Kubernetes cluster:

doctl kubernetes options sizes

List Kubernetes versions that can be used with DigitalOcean clusters:

doctl kubernetes options versions

For more information, check out these docs ๐Ÿ”—

I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , Facebook , Telegram and GitHub .

Share: