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.
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
getcommand 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
editcommand 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
applycommand 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
describeshows 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
logsretrieves pod logs, which can be filtered with
grepor limited to a specific container using
-c <container>:
<code>$ kubectl logs cherry-chart-88d49478c-dmcfv -n charts | grep -vie kube-probe</code>kubectl exec
execruns 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
cpcopies 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
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.