Some useful Kubectl commands

Nishit Ranjan
2 min readAug 16, 2019

--

1.)kubectl -f apply <file.yaml/folder_name> #Will deploy the YAML file(s) to the cluster.

2.)kubectl get all #Will list all the kubernetes objects in the cluster

3.)kubectl get pods/deployment/svc/rs #Will list the pods/deployment/service/replicasets on the cluster

4.)kubectl delete po — all #Will delete all the pods in the cluster

5.)kubectl delete <object><object_name> #Will delete the specific object from the cluster

6.)kubectl describe <object><object_name> #Will describe all the properties of the Kubernetes object in the cluster

7.)kubectl desccribe <object><object_name> -n <namespace> #Will describe the properties of the Kubernetes object in a given namespace. Note: when the namespace is default, it isn’t mentioned.

8.)kubectl rollout status <object><object_name> #Will describe the rollout status of the Kubernetes object.

9.)kubectl rollout history <object>/<object_name> #Will describe the rollout history of the Kubernetes object.

10.)kubectl exec -it <object_name> — /bin/sh #Get inside the shell of the Kuberenets object

11.)kubectl create secret docker-registry <name_of_secret_key> — docker-server=<utl of the private registry> — docker-username=<username>— docker-password=<password> — docker-email=<email_id> #Will create a secret key for the Kubernetes object which can then be used inside the YAML to pull the object from the private registry to the Kubernetes cluster.

12.)kubectl get ns #Will list the namespaces.

13.)kubectl get po -n-kube-system #Will list the pods in kube-system namespace

14.)kubectl get all -n-kube-system #Will list all the Kubernetes objects in the kube-system namespace

15.) ns lookup <service_name> #Will lookup for the service inside another pod from a different pod’s shell

16.) netstat -tunelp #Will list the ports inside the pod

17.) kubectl apply -f . #Will deploy all the files in the current context

18.) kubectl delete -f . #Will delete all the files in the current context

19.) kubectl logs <pod_name> #Will shiw the logs of the pods

20.)kubectl top pod #Will list the memory and CPU utilization of all the pods

21.)kubectl top nodes #Will list memory and CPU utilization of all the nodes

22.) kubectl get nodes — show-labels #Will show the labels of the nodes

23.) kubectl label nodes <node_name> <label_key:label_value> #Will add a label name(label_key:label_value) to the respective deployments. This is important if we have mentioned a nodeSelector property for our deployments which means that our deployment will run on only those node that has the same nodeSelector name(which a key-value pair).

--

--

Nishit Ranjan
Nishit Ranjan

Written by Nishit Ranjan

0 Followers

I am a software engineer. I specialize in JavaScript, React.js, NodeJS, Docker and Kubernetes.

No responses yet