The Ultimate List of Must-Know Kubernetes Commands
Kubernetes is an open-source container orchestration tool that was initially developed by Google and now maintained by Cloud Native Computing Foundation. kubectl is the command line tool to manage kubernetes clusters. Whoever likes writing commands rather than using Kubernetes Dashboard will end up using kubectl. In that sense you will find quite a few commands in this article that for my point of view every Cloud Developer should know.
The structure of kubectl commands is built in following way:
kubectl [command] [Type] [Name] [flags][command] is basically the imperative way associated with specific verb like create/apply/get/deletethat instructs to do the special activity. [Type] refers to specific kubernetes resource pods, services, replicasets, statefulsets etc. [Name] points to the exact name of resource which is by the way case-sensitive. [flags] are extra options that give special functionalities.
Without further due, here is a whole list of commands, that I think are pretty important ones in every Cloud Developers daily work.
# get resource(s)
$ kubectl get services
$ kubectl get pods --all-namespaces
$ kubectl get pods -o wide
$ kubectl get rc <rc-name>
$ kubectl get secrets
# interaction with config
kubectl config get-contexts
kubectl config current-context
kubectl config use-context my-cluster-name # set the default context to my-cluster-name
kubectl config set-cluster my-cluster-name # set a cluster entry in the kubeconfig
# create resource(s)
$ kubectl create -f ./file.yml
$ kubectl create -f ./dir
# interact with resources
$ kubectl label pods <pod-name> new-label=awesome # Add a Label
$ kubectl annotate pods <pod-name> icon-url=http://goo.gl/XXBTWq # Add an annotation
kubectl run -i --tty busybox --image=busybox:1.28 -- sh # Run pod as interactive shell
kubectl run nginx --image=nginx -n mynamespace # Start a single instance of nginx pod in the namespace of mynamespace
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
# Generate spec for running pod nginx and write it into a file called pod.yaml
kubectl attach my-pod -i # Attach to Running Container
kubectl port-forward my-pod 5000:6000 # Listen on port 5000 on the local machine and forward to port 6000 on my-pod
kubectl exec my-pod -- ls / # Run command in existing pod (1 container case)
# log resources
kubectl logs my-pod # dump pod logs (stdout)
kubectl logs -l name=myLabel # dump pod logs, with label name=myLabel (stdout)
kubectl logs my-pod --previous # dump pod logs (stdout) for a previous instantiation of a container
kubectl logs my-pod -c my-container # dump pod container logs (stdout, multi-container case)
kubectl logs -l name=myLabel -c my-container # dump pod logs, with label name=myLabel (stdout)
kubectl logs my-pod -c my-container --previous # dump pod container logs (stdout, multi-container case) for a previous instantiation of a container
# apply changes to the resources
kubectl apply -f ./my-manifest.yaml # create resource(s)
kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
kubectl apply -f ./dir # create resource(s) in all manifest files in dir
kubectl apply -f https://git.io/vPieo # create resource(s) from url
kubectl create deployment nginx --image=nginx # start a single instance of nginx
# scaling resources
kubectl scale --replicas=3 rs/foo # Scale a replicaset named 'foo' to 3
kubectl scale --replicas=3 -f foo.yaml # Scale a resource specified in "foo.yaml" to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # If the deployment named mysql's current size is 2, scale mysql to 3
kubectl scale --replicas=5 rc/foo rc/bar rc/baz # Scale multiple replication controllers
# deleting resources
kubectl delete -f ./pod.json # Delete a pod using the type and name specified in pod.json
kubectl delete pod unwanted --now # Delete a pod with no grace period
kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabkubectl -n my-ns delete pod,svc --all # Delete all pods and services in namespace my-ns,
# interact with the nodes and clusters
kubectl cordon my-node # Mark my-node as unschedulable
kubectl drain my-node # Drain my-node in preparation for maintenance
kubectl uncordon my-node # Mark my-node as schedulable
kubectl top node my-node # Show metrics for a given node
kubectl cluster-info # Display addresses of the master and servicesIf you are interested in more Software Engineering topics take a look at my other articles.
Relevant Articles:




