Kubernetes (K8s): Examples of Some Basic Kubectl Commands

over 1 year ago

Kubectl is a command line that interfaces with Kubernetes environments. kubectl is primarily used to communicate with Kubernetes API servers to create, update, delete workloads within Kubernetes. 

Refer to this URL on how to install kubectl.

To access your Kubernetes cluster, kubectl uses a configuration file. The default kubectl configuration file is located at ~/.kube/config and is referred to as the kubeconfig file.

kubeconfig files combine information about clusters, users, namespaces, and authentication mechanisms. The kubectl command applies these files to find the information it requires to pick a cluster and communicate with it.

1. kubectl version

Use this command to print the client and server versions.


2. kubectl cluster-info

Use this command to print information about the control plane and add-ons.


3. kubectl explain command can be used to print metadata about specific Resource types. This is useful for learning about the type.


4. kubectl api-versions and kubectl api-resources print information about the available Kubernetes APIs.


5. Kubectl get

Use get to pull a list of resources you have currently on your cluster. The types of resources you can get include:

  • Namespace
  • Pod
  • Node
  • Deployment
  • Service
  • ReplicaSets

Each of these provides details about the available resources in the cluster. Without a namespace flag, the command will always show the information of the default.

Depending on the kubectl operation, the following output formats are supported:

-o name : Print only the resource name and nothing else.

-o wide : Output in the plain-text format with any additional information.

-o yaml : Output a YAML formatted API object.

-o json : Output a JSON formatted API object

5.a. Checking the Status of Cluster Components

After selecting the appropriate context for the cluster, we can check on the status of the core master components using the get componentstatuses (abbreviated to get cs) command

kubectl get cs


 5.b. List all pods in the plain-text output format.

kubectl get pods  or kubectl get po


5.c. List all pods in plain-text output format and include additional information (such as node name).

kubectl get pods -o wide

kubectl get pod --all-namespaces

Instead of --all-namespaces, you can use shorter version -A


5.d. PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.

kubectl get pv


5.e. PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources.

kubectl get pvc


5.f.  List all nodes

kubectl get nodes OR  kubectl get no


5.g. List all namespaces

kubectl get ns or kubectl get namespaces



How To-Best Practice
Comment List
Related Discussions