Getting Started
Command | Explanation |
minikube start –driver=virtualbox | If you are not using Docker Desktop, minikube can help setup a cluster with Virtualbox as driver |
kubectl run hello-minikube –image=nginx | Runs a Pod named hello-minikube with a nginx image. |
kubectl cluster-info | Displays cluster information, including API server and core services URLs. |
kubectl run nginx –image=nginx –dry-run=client -o yaml > nginx.yaml | Generates a YAML configuration for a pad using nginx image without actually creating it in the cluster and saves the generated yaml file to nginx.yaml. –dry-run=client option simulates the command locally without sending it to the API Server. Useful for testing and generating configuration files. |
kubectl run nginx –image nginx -n mynamespace | Runs a pod named ‘nginx’ in the ‘mynamespace’ namespace. |
kubectl get events -o wide | Lists events across the cluster with additional details. |
kubectl get all | Lists all resources in the current namespace |
kubectl get all -A | Lists all resources across all namespaces. |
kubectl get all –selector app=web-app | Lists all resources labeled with app=web-app |
kubectl port-forward –help | Displays help for the port-forward command. |
kubectl port-forward pod/mypod 8888:5000 | Forwards local port 8888 to port 5000 on mypod |
kubectl exec -it ubuntu –bash | Opens an interactive Bash session in the ubuntu Pod |
kubectl exec -it mypod -c sidecar –touch /tmp/crash | Creates a file /tmp/crash in the sidecar container of mypod |
kubectl explain pod.spec.restartPolicy | Provides documentation for the restartPolicy field in the Pod spec |
kubectl api-resources | To find out shortnames, namespaced or not and kind of resources. Retrieve list of all available API groups in a k8 cluster. |
Nodes & Node Management
Command | Explanation |
kubectl get nodes | Lists all nodes in current namespace |
kubectl get nodes -o wide | Lists nodes with additional details like IP Addresses and roles |
kubectl get node <NODE_NAME> –show-labels | View labels for a specific node |
kubectl drain <NODE_NAME> –delete-emptydir-data –ignore-daemonsets | Prepares a node for maintenance by evicting pods |
kubectl cordon control-plane | Marks the control-plane node as unschedulable |
kubectl uncordon node-name | Mark a previously cordoned node as schedulable again. |
kubectl taint nodes node1 role=frontend:PreferNoSchedule | Applies a taint to a node to control Pod scheduling. |
kubectl taint nodes node1 node2 dedicated=example:NoExecute | Can taint multiple nodes at once |
Pods
Command | Explanation |
kubectl get pods | Lists all nodes in the cluster with basic information |
kubectl get pods -o wide | Lists nodes with additional information like IP Addresses and Node placement |
kubectl get pods -n mynamespace | Lists all pods in the given namespace |
kubectl get pods –watch | Watch all pods in the default namespace |
kubectl get pods –field-selector=status.phase=Running | retrieves pods with a status of ‘Running’. Can be combined with label selectors also. |
kubectl describe pod nginx | Shows detailed information about the pod named ‘nginx’ |
kubectl create -f pod-definition.yml | Creates a resource (pod) from the yaml file. |
kubectl apply -f pod-definition.yml | Apply changes from the yaml file to the existing resources. |
kubectl delete pod mypodname | Deletes the Pod named mypodname . |
kubectl delete pod/nginx pod/ubuntu –now | Immediately deletes the nginx and ubuntu Pods. |
kubectl delete -f nginx.yaml | Deletes resources defined in nginx.yaml |
kubectl create pdb nginx –selector=app=nginx –min-available=2 | Creates a pod disruption budget to ensure at least two nginx pods remain available |
Replicas
kubectl get replicationcontroller | Lists all replication controllers in the namespace. |
kubectl get replicasets | Lists all replica sets in the namespace. |
kubectl describe replicaset myapp-replicaset | Displays details about myapp-replicaset |
kubectl edit replicaset myapp-replicaset | Opens an editor to modify myapp-replicaset . |
kubectl replace -f replicaset-definition.yml | Replaces the existing ReplicaSet with the definition in replicaset-definition.yml |
kubectl scale -f replicaset-definition.yml –replicas=6 | Scales the ReplicaSet defined in replicaset-definition.yml to 6 replicas. |
kubectl scale replicaset myapp-replicaset –replicas=6 | Scales the myapp-replicaset to 6 replicas |
kubectl scale deployment/nginx –replicas=6; watch kubectl get pods -o wide | Scales the nginx Deployment to 6 replicas and monitors the Pod status. |
kubectl delete replicaset myapp-replicaset | Deletes the ReplicaSet named myapp-replicaset |
Deployments
kubectl create -f deployment-definition.yml | Creates resources defined in deployment-definition.yml |
kubectl create -f deployment-definition.yml –record | Creates resources while recording the changes. |
kubectl create deployment –image=nginx nginx | Creates a Deployment named nginx with the nginx image |
kubectl get deployments | Lists all Deployments in the namespace. |
kubectl rollout status deployment/myapp-deployment | Checks the rollout status of myapp-deployment . |
kubectl rollout history deployment/myapp-deployment | Shows the rollout history of myapp-deployment . |
kubectl rollout undo deployment/myapp-deployment | Rolls back myapp-deployment to the previous revision |
kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1 | Updates the image for nginx container in myapp-deployment to nginx:1.9.1 |
kubectl annotate deployment/nginx kubernetes.io/change-cause=”initial deployment” | Adds a change-cause annotation to the nginx Deployment. |
kubectl expose deployment nginx –port 80 | Exposes the nginx Deployment on port 80. |
kubectl delete deployment myapp-deployment | Deletes the myapp-deployment Deployment. |
Namespaces
kubectl create -f namespace-dev.yml | Creates a namespace using namespace-dev.yml . |
kubectl create namespace dev | Creates a namespace named dev . |
kubectl get namespaces (or) kubectl get ns | Lists all namespaces |
kubectl delete namespace/mynamespace –now | Immediately deletes the mynamespace namespace. |
Services & Endpoints
kubectl get endpoints | Lists all service endpoints in the namespace |
kubectl get services | Lists all services in the namespace |
kubectl expose deployment nginx –type=NodePort | Exposes the nginx deployment as a NodePort service |
kubectl expose deployment nginx –type=LoadBalancer –port 8080 –target-port 80 | Exposes nginx on an external LoadBalancer. |
kubectl create service externalname my-service –external-name nginx-red.default.svc.cluster.local | Creates an ExternalName service that points to a DNS address. |
kubectl delete service/nginx | Deletes the nginx service |
kubectl delete service/nginix-blue service/nginx-red service/my-service | Delete multiple services |
kubectl get serviceaccount | Lists all service accounts in the namespace. |
kubectl create serviceaccount dashboard-sa | Creates a service account named dashboard-sa . |
kubectl describe serviceaccount dashboard-sa | Shows details of the dashboard-sa service account. |
Roles & Permissions
kubectl get clusterrolebindings -o wide | Lists cluster role bindings with detailed output. |
kubectl describe ClusterRole/cluster-admin | Shows details for the cluster-admin role. |
kubectl create clusterrole cluster-superuser1 –verb=’*’ –resource=’*’ | Creates a cluster role with full permission on all resources |
kubectl create clusterrolebinding cluster-superuser1 –clusterrole=cluster-superuser1 –group=cluster-superusers1 | Binds cluster role to a group |
kubectl auth can-i ‘*‘ ‘*’ | Checks if the user has permissions for all actions on all resources. |
kubectl auth can-i ‘*’ ‘*’ –as-group=”cluster-superusers1″ –as=”msd” | Checks if user ‘msd ‘ has full permissions under the specified group |
kubectl get roles | Lists all roles in the namespace. |
kubectl get rolebindings | Lists all role bindings in the namespace. |
kubectl describe role <ROLE_NAME> | Shows details of the specified role. |
Daemonsets
kubectl get daemonsets | Lists all DaemonSets in the namespace. |
kubectl describe daemonsets monitoring-daemon | Displays details of the monitoring-daemon DaemonSet. |
Jobs
kubectl create job calculatepi –image=perl:5.34.0 — “perl” “-Mbignum=bpi” “print bpi(2000)” | Creates a job to calculate pi using a Perl script. |
kubectl delete job calculatepi | Deletes the calculatepi job. |
kubectl delete job calculatepi –cascade=false | Deletes calculatepi without cascading deletion. |
kubectl delete jobs –all -n | Deletes all jobs in the specified namespace. |
kubectl delete job myjob1 –grace-period=0 –force | Forcibly deletes myjob1 without waiting. |
Storage & Persistent Volume
kubectl get storageclass | Lists all storage classes |
kubectl get persistentvolume | Liss all persistent volumes |
kubectl get persistentvolumeclaim | Lists all persistent volume claims. |
Labels, Configs & Sectrets
kubectl config set-context $(kubectl config current-context) –namespace=prod | Sets the default namespace to prod for the current context. |
kubectl config use-context | Switches to a specified context. |
kubectl config view | Displays the Kubernetes configuration settings. |
kubectl config view –kubeconfig=/home/user/custom-config.yaml | Views a specific kubeconfig file. |
kubectl config view –raw | Displays the full, raw configuration |
kubectl config set-context –current –namespace=mynamespace | Sets the default namespace for the current context to mynamespace . |
kubectl label namespace default istio-injection=enabled | Adds the istio-injection=enabled label to the default namespace. |
kubectl label nodes node-name label-key=label-value | Adds a label to a node |
kubectl create configmap myconfigmap –from-literal=COLOR=red –from-literal=City=chennai | Creates a configmap from literal values. |
kubectl create configmap myconfigmap –from-env-file=config.properties | Creates a configmap from an env file |
kubectl create secret docker-registry regcred –docker-server=private-registry.io –docker-username=user | Creates a docker registry secret |
kubectl create token dashboard-sa | Generates a token for dashboard-sa |
kubectl describe secret dashboard-sa-token-kbbdm | Show details of token |
Logs
Command | Explanation |
kubectl logs pod/nginx | Retrieves logs from the pod named ‘nginx’ in the current namespace |
kubectl logs pod/nginx -c my-container -f | If your pod has multiple containers, specify the container name with -c. The -f flag (follow) streams the logs in real time. |
kubectl logs pod/nginx -c my-container -p | -p flag retrieves logs from previous instance of the container if it has restarted. Useful in debugging a crashed container. |
kubectl logs pod/nginx –all-containers=true | Get logs from all containers |
kubectl logs -l app=mywebapp | Retrieves logs from all pods with label app=mywebapp. |
kubectl logs deployment/nginx | Retrieves logs from one of the pods in the ‘nginx’ deployment. Shows logs from a single pod only. |
Scaling
kubectl get hpa | Lists all horizontal pod autoscalers in the namespace |
kubectl scale deployment my-deployment –replicas=5 | Scale a deployment |
kubectl scale replicaset my-replicaset –replicas=3 | Scale a replicaset |
kubectl scale statefulset my-statefulset –replicas=4 | Scale a statefulset |
kubectl autoscale deployment my-deployment –min=2 –max=10 –cpu-percent=80 | Autoscale a deployment based on CPU usage |