Basic Kubectl Commands
The kubectl command line tool is used to inspect and manage its resources. After you install the official Kubernetes dashboard, which provides a web interface for managing the cluster, you do not need to use kubectl commands, so it is not necessary to memorize them. However, they are useful for verifying installations and troubleshooting when the dashboard is unavailable.
The following table describes some of the most common commands for inspecting resources.
|
Command |
Description |
|---|---|
|
|
Displays documentation for a resource and its fields—helpful for learning the Kubernetes API structure. |
|
|
Lists resources of that type with the most important fields. Use the |
|
|
Shows detailed information about a specific resource instance, including events and status. |
You can use these commands with virtually any Kubernetes resource. To learn more about the most commonly used resources, refer to Kubernetes Resources.
Resource names in kubectl are case-insensitive, support both singular and plural forms, and many have short aliases. For example, in case of the Node resource, "Nodes", "nodes", "node", and "no" all work in the commands above.
Kubectl also provides commands for creating, editing, and deleting resources:
-
kubectl apply -
kubectl edit -
kubectl delete
You can use these as an alternative to installing applications with Helm, which is described in this guide.
The following table lists some additional commonly used resource-specific commands.
|
Command |
Description |
|---|---|
|
|
Displays logs from a pod’s main container—useful for troubleshooting applications. |
|
|
Opens an interactive shell inside a running pod (use |
|
|
Displays logs from all pods that belong to a deployment. |
The following is an example of inspecting pods in your cluster.
Most Kubernetes resources are namespaced, while some (such as nodes or persistent volumes) are not. When no namespace is specified in a command, the default namespace is used.
-
List all pods in the default namespace:
kubectl get podsExample output:
NAME READY STATUS RESTARTS AGE ico-webhmi-54fbd54dd6-bwp6m 1/1 Running 0 6d1h ico-webhmi-54fbd54dd6-hwlzk 1/1 Running 0 6d1h ico-webhmi-54fbd54dd6-pfwh7 1/1 Running 0 6d1h ico-ingress-haproxy-bf8659b99-khz9p 1/1 Running 0 6d1h ico-ingress-haproxy-bf8659b99-tjf6p 1/1 Running 0 6d1h -
Select a pod—for example, the first one in the list—and use its name in the following commands to view the pod's details, logs, or open an interactive shell inside the pod:
kubectl describe ico-webhmi-54fbd54dd6-bwp6mkubectl logs ico-webhmi-54fbd54dd6-bwp6mkubectl exec -it ico-webhmi-54fbd54dd6-bwp6m -- /bin/sh -
To inspect resources in another namespace, use the
-nflag to specify the namespace, or-Ato view resources from all namespaces:kubectl get pods -n kube-systemkubectl get pods -A