Kubernetes Entry Created: 05 Mar 2026 Updated: 05 Mar 2026

Common kubectl Commands

Overview

The kubectl command-line utility is the Swiss Army knife of Kubernetes. Before you start creating Pods, Deployments, and Services, you need to master the everyday kubectl commands that work across all Kubernetes objects. This article covers namespaces, contexts, viewing objects, creating and deleting resources, labeling, debugging, and cluster management — everything you need to operate confidently from the terminal.

Prerequisites

  1. A running Kubernetes cluster (local via minikube / kind, or a cloud-based cluster)
  2. kubectl installed and configured on your machine
  3. Basic familiarity with YAML and JSON file formats
  4. Comfortable using a terminal / command line

Core Concepts

Namespaces

Namespaces are like folders inside your cluster. They organize Kubernetes objects into logical groups. By default, kubectl works with the default namespace. If your team uses separate namespaces for different environments (e.g., dev, staging, production), you can target a specific namespace with the --namespace (or -n) flag.

If you want to see objects across every namespace at once, use the --all-namespaces (or -A) flag.

Contexts

Typing --namespace=mystuff on every command gets tedious. A context saves your preferred namespace (and cluster/user settings) so kubectl remembers them automatically. Contexts are stored in your kubeconfig file, usually located at $HOME/.kube/config.

Think of a context as a bookmark: it remembers which cluster you're talking to, which user credentials to use, and which namespace to default to.

Kubernetes API Objects

Everything in Kubernetes is a RESTful resource (also called a Kubernetes object). Each object lives at a unique HTTP path on the API server. For example, a Pod named my-pod in the default namespace can be found at a URL like /api/v1/namespaces/default/pods/my-pod. When you run kubectl commands, it makes HTTP requests to these URLs behind the scenes.

Declarative vs. Imperative Management

Kubernetes encourages a declarative approach: you describe the desired state in a YAML file and let Kubernetes figure out how to get there. The kubectl apply command is the primary tool for this — it creates objects if they don't exist, and updates them if they do. This is different from imperative commands like kubectl create which only create and will fail if the object already exists.

Labels and Annotations

Labels are key-value pairs attached to objects that are used for identifying and selecting resources. Annotations are also key-value pairs, but they hold non-identifying metadata (e.g., build info, contact details). Both can be added, modified, or removed from the command line.

Cluster Management: Cordon and Drain

Sometimes you need to take a node out of service — for maintenance, repairs, or upgrades. Kubernetes provides two commands for this:

  1. Cordon — Marks a node as unschedulable. No new Pods will be placed on it, but existing Pods keep running.
  2. Drain — Evicts all Pods from the node, moving them to other healthy nodes. Use this before physically removing a machine.

After the maintenance is done, you uncordon the node to allow Pods to be scheduled on it again.

Hands-On: Kubernetes Commands

Namespaces

List objects in a specific namespace:

kubectl get pods --namespace=mystuff

Shorthand version with -n:

kubectl get pods -n mystuff

List Pods across all namespaces:

kubectl get pods --all-namespaces

Contexts

Create a new context with a custom default namespace:

kubectl config set-context my-context --namespace=mystuff

Switch to the newly created context:

kubectl config use-context my-context

From now on, all kubectl commands will target the mystuff namespace by default — no need to pass -n every time.

Viewing Objects

List all resources of a given type in the current namespace:

kubectl get pods

Get a specific object by name:

kubectl get pods my-pod

Get more details with wide output:

kubectl get pods -o wide

View the full object definition in YAML:

kubectl get pods my-pod -o yaml

View the full object definition in JSON:

kubectl get pods my-pod -o json

Remove headers from the output (useful for piping to other commands):

kubectl get pods --no-headers

