Cloud Native 10 min read

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.

Efficient Ops
Efficient Ops
Efficient Ops
Master 9 Essential kubectl Commands for Efficient Kubernetes 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   9d

kubectl 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 created

Creating a CronJob that runs every five seconds:

$ kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hello
cronjob.batch/my-namespaced-cron created

The short form cj works as well:

$ kubectl create cj my-existing-cron --image=busybox --schedule="*/15 * * * *" -- echo hello
cronjob.batch/my-existing-cron created

kubectl 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 edited

You can also specify a different editor with KUBE_EDITOR:

$ KUBE_EDITOR="nano" kubectl edit cronjob/my-existing-cron

kubectl delete

To remove resources, use delete. Example:

$ kubectl delete cronjob my-existing-cron
cronjob.batch "my-existing-cron" deleted

Be 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 created

2. Troubleshooting with kubectl

kubectl describe

describe

shows 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

logs

retrieves 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-probe

kubectl exec

exec

runs 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

cp

copies 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.txt
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.

CLIcommandskubectl
Efficient Ops
Written by

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.

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.