Cloud Native 6 min read

Master kubectl: Essential Commands and Tips for Kubernetes

This guide walks you through the core kubectl syntax, common subcommands, global flags, output formats, and practical examples, enabling you to efficiently manage Kubernetes resources from the command line.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master kubectl: Essential Commands and Tips for Kubernetes

Understanding kubectl is essential for anyone who wants to develop on or simply operate a Kubernetes cluster. Below is a concise reference of the most frequently used commands, options, and usage patterns.

1. kubectl Syntax

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

command : the sub‑command that operates on cluster resources, e.g., create, delete, describe, get, apply etc.

TYPE : the resource type (case‑sensitive). It can be expressed in singular, plural, or short form. For example, the following three commands are equivalent:

kubectl get pod pod1</code>
<code>kubectl get pods pod1</code>
<code>kubectl get po pod1

NAME : the name of the specific resource. Omitting it returns a list of all objects of the given TYPE.

flags : optional parameters for the sub‑command, such as -s to specify a custom API server URL.

Multiple resource types and names can be combined in a single command, for example:

kubectl get pods pod1 pod2
kubectl get pod/pod1 rc/rc1

Multiple -f files can be supplied to apply several manifests at once:

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

2. kubectl Sub‑commands Overview

The sub‑commands cover the main operations on a Kubernetes cluster, including creation, deletion, inspection, configuration, and execution of resources. See the image below for a detailed table.

3. kubectl Global Parameters

Common flags that apply to all sub‑commands are listed in the following image.

4. Output Formats

The result format can be chosen with the -o flag, as illustrated below.

5. Practical kubectl Examples

Create a Service and ReplicationController 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 ReplicationControllers 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 with a specific label:

kubectl delete pods,services -l name=<label-name>

Delete all Pods in the cluster: 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:

kubectl exec <pod-name> -c <container-name> date

Open an interactive bash shell in a container:

kubectl exec -it <pod-name> -c <container-name> /bin/bash

View container logs: kubectl logs <pod-name> Tail logs of a specific container:

kubectl logs -f <pod-name> -c <container-name>

These commands provide a solid foundation for day‑to‑day Kubernetes operations.

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 NativeKubernetesDevOpskubectl
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.