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 course:
https://learning.sap.com/courses/developing-applications-in-sap-btp-kyma-runtime
Configuration
To use kubectl, you must install it on your local machine and configure it to point to a specific Kubernetes cluster. First, 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 example, on Linux, the kubeconfig file is located at ~/.kube/config, and on Windows, you can find it at %USERPROFILE%\.kube\config.
In one of the next lessons, you'll learn how to download a kubeconfig file and configure kubectl for interacting with your Kubernetes cluster. For Kyma, along with kubectl, the kubelogin plugin is also required.
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. You can also configure kubectl by exporting the kubeconfig file in the terminal or use the KUBECONFIG environment variable to specify the location of the kubeconfig file.
Hint
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:
1kubectl [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:
1kubectl get podsTo list all Pods in a specific namespace, you can use the following command:
1kubectl get pods --namespace my-namespaceNote
Here are some other examples of the get command, which lists the resources of a specified type in the default namespace:
1kubectl get pods1kubectl get services1kubectl get replicasets1kubectl get deploymentsCreate 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:
1kubectl create -f my-pod.yamlThe -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:
1kubectl create -f https://raw.githubusercontent.com/SAP-samples/kyma-runtime-course/main/unit_1/my-pod.yamlNote
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:
1kubectl apply -f my-pod.yamlDuring 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:
1kubectl delete pod my-podDescribe 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:
1kubectl describe pod my-podYou 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:
1kubectl describe pod my-pod -o yaml > my-pod.yamlLabel 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:
1kubectl label pod my-pod my-label=my-valueRetrieving Cluster Information
To retrieve cluster information, you can use the cluster-info command. The cluster-info command displays information about the cluster.
1kubectl cluster-infoTo retrieve resource usage information, you can use the top command. The top command displays resource usage information for nodes and pods.
1kubectl top nodes1kubectl top podsOther Commands
To get a full list of all available commands, you can use the help command.
1kubectl help