Basic Commands
Get cluster information:
kubectl cluster-info
Display available commands:
kubectl help
Get current context:
kubectl config current-context
Set current context:
kubectl config use-context <context-name>
Create and Apply Resources
Create a resource from a file or stdin:
kubectl create -f <filename>
Apply a configuration to a resource by filename or stdin:
kubectl apply -f <filename>
View Resources
Get all resources:
kubectl get all
Get all resources in a specific namespace:
kubectl get all -n <namespace>
Get specific resource types:
kubectl get <resource-type>
Get a specific resource:
kubectl get <resource-type> <resource-name>
Get detailed information about a resource:
kubectl describe <resource-type> <resource-name>
Edit and Update Resources
Edit a resource:
kubectl edit <resource-type> <resource-name>
Patch a resource:
kubectl patch <resource-type> <resource-name> --patch '{"spec": {"replicas": 2}}'
Scale a resource:
# kubectl scale kubectl scale <resource-type> <resource-name> --replicas=<number> Eg., kubectl scale deployment my-app --replicas=3 # kubectl set kubectl set image <resource>/<resource-name> <container-name>=<image>:<tag> Eg., kubectl set image deployment/my-deployment my-container=nginx:1.19
kubectl scale
is used to change the number of replicas for resources that support it (e.g., Deployments).kubectl set
allows you to modify fields (e.g., image, environment variables) in a resource's specification.
Delete Resources
Delete a resource:
kubectl delete <resource-type> <resource-name>
Delete a resource by filename:
kubectl delete -f <filename>
Delete all resources of a specific type:
kubectl delete <resource-type> --all
Namespace Management
Get all namespaces:
kubectl get namespaces
Create a namespace:
kubectl create namespace <namespace-name>
Delete a namespace:
kubectl delete namespace <namespace-name>
Logs and Events
View logs of a specific pod:
kubectl logs <pod-name>
View logs of a specific container in a pod:
kubectl logs <pod-name> -c <container-name>
Stream logs of a specific pod:
kubectl logs -f <pod-name>
View cluster events:
kubectl get events
Exec and Port Forward
Execute a command in a container:
kubectl exec <pod-name> -- <command>
Start a bash session in a container:
kubectl exec -it <pod-name> -- /bin/bash
Forward a local port to a port on a pod:
kubectl port-forward <pod-name> <local-port>:<pod-port>
or
kubectl port-forward svc/<svc-name> <local-port>:<pod:port>
Label the Nodes
Create a namespace so that we can dedicate a node to the namespace.
kubectl create namespace high5
Label the nodes you want to schedule the namespace's pods on. For example, let's say you have two nodes named node1
and node2
.
kubectl label nodes <node1-name> dedicated=high5
kubectl label nodes <node2-name> dedicated=high5
Resource Types
Pods:
kubectl get pods kubectl describe pod <pod-name> kubectl delete pod <pod-name>
Deployments:
kubectl get deployments kubectl describe deployment <deployment-name> kubectl delete deployment <deployment-name>
Replicaset:
kubectl get replicaset kubectl describe replicaset <replicaset-name> kubectl delete replicaset <replicaset-name>
Services:
kubectl get services kubectl describe service <service-name> kubectl delete service <service-name>
ConfigMaps:
kubectl get configmaps kubectl describe configmap <configmap-name> kubectl delete configmap <configmap-name>
Secrets:
kubectl get secrets kubectl describe secret <secret-name> kubectl delete secret <secret-name>
Ingresses:
kubectl get ingresses kubectl describe ingress <ingress-name> kubectl delete ingress <ingress-name>
Ingress controller:
kubectl get pods -n ingress-nginx //To check ingress-controller is running or not
Webhook configurations:
kubectl get validatingwebhookconfigurations
PersistentVolumeClaims (PVCs):
kubectl get pvc kubectl describe pvc <pvc-name> kubectl delete pvc <pvc-name>
PersistentVolumes (PVs):
kubectl get pv kubectl describe pv <pv-name> kubectl delete pv <pv-name>
StatefulSets:
kubectl get statefulsets kubectl describe statefulset <statefulset-name> kubectl delete statefulset <statefulset-name>
DaemonSets:
kubectl get daemonsets kubectl describe daemonset <daemonset-name> kubectl delete daemonset <daemonset-name>
Jobs:
kubectl get jobs kubectl describe job <job-name> kubectl delete job <job-name>
CronJobs:
kubectl get cronjobs kubectl describe cronjob <cronjob-name> kubectl delete cronjob <cronjob-name>
Other Useful Commands
Apply a resource configuration from a file:
kubectl apply -f <filename>
Create a secret from literals:
kubectl create secret generic <secret-name> --from-literal=<key>=<value>
Expose a deployment as a service:
kubectl expose deployment <deployment-name> --type=LoadBalancer --name=<service-name>
Roll out a new version of a deployment:
kubectl rollout restart deployment <deployment-name>
Roll back to a previous version of a deployment:
kubectl rollout undo deployment <deployment-name>
Check the rollout status of a deployment:
kubectl rollout status deployment <deployment-name>
Context and Configuration Management
View current contexts:
kubectl config get-contexts
Use a specific context:
kubectl config use-context <context-name>
Set a context's namespace:
kubectl config set-context --current --namespace=<namespace-name>
Few other commands to Delete the pods
To delete pods using kubectl
, you can use the kubectl delete
command. Here are some examples of how to delete pods in different scenarios:
Delete a specific pod:
kubectl delete pod <pod-name>
Delete all pods in a namespace:
kubectl delete pods --all -n <namespace>
Delete pods by label:
kubectl delete pods -l <label-key>=<label-value>
label-key:
key which we write under metadata in deployment.yaml file.label-value:
Value of the key which we write under metadata in deployment.yaml file.Delete pods in a specific namespace by label:
kubectl delete pods -l <label-key>=<label-value> -n <namespace>
label-key:
key which we write under metadata in deployment.yaml file.label-value:
Value of the key which we write under metadata in deployment.yaml file.Force delete a pod (useful if a pod is stuck in a terminating state):
kubectl delete pod <pod-name> --grace-period=0 --force
Here are some specific examples:
Delete a pod named
nginx-pod
:kubectl delete pod nginx-pod
Delete all pods in the
default
namespace:kubectl delete pods --all -n default
Delete pods with the label
app=myapp
:kubectl delete pods -l app=myapp
Delete pods with the label
app=myapp
in thedevelopment
namespace:kubectl delete pods -l app=myapp -n development
Force delete a pod named
stuck-pod
:kubectl delete pod stuck-pod --grace-period=0 --force