Interacting with the Kubernetes API via Kubectl

Objectives

After completing this lesson, you will be able to:

  • Install, configure, and use kubectl to interact with the Kubernetes API.
  • Use the most important kubectl commands.

Interacting with the Kubernetes API via Kubectl

Usage Scenario

Having just discovered which kind of objects you can create in Kubernetes, let's find out how to interact with the Kubernetes API to create, update, and delete these objects. For this, you will use the kubectl command-line tool, which is the primary way to interact with the Kubernetes API.

What is kubectl?

To interact with the Kubernetes API, you need a client. The Kubernetes API is a REST API, which means that you can interact with it using any HTTP client. Since, you typically also have to deal with authentication and authorization, it is much easier to use a dedicated client like kubectl.

Installation and Configuration of Kubectl

Installing kubectl

The kubectl command-line interface (CLI) must be installed on your local machine. You can install it following the instructions on the official Kubernetes documentation. Scroll down to the further reading section for the link to the official Kubernetes documentation.

Configuring kubectl

To tell kubectl which Kubernetes cluster to interact with, you need to configure it somewhere. This configuration is typically stored in a kubeconfig file. The kubeconfig file contains information about the cluster, such as the API server URL, the authentication method, and the certificate authority. It is usally located in the home directory of your user account.

For example, on Linux, the kubeconfig file is located at ~/.kube/config. On Windows, it is located at %USERPROFILE%\.kube\config.

If you now interact with the Kubernetes API using kubectl, it will use the kubeconfig file in your home directory.

Using Kubectl

Before moving on to commands, remember that everything defined in Kubernetes can be accessed via the RESTful Kubernetes API.

Every command needs to specify the namespace and the object type. If no namespace is specified, the default namespace is used. The object type is the name of the resource type, such as pods or deployments. The name of the object is the name of the resource, such as my-pod or my-deployment.

The kubectl syntax is as follows:

kubectl [command] [TYPE] [NAME] [flags]

List objects

To list objects, you can use the get command. The get command lists one or more resources. For example, to list all pods in the default namespace, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl get pods

To list all pods in a specific namespace, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl get pods --namespace my-namespace

Note

The--namespace flag is optional. If you don't specify a namespace, the default namespace is used. However, you can also specify the namespace in the kubeconfig file, or create different contexts for different namespaces, which you can then switch between or set your custom namespace as the default. Read more about contexts in the official Kubernetes documentation here.

Here are some other examples of the get command:

List all pods in the current namespace:

Code Snippet
Copy code
Switch to dark mode
1
kubectl get pods

List all services in the current namespace:

Code Snippet
Copy code
Switch to dark mode
1
kubectl get services

List all ReplicaSets in the current namespace:

Code Snippet
Copy code
Switch to dark mode
1
kubectl get replicasets

List all deployments in the current namespace:

Code Snippet
Copy code
Switch to dark mode
1
kubectl get deployments

Create objects

To create objects, you can use the create command. The create command creates a Kubernetes object from a YAML or JSON file. For example, to create a pod, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl create -f my-pod.yaml

The -f flag is short for --filename. It specifies the path to the YAML or JSON file that contains the object definition.

The create command can also create objects from a URL. For example, to create a pod from a URL, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl create -f https://raw.githubusercontent.com/SAP-samples/kyma-runtime-learning-journey/main/unit_1/my-pod.yaml

Note

If you run the createcommand twice with the same object definition, it will throw an error. To avoid this, you can use the applycommand instead.

Update Objects

To update objects, you can use the apply command. The apply command updates a Kubernetes object from a YAML or JSON file. For example, to update a pod, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl apply -f my-pod.yaml

During an apply operation, the Kubernetes API server compares the object definition in the YAML or JSON file with the current state of the object in the cluster. If there are any differences, the object is updated.

Delete Objects

To delete objects, you can use the delete command. The delete command deletes a Kubernetes object. For example, to delete a pod, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl delete pod my-pod

Describe Objects

To describe objects, you can use the describe command. The describe command displays detailed information about a Kubernetes object. For example, to describe a pod, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl describe pod my-pod

You can even export the output of the describe command to a YAML or JSON file. For example, to export the output of the describecommand to a YAML file, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl describe pod my-pod -o yaml > my-pod.yaml

Label Objects

To label objects, you can use the label command. The label command adds or updates labels on a Kubernetes object. For example, to add a label to a pod, you can use the following command:

Code Snippet
Copy code
Switch to dark mode
1
kubectl label pod my-pod my-label=my-value

Retrieving Cluster Information

To retrieve cluster information, you can use the cluster-info command. The cluster-info command displays information about the cluster.

Code Snippet
Copy code
Switch to dark mode
1
kubectl cluster-info

To retrieve resource usage information, you can use the top command. The top command displays resource usage information for nodes and pods.

Code Snippet
Copy code
Switch to dark mode
1
kubectl top nodes
Code Snippet
Copy code
Switch to dark mode
1
kubectl top pods

Other Commands

To get a full list of all available commands, you can use the help command.

Code Snippet
Copy code
Switch to dark mode
1
kubectl help

Other Clients

The most common way to interact with the Kubernetes API is using kubectl. There are also Dashboards and other clients available that you can use to interact with the Kubernetes API. Kyma, for example, comes with a Dashboard that you can use to interact with the Kubernetes API.

Summary

In this lesson, you learned how to interact with the Kubernetes API using kubectl. You also learned how to configure kubectl and how to use it to list, create, update, delete, and describe objects. You also learned how to label objects and how to get a list of all available commands.

Further Reading about Interacting with the Kubernetes API via Kubectl

Find further information about Interacting With the Kubernetes API via kubectl here:

Log in to track your progress & complete quizzes