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 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 createdkubectl 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-cronkubectl delete
Resources can be removed with delete. Example:
$ kubectl delete cronjob my-existing-cron</code><code>cronjob.batch "my-existing-cron" deletedBe 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 created2. Troubleshooting with kubectl
kubectl describe
describeshows 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
logsretrieves 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-probekubectl exec
execruns 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
cpcopies 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.txtImages
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
