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

kubectl explain <resource>

Displays documentation for a resource and its fields—helpful for learning the Kubernetes API structure.

kubectl get <resource>

Lists resources of that type with the most important fields. Use the -o wide option for an extra field.

kubectl describe <resource> <resource_name>

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

kubectl logs <pod_name>

Displays logs from a pod’s main container—useful for troubleshooting applications.

kubectl exec -it <pod_name> -- /bin/sh

Opens an interactive shell inside a running pod (use /bin/bash if available).

kubectl logs deployment/<deployment_name>

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.

  1. List all pods in the default namespace:

    kubectl get pods

    Example 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
  2. 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-bwp6m
    kubectl logs ico-webhmi-54fbd54dd6-bwp6m
    kubectl exec -it ico-webhmi-54fbd54dd6-bwp6m -- /bin/sh
  3. To inspect resources in another namespace, use the -n flag to specify the namespace, or -A to view resources from all namespaces:

    kubectl get pods -n kube-system
    kubectl get pods -A