What Is kubectl? Check Contexts, Apply Changes Safely, and Troubleshoot
`kubectl` is the command-line client for inspecting, creating, and changing Kubernetes resources through the Kubernetes API. A command uses local configuration to choose a cluster and user credentials, sends a request to the API server, and returns a result based on permissions and object state. Safe use starts by confirming **which cluster and namespace the command targets**; changes should be reviewed and applied from declarative files.
How does kubectl connect to a cluster?
By default, kubectl reads cluster addresses, user credentials, and contexts from the config file under the user’s .kube directory. A context is a convenient name grouping a cluster, a user, and a default namespace. The current-context becomes the default target, so opening a terminal does not guarantee that the intended development, staging, or production cluster is selected. Flags such as --context and --namespace make a command’s target explicit.
kubectl config get-contexts
kubectl config current-context
kubectl config use-context <context-name>
kubectl get pods --context <context-name> --namespace <namespace>The first two commands inspect configuration; use-context changes the default context in the local kubeconfig. Supplying --context and --namespace on each command makes it less dependent on hidden defaults. The KUBECONFIG environment variable can name multiple files to merge, while --kubeconfig selects one file. Do not use kubeconfig files from untrusted sources: a crafted file can expose files or cause code execution. See Organizing Cluster Access Using kubeconfig Files for merge rules.
What does a kubectl command look like?
The general form is kubectl [command] [TYPE] [NAME] [flags]. For example, get reads objects, describe shows an object’s status and related events, and logs reads container output. apply changes cluster objects from configuration files; exec runs a command inside a running container. Omitting resource names or options can broaden a command, so identify the namespace and target clearly.
| Goal | Example | Consideration |
|---|---|---|
| Check target | kubectl config current-context | Shows only the current default context. |
| List resources | kubectl get pods -n shop | Without -n, uses the context’s default namespace. |
| Inspect one object and its events | kubectl describe pod checkout-7d9f -n shop | Events are clues; compare them with relevant container logs. |
| Read logs | kubectl logs deploy/checkout -n shop | Multiple containers or a restarted container may require -c or --previous. |
| Review a change | kubectl diff -f ./app/ | May require server-side dry run and permissions. It shows differences; it does not apply them. |
| Apply configuration | kubectl apply -f ./app/ | Creates or changes actual cluster objects. |
kubectl turns commands into Kubernetes API requests. For instance, get is a read request, while apply requests object changes. The API server rejects requests the user is not authorized to make. A successful command does not by itself prove that an application works; check object status and the real service path too. See the official kubectl command reference for command flags and output formats.
Why manage operational changes declaratively?
For operational configuration, prefer recording the desired state in YAML or another manifest and applying it with kubectl apply -f. Version-controlled files make changes easier to review, reproduce, and share. kubectl apply calculates a patch by comparing the manifest, the live object, and the prior apply record. If a field recorded by an earlier apply is removed from the file, it can be removed from the live object too, so review the meaning of file changes.
For example, to change a Deployment image, edit the manifest in version control, inspect the proposed difference with kubectl diff -f, and then run kubectl apply -f. Afterward, check the rollout with kubectl rollout status deployment/checkout -n shop and inspect kubectl get pods -n shop; check logs and the application response if needed. This is an illustrative workflow, not a report of a live-cluster test.
The official documentation does not support managing the same object by mixing apply with separate imperative create or replace commands. Also, kubectl diff uses server-side dry run and may require API-server support plus PATCH, CREATE, and UPDATE permissions. diff shows changes; it is not approval or application. See Declarative Management of Kubernetes Objects Using Configuration Files.
How can kubectl narrow down a Pod problem?
First confirm the cluster and namespace. Use read commands to see whether the symptom affects one object or nearby resources too.
Run `kubectl get pods -n <namespace> -o wide` to see Pod status and assigned nodes.
Run `kubectl describe pod <pod> -n <namespace>` to inspect conditions and Events. `FailedScheduling` can point to placement; image-pull errors to image, registry, or authentication; and `FailedMount` to volume setup.
Read the container output with `kubectl logs <pod> -n <namespace> -c <container>`. If the container restarted, `--previous` can retrieve the prior container’s logs.
Compare whether multiple Pods on the same node or in the same Deployment fail together. A wider pattern may implicate the node, network, storage, or shared configuration rather than one application.
After changing configuration, query again to see whether the object converges to the expected state. `exec` runs real commands inside a container, so use it only when needed and take care with writes and secret output.
Events can expire, and some diagnostic commands require RBAC permissions. If output is empty or says Forbidden, first check the target cluster, namespace, and your permissions. Do not grant broader access as a shortcut; ask an administrator for the narrow permission and namespace needed. The official Troubleshooting Applications guide covers logs, Events, describe, and exec.
Version compatibility and a safety check
kubectl version --client reports the client version; kubectl version also attempts to contact the cluster for server information. Current Kubernetes version-skew policy supports kubectl within one minor version older or newer than the API server. If API-server instances in a high-availability cluster run different minor versions, the supported range can narrow. Check the version skew policy before an upgrade.
Before a change, confirm the current context, specify the namespace and resource, start with reads for broad operations, review manifest differences, then verify rollout and actual behavior. Treat permission errors as a cue to check the target and required access, not as a reason to switch blindly to administrator privileges. Review the scope carefully for deletion, forced replacement, and --prune, which can remove objects.
Key takeaways
kubectl is a client that calls the Kubernetes API using the user’s configuration and permissions. Confirm context and namespace first, narrow problems with status reads, apply operational changes through reviewed declarative files, and verify the result. This reduces wrong-target changes and mistaken diagnoses.
Sources
[kubectl overview and command syntax](https://kubernetes.io/docs/reference/kubectl/)
[kubectl’s role and API request path](https://kubernetes.io/docs/concepts/overview/kubectl/)
[Organizing cluster access with kubeconfig](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/)
[Declarative management using configuration files](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/declarative-config/)
[Troubleshooting applications](https://kubernetes.io/docs/tasks/debug/debug-application/)
[Kubernetes version skew policy](https://kubernetes.io/releases/version-skew-policy/)
Scope: general kubectl usage, kubeconfig contexts, declarative changes, and workload diagnosis. Official Kubernetes documentation was reviewed on 2026-11-23. Commands are examples and were not run against a cluster. Check platform documentation for permissions, policies, and managed-cluster behavior.