Kubernetes provides a command line tool for communicating with a Kubernetes cluster's control plane, using the Kubernetes API.
kubectl
is a Kubernetes command-line tool that allows you to run commands against Kubernetes clusters. You can usekubectl
to deploy applications, inspect and manage cluster resources, and view logs.The descriptions are taken directly from the official documentation at kubernetes.io, and each will link to the appropriate documentation page should you require more information or a deeper dive.
kubectl create: This command is used to create Kubernetes resources from files or stdin.
kubectl create -f pod.yaml
kubectl get: It is used to retrieve Kubernetes resources.
kubectl get pods
kubectl describe: This command provides detailed information about a Kubernetes resource.
kubectl describe pod my-pod
- This command describes the pod named my-pod, displaying detailed information including its status, containers, and events
kubectl apply: It applies changes to Kubernetes resources defined in YAML or JSON files.
kubectl apply -f deployment.yaml
kubectl delete: It is used to delete Kubernetes resources
kubectl delete pod my-pod
- This command deletes the pod named my-pod
kubectl exec: This command executes commands inside a running container in a pod.
kubectl exec -it my-pod -- /bin/bash
- This command starts an interactive shell (/bin/bash) inside the pod named my-pod
kubectl logs: It retrieves the logs of a pod.
kubectl logs my-pod
- This command displays the logs of the pod named my-pod
kubectl port-forward: This command forwards one or more local ports to a pod
kubectl port-forward my-pod 8080:80
- This command forwards local port 8080 to port 80 on the pod named my-pod
kubectl scale: It scales the number of replicas of a resource
kubectl scale --replicas=3 deployment/my-deployment
- This command scales the number of replicas of the deployment named my-deployment to 3.
kubectl edit: This command edits the resource definition in a text editor
kubectl edit pod my-pod
- This command opens the resource definition of the pod named my-pod in a text editor, allowing you to make changes
kubectl rollout: This command manages rollouts of updates to Kubernetes resources
kubectl rollout status deployment/my-deployment
- This command checks the status of the rollout for the deployment named my-deployment
kubectl label: It adds or updates labels on Kubernetes resources.
kubectl label pod my-pod app=backend
- This command adds the label app=backend to the pod named my-pod.
kubectl annotate: This command adds or updates annotations on Kubernetes resources
kubectl annotate pod my-pod description="This is my pod"
- This command adds the annotation description="This is my pod" to the pod named my-pod
kubectl apply -f -: This command applies configuration from the standard input.
cat pod.yaml | kubectl apply -f -
- This command applies the configuration defined in pod.yaml piped from the standard input
kubectl cluster-info: It displays cluster info such as server URL and Kubernetes version
kubectl cluster-info
- This command displays information about the Kubernetes cluster.
kubectl rollout history: This command views rollout history of a deployment
kubectl rollout history deployment/my-deployment
- This command displays the rollout history of the deployment named my-deployment.
kubectl rollout undo: It rolls back deployment to a previous revision
kubectl rollout undo deployment/my-deployment
- This command rolls back the deployment named my-deployment to the previous revision
kubectl create namespace: This command creates a new Kubernetes namespace
kubectl create namespace my-namespace
kubectl apply --dry-run: It simulates the apply of configuration without actually executing it
kubectl apply -f pod.yaml --dry-run=client
- This command checks if the configuration in pod.yaml can be applied without actually applying it.
kubectl api-resources: This command lists all available API resources
kubectl api-resources
- This command lists all the API resources supported by the Kubernetes API server
kubectl create -f : This command creates resources defined in all .yaml files in a directory
kubectl create -f ./my-resources/
- This command creates Kubernetes resources defined in all .yaml files located in the my-resources directory
kubectl get pods -o wide: It retrieves pods with additional details including node name and IP address
kubectl get pods -o wide
kubectl describe node: This command provides detailed information about a Kubernetes node.
kubectl describe node my-node
kubectl rollout pause: It pauses a rollout of a deployment
kubectl rollout pause deployment/my-deployment
- This command pauses the rollout of the deployment named my-deployment
kubectl rollout resume: This command resumes a paused rollout of a deployment
kubectl rollout resume deployment/my-deployment
- This command resumes the paused rollout of the deployment named my-deployment.
kubectl delete namespace: It deletes a Kubernetes namespace and all resources within it.
kubectl delete namespace my-namespace
kubectl get events: This command retrieves events from the cluster.
kubectl get events
- This command retrieves all events from the cluster, displaying information such as type, reason, and message
kubectl get pods --show-labels: It displays additional labels associated with pods.
kubectl get pods --show-labels
kubectl exec -it my-pod -- ls /app: This command executes a command (ls/app) inside a running container in a pod interactively.
kubectl exec -it my-pod -- ls /app
- This command lists the contents of the /app directory inside the pod named my-pod
kubectl create secret: It creates a secret in the cluster.
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=passw0rd
- This command creates a secret named my-secret with two key-value pairs: username=admin and password=passw0rd
kubectl edit deployment: This command opens the deployment configuration in a text editor, allowing you to make changes.
kubectl edit deployment/my-deployment
kubectl rollout restart: It restarts a rollout of a deployment by reapplying the current configuration
kubectl rollout restart deployment/my-deployment
kubectl rollout status: This command checks the status of a rollout for a deployment.
kubectl rollout status deployment/my-deployment
kubectl exec -it my-pod -- sh -c 'echo $ENV_VAR': This command executes a shell command (echo $ENV_VAR) inside a running container in a pod.
kbectl exec -it my-pod -- sh -c 'echo $ENV_VAR'
- This command prints the value of the environment variable ENV_VAR inside the pod named my-pod.
kubectl apply -f deployment.yaml --record: It applies changes to a deployment and records the changes in the revision history.
kubectl apply -f deployment.yaml --record
- This command applies the changes specified in the deployment.yaml file to the deployment and records the changes in the revision history.
kubectl get pods --field-selector=status.phase=Running: This command retrieves pods with a specific status phase, such as Running
kubectl get pods --field-selector=status.phase=Running
- This command retrieves all pods in the current namespace that are in the Running phase
kubectl delete pod --grace-period=0 --force my-pod: It forcefully deletes a pod without waiting for the grace period.
kubectl delete pod --grace-period=0 --force my-pod
- This command forcefully deletes the pod named my-pod without waiting for the grace period to elapse
kubectl describe service: This command provides detailed information about a Kubernetes service
kubectl describe service my-service
kubectl create deployment: It creates a deployment using the specified image.
kubectl create deployment my-deployment --image=my-image:tag
kubectl get deployment -o yaml: This command retrieves deployments and outputs the result in YAML format.
kubectl get deployment -o yaml
- This command retrieves all deployments in the current namespace and outputs the result in YAML format
kubectl scale deployment: This command scales the number of replicas of a deployment.
kubectl scale deployment/my-deployment --replicas=3
kubectl rollout history deployment: It displays the revision history of a deployment.
kubectl rollout history deployment/my-deployment
kubectl rollout undo deployment --to-revision=: This command rolls back a deployment to a specific revision.
kubectl rollout undo deployment/my-deployment --to-revision=3
kubectl apply -f pod.yaml --namespace=: It applies a YAML file to a specific namespace.
kubectl apply -f pod.yaml --namespace=my-namespace
- This command applies the configuration specified in pod.yaml to the namespace my-namespace.
kubectl logs -f my-pod: This command streams the logs of a pod continuously.
kubectl logs -f my-pod
kubectl get svc: It retrieves information about services in the cluster.
kubectl get svc
kubectl get pods -n : This command retrieves pods from a specific namespace.
kubectl get pods -n my-namespace
- This command retrieves all pods from the namespace my-namespace
kubectl delete -f pod.yaml: It deletes resources specified in a YAML file.
kubectl delete -f pod.yaml
kubectl rollout status deployment/my-deployment: This command checks the status of a deployment rollout.
kubectl rollout status deployment/my-deployment
kubectl exec -it my-pod -- /bin/bash: This command starts an interactive shell inside a pod.
kubectl exec -it my-pod -- /bin/bash
- This command opens an interactive shell (/bin/bash) inside the pod named my-pod, allowing you to execute commands within it.
kubectl apply -f --recursive: This command applies all YAML files in a directory and its subdirectories.
kubectl apply -f ./my-resources/ --recursive
kubectl rollout history deployment/my-deployment --revision=3: It displays details of a specific revision in the rollout history of a deployment.
kubectl rollout history deployment/my-deployment --revision=3
- This command shows details of the third revision in the rollout history of the deployment named my-deployment.
This command rolls back a deployment to a specific revision
kubectl rollout undo deployment/my-deployment --to-revision=2
It validates the configuration file before applying changes.
kubectl apply -f pod.yaml --validate=true
- This command validates the pod.yaml file before applying changes to the cluster.
This command retrieves the last 100 lines of logs from a pod.
kubectl logs my-pod --tail=100
It retrieves services with additional details including node port and cluster IP.
kubectl get services -o wide
This command retrieves pods with a status phase other than Running.
kubectl get pods --field-selector=status.phase!=Running
It forcefully deletes a pod without waiting for the grace period.
kubectl delete pod my-pod --force --grace-period=0
This command provides detailed information about a Kubernetes service.
kubectl describe service my-service
It exposes a deployment as a service with a specified type, port, and target port.
kubectl expose deployment my-deployment --type=LoadBalancer --port=80 --target-port=8080
This command retrieves deployments labeled with app=my-app.
kubectl get deployments -l app=my-app
It pauses the rollout of a deployment.
kubectl rollout pause deployment/my-deployment
This command resumes the rollout of a deployment.
kubectl rollout resume deployment/my-deployment
It retrieves logs from a specific container within a pod.
kubectl logs my-pod --container=nginx
- This command retrieves logs from the container named nginx within the pod my-pod.
This command validates the configuration file without actually applying changes.
kubectl apply -f pod.yaml --dry-run=client
- This command checks if the configuration in pod.yaml can be applied without actually applying it.
It retrieves pods sorted by creation timestamp.
kubectl get pods --sort-by=.metadata.creationTimestamp
- This command retrieves all pods in the current namespace sorted by their creation timestamp in ascending order.
This command provides detailed information about a persistent volume claim.
kubectl describe persistentvolumeclaim my-pvc
- This command describes the persistent volume claim named my-pvc, displaying detailed information including its status and storage class.
It continuously monitors the status of a deployment rollout.
kubectl rollout status deployment/my-deployment --watch
- This command continuously monitors the status of the rollout for the deployment named my-deployment.
This command retrieves pods with a status phase of Pending.
kubectl get pods --field-selector=status.phase=Pending
It creates a generic secret from a file.
kubectl create secret generic my-secret --from-file=./my-secret-file
- This command creates a generic secret named my-secret from the contents of the file my-secret-file.
This command restarts a rollout of a deployment by reapplying the current configuration.
kubectl rollout restart deployment/my-deployment
- This command restarts the rollout of the deployment named my-deployment.
It adds a label to a namespace.
kubectl label namespace my-namespace env=dev
This command deletes a deployment.
kubectl delete deployment my-deployment
It retrieves pods from a specific namespace.
kubectl get pods --namespace=my-namespace
This command provides detailed information about a secret.
kubectl describe secret my-secret
It deletes a service.
kubectl delete service my-service
This command retrieves information about nodes in the cluster.
kubectl get nodes
It creates a config map from literal values.
kubectl create configmap my-config --from-literal=key1=value1 --fromliteral=key2=value2
- This command creates a config map named my-config with two key-value pairs: key1=value1 and key2=value2.
This command displays details of a specific revision in the rollout history of a deployment.
kubectl rollout history deployment/my-deployment --revision=3
- This command shows details of the third revision in the rollout history of the deployment named my-deployment.
It displays resource usage (CPU and memory) of pods in the cluster.
kubectl top pods
This command provides documentation about the Pod resource, including all its fields and their descriptions.
kubectl explain pod
It deletes a namespace and all resources within it.
kubectl delete namespace my-namespace
This command retrieves information about persistent volumes in the cluster.
kubectl get pv
It checks the status of a rollout and waits for a specific timeout before exiting.
kubectl rollout status deployment/my-deployment --timeout=2m
- This command checks the status of the rollout for the deployment named mydeployment and waits for a maximum of 2 minutes.
This command applies a configuration file to a specific namespace.
kubectl apply -f pod.yaml --namespace=my-namespace
It retrieves information about secrets in the cluster.
kubectl get secrets
This command creates a NodePort service to expose a deployment on a specific port.
kubectl create service nodeport my-service --tcp=80:8080
It simulates rolling back a deployment to a specific revision without actually performing the rollback.
kubectl rollout undo deployment/my-deployment --to-revision=2 --dry-run
This command validates a configuration file without actually creating the resource.
kubectl create -f pod.yaml --dry-run=client
This command starts an interactive shell inside a specific container within a pod.
kubectl exec -it my-pod --container=my-container -- /bin/bash
- This command opens an interactive shell (/bin/bash) inside the container named my-container within the pod named my-pod
This command creates a role within a namespace.
kubectl create role my-role --verb=get --resource=pods
- This command creates a role named my-role with permissions to get pods within the namespace.
It applies changes to a deployment within a specific namespace and records the changes.
kubectl apply -f deployment.yaml --namespace=my-namespace --record
This command provides detailed information about a persistent volume.
kubectl describe persistentvolume my-pv
It creates a service account within a namespace.
kubectl create serviceaccount my-service-account
This command retrieves events sorted by creation timestamp.
kubectl get events --sort-by=.metadata.creationTimestamp
- This command retrieves all events in the current namespace sorted by their creation timestamp in ascending order.
It provides detailed information about an Ingress resource.
kubectl describe ingresses.extensions
- This command describes all Ingress resources in the current namespace, displaying detailed information about each Ingress.
It simulates rolling back deployment to the previous revision without actually performing the rollback.
kubectl rollout undo deployment/my-deployment --dry-run=client
This command scales the number of replicas of a deployment to 5 and records the change.
kubectl scale deployment/my-deployment --replicas=5 --record
It deletes a secret.
kubectl delete secret my-secret
This command retrieves information about Ingress resources in the cluster.
kubectl get ingress