Skip to main content

kubectl Cheatsheet

Cluster and Nodes#

# Inspect the endpoints for the cluster
kubectl cluster-info
# List all the nodes in the cluster
kubectl get nodes [-o wide]
# Describe a cluster node.
kubectl describe node <node-name>

Deployment#

# create a Deployment
kubectl create deploy <name> --image=image [OPTIONS] -- [COMMAND] [args]
# OPTIONS
-r or --replicas set the number of replicas
-n or --namespace set the namespace to run
--port expose the container port
# Ex: create a deployment with a single pod in the lab namespace
kubectl create deploy nginx-deploy --image=nginx -n lab

Pod#

# create a Pod
kubectl run <pod-name> --image=image [OPTIONS] -- [COMMAND] [args...]
# OPTIONS
--restart set the restart policy [Always, OnFailure, Never]
--dry-run dry run the command [none, client, server]
-it open an interactive shell to the container
# Ex:
kubectl run -it busybox-pod --image=busybox --restart=Never

Service#

# expose a Deployment through a Service
kubectl expose deploy <name> --port=<port> [--target-port=<port>] [OPTIONS]
# OPTIONS
--protocol set the network protocol [TCP, UDP, SCTP]
--type set the type of service [ClusterIP, NodePort, LoadBalancer]
# Ex:
kubectl expose deploy nginx-deploy --port=80 --target-port=80

ConfigMap#

# create a ConfigMap
kubectl create configmap <name> [OPTIONS]
# OPTIONS
--from-file set path to file with key-value pairs
--from-literal set key-value pair from command-line
# Ex:
kubectl create configmap app-config --from-literal=APP_PORT=8080

Secret#

# create a Secret
kubectl create secret generic <name> [OPTIONS]
# OPTIONS
--from-file set path to file with the sensitive key-value pairs
--from-literal set key-value pair from command-line
# Ex:
kubectl create secret generic app-secret --from-literal=DB_PASSWORD=topsecret

Namespace#

# create a Namespace
kubectl create ns <name>
# Ex:
kubectl create ns lab
# get all the pods in the lab Namespace
kubectl get po -n lab

kubectl command pattern#

# create resources
kubectl create RESOURCE NAME [OPTIONS]
# describe resources
kubectl describe RESOURCE NAME
# get resources
kubectl get RESOURCE NAME [-o yaml]
# edit resources
kubectl edit RESOURCE NAME [-o yaml]
# label resources
kubectl label RESOURCE NAME [PARAMS]
# port-forward to resources
kubectl port-forward RESOURCE/NAME [PARAMS]
# download logs
kubectl logs RESOURCE/NAME [FLAGS]
# delete resources
kubectl delete RESOURCE NAME