Setting and Configuring Kubectl for Kyma

Objective

After completing this lesson, you will be able to set up and configure kubectl for Kyma

Configuring Kubectl for Kyma

Usage Scenario

You have successfully created a Kyma runtime instance. Now, you want to interact with it and learn how to use the kubectl command line interface (CLI) to manage your resources. But first, you need to know how to set up the kubectl CLI to connect to your Kyma runtime, especially because Kyma runtime requires a special plugin of kubectl for the authentication process.

Task Flow

In this exercise, you will perform the following steps:

  1. Install the kubectl CLI.
  2. Install the kubelogin plugin.
  3. Configure the kubectl CLI to connect to your Kyma runtime instance.
  4. Interact with your Kyma runtime instance using kubectl.

Task 1: Install the Kubectl CLI

Prerequisites

You have successfully created a Kyma runtime instance in the SAP BTP subaccount.

Steps

  1. Install the Kubectl CLI.

    1. Follow the official documentation to install the kubectl CLI.

    2. To verify that the kubectl CLI is installed correctly, run the following command in your terminal or command line:

      Code Snippet
      1
      kubectl version --client

      If the installation was successful, you see the client and Kustomize versions as an output.

  2. Install the Kubelogin Plugin.

    Kyma runtime requires a special plugin called kubelogin for the kubectl CLI to connect to your Kyma runtime instance.

    1. Follow the official documentation to install the kubelogin plugin.

    2. 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 see the client and kubelogin plugin versions as an output.

Task 2: Connect to Your Kyma Runtime Instance.

As already mentioned, kubectl relies on the kubeconfig file, which is typically located in the ~/.kube directory (macOS/Linux) or %USERPROFILE%\.kube (Windows).

Steps

  1. Go to your SAP BTP subaccount overview page.

  2. Select the link for the KubeconfigURL to download the kubeconfig.yaml file for your Kyma runtime instance.

    KubeconfigURL
  3. 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 credentials for accessing your Kyma runtime instance. Therefore, it is important to keep it secure.

  4. To verify that the kubeconfig file is configured correctly, run the following command in your terminal or command line:

    Code Snippet
    1
    kubectl cluster-info

    If successful, you see the following output:

    Terminal output showing the result of the command kubectl cluster-info. It displays the Kubernetes control plane and CoreDNS running at specific URLs, with a suggestion to use kubectl cluster-info dump for debugging.

Task 3: Access Resources in Your Kyma Runtime

It's now time to use a few kubectl commands to interact with your Kyma runtime instance. Remember that everything defined in Kubernetes can be accessed using the RESTful Kubernetes API.

Every kubectl command must specify the namespace and the object type. If no namespace is specified, kubectl assumes the default namespace. 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]

Steps

  1. List Objects.

    To list objects, you can use the get command. The get command lists one or more resources.

    1. To list all Pods in all namespaces, use the following command:

      Code Snippet
      1
      kubectl get all --all-namespaces
    2. To list all Pods in a specific namespace, you can use the following command:

      Code Snippet
      1
      kubectl get pods --namespace my-namespace

      Hint

      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.

    3. Try out some other examples of the get command, which lists 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
      Code Snippet
      1
      kubectl get nodes
  2. Create objects.

    1. Create a new namespace:

      Code Snippet
      1
      kubectl create namespace my-namespace
    2. Create a Pod in your newly created namespace:

      Code Snippet
      12345678910
      cat <<EOF | kubectl -n my-namespace apply -f - apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: nginx:1.21.3 EOF
    3. The create command can also create a Kubernetes object from a YAML or JSON file. For example, you can define a Pod in a YAML file and create it using this 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.

    4. The third approach is to create objects from a URL. For example, you can use this command:

      Code Snippet
      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 create command twice with the same object definition, it throws an error. To avoid this, use the apply command instead.

  3. 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.

  4. Describe objects.

    To describe objects, you can use the describe command, which displays detailed information about a Kubernetes object.

    1. For example, to describe a Pod, you can use the following command:

      Code Snippet
      1
      kubectl describe pod my-pod
    1. 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
  5. 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
  6. Delete objects.

    1. To delete objects, you can use the delete command. For example, to delete a Pod, run:

      Code Snippet
      1
      kubectl delete pod my-pod
    2. To delete the namespace you created, run:

      Code Snippet
      1
      kubectl delete namespace my-namespace
  7. Get cluster details.

    1. To retrieve cluster information, use the cluster-info command. It displays information about the cluster.

      Code Snippet
      1
      kubectl cluster-info
    2. To retrieve resource usage information for Nodes and Pods, use the top command.

      Code Snippet
      1
      kubectl top nodes
      Code Snippet
      1
      kubectl top pods
  8. To get a full list of all available commands, you can use the help command.

    Code Snippet
    1
    kubectl help

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.

Log in to track your progress & complete quizzes