Master kubectl: Essential Commands and Tips for Kubernetes Management
This guide introduces the fundamental syntax of kubectl, explains its command structure, resource types, flags, output formats, and provides a comprehensive collection of practical examples—from creating resources with YAML files to querying pods, managing services, and executing commands inside containers—helping users efficiently operate Kubernetes clusters.
We already know common Kubernetes terminology and concepts; to develop or simply run a small instance, you need to be familiar with typical operations. Learning kubectl is a fast way to get started.
1. kubectl Syntax
kubectl follows the pattern: kubectl [command] [TYPE] [NAME] [flags] command : sub‑command that operates on cluster resources, e.g., create, delete, describe, get, apply, etc.
TYPE : the resource type (case‑sensitive), which can be expressed in singular, plural, or short form. For example, pod, pods, and po are equivalent.
NAME : the name of the specific resource. If omitted, the command acts on all objects of the given TYPE.
flags : optional parameters for the sub‑command, such as -s to specify the API server URL.
Examples of specifying resources:
kubectl get pod pod1 kubectl get pods pod1 pod2 kubectl get pod/pod1 rc/rc1 kubectl get -f pod1.yaml -f pod2.yaml kubectl create -f pod1.yaml -f rc1.yaml -f service1.yaml2. kubectl Subcommands
kubectl offers a rich set of subcommands covering most operations on a Kubernetes cluster, including creation, deletion, inspection, configuration, and execution. The table below summarizes the most common subcommands.
3. Common Global Parameters
The following image lists the public startup parameters for kubectl.
4. Output Formats
kubectl can display results in various formats, selected with the -o flag.
5. Practical kubectl Examples
Below are common usage scenarios with the corresponding commands. kubectl create -f my-service.yaml -f my-rc.yaml Create resources from a directory containing YAML/JSON files: kubectl create -f <directory> List all pods: kubectl get pods List replication controllers and services: kubectl get rc,service Show detailed node information: kubectl describe nodes <node-name> Show detailed pod information: kubectl describe pods/<pod-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 inside a pod (default first container): kubectl exec <pod-name> date Execute a command in a specific container of a pod:
kubectl exec <pod-name> -c <container-name> dateOpen an interactive bash shell in a container:
kubectl exec -it <pod-name> -c <container-name> /bin/bashView container logs: kubectl logs <pod-name> Follow container logs (similar to 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.
