Essential kubectl Commands and Concepts for Managing Kubernetes Clusters
This article introduces the role of the Kubernetes API server, explains kubectl syntax and concepts, and provides detailed explanations of eight essential kubectl commands—including listing resources, describing objects, creating and modifying resources, deleting objects, handling PersistentVolumes, configuring security contexts, managing deployments, and executing commands inside pods—helping readers master everyday Kubernetes operations.
Kubernetes is a container orchestration platform composed of a control‑plane (master) node and worker nodes, and all communication goes through the API server, which exposes an HTTP REST API for internal and external components.
The API server can be seen as the primary user interface or front‑end of Kubernetes, allowing you to query, update, or manage the state of Kubernetes objects and resources via direct REST calls, client libraries, or the kubectl command‑line tool.
Kubectl Concepts
Before using kubectl, it is important to understand its command structure. The general syntax is: kubectl [command] [TYPE] [NAME] [flags] The components are:
command – the operation to perform (e.g., create, get, apply, delete). Commands either create new objects, modify existing ones, or retrieve information.
TYPE – the resource type, such as pod, service, deployment, daemonset, statefulset, job, or cronjob.
NAME – the case‑sensitive name of the specific resource. Supplying a name limits the command to that resource; omitting it applies to all resources of the given type in the current namespace.
flags – optional modifiers that override default values or request specific output.
Eight Essential kubectl Commands
1. List Kubernetes Resources
Use kubectl get to list one or more resources, e.g., kubectl get pods lists all pods. Adding -o wide shows additional fields such as the node name. You can also list services, replication controllers, etc., and filter by node or use short aliases.
2. Describe
While kubectl get provides a concise list, kubectl describe gives a detailed report of one or more resources. For example, kubectl describe pods shows in‑depth information about each pod, and kubectl describe nodes displays node details.
3. Create and Modify
The kubectl apply command creates resources from a file or standard input. Example: kubectl apply -f servicename.yaml creates a new Service from the specified YAML file. Similarly, kubectl apply -f controllername.yaml can create a ReplicationController, and you can apply entire directories of JSON/YAML manifests.
4. Delete
kubectl deleteremoves unwanted services and resources, freeing compute capacity. Examples include kubectl delete pods --all to delete all pods, or kubectl delete -f examplepod.yaml to delete a pod defined in a YAML file. You can also delete resources by label, e.g., kubectl delete pods,services -l name=example-name.
5. PersistentVolume (PV)
PersistentVolumes provide storage that outlives pod lifecycles. They can be manually provisioned or dynamically created via StorageClasses. Useful commands include:
kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml– creates a PV.
kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml– creates a PersistentVolumeClaim (PVC) that binds to a suitable PV. kubectl get pv – shows the status of PVs.
6. Security Context
Security contexts allow you to define security‑related settings for pods and containers, such as the user and group IDs under which they run. Include a securityContext block in your pod spec, for example:
securityContext:
runAsUser: 1000
fsGroup: 2000Unlike RBAC, a security context does not require separate Role or RoleBinding objects; it is applied automatically when the pod is created.
7. Managing Deployments
Several commands help manage Deployments, StatefulSets, and DaemonSets. Use kubectl rollout status to view the progress of an update, kubectl rollout undo <resource> to roll back, and kubectl rollout history <resource> for change history. Scaling can be done with kubectl scale --replicas=N <deployment> or by editing the deployment directly. Autoscaling is available via kubectl autoscale, which currently supports CPU‑based metrics.
8. Executing Commands
The kubectl exec command runs arbitrary commands inside a container of a pod. Example: kubectl exec mypod -- date runs date in the first container of mypod. You can target a specific container with -c container-name.
Conclusion
While this list does not cover every possible operation, it provides enough commands to handle many routine tasks in Kubernetes and helps you become more proficient with containerized environments.
This article is translated from "Kubernetes Cheat Sheet: 8 Commands You Can’t Do Without" by Gilad David Mayaan.
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.
Cloud Native Technology Community
The Cloud Native Technology Community, part of the CNBPA Cloud Native Technology Practice Alliance, focuses on evangelizing cutting‑edge cloud‑native technologies and practical implementations. It shares in‑depth content, case studies, and event/meetup information on containers, Kubernetes, DevOps, Service Mesh, and other cloud‑native tech, along with updates from the CNBPA alliance.
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.
