Cloud Native 6 min read

Master kubectl: Essential Commands and Tips for Managing Kubernetes

This guide provides a comprehensive overview of kubectl, covering its syntax, resource types, common flags, output formats, and dozens of practical command examples for creating, listing, describing, and deleting Kubernetes objects efficiently.

Efficient Ops
Efficient Ops
Efficient Ops
Master kubectl: Essential Commands and Tips for Managing Kubernetes

1. kubectl syntax

kubectl [command] [type] [NAME] [flags]

command : subcommand for operating Kubernetes resources, e.g., create, delete, describe, get, apply, etc.

TYPE : resource type, case‑sensitive, can be singular, plural, or abbreviated. Examples:

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

NAME : name of the resource; if omitted, all objects of the specified TYPE are listed.

flags : optional parameters for subcommands, e.g., -s to specify the API server URL.

kubectl resource types and abbreviations

kubectl resource types
kubectl resource types

You can operate on multiple resources in a single command by specifying multiple TYPE and NAME pairs.

Examples of multiple pods

kubectl get pods pod1 pod2

Example of multiple object types

kubectl get pod/pod1 rc/rc1

Applying multiple YAML files

kubectl get pod -f pod1.yaml -f pod2.yaml
kubectl create -f pod1.yaml -f rc1.yaml -f service1.yaml

kubectl subcommands

kubectl offers a rich set of subcommands for creating, deleting, viewing, modifying, configuring, and running resources. Detailed subcommand lists are typically presented in reference tables.

kubectl common parameters

kubectl common flags
kubectl common flags

kubectl output formats

The output format is controlled with the -o flag.

kubectl output formats
kubectl output formats

kubectl operation examples

Create a service and replication controller from YAML files: kubectl create -f my-service.yaml -f my-rc.yaml Create all resources defined in a directory: kubectl create -f <directory> List all pods: kubectl get pods List replication controllers and services: kubectl get rc,service Describe a node: kubectl describe nodes <node-name> Describe a pod: kubectl describe pods/<pod-name> Describe pods managed by a replication controller: kubectl describe pods <rc-name> Delete a pod defined in a YAML file: kubectl delete -f pod.yaml Delete all pods and services with a specific label: kubectl delete pods,services -l name=<label-name> Delete all pods: kubectl delete pods --all Execute a command in a pod (default first container): kubectl exec <pod-name> date Execute a command in a specific container: kubectl exec <pod-name> -c <container-name> date Open an interactive shell in a container: kubectl exec -it <pod-name> -c <container-name> /bin/bash View pod logs: kubectl logs <pod-name> Follow pod logs (like tail -f):

kubectl logs -f <pod-name> -c <container-name>
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 NativeKubernetesDevOpscommand-linekubectl
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

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.