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:

<code>$ kubectl get ns</code>

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:

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

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

:

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

kubectl delete

Resources can be removed with

delete

. Example:

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

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:

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

2. Troubleshooting with kubectl

kubectl describe

describe

shows detailed information about resources such as Nodes, Pods, Services, Deployments, ReplicaSets, and CronJobs. Example:

<code>$ kubectl describe cronjob my-cron</code>

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 &lt;container&gt;

:

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

kubectl exec

exec

runs commands inside a container, similar to

docker exec

, useful when logs are insufficient:

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

kubectl cp

cp

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

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

Images

KubernetesDevOpsTroubleshootingcommand lineCluster Managementkubectl
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

login 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.