Master 9 Essential kubectl Commands for Efficient Kubernetes Management
This guide introduces nine essential kubectl commands—get, create, edit, delete, apply, describe, logs, exec, and cp—explaining how to query, provision, modify, and troubleshoot Kubernetes resources, with practical code snippets and tips for efficient cluster management.
Introduction
kubectl is the command‑line tool for managing Kubernetes clusters, allowing administrators to deploy applications and perform routine operations.
1. Query, Create, Edit and Delete Resources
kubectl get
The get command lists resources such as Namespace, Pod, Node, Deployment, Service, and ReplicaSet. Example:
$ kubectl get ns
NAME STATUS AGE
charts Active 8d
default Active 9d
kube-node-lease Active 9d
kube-public Active 9d
kube-system Active 9dkubectl create
After identifying resources, create can provision new objects, e.g., Service, CronJob, Deployment, Job, or Namespace.
$ kubectl create ns hello-there
namespace/hello-there createdCreating a CronJob that runs every five seconds:
$ kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hello
cronjob.batch/my-namespaced-cron createdThe short form cj works as well:
$ kubectl create cj my-existing-cron --image=busybox --schedule="*/15 * * * *" -- echo hello
cronjob.batch/my-existing-cron createdkubectl edit
The edit command opens the default editor for any resource. Example editing a CronJob: $ kubectl edit cronjob/my-existing-cron After editing, the change is applied:
$ kubectl edit cronjob/my-existing-cron
cronjob.batch/my-existing-cron editedYou can also specify a different editor with KUBE_EDITOR:
$ KUBE_EDITOR="nano" kubectl edit cronjob/my-existing-cronkubectl delete
To remove resources, use delete. Example:
$ kubectl delete cronjob my-existing-cron
cronjob.batch "my-existing-cron" deletedBe cautious, as deletion is irreversible without a backup.
kubectl apply
The apply command applies configuration files to resources, often used with manifests such as Helm‑generated RBAC definitions.
$ kubectl apply -f commands.yaml
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created2. Troubleshooting with kubectl
kubectl describe
describeshows detailed information about a resource (e.g., Nodes, Pods, Services, Deployments, ReplicaSets, CronJobs). $ kubectl describe cronjob my-cron Sample output includes schedule, concurrency policy, and pod template details.
kubectl logs
logsretrieves the stdout of a Pod. You can filter output with grep or specify a container with -c <container>.
$ kubectl logs cherry-chart-88d49478c-dmcfv -n charts
172.17.0.1 - - [19/Apr/2020:16:01:15 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"
... (additional log lines) ... $ kubectl logs cherry-chart-88d49478c-dmcfv -n charts | grep -v kube-probekubectl exec
execruns a command inside a container, similar to docker exec:
$ kubectl exec -it cherry-chart-88d49478c-dmcfv -n charts -- /bin/bash
root@cherry-chart-...#kubectl cp
cpcopies files between the local machine and a container.
$ kubectl cp commands_copy.txt charts/cherry-chart-88d49478c-dmcfv:commands.txt
$ kubectl cp charts/cherry-chart-88d49478c-dmcfv:commands.txt commands_copy.txtSigned-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.
