Cloud Native 16 min read

Mastering kubectl: Essential Commands and Tips for Kubernetes Management

This guide provides a comprehensive overview of the kubectl command‑line tool, covering its syntax, core components, common commands, output formatting options, custom columns, server‑side printing, sorting, and practical examples for everyday Kubernetes cluster operations.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Mastering kubectl: Essential Commands and Tips for Kubernetes Management

kubectl Overview

kubectl is the command‑line tool for interacting with a Kubernetes cluster. It reads a config file from $HOME/.kube/config (or a file specified with --kubeconfig) and communicates with the API server to create, read, update, and delete resources.

Basic Syntax

kubectl [command] [TYPE] [NAME] [flags]

command : operation such as create, get, describe, delete, etc.

TYPE : resource type (case‑insensitive, singular, plural, or short name). Examples:

kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1

NAME : optional resource name; omitted lists all resources of the type.

flags : optional parameters, e.g., -s or --server to specify the API server address.

Specifying Multiple Resources

By type and name: kubectl get pod pod1 pod2 Mixed types: kubectl get pod/pod1 deployment/rc1 From files: kubectl get -f ./pod.yaml (YAML preferred over JSON).

Help

Run kubectl --help for the full help text.

Common Commands

annotate :

kubectl annotate (-f FILE | TYPE NAME) KEY=VAL ... [--overwrite] [--all]

api-versions : kubectl api-versions apply : kubectl apply -f FILE attach : kubectl attach POD -c CONTAINER [-i] [-t] autoscale :

kubectl autoscale (-f FILE | TYPE NAME) --min=MIN --max=MAX [--cpu-percent=CPU]

cluster-info : kubectl cluster-info config : kubectl config SUBCOMMAND create : kubectl create -f FILE delete : kubectl delete (-f FILE | TYPE NAME | TYPE/NAME) [flags] describe : kubectl describe TYPE NAME diff : kubectl diff -f FILE (beta)

edit : kubectl edit (-f FILE | TYPE NAME) exec :

kubectl exec POD [-c CONTAINER] [-i] [-t] -- COMMAND [args...]

explain : kubectl explain RESOURCE expose :

kubectl expose (-f FILE | TYPE NAME) [--port=PORT] [--type=TYPE]

get : kubectl get TYPE [NAME] [flags] label : kubectl label (-f FILE | TYPE NAME) KEY=VAL ... logs : kubectl logs POD [-c CONTAINER] [--follow] patch : kubectl patch (-f FILE | TYPE NAME) --patch PATCH port-forward : kubectl port-forward POD LOCAL:REMOTE [...] proxy : kubectl proxy [--port=PORT] replace : kubectl replace -f FILE run : kubectl run NAME --image=IMAGE [--port=PORT] [--dry-run=...] scale : kubectl scale (-f FILE | TYPE NAME) --replicas=N version :

kubectl version [--client]

Output Formatting

The default output is human‑readable text. Use -o or --output to select formats such as yaml, json, wide, or custom-columns.

kubectl get pod web-pod-13je7 -o yaml

Custom Columns

Define custom columns inline or via a file:

kubectl get pods my-pod -ocustom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion

Or with a file:

kubectl get pods my-pod -ocustom-columns-file=template.txt

Server‑Side Columns

From Kubernetes 1.11+, the server can return specific columns. Disable with --server-print=false:

kubectl get pods my-pod --server-print=false

Sorting

Sort results with --sort-by=JSONPATH:

kubectl get pods -n kube-system --sort-by=.metadata.name

Resource Types

Supported resource types and short names can be listed with: kubectl api-resources Example output (Kubernetes 1.13.3) is shown in the image below:

Resource types table
Resource types table

Common Operation Examples

Apply a manifest kubectl apply -f example-service.yaml List pods kubectl get pods Describe a node kubectl describe node <node-name> Delete a pod by file kubectl delete -f pod.yaml Execute a command in a container kubectl exec my-pod -- /bin/bash Stream pod logs

kubectl logs -f my-pod
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLICloud NativeKubernetesDevOpskubectlKubernetes Commands
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.