Extract a specific field using JSONPath (e.g., the Pod's IP address):

kubectl get pods my-pod -o jsonpath --template={.status.podIP}

View multiple object types at once:

kubectl get pods,services

Get a detailed, human-readable description of an object:

kubectl describe pods my-pod

See the supported fields for a resource type:

kubectl explain pods

Watch an object for live changes:

kubectl get pods my-pod --watch

Creating, Updating, and Deleting Objects

Create or update an object from a YAML file (declarative approach):

kubectl apply -f obj.yaml

Preview what apply would do without making changes:

kubectl apply -f obj.yaml --dry-run=client

Edit a live object interactively in your default editor:

kubectl edit deployment my-deployment

View the last applied configuration for an object:

kubectl apply -f myobj.yaml view-last-applied

Delete an object using its YAML file:

kubectl delete -f obj.yaml

Delete an object by type and name:

kubectl delete pods my-pod

Labeling and Annotating Objects

Add a label to a Pod:

kubectl label pods bar color=red

Overwrite an existing label:

kubectl label pods bar color=blue --overwrite

Remove a label from a Pod (note the trailing -):

kubectl label pods bar color-

Add an annotation to a Pod:

kubectl annotate pods bar description="my test pod"

Debugging Commands

View logs of a running Pod:

kubectl logs my-pod

View logs of a specific container in a multi-container Pod:

kubectl logs my-pod -c my-container

Stream logs continuously (like tail -f):

kubectl logs my-pod -f

Open an interactive shell inside a running container:

kubectl exec -it my-pod -- bash

Attach to the primary process of a running container:

kubectl attach -it my-pod

Copy a file from a container to your local machine:

kubectl cp my-pod:/path/to/remote/file /path/to/local/file

Copy a file from your local machine to a container:

kubectl cp /path/to/local/file my-pod:/path/to/remote/file

Forward a local port to a Pod (local 8080 → container 80):

kubectl port-forward my-pod 8080:80

Forward a local port to a Service:

kubectl port-forward services/my-service 8080:80

View recent cluster events:

kubectl get events

Stream cluster events in real time across all namespaces:

kubectl get events --watch -A

View resource usage by nodes:

kubectl top nodes

View resource usage by Pods in the current namespace:

kubectl top pods

View resource usage by all Pods across every namespace:

kubectl top pods --all-namespaces

Cluster Management

Prevent new Pods from being scheduled on a node:

kubectl cordon node-name

Evict all Pods from a node (before maintenance):

kubectl drain node-name

Re-enable scheduling on a node (after maintenance):

kubectl uncordon node-name

Command Autocompletion

Install bash-completion (choose your OS):

# macOS
brew install bash-completion

# CentOS / Red Hat
yum install bash-completion

# Debian / Ubuntu
apt-get install bash-completion

Activate kubectl autocompletion for your current terminal session:

source <(kubectl completion bash)

Make it permanent by adding it to your shell profile:

echo "source <(kubectl completion bash)" >> ${HOME}/.bashrc

Getting Help

View general kubectl help:

kubectl help

View help for a specific command:

kubectl help get

Step-by-Step Example

Let's walk through a realistic workflow: creating an object, inspecting it, labeling it, debugging it, and cleaning up.

  1. Set up your context so you work in a dedicated namespace:
kubectl create namespace demo
kubectl config set-context demo-ctx --namespace=demo --cluster=minikube --user=minikube
kubectl config use-context demo-ctx
  1. Create a simple Pod from a YAML file:
  2. Assume you have a file called nginx-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
  1. Apply it:
kubectl apply -f nginx-pod.yaml
  1. Verify the Pod is running:
kubectl get pods
  1. Expected output:
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 10s
  1. Add a label to the Pod:
kubectl label pods nginx env=demo
  1. Inspect the Pod in detail:
kubectl describe pods nginx
  1. Check the Pod logs:
kubectl logs nginx
  1. Open an interactive shell inside the container:
kubectl exec -it nginx -- bash
  1. Type exit to leave the shell.
  2. Forward traffic to the Pod and test it locally:
kubectl port-forward nginx 8080:80
  1. Open http://localhost:8080 in your browser — you should see the nginx welcome page. Press Ctrl+C to stop forwarding.
  2. Clean up — delete the Pod:
kubectl delete -f nginx-pod.yaml

Summary

  1. Namespaces organize objects into logical groups. Use -n to target a specific namespace or --all-namespaces to see everything.
  2. Contexts save your default namespace, cluster, and user so you don't have to type them every time.
  3. Use kubectl get to list objects, kubectl describe for detailed info, and kubectl explain to explore resource fields.
  4. Use kubectl apply -f to create or update objects declaratively from YAML files.
  5. Use kubectl delete to remove objects — be careful, there is no confirmation prompt.
  6. Use kubectl label and kubectl annotate to tag objects with metadata.
  7. For debugging: kubectl logs, kubectl exec, kubectl port-forward, and kubectl top are your best friends.
  8. For cluster maintenance: kubectl cordon, kubectl drain, and kubectl uncordon let you safely take nodes in and out of service.
  9. Enable tab autocompletion to speed up your workflow — it saves a lot of typing.

Further Reading

  1. kubectl Reference Documentation
  2. kubectl Cheat Sheet
  3. Namespaces
  4. Organizing Cluster Access Using kubeconfig Files
  5. Debug Applications

Link Interceptor Active


Share this lesson: