Using the `kubectl` Command (with examples)

Using the `kubectl` Command (with examples)

List information about a resource with more details

kubectl get pod|service|deployment|ingress|... -o wide

Motivation: This command allows us to get more detailed information about a specific resource in the Kubernetes cluster. It can be useful when we need to troubleshoot or gather more information about a particular resource.

Explanation:

  • kubectl get: This is the main command for retrieving information about Kubernetes resources.
  • pod|service|deployment|ingress|...: Specifies the type of resource we want to get information about. We can replace this with the desired resource type.
  • -o wide: This option displays additional information about each resource, including the node name and IP address.

Example Output:

NAME       READY   STATUS    RESTARTS   AGE   IP             NODE
app-pod    1/1     Running   0          10h   192.168.0.10   node-1

Update specified pod with the label ‘unhealthy’ and the value ’true’

kubectl label pods name unhealthy=true

Motivation: This command allows us to add or update labels on pods. Labels are key-value pairs attached to Kubernetes resources, and they can be used for organizing, selecting, and filtering resources. By labeling a pod as ‘unhealthy’, we can easily identify and manage problematic pods.

Explanation:

  • kubectl label: This subcommand is used for managing labels on Kubernetes resources.
  • pods: Specifies that we want to label pods.
  • name: The name of the pod we want to label.
  • unhealthy=true: The label key (unhealthy) and value (true) that we want to apply to the pod.

Example Output: None (successful execution does not produce any output).

List all resources with different types

kubectl get all

Motivation: This command provides a convenient way to get a comprehensive list of all resources in the Kubernetes cluster. It can be useful when we need to see a summary of the different types of resources deployed in the cluster.

Explanation:

  • kubectl get: This is the main command for retrieving information about Kubernetes resources.
  • all: Specifies that we want to get information about all resources.

Example Output:

NAME             READY   STATUS    RESTARTS   AGE
pod/app-pod      1/1     Running   0          10h
service/app-svc  1/1     Running   0          10h

Display resource (CPU/Memory/Storage) usage of nodes or pods

kubectl top pod|node

Motivation: This command allows us to monitor the resource usage of nodes or pods in the Kubernetes cluster. By checking resource usage, we can identify potential bottlenecks, optimize resource allocation, and ensure the smooth operation of our applications.

Explanation:

  • kubectl top: This subcommand is used for retrieving resource usage information.
  • pod|node: Specifies whether we want to display resource usage for pods or nodes.

Example Output:

POD      CPU(cores)   MEMORY(bytes)
app-pod  50m          100Mi
kubectl cluster-info

Motivation: This command provides us with essential information about the Kubernetes cluster, including the address of the master and cluster services. It allows us to quickly access and communicate with the Kubernetes cluster.

Explanation: None (This command does not require any additional arguments).

Example Output:

Kubernetes master is running at https://<master-ip>

Display an explanation of a specific field

kubectl explain pods.spec.containers

Motivation: This command provides detailed information about a specific field in a Kubernetes resource specification. It can be useful when we need to understand the purpose and possible values of a particular field.

Explanation:

  • kubectl explain: This subcommand is used for getting documentation about Kubernetes resource fields.
  • pods.spec.containers: Specifies the field we want to get an explanation for. In this example, it explains the containers field of the spec section in a pod specification.

Example Output:

KIND:     Pod
VERSION:  v1

RESOURCE: containers <[]Object>

DESCRIPTION:
     List of containers belonging to the pod. Containers cannot currently be
     added or removed. There must be at least one container in a Pod. Cannot
     be updated.
...
kubectl logs pod_name

Motivation: This command allows us to retrieve the logs generated by a container in a pod. It can be useful for troubleshooting issues, debugging, and monitoring the behavior of our applications.

Explanation:

  • kubectl logs: This subcommand is used for retrieving logs from a pod or specified resource.
  • pod_name: The name of the pod whose logs we want to retrieve.

Example Output:

2021-01-01 12:34:56 [INFO] Application started
2021-01-01 12:35:01 [ERROR] Internal server error occurred
2021-01-01 12:35:10 [INFO] Request processed successfully

Run command in an existing pod

kubectl exec pod_name -- ls /

Motivation: This command allows us to execute a command inside a running pod. It can be useful for running diagnostic commands, troubleshooting, or performing administrative tasks within the pod’s environment.

Explanation:

  • kubectl exec: This subcommand is used for executing commands in a pod.
  • pod_name: The name of the pod in which we want to run the command.
  • -- ls /: The command we want to run inside the pod (in this case, listing the contents of the root directory).

Example Output:

bin    dev   home   usr

Conclusion

In this article, we explored various use cases of the kubectl command. We learned how to retrieve information about resources, update labels on pods, display resource usage, print cluster information, explain specific fields, retrieve container logs, and execute commands inside pods. Understanding and effectively using these commands can greatly enhance our ability to manage and interact with Kubernetes clusters.

Related Posts

How to use the command "bash" (with examples)

How to use the command "bash" (with examples)

Code example: bash Motivation: Starting an interactive shell session allows the user to directly interact with the Bourne-Again SHell (bash) command-line interpreter.

Read More
repair-bde Command Examples (with examples)

repair-bde Command Examples (with examples)

Use Case 1: Attempt to repair a specified volume Code: repair-bde C: Motivation: This command is used to attempt to repair a specified BitLocker-encrypted volume.

Read More
How to use the command 'linode-cli nodebalancers' (with examples)

How to use the command 'linode-cli nodebalancers' (with examples)

This article will provide examples and explanations for using the linode-cli nodebalancers command.

Read More