The newly released kubectl cheat sheet can help everyone understand useful commands related to the Kubernetes command-line utility.
Author: Jessica Cherry / Translation:Bach Proofread:Wenzi under the Starry Sky、bot (Caiyun)
Source: K8sMeetup Community / Original link
kubectl is a command-line management tool for Kubernetes, used for application deployment and daily management on Kubernetes. This article lists 9 common kubectl commands and summarizes some usage tips, hoping to help system administrators simplify management tasks.

<span>get</span>
command is a good choice.Kubectl get
<span>get</span>
command can retrieve a list of available resources in the current cluster, including:-
Namespace -
Pod -
Node -
Deployment -
Service -
ReplicaSet
<span>get</span>
command can provide detailed information about available resources in the cluster. For example, the <span>get nodes</span>
command provides the status and version of Kubernetes.$ kubectl get nodesNAME STATUS ROLES AGE VERSIONminikube Ready master 9d v1.18.0
<span>kubectl get namespaces</span>
or <span>kubectl get ns</span>
commands:$ kubectl get nsNAME STATUS AGEcharts Active 8ddefault Active 9dkube-node-lease Active 9dkube-public Active 9dkube-system Active 9d
<span>get</span>
command can use the <span>–namespace</span>
or <span>-n</span>
parameter to specify the corresponding namespace. This is very useful for viewing Pods in the <span>kube-system</span>
namespace, as these Pods are services required for Kubernetes itself to run.$ kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-66bff467f8-mjptx 1/1 Running 2 9dcoredns-66bff467f8-t2xcz 1/1 Running 2 9detcd-minikube 1/1 Running 1 9dkube-apiserver-minikube 1/1 Running 1 9dkube-controller-manager-minikube 1/1 Running 2 9dkube-proxy-rpc9d 1/1 Running 1 9dkube-scheduler-minikube 1/1 Running 2 9dstorage-provisioner 1/1 Running 1 9d
Kubectl create
-
Service -
Cronjob -
Deployment -
Job -
Namespace (ns)
$ kubectl create ns hello-therenamespace/hello-there created
<span>cronjob</span>
to return “hello” every five seconds.$ kubectl create cronjob my-cron --image=busybox --schedule="*/5 * * * *" -- echo hellocronjob.batch/my-namespaced-cron created
<span>cronjob</span>
, which is <span>cj</span>
.$ kubectl create cj my-existing-cron --image=busybox --schedule="*/15 * * * *" -- echo hellocronjob.batch/my-existing-cron created
Kubectl edit
<span>kubectl edit</span>
command.$ kubectl edit cronjob/my-existing-cron
# Please edit the object below. Lines beginning with a '#' will be ignored,# and an empty file will abort the edit. If an error occurs while saving this file will be# reopened with the relevant failures.#apiVersion: batch/v1beta1kind: CronJobmetadata: creationTimestamp: "2020-04-19T16:06:06Z" managedFields: - apiVersion: batch/v1beta1 fieldsType: FieldsV1 fieldsV1: f:spec: f:concurrencyPolicy: {} f:failedJobsHistoryLimit: {} f:jobTemplate: f:metadata: f:name: {} f:spec: f:template: f:spec: f:containers: k:{"name":"my-new-cron"}: .: {} f:command: {} f:image: {} f:imagePullPolicy: {}
$ kubectl edit cronjob/my-existing-croncronjob.batch/my-existing-cron edited
<span>KUBE_EDITOR</span>
command to use other editors.$ KUBE_EDITOR="nano" kubectl edit cronjob/my-existing-cron
Kubectl delete
<span>cronjob</span>
is one of two <span>cronjobs</span>
, and now we delete the entire resource.$ kubectl delete cronjob my-existing-croncronjob.batch "my-existing-cron" deleted
Kubectl apply
<span>apply</span>
command can adjust configuration files applied to resources within the cluster. Although it can also be done through the command line <span>standard in (STNIN)</span>
, the apply command is better because it lets you know how to use the cluster and what configuration files to apply. As an example, the following will apply the Helm role-based access control (RBAC) configuration for service accounts.$ kubectl apply -f commands.yamlserviceaccount/tiller createdclusterrolebinding.rbac.authorization.k8s.io/tiller created
We can apply almost any configuration, but it is essential to clarify the configuration to be applied; otherwise, it may lead to unexpected consequences.
Kubectl describe
<span>describe</span>
command can view detailed information about resources. A common usage is to check the information of a Pod or node to see if there are any anomalies or if resources are exhausted.The resources that can be viewed with this command include:
-
Nodes
-
Pods
-
Services
-
Deployments
-
Replica sets
-
Cronjobs
<span>describe</span>
command to view detailed information about the <span>cronjob</span>
in the cluster mentioned above.$ kubectl describe cronjob my-cron
Name: my-cronNamespace: defaultLabels: <none>Annotations: <none>Schedule: */5 * * * *Concurrency Policy: AllowSuspend: FalseSuccessful Job History Limit: 3Failed Job History Limit: 1Starting Deadline Seconds: <unset>Selector: <unset>Parallelism: <unset>Completions: <unset>Pod Template: Labels: <none> Containers: my-cron: Image: busybox Port: <none> Host Port: <none>
Kubectl logs
<span>describe</span>
command can let you know what is happening inside the Pod’s application, the <span>logs</span>
command can provide more detailed information about the Pod in Kubernetes. Understanding this distinction can help developers better troubleshoot issues happening within applications and Kubernetes, as these two are often not the same.$ 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" "-"172.17.0.1 - - [19/Apr/2020:16:01:20 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:25 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:30 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:35 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:40 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:45 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:50 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"172.17.0.1 - - [19/Apr/2020:16:01:55 +0000] "GET / HTTP/1.1" 200 612 "-" "kube-probe/1.18" "-"
<span>grep</span>
command can filter out irrelevant information or view specific events. For example, the following <span>kube-probe</span>
might be irrelevant information, and we can filter it using the <span>grep</span>
command.$ kubectl logs cherry-chart-88d49478c-dmcfv -n charts | grep -vie kube-probe127.0.0.1 - - [10/Apr /2020:23:01:55 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0" “-”
<span>-c <container name></span>
in the <span>logs</span>
command to find the logs of a specific container.Kubectl exec
<span>docker exec</span>
command, the <span>exec</span>
command can also directly troubleshoot applications within containers. It is especially useful when the Pod’s logs cannot pinpoint the issue. Also, note that when using the <span>exec</span>
command, you must use the shell used inside the Pod as the last parameter of the command.$ kubectl exec -it cherry-chart-88d49478c-dmcfv -n charts -- /bin/bashroot@cherry-chart-88d49478c-dmcfv:/#
Kubectl cp
<span>cp</span>
command, used for copying files and directories between containers. Additionally, this command can also be used for recovery backups in emergencies such as automation failures.<span>kubectl cp <filename> <namespace/podname:/path/tofile></span>
.$ kubectl cp commands_copy.txt charts/cherry-chart-88d49478c-dmcfv:commands.txt$ kubectl exec -it cherry-chart-88d49478c-dmcfv -n charts -- /bin/bashroot@cherry-chart-88d49478c-dmcfv:/# lsbin boot commands.txt dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
<span>kubectl cp <namespace/podname:/path/tofile></span>
.$ kubectl cp charts/cherry-chart-88d49478c-dmcfv:commands.txt commands_copy.txt$ lscommands_copy.txt
Shenzhen Station: June 19-21
