0% found this document useful (0 votes)
21 views

Kubernetes Cheat Sheet

The document provides a cheat sheet for common Kubernetes commands for installing Kubernetes, applying configurations, creating and managing resources like deployments and services, setting labels and annotations, viewing logs and resource usage.

Uploaded by

kevin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Kubernetes Cheat Sheet

The document provides a cheat sheet for common Kubernetes commands for installing Kubernetes, applying configurations, creating and managing resources like deployments and services, setting labels and annotations, viewing logs and resource usage.

Uploaded by

kevin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Kubernetes Cheat Sheet

Installation Apply configuration manifests


Command Description
Install the kubectl command line tool to interact with the Kubernetes API:
https://kubernetes.io/docs/tasks/tools/#kubectl kubectl apply -f <file> Apply a manifest from a file
kubectl apply -f <dir> Apply all manifests in a directory
Enable autocompletion in bash:
kubectl apply -k <dir> Apply resources from a kustomize directory
composer completion bash | sudo tee /etc/bash_completion.d/kubectl

Create resources manually


Global flags
Command Description
Flag Description
kubectl run <name> --image=<image> Start a pod
--namespace <namespace> The name of the namespace to use
kubectl create deployment <name>
--context <context> The name of the context to use Create a deployment
--image=<image>
--help Show information about a given command kubectl expose pod <pod> --port=<port> Create a service for an existing pod
kubectl expose deployment <name>
Create a service for an existing deployment
Context and configuration --port=<port>
Command Description kubectl create ingress <name>
Create an ingress that routes traffic to a service
--rule=<host/path=svc:port>
kubectl config get-contexts List all contexts
kubectl create job <name>
kubectl config current-context Display the current context Create a job
--image=<image>
kubectl config use-context <context> Switch to another context kubectl create job <name>
Create a job from a cronjob
kubectl config delete-context <context> Delete the specified context from the kubeconfig --from=cronjob/<name>
kubectl create cronjob <name>
Create a cronjob, using a schedule in Cron format
--image=<image> --schedule=<schedule>
Display resources
kubectl create secret generic <name>
Command Description Create a secret containing <key> and <value>
--from-literal=<key>=<value>
List all resources of this type in the current kubectl create secret docker-registry
kubectl get <resource>
namespace <name> --docker-server=<server>
Create a secret for a Docker registry
kubectl get <resource> -o wide List all resources with more details --docker-username=<username>
--docker-password=<password>
kubectl get <resource> -A List all resources of this type in all namespaces
kubectl get <resource> <name> List a particular resource
Generate YAML configuration manifests
kubectl get <resource> <name> -o yaml Print a particular resource in YAML format
Command Description
kubectl get <resource> <name> -l List resources where label <key1> contains
kubectl create deployment <name>
<key1>=<value1> <value1>
--image=<image> --dry-run=client -o Generate a deployment manifest
kubectl describe <resource> Show detailed information about a resource yaml
kubectl expose deployment <name>
Generate a service manifest for a deployment
--port=<port> --dry-run=client -o yaml

© Nic Wortel, Software Consultant & Trainer - Last updated on December 20, 2023 - Find more cheat sheets at https://nicwortel.nl/cheat-sheets
Edit resources Execute commands
Command Description Command Description
kubectl edit <resource> <name> Edit a resource in a text editor kubectl exec <pod> -- <command> Execute a command in a running pod
kubectl set image <resource> <name> kubectl exec -it <pod> -- sh Open a shell in a running pod
Update the image of a container in a pod
<container>=<image>

View logs
Set labels and annotations Command Description
Command Description
kubectl logs <pod> Print the logs for a pod
kubectl label <resource> <name>
Add a label to a resource kubectl logs -f <pod> Print the logs for a pod and keep streaming
<key>=<value>
kubectl annotate <resource> <name>
Add an annotation to a resource
<key>=<value> Resource usage
Command Description
Delete resources kubectl top node Show resource (CPU/memory) usage of nodes
Command Description kubectl top pod Show resource (CPU/memory) usage of pods
kubectl delete <resource> <name> Delete a particular resource

kubectl delete <resource> --all


Delete all resources of a particular type in the Other commands
current namespace
Command Description
kubectl delete -f <file> Delete a resource from a file
kubectl version Show the version of the client and server
kubectl api-resources Print the supported API resources on the server
Manage deployments
Command Description
kubectl rollout status deployment
Show the status of a deployment rollout
<name>
kubectl rollout history deployment
View the rollout history of a deployment
<name>
kubectl rollout undo deployment <name> Undo a previous rollout deployment
kubectl rollout restart deployment
Restart a deployment
<name>
kubectl scale deployment <name>
Scale a deployment to <n> replicas
--replicas=<n>
kubectl autoscale deployment <name> Autoscale a deployment between <n> and <n>
--min=<min> --max=<max> replicas

© Nic Wortel, Software Consultant & Trainer - Last updated on December 20, 2023 - Find more cheat sheets at https://nicwortel.nl/cheat-sheets

You might also like