1001Ferramentas
๐ŸšขGenerators

kubectl Command Builder

Build common kubectl commands with namespace, labels and output formats.


  

kubectl in depth: verbs, resources, output formats, and context management

kubectl (pronounced "kube-control" or "kube-cuddle") is the primary CLI for Kubernetes. It is a thin client that talks to the cluster's API server over HTTPS โ€” every command becomes a REST call against /api or /apis, authenticated via the kubeconfig file at ~/.kube/config. Understanding kubectl is really understanding the API: once you know how a Pod, Deployment, and Service are shaped, the CLI is just a way to list, fetch, create, and patch those objects.

The general syntax is kubectl <verb> <TYPE>[/NAME] [flags], where the verb describes the action and the type identifies the resource kind.

Core verbs

  • get โ€” list resources (most-used command of all).
  • describe โ€” verbose human-readable detail including events.
  • create โ€” imperative create from CLI args; fails if the object exists.
  • apply -f file.yaml โ€” declarative create-or-update; the preferred GitOps workflow.
  • delete โ€” remove a resource (by name, label, or -f file.yaml).
  • logs โ€” fetch container stdout/stderr; -f to tail, --previous for the last crashed instance.
  • exec -it pod -- /bin/sh โ€” open a shell inside a container.
  • port-forward โ€” tunnel a local port to a pod/service for debugging.
  • cp โ€” copy files between local and container.
  • top pods / top nodes โ€” live CPU/memory (requires metrics-server).
  • rollout status / rollout undo โ€” track and revert Deployment changes.
  • scale deployment api --replicas=5 โ€” change replica count without editing YAML.

Common resource types

Each type has a short alias usable everywhere:

po       pods
deploy   deployments
svc      services
ing      ingresses
cm       configmaps
sec      secrets
ns       namespaces
no       nodes
pv       persistentvolumes
pvc      persistentvolumeclaims
sts      statefulsets
ds       daemonsets
job/cj   jobs / cronjobs
sa       serviceaccounts
ep       endpoints
hpa      horizontalpodautoscalers

Run kubectl api-resources to see every type the cluster supports, including CRDs.

Worked examples

# Pods in a namespace
kubectl get pods -n my-ns

# Pods across all namespaces
kubectl get pods -A

# Verbose details + recent events
kubectl describe pod nginx-abc123

# Tail logs of a deployment (picks any pod)
kubectl logs -f deploy/api --tail=200

# Get a shell inside the first container
kubectl exec -it api-7d4f -- /bin/sh

# Apply manifests from a directory tree
kubectl apply -f k8s/

# Watch a rolling update
kubectl rollout status deployment/api -w

# Roll back the last deploy
kubectl rollout undo deployment/api

# Scale to 5 replicas
kubectl scale deployment api --replicas=5

# Port-forward a service to localhost
kubectl port-forward svc/api 8080:80

# Throwaway debug pod
kubectl run debug --image=alpine -it --rm -- sh

Output formats

  • -o wide โ€” extra columns (node, IP).
  • -o yaml / -o json โ€” full object as you would write it.
  • -o name โ€” just type/name, handy for shell pipes.
  • -o jsonpath='{.items[*].metadata.name}' โ€” extract a single field.
  • -o custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[0].image โ€” tabular custom view.
  • --watch / -w โ€” stream updates instead of one snapshot.

Contexts and kubeconfig

A context bundles a cluster, a user, and a default namespace. Switch between them with:

kubectl config get-contexts          # list
kubectl config current-context       # show active
kubectl config use-context prod      # switch
kubectl config set-context --current --namespace=staging

Tools like kubectx and kubens wrap these commands with arrow-key pickers, and most teams set up alias k=kubectl with shell completion: source <(kubectl completion bash).

FAQ

How do I debug a CrashLoopBackOff pod? Combine kubectl describe pod <name> (look at Events at the bottom), kubectl logs <pod> --previous (logs from the crashed container), and kubectl get events --sort-by=.lastTimestamp on the namespace.

Why does kubectl delete pod keep recreating the pod? Because a Deployment, ReplicaSet, or StatefulSet owns it and the controller immediately spawns a replacement. Delete the owner (kubectl delete deploy api) or scale it to 0.

create vs apply โ€” which one? Always prefer apply -f in scripts: it is idempotent and tracks intent in a special last-applied-configuration annotation. create is fine for ad-hoc one-shots from CLI flags (kubectl create secret generic ...).

How can I copy a file out of a pod? kubectl cp my-ns/pod-name:/path/in/container ./local-file. The container needs tar installed (works for all distroless and standard images).

How do I explore a resource schema? kubectl explain pod.spec.containers.resources โ€” works recursively, with --recursive for everything. Pulls live OpenAPI from the cluster, so it always matches your version.

How long does a rolling update take? Depends on maxSurge, maxUnavailable, readiness probe timing, and pod startup time. Watch progress with kubectl rollout status deploy/api; if it stalls, kubectl describe the new ReplicaSet for the reason.

Related Tools