kubectl commands - ishankakde/Kubernetes GitHub Wiki

create

kubectl run nginx --image nginx kubectl run nginx --image=nginx kubectl run nginx --image=nginx -n=dev_namespace

kubectl run redis --image=redis --dry-run=client -o yaml kubectl run redis --image=redis --dry-run=client -o yaml > pod-definition.yaml (creates a pod definition / manifest file)

kubectl create -f pod-definition.yaml kubectl create -f deployment-definition.yaml kubectl create deployment httpd-frontend --image=httpd:2.4-alpine --replicas=3 kubectl create deployment httpd-frontend --image=httpd:2.4-alpine --replicas=3 --dry-run=client -o yaml > deployment-definition.yaml kubectl create -f replicaset-definition.yaml

kubectl create namespace [namespace-name]

kubectl create service -> commands to be added check course #32

exec

kubectl exec [ubuntu-pod] -- whoami (execute command run command inside the container)

edit

kubectl edit pod [pod-name] (this command opens an VI editor to make modifications) kubectl edit rs [replicaset-name] delete old pods for the change to take effect, new pods will automatically recreated

kubectl edit deployment [deployment-name] (since the pod template is child to deployment, with every change deployment will automatically delete old pod and create new pod with new changes)

Or If the pod definition file is available, update the file using VI editor and then execute below command

kubectl apply -f pod-definition.yaml

Or If the pod definition file is not available, use current pod to generate file and then make changes, then delete pod and recreate

kubectl get pod [pod-name] -o yaml > pod-definition.yaml kubectl create -f pod-definition.yaml

replace

kubectl replace --force -f updated-pod-definition.yaml (this command deletes the existing pod and creates a new pod per the updated definition file)

version

kubectl version kubectl version --client kubectl cluster-info

get

kubectl get pods kubectl get po kubectl get po -A kubectl get pods -o wide (provides additional node information)

kubectl get nodes kubectl get no kubectl get no -A

kubectl get service kubectl get svc

kubectl get deployment kubectl get deploy

kubectl get replicationcontroller (deprecated) kubectl get replicaset kubectl get rs

kubectl get all

namespaces

kubectl get namespaces kubectl get pods --namespace=[namespace_name] kubectl get service -n=[namespace_name] kubectl get pods --all-namespaces or -A

describe

kubectl describe pod [pod-name] (to fetch all the details about the pod) kubectl describe replicaset [replicaset-name]

explain

kubectl explain [command] ex. kubectl explain pod kubectl explain replicaset

create definition yaml file

kubectl get pod [pod-name] -o yaml > pod-definition.yaml (fetch definition of an existing pod) kubectl run redis --image=redis --dry-run=client -o yaml > pod-definition.yaml (creates a pod definition / manifest file) kubectl create deployment nginx-deploy --image=nginx --replicas=4 --dry-run -o yaml > deployment-definition.yaml

kubectl output format

kubectl command's output is in plain text by default which can be changed. pseudo command - kubectl [command] [type] [name] -o [output-format]

ex. kubectl get pod [pod-name] -o json (converts plaintext to json) kubectl get pod [pod-name] -o name (prints only the name of the resource) kubectl get pod [pod-name] -o wide (output in plaintext with additional information) kubectl get pod [pod-name] -o yaml (converts plaintext to yaml)

delete

kubectl delete pod [pod-name] kubectl delete pod [pod-name] --force (to delete pod faster - not recommended to use in production) kubectl delete replicaset [rs-name]

scale

kubectl scale rs [replica-set-name] --replicas=5 Or use edit command and update the replicaset. kubectl edit rs [replicaset-name] (VI editor)

help

kubectl create deployment --help