Using kubectl to interact with the Kubernetes API and configure it for Kyma
Objectives
After completing this lesson, you will be able to:
Install, configure, and use kubectl
Use the most important kubectl commands
Set up and configure kubectl for Kyma
Describe the interaction of kubectl with Kubernetes API
What is kubectl?
To interact with the Kubernetes API, you need a client. The tool kubectl is a command-line tool for interacting with Kubernetes clusters. It allows you to manage and control Kubernetes resources, such as pods, services, deployments, and more. With kubectl, you can perform various operations, including creating, updating, and deleting resources. 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.
Note
Before diving into the content, we want to point out that if you are interested in SAP BTP, the Kyma runtime, and the kubectl command-line tool, you can find more detailed information in the Learning Journey: Delivering Side-by-Side Extensibility based on SAP BTP, Kyma runtime
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 in the official Kubernetes documentation. Scroll down to the further reading section for the link to the official Kubernetes documentation.
Configuring Kubectl
To configure kubectl to point to a specific Kubernetes cluster, you need a kubeconfig file. This file includes important information about your cluster, such as the API server URL, the authentication method, and the certificate authority. The kubeconfig file is usually stored in the home directory of your user account.
For Kyma , along with kubectl, the kubelogin plugin is also required. Read more here.
For example, on Linux, the kubeconfig file is located at ~/.kube/config, and on Windows, you can find it at %USERPROFILE%\.kube\config.
Using Kubectl
Once the kubeconfig file is properly configured, kubectl will use the information from that file to interact with the Kubernetes API when you issue commands. It is also possible to configure kubectl by exporting the kubeconfig file in the terminal. You can use the KUBECONFIG environment variable to specify the location of the kubeconfig file.
Before moving on to commands, remember that everything defined in Kubernetes can be accessed using 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, use the following command:
Code Snippet
1
kubectl get pods
To list all Pods in a specific namespace, you can use the following command:
Code Snippet
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 default. Read more about contexts in the official Kubernetes documentation.
Here are some other examples of the get command, which lists the resources of a specified type in the default namespace:
Code Snippet
1
kubectl get pods
Code Snippet
1
kubectl get services
Code Snippet
1
kubectl get replicasets
Code Snippet
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, use the following command:
Code Snippet
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:
If you run the create command twice with the same object definition, it will throw an error. To avoid this, you can use the apply command 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
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 object's current state 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
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
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 describe command to a YAML file, you can use the following command:
Code Snippet
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
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
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
1
kubectl top nodes
Code Snippet
1
kubectl top pods
Other Commands
To get a full list of all available commands, you can use the help command.
Code Snippet
1
kubectl help
Other Clients
The most common way to interact with the Kubernetes API is using kubectl. 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:
Accessing Your Kyma Runtime Instance Using Kubectl
You have successfully created a Kyma runtime instance. Now, you want to interact with your Kyma runtime instance and learn how to use the kubectl CLI to manage your resources. But first, you need to know how to set up the kubectl command line interface (CLI) to connect to your Kyma runtime instance, especially because Kyma runtime requires a special plugin of kubectl for the authentication process.
Set up and configure kubectl for Kyma
In this exercise, you will perform the following steps:
Install the kubectl CLI.
Install the kubelogin plugin.
Configure the kubectl CLI to connect to your Kyma runtime instance.
Interact with your Kyma runtime instance using kubectl.
Prerequisites
You have successfully created a Kyma runtime instance in the SAP BTP subaccount.
To verify that the kubelogin plugin is installed correctly, execute the following command in your terminal or command line:
Code Snippet
1
kubectl version --client
If the installation was successful, you should see the client and kubelogin plugin versions as an output.
Connect to your Kyma runtime instance.
As already mentioned, the kubectl relies on the kubeconfig file, which is typically located in the ~/.kube directory (macOS/Linux) or %USERPROFILE%\.kube (Windows).
Go to your SAP BTP subaccount overview page.
Select the link for the KubeconfigURL to download the kubeconfig.yaml file for your Kyma runtime instance.
In the terminal, export the location of your kubeconfig file as an environment variable.
For MacOS/Linux:
Code Snippet
1
export KUBECONFIG={KUBECONFIG_FILE_PATH}
For Windows:
Code Snippet
1
$ENV:KUBECONFIG="{KUBECONFIG_FILE_PATH}"
Note
The kubeconfig file contains the credentials for accessing your Kyma runtime instance. Therefore, it is important to keep it secure.
To verify that the kubeconfig file is configured correctly, execute the following command in your terminal or command line:
Code Snippet
1
kubectl cluster-info
If the configuration was successful, you should see the following output:
Interact with your Kyma runtime instance.
It's now time to use some of the kubectl commands to interact with your Kyma runtime instance.
Execute the following command to get a list of all available resources in your Kyma runtime instance:
Code Snippet
1
kubectl get all --all-namespaces
Get a list of your nodes in your Kyma runtime instance:
Code Snippet
1
kubectl get nodes
Get a list of your Pods in your Kyma runtime instance:
Code Snippet
1
kubectl get pods --all-namespaces
Get a list of your Deployments in the namespace kyma-system:
Get a list of all namespaces in your Kyma runtime instance:
Code Snippet
1
kubectl get namespaces
Delete the namespace you created:
Code Snippet
1
kubectl delete namespace my-namespace
Result
You have successfully installed the kubectl CLI and configured it to connect to your Kyma runtime instance. You can use the basic kubectl commands to interact with it.