Operations 10 min read

Master 9 Essential kubectl Commands for Efficient Kubernetes Management

This guide introduces nine commonly used kubectl commands—get, create, edit, delete, apply, describe, logs, exec, and cp—explaining their purposes, providing practical examples, and offering tips to help system administrators streamline Kubernetes resource management and troubleshooting.

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, used for deploying applications and routine administration. The article lists nine common kubectl commands and shares usage tips to help administrators simplify their workflow.

1. Query, Create, Edit, and Delete Resources

kubectl get

The get command retrieves lists of resources such as Namespace, Pod, Node, Deployment, Service, and ReplicaSet. Example: $ kubectl get ns Output shows each namespace with its status and age.

kubectl create

After querying, resources can be created. Supported types include Service, CronJob, Deployment, Job, and Namespace. Examples:

$ kubectl create ns hello-there</code><code>namespace/hello-there created
$ kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hello</code><code>cronjob.batch/my-namespaced-cron created
$ kubectl create cj my-existing-cron --image=busybox --schedule="*/15 * * * *" -- echo hello</code><code>cronjob.batch/my-existing-cron created

kubectl edit

The edit command opens the default editor to modify any resource, such as a CronJob. It can also use a custom editor via KUBE_EDITOR:

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

kubectl delete

Resources can be removed with delete. Example:

$ kubectl delete cronjob my-existing-cron</code><code>cronjob.batch "my-existing-cron" deleted

Be cautious, as deletion is irreversible without recreating the resource.

kubectl apply

The apply command applies configuration files to resources, useful for declarative management. Example applying an RBAC configuration for Helm:

$ kubectl apply -f commands.yaml</code><code>serviceaccount/tiller created</code><code>clusterrolebinding.rbac.authorization.k8s.io/tiller created

2. Troubleshooting with kubectl

kubectl describe

describe

shows detailed information about resources such as Nodes, Pods, Services, Deployments, ReplicaSets, and CronJobs. Example: $ kubectl describe cronjob my-cron Partial output includes schedule, concurrency policy, and pod template details.

kubectl logs

logs

retrieves pod logs, which can be filtered with grep or limited to a specific container using -c <container>:

$ kubectl logs cherry-chart-88d49478c-dmcfv -n charts | grep -vie kube-probe

kubectl exec

exec

runs commands inside a container, similar to docker exec, useful when logs are insufficient:

$ kubectl exec -it cherry-chart-88d49478c-dmcfv -n charts -- /bin/bash</code><code>root@cherry-chart-88d49478c-dmcfv:/#

kubectl cp

cp

copies files between the local machine and a container, aiding backup and recovery:

$ kubectl cp commands_copy.txt charts/cherry-chart-88d49478c-dmcfv:commands.txt
$ kubectl cp charts/cherry-chart-88d49478c-dmcfv:commands.txt commands_copy.txt

Images

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.

KubernetesDevOpstroubleshootingkubectlcommand-linecluster-management
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.