Komodor is an autonomous AI SRE platform for Kubernetes. Powered by Klaudia, it’s an agentic AI solution for visualizing, troubleshooting and optimizing cloud-native infrastructure, allowing enterprises to operate Kubernetes at scale.
Proactively detect & remediate issues in your clusters & workloads.
Easily operate & manage K8s clusters at scale.
Reduce costs without compromising on performance.
Guides, blogs, webinars & tools to help you troubleshoot and scale Kubernetes.
Tips, trends, and lessons from the field.
Practical guides for real-world K8s ops.
How it works, how to run it, and how not to break it.
Short, clear articles on Kubernetes concepts, best practices, and troubleshooting.
Infra stories from teams like yours, brief, honest, and right to the point.
Product-focused clips showing Komodor in action, from drift detection to add‑on support.
Live demos, real use cases, and expert Q&A, all up-to-date.
The missing UI for Helm – a simplified way of working with Helm.
Visualize Crossplane resources and speed up troubleshooting.
Validate, clean & secure your K8s YAMLs.
Navigate the community-driven K8s ecosystem map.
Who we are, and our promise for the future of cloud-native.
Have a question for us? Write us.
Come aboard the K8s ship – we’re hiring!
Discover our events, webinars and other ways to connect.
Here’s what they’re saying about Komodor in the news.
Join the Komodor partner program and accelerate growth.
Kubectl is the Kubernetes CLI used to communicate with the Kubernetes control plane, deploy applications, inspect resources, view logs, and manage cluster objects.
Kubectl works by communicating with the Kubernetes API server: It authenticates with the Master Node of your cluster and makes API calls to perform management actions. You can use kubectl to create, inspect, update, and delete Kubernetes objects, deploy applications, inspect and manage cluster resources, and view logs.
Kubectl offers three techniques.
kubectl create pod
kubectl delete service
kubectl create -f manifest.yaml
kubectl apply -f manifest.yaml
The object notation is usually in the form of object type, followed by a slash, followed by the object name. For example, to address the “mypod” pod, the notation will be “pods/mypod”. Some commands accept different notations (e.g., “kubectl get pod mypod”), so you might see different notations used throughout this article.
For more information, including a complete list of kubectl operations, see the kubectl reference documentation.
This is part of an extensive series of guides about DevOps.
This is the TL;DR section of this article—a quick-access section to remind you of the most important commands. No explanations are given, but the commands are explained further down in the article.
Use this table to quickly find the right kubectl command based on what you need to do in a Kubernetes cluster.
kubectl config get-contexts
kubectl config use-context <context-name>
kubectl config set-context --current --namespace=<namespace>
kubectl get pods
kubectl get pods -A
kubectl get pods -o wide
kubectl describe pod <pod-name>
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl events --types=Warning
kubectl logs <pod-name>
kubectl logs <pod-name> -c <container-name>
kubectl logs <pod-name> --previous
kubectl logs -f <pod-name>
kubectl exec --stdin --tty <pod-name> -- /bin/sh
kubectl debug <pod-name> -it --image=busybox:1.28
kubectl port-forward <pod-name> 8888:8080
kubectl cp <source> <destination>
kubectl apply -f <file.yaml>
kubectl diff -f <file.yaml>
kubectl delete -f <file.yaml>
kubectl edit <resource>/<name>
kubectl patch <resource>/<name> -p '<patch>'
kubectl rollout status deployment/<deployment-name>
kubectl rollout history deployment/<deployment-name>
kubectl rollout restart deployment/<deployment-name>
kubectl rollout undo deployment/<deployment-name>
kubectl scale deployment/<deployment-name> --replicas=<number>
kubectl top pod
kubectl top node
kubectl auth can-i <verb> <resource>
kubectl api-resources
kubectl explain <resource>
To get the current contexts configured in your kubeconfig file:
$ kubectl config get-contexts
To switch context:
$ kubectl config use-context minikube
To get the name of the containers of a running pod:
$ kubectl get pod MYPOD -o 'jsonpath={.spec.containers[*].name}'
To get the value of a secret (if you have the base64 command available):
$ kubectl -n mynamespace get secret MYSECRET -o 'jsonpath={.data.DB_PASSWORD}' | base64 -d SuperSecretPassword
If you don’t have the base64 command available, you can use the go-template:
$ kubectl -n mynamespace get secret MYSECRET -o ‘go-template={{.data.DB_PASSWORD | base64decode}}’
An example of a filter in jsonpath:
$ kubectl get pod nginx -o 'jsonpath={.spec.containers[?(@.name=="nginx")].image}' nginx:1.9.1
To create a secret from your current Docker credentials to pull images from a private registry:
$ kubectl create secret generic SECRETNAME --from-file=.dockerconfigjson=$HOME/.docker/config.json --type=kubernetes.io/dockerconfigjson
To forward pod port 8080 to your local computer on port 8888:
$ kubectl port-forward MYPOD 8888:8080
To test RBAC rules:
$ kubectl --as=system:serviceaccount:MYNS:MYSA auth can-i get configmap/MYCM yes
To login to a specific container within a pod:
kubectl exec -it <podname> -c <containername> — /bin/bash
This will open a Bash shell inside the specified container, allowing you to run commands and interact with the container’s file system.
Use /bin/bash only if the container image includes Bash. Many minimal or distroless images do not include a shell, so kubectl debug may be a better option for troubleshooting.
Itiel Shwartz
Co-Founder & CTO
In my experience, here are tips that can help you better utilize kubectl:
Create aliases for frequently used kubectl commands to save time and reduce errors.
Manage multiple clusters efficiently by using context switching with kubectl config set-context.
Extract specific fields from kubectl output using JSONPath or jq for more precise information.
Use –dry-run to test configurations before applying changes to your cluster.
Create shell scripts for repetitive kubectl tasks to streamline your workflows.
Kubectl uses “contexts” to know how to communicate with the cluster. Contexts are stored in a kubeconfig file, which can store multiple contexts. Contexts will usually be provided by some other commands related to the control plane or some other management commands.
Such commands will usually add context to your kubeconfig file. The default kubectl config file is located at $HOME/.kube/config. You can use a different kubectl config file by specifying the --kubeconfig=PATH arguments on the kubectl command line.
$HOME/.kube/config
--kubeconfig=PATH
To list the contexts available in your kubeconfig file, use kubectl get-contexts:
$ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * arn:aws:eks:... arn:aws:eks:... arn:aws:eks:... minikube minikube minikube default
To switch from one context to another, use “use-context”:
$ kubectl config use-context minikube Switched to context "minikube". $ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE arn:aws:eks:... arn:aws:eks:... arn:aws:eks:... * minikube minikube minikube default
If you have only one context (i.e. you are managing a single cluster), then you are not concerned about contexts. If you are working with two or more clusters, you might want to consider using the --context command line option.
--context
The reason is that if you often switch between clusters, you are pretty much guaranteed that one day you will run a kubectl command that you intended for another cluster, and the consequences might very well be dramatic.
With this in mind, you might want to set your default context to something innocuous, like minikube, and force yourself to explicitly provide the --context option on every kubectl command.
Kubectl get is probably the command you will use the most, so it deserves its own section. Kubectl get can retrieve information about all Kubernetes objects, as well as nodes in the Kubernetes data plane. The most common Kubernetes objects you are likely to query are pods, services, deployments, stateful sets, and secrets.
Kubectl get
Kubectl get offers a range of output formats:
-o wide
-o yaml
-o json
-o jsonpath
-o go-template
Here are some examples of commonly used commands:
List all pods in the default namespace (in this case, there are no pods in the default namespace):
default
$ kubectl get pod No resources found in default namespace.
Get more information about a given pod:
$ kubectl -n mynamespace get po mypod-0 -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mypod-0 2/2 Running 0 4d3h 192.168.181.98 node1.lan [none] [none]
The READY column shows how many containers are in the “ready” state in the given pod. The IP column shows the allocated IP address of this pod inside the Kubernetes cluster. The NODE column shows on which node the pod is running (or is scheduled).
Get the full state in YAML of the same pod as above:
$ kubectl -n mynamespace get pods/mypod -o yaml apiVersion: v1 kind: Pod metadata: [ --- snip --- ]
To get the services in the default namespace:
$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.100.0.1 [none] 443/TCP 24d mysql ClusterIP 10.100.78.115 [none] 3306/TCP 22d mysql-headless ClusterIP None [none] 3306/TCP 22d
The kubernetes service is used to access the Kubernetes API from within the cluster and is usually located in the default namespace.
kubernetes
To get the value of a secret:
$ kubectl -n mynamespace get secrets MYSECRET -o 'jsonpath={.data.DB_PASSWORD}' | base64 -d SuperSecretPassword
Kubernetes stores secrets as base64-encoded values, hence the | base64 -d at the end to show a human-readable value. The base64 command is available on many operating systems, but you might need to change for your OS.
| base64 -d
Retrieve the client and server version for the current context:
kubectl version
To check only the kubectl client version, use:
kubectl version --client
For machine-readable output, use:
kubectl version -o yamlkubectl version -o json
These commands directly instruct Kubernetes to perform a specific operation on a given object. This section will show the most common such commands in no particular order. These are useful during the development stages, but you should definitely avoid them in a production environment.Use kubectl create to create a Kubernetes object, except for pods that are created using the kubectl run command. So to create a pod directly:
kubectl create
kubectl run
$ kubectl run debug --image=busybox -- sleep infinity pod/debug created $ kubectl get pod NAME READY STATUS RESTARTS AGE debug 1/1 Running 0 6s
If you only want to generate the YAML first instead of creating the pod immediately, use:
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
Using the run command is good enough for running simple pods. For more complex pods with multiple containers, persistent volumes, mounted ConfigMaps or Secrets, and a lot of environment variables, this method will very quickly become intractable, and it’s much easier to use a manifest file.To delete the pod:
run
$ kubectl delete pod/debug pod "debug" deleted
You can directly create all other Kubernetes objects using the kubectl create command. Here is a (silly) example for creating a Deployment:
$ kubectl create deployment nginx --image=nginx --replicas=2 deployment.apps/nginx created $ kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-6clhp 1/1 Running 0 9s nginx-6799fc88d8-cjz56 1/1 Running 0 9s
Again, you can delete the deployment using the delete command:
delete
$ kubectl delete deployment nginx deployment.apps "nginx" deleted
You can modify the deployment like so (this will kick off your favorite text editor):
$ kubectl edit deployment nginx
If you don’t want kubectl to start a text editor, you can use the replace command. You will need to obtain the object’s state in YAML format first:
replace
$ kubectl get deploy/nginx -o yaml ] tmp.yaml $ # edit tmp.yaml $ kubectl replace -f tmp.yaml
Please note that not all elements of a Kubernetes object can be modified after it has been created.
Another method to modify a Kubernetes object is to use the patch command. With the above deployment object, such a command could look like this:
$ kubectl patch deployment/nginx -p '{"spec": {"replicas": 3}}' deployment.apps/nginx patched $ kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-8jtw9 1/1 Running 0 11m nginx-6799fc88d8-dgkqr 1/1 Running 0 24s nginx-6799fc88d8-vqnkj 1/1 Running 0 24s
The -p argument accepts either JSON or YAML, although JSON tends to be a bit easier to type in. In any case, you are probably thinking that this isn’t a user-friendly way of doing things, and you would be right. This is one example where working with manifest files and using the declarative style make things much easier.
-p
You can also use the kubectl set command to easily modify certain objects, but it is limited in scope and not generic. It is mostly used to modify the image of pods or deployments, for example:
$ kubectl set image deploy/nginx nginx=nginx:1.9.1 deployment.apps/nginx image updated $ kubectl get pod nginx-684c8b4f65-c6ws6 -o 'jsonpath={.spec.containers[?(@.name=="nginx")].image}' nginx:1.9.1
BTW, the jsonpath used above contains a filter on the “containers” array to extract only the information we want. Another way to modify objects is to add or change the labels and annotations attached to it, for example:
$ kubectl label deploy/nginx --overwrite app=frontend deployment.apps/nginx labeled $ kubectl get deploy nginx -o jsonpath='{.metadata.labels}' {"app":"frontend"} $ kubectl annotate deploy/nginx test=yes deployment.apps/nginx annotated $ kubectl get deploy/nginx -o 'jsonpath={.metadata.annotations}' {"deployment.kubernetes.io/revision":"2","test":"yes"}
Related content: Read our guide to kubectl patch
When you want to work in declarative mode, you will have a manifest file (or a set of them), and essentially use only one command:
$ kubectl apply -f X
Here, X is a file or a directory (you can add multiple -f arguments if necessary). If you want to make some changes to your existing Kubernetes objects, just modify the manifest files and run kubectl apply again. This will compute the differences between your desired state and the existing state and make the necessary changes to reconcile them. The only caveat is that it won’t delete objects that you removed from your manifest files.
-f
kubectl apply
If you use kustomization files, you should use the -k option instead, and the argument must be a directory:
$ kubectl apply -k DIR
Before applying a manifest, preview the changes with:
kubectl diff -f ./my-manifest.yaml
This compares the current cluster state with the state that would exist if the manifest were applied.
There is a special case where the reconciliation is problematic, which has to do with the Kubernetes authorization system. The reasons for this are quite obscure and are touched on here if you are interested. If you make changes to RBAC resources or roles, you should use the kubectl auth reconcile command. You can find more information on this in the official Kubernetes documentation.
kubectl auth reconcile
You can delete objects declared in manifests files by using the following command:
$ kubectl delete -f X
Related content: Read our guide to kubectl apply
Here are some useful commands to manage deployments (in a general sense, so that also includes stateful sets and daemon sets).
When you update a deployment (or stateful set or daemon set), you can view the status of the update using kubectl rollout status deploy/myapp. You can cancel a rollout using this command: kubectl rollout undo statefulset/myapp and get a history of changes using kubectl rollout history deployment/myapp.
kubectl rollout status deploy/myapp
kubectl rollout undo statefulset/myapp
kubectl rollout history deployment/myapp
Even if deployments are managed through Helm, GitOps, or another release tool, these commands are still useful for checking rollout status, validating a release, and responding quickly during an incident.
Check rollout status
kubectl rollout status deployment/
View rollout history
kubectl rollout history deployment/
Restart a deployment
kubectl rollout restart deployment/
kubectl rollout undo deployment/
You can modify the number of pods running for a given deployment using kubectl scale --replicas=N deploy/myapp, where N is the new desired number of replicas. The end result is the same as using kubectl edit deploy/myapp and modifying the number of replicas there.
kubectl scale --replicas=N deploy/myapp
kubectl edit deploy/myapp
Again, in practice, you are likely either to use Helm to perform static, manual changes or the pod autoscaler and not perform any manual operation. BTW, you can set up some basic autoscaling using kubectl autoscale, although that works only with one metric: CPU utilization.
kubectl autoscale
If your Docker registry is private, you will need to pass some credentials to Kubernetes. The easiest way to do this is to first login using the Docker command line, for example, on AWS:
$ aws ecr get-login-password | docker login -u AWS --password-stdin ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com
Once logged in, you can manually create the secret like so:
You can then use the secret SECRETNAME in the pod specification inside your manifest file like so:
imagePullSecrets: [name: SECRETNAME]
Please note that such secrets are usually valid only for a limited period of time.
It is very common to forget to set imagePullSecrets, in which case your pods will show a “pull image error”. So, if you see that error and you are using a private registry, you might want to check that you did set imagePullSecrets, and that the secret is not expired.
imagePullSecrets
Related content: Read our guides to kubectl rollout and kubectl config.
This section will list a bunch of commands that are very useful to interact with your pods.
Nothing beats getting a shell to a running pod! Here’s how to do it (you can skip the -c argument if you have only one container running inside the pod):
$ kubectl -n NS exec -it POD -c CONTAINER -- sh
Please note that the shell will be executed as the default user specified by the container’s image. Kubectl currently doesn’t allow you to run the shell as another user (e.g., root), although there are some plugins that can do that. Also note that you need an executable shell available inside the container image (so you can’t use it on distroless images, for example).
Consequently, you might need to think about how such containers will be troubleshooted. You might want to have two versions of such containers: One for development (with the shell and maybe other programs) and one for production (which would be distroless).
For containers that do not include a shell or debugging tools, use kubectl debug to create a temporary debugging session:
kubectl debug -it --image=busybox:1.28
You can also debug a node directly:
kubectl debug node/ -it --image=busybox:1.28
You can quickly create a link between a given port number on a given pod running inside the Kubernetes cluster and your computer (this is called port forwarding – learn more in our detailed guide on kubectl port forwarding). Here is how to do it, assuming your pod exposes port 8080:
You can then open up port 8888 on your local computer and that will forward the traffic to the pod MYPOD on its port 8080. This is very useful to do a quick check on your pod to verify that it looks OK.
You can copy files and directories using the kubectl cp command. We won’t go into detail here, but it is sometimes better for inspecting large and/or binary files on your computer rather than battling with the shell and a couple of utilities in an exec session.
kubectl cp
Finally, you can use kubectl attach to attach to the container terminal, although this is usually of limited use because you get the output from kubectl logs and, in general, apps don’t read stdin.
kubectl attach
kubectl logs
Related content: Read our guide to kubectl logs
It is useful to be able to complete your shell commands, and kubectl has a ready-made solution for that:
source <(kubectl completion bash)
Kubectl supports a number of shells, and you will need to consult Kubernetes documentation for more details. Please note you will also need to install the bash-completion package for your Operating System (if not already installed).
bash-completion
Also, the above command will enable autocompletion only for the current session; to make it permanent, add this command to your shell initialization file.
Pro tip: If, like me, you use bash and like to have an alias to abbreviate “kubectl” into “k”, add the following line into your ~/.bashrc file to make autocompletion work when using just “k”:
~/.bashrc
alias k=kubectlcomplete -o default -F __start_kubectl k
For commands across all namespaces, you can use the shorter -A flag instead of –all-namespaces:
You can show the APIs and resource types available on your cluster like so:
$ kubectl api-versions admissionregistration.k8s.io/v1 admissionregistration.k8s.io/v1beta1 apiextensions.k8s.io/v1 [ --- snip --- ] $ kubectl api-resources NAME SHORTNAMES APIVERSION NAMESPACED KIND bindings v1 true Binding componentstatuses cs v1 false ComponentStatus configmaps cm v1 true ConfigMap endpoints ep v1 true Endpoints events ev v1 true Event [ --- snip --- ]
Finally, a very useful command to test your RBAC rules is kubectl auth can-i. Here is an example to test whether a given service account can read a certain config map:
kubectl auth can-i
You will obviously need to replace the capitalized placeholders with the names that make sense for your setup.
Related content: Read our guide to kubectl autocomplete.
Kubectl is essential for inspecting Kubernetes resources, checking logs, reviewing events, and debugging workloads. But as clusters scale, manual troubleshooting can quickly turn into command-by-command investigation across pods, deployments, namespaces, logs, events, alerts, configuration changes, and resource usage.
That is where Komodor helps.
Komodor is an autonomous AI SRE platform for Kubernetes, powered by Klaudia. Instead of relying only on manual kubectl investigation, Komodor helps teams detect, investigate, remediate, and optimize Kubernetes issues across complex cloud-native environments.
With Komodor, teams can:
Kubectl remains a critical tool for direct Kubernetes inspection. Komodor adds the operational context and automation layer teams need when troubleshooting moves beyond a single pod, namespace, or command.
If your team is spending too much time manually piecing together Kubernetes incidents, Komodor can help you move from reactive kubectl investigation to autonomous AI SRE workflows.
Kubectl is the Kubernetes command-line tool used to communicate with a Kubernetes cluster through the Kubernetes API. You can use kubectl to deploy applications, inspect resources, manage Kubernetes objects, view logs, update workloads, and troubleshoot issues across pods, nodes, namespaces, services, and deployments.
The most useful kubectl commands are kubectl get, kubectl describe, kubectl logs, kubectl exec, kubectl apply, kubectl delete, kubectl rollout, kubectl config, kubectl top, kubectl events, and kubectl debug. These commands help teams inspect resources, apply configuration changes, review logs, check events, manage deployments, and troubleshoot Kubernetes workloads.
Use kubectl get pods to list pods in the current namespace. To check pods across all namespaces, use kubectl get pods -A. For more details about a specific pod, use kubectl describe pod . This can help you review pod status, events, containers, restart counts, scheduling issues, and configuration details.
Use kubectl logs to view logs from a pod. For a specific container in a multi-container pod, use kubectl logs -c . To stream logs in real time, use kubectl logs -f . To check logs from a previously crashed container, use kubectl logs –previous.
A practical kubectl troubleshooting workflow is to check pod status, describe the pod, review recent events, inspect logs, and then use exec or debug if needed. Common commands include kubectl get pods -A, kubectl describe pod , kubectl get events –sort-by=.metadata.creationTimestamp, kubectl logs –previous, and kubectl debug -it –image=busybox:1.28.
Kubectl create creates a new Kubernetes resource from a file or command. Kubectl apply creates the resource if it does not already exist and updates it if it does. In most GitOps, CI/CD, and production workflows, kubectl apply -f is more common because it supports declarative configuration updates.
You can set the default namespace for the current context with kubectl config set-context –current –namespace=. You can also run a single command in a specific namespace with kubectl get pods -n . This is useful when working across multiple teams, applications, or environments in the same cluster.
Use kubectl config get-contexts to list available contexts, then switch clusters with kubectl config use-context . This is useful when working across multiple Kubernetes clusters, cloud accounts, environments, or regions.
Use kubectl get events –sort-by=.metadata.creationTimestamp to view Kubernetes events sorted by creation time. To focus on warnings, use kubectl events –types=Warning. Events are especially useful for troubleshooting scheduling failures, image pull errors, failed probes, pod restarts, and other workload issues.
Use kubectl top pod to check pod resource usage and kubectl top node to check node resource usage. These commands show current CPU and memory usage, but they require Metrics Server or another compatible metrics pipeline to be available in the cluster.
Use kubectl rollout restart deployment/ to restart a deployment. Then check the rollout status with kubectl rollout status deployment/. This is commonly used after configuration changes, secret updates, or when forcing pods to restart cleanly.
Kubectl helps with Kubernetes troubleshooting by giving teams direct access to cluster state, workload status, logs, events, resource usage, and configuration details. It can help identify why pods are pending, crashing, restarting, failing probes, pulling images incorrectly, or consuming too much CPU and memory.
Kubectl is essential for direct Kubernetes inspection, but manual troubleshooting can be slow when teams need to correlate pod status, logs, events, deployments, configuration changes, and resource usage across many clusters. Komodor’s autonomous AI SRE platform helps teams detect, investigate, remediate, and optimize Kubernetes issues with less manual command-by-command investigation.
Share:
Gain instant visibility into your clusters and resolve issues faster.
May 12 · 9:00EST / 15:00 CET · Live & Online
🎯 8+ Sessions 🎙️ 10+ Speakers ⚡ 100% Free
By registering you agree to our Privacy Policy. No spam. Unsubscribe anytime.
Check your inbox for a confirmation. We'll send session links closer to May 12.