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.
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 pod1NAME : 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
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 pod2Example of multiple object types
kubectl get pod/pod1 rc/rc1Applying multiple YAML files
kubectl get pod -f pod1.yaml -f pod2.yaml kubectl create -f pod1.yaml -f rc1.yaml -f service1.yamlkubectl 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 output formats
The output format is controlled with the -o flag.
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>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
