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
getcommand lists resources such as Namespace, Pod, Node, Deployment, Service, and ReplicaSet. Example:
<code>$ kubectl get ns
NAME STATUS AGE
charts Active 8d
default Active 9d
kube-node-lease Active 9d
kube-public Active 9d
kube-system Active 9d</code>kubectl create
After identifying resources,
createcan provision new objects, e.g., Service, CronJob, Deployment, Job, or Namespace.
<code>$ kubectl create ns hello-there
namespace/hello-there created</code>Creating a CronJob that runs every five seconds:
<code>$ kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hello
cronjob.batch/my-namespaced-cron created</code>The short form
cjworks as well:
<code>$ kubectl create cj my-existing-cron --image=busybox --schedule="*/15 * * * *" -- echo hello
cronjob.batch/my-existing-cron created</code>kubectl edit
The
editcommand opens the default editor for any resource. Example editing a CronJob:
<code>$ kubectl edit cronjob/my-existing-cron</code>After editing, the change is applied:
<code>$ kubectl edit cronjob/my-existing-cron
cronjob.batch/my-existing-cron edited</code>You can also specify a different editor with
KUBE_EDITOR:
<code>$ KUBE_EDITOR="nano" kubectl edit cronjob/my-existing-cron</code>kubectl delete
To remove resources, use
delete. Example:
<code>$ kubectl delete cronjob my-existing-cron
cronjob.batch "my-existing-cron" deleted</code>Be cautious, as deletion is irreversible without a backup.
kubectl apply
The
applycommand applies configuration files to resources, often used with manifests such as Helm‑generated RBAC definitions.
<code>$ kubectl apply -f commands.yaml
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created</code>2. Troubleshooting with kubectl
kubectl describe
describeshows detailed information about a resource (e.g., Nodes, Pods, Services, Deployments, ReplicaSets, CronJobs).
<code>$ kubectl describe cronjob my-cron</code>Sample output includes schedule, concurrency policy, and pod template details.
kubectl logs
logsretrieves the stdout of a Pod. You can filter output with
grepor specify a container with
-c <container>.
<code>$ 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) ...</code> <code>$ kubectl logs cherry-chart-88d49478c-dmcfv -n charts | grep -v kube-probe</code>kubectl exec
execruns a command inside a container, similar to
docker exec:
<code>$ kubectl exec -it cherry-chart-88d49478c-dmcfv -n charts -- /bin/bash
root@cherry-chart-...#</code>kubectl cp
cpcopies files between the local machine and a container.
<code>$ kubectl cp commands_copy.txt charts/cherry-chart-88d49478c-dmcfv:commands.txt
$ kubectl cp charts/cherry-chart-88d49478c-dmcfv:commands.txt commands_copy.txt</code>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.