kubernetes version: 1.22 - Date: June 2022
Kubectl Autocomplete
Setup autocomplete in bash
apt install bash-completion
source <(kubectl completion bash)
Context and Configuration
Show kubeconfig settings
kubectl config view
Set the default context (cluster)
kubectl config use-context CLUSTER-NAME
Creating Objects
Create resource(s)
kubectl apply -f FILE.yaml [FILE2.yaml]
Create resource(s) from a directory
kubectl apply -f PATH/DIR/
Create resource(s) from url
kubectl apply -f https://URL
Run a single pod
kubectl run NAME --image=IMAGE
Run a single pod and connect into itkubectl run --ti NAME --image=IMAGE
Viewing, Finding Resources
List main resources
kubectl get all
List all services in the namespace
kubectl get services [--namespace NS]
List all pods in all namespaces
kubectl get pods --all-namespaces
List a particular deployment
kubectl get deployment DEPLOYMENT
Get the version label of all pods with label version=v1
kubectl get pods --selector=version=v1
Describe commands with verbose output
kubectl describe nodes NODE
kubectl describe pods POD
Updating Resources
Rolling update
kubectl rolling-update POD ARGS
Rolling update pods of app-v1:
kubectl rolling-update app-v1 -f app-v2.yaml
Change the name of the resource and update the image:
kubectl rolling-update app-v1 app-v2 --image=image:v2
Force replace, delete and then re-create the resource. Will cause a service outage.
kubectl replace --force -f POD.json