Introduction to DevOps with Kubernetes
上QQ阅读APP看书,第一时间看更新

Accessing Kubernetes Clusters

Accessing the Kubernetes clusters is a crucial step for installing and operating the cloud-native applications. In this section, we will look at the two primary ways of reaching Kubernetes clusters. The first method will cover the Kubernetes Dashboard, which is a web-based Kubernetes user interface. The second method will use the Kubernetes CLI, namely kubectl, to access the Kubernetes API. Kubernetes provide a rich API that enables us to install and operate complex cloud-native applications. It is designed as a RESTful API and can be consumed programmatically using client libraries as well as tools such as kubectl, Terraform, or Ansible.

Kubernetes Dashboard is the official user interface, which also runs as a containerized web application in the cluster. It is possible to deploy applications, troubleshoot running applications, and check the status of Kubernetes resources. The Dashboard enables basic cluster management and operational tasks, such as scaling applications up or down, or restarting instances. It is a very friendly tool for checking the status of applications and the overall cluster easily. In the following exercise, we'll learn to access the Dashboard and monitor the status of an application.

Exercise 9: Checking Application Status in Kubernetes Dashboard

In this exercise, we aim to access the Kubernetes dashboard and check the status of a running application. To complete the exercise, we need to ensure that the following steps are executed:

  1. Run the following command in Terminal to access the Kubernetes dashboard of the cluster running in minikube:

    minikube dashboard

    Figure 3.9: minikube dashboard command

    This output indicates that the dashboard is enabled, and a local proxy is started.

  2. Open the address from Step 1, if it is not automatically opened, in a browser:

    Figure 3.10: Kubernetes dashboard

  3. Click default and select the kube-system namespace from the dropdown:

    Figure 3.11: Changing namespace in Kubernetes dashboard

  4. Scroll down to the Pods section and click on kube-apiserver-minikube:

    Figure 3.12: Pod view in Kubernetes dashboard

    In this pod view, all details of the kube-apiserver-minikube pod are visible, including the containers, environment variables, commands, and the condition of the pod.

  5. Click Logs in the header menu inside the pod view:

    Figure 3.13: Logs of a pod in Kubernetes dashboard

    On this screen, the stream of logs from the pod is presented in real time, as if the application were running locally. This is a straightforward way of checking the status of an application and troubleshooting the errors.

  6. Stop the proxy started in Step 1 by pressing Ctrl + C.

This exercise walks you through the steps to access the Kubernetes dashboard and check the logs of a running application. Although the dashboard is a handy and human-friendly tool, it is always required to access the Kubernetes API programmatically. Kubernetes provides an open source official CLI tool, namely kubectl, to interact with Kubernetes API. It is a CLI tool that can be installed on the local system and it can connect to any cluster with the required credentials. kubectl is a powerful tool that can handle not only basic operations, such as getting, deleting, or editing resources, but also cluster management and troubleshooting operations.

For instance, it is possible to deploy an application, check the logs, and create a proxy to the local system to access ports by using kubectl. Also, operational tasks, such as marking a node unschedulable or checking the resource (CPU/Memory/Storage) usage levels can be done by kubectl.

In the following exercise, we will look at kubectl and the use of cluster management commands. kubectl is an indispensable tool for using and managing Kubernetes clusters and cloud-native applications, so getting hands-on experience with kubectl and incorporating it into your daily workload is crucial.

Exercise 10: Carrying Out Cluster Management Using kubectl

In this exercise, we aim to access the Kubernetes API using kubectl and run cluster management commands.

To complete the exercise, we need to ensure that the following steps are executed:

  1. Download the kubectl executable based on your operating system by running the commands in your local Terminal:

    # Linux

    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/linux/amd64/kubectl

    # MacOS

    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/darwin/amd64/kubectl

  2. Make the downloaded kubectl an executable and move to the following path:

    chmod +x kubectl

    sudo mv kubectl /usr/local/bin

  3. Check kubectl config with the following command:

    kubectl config current-context

    Figure 3.14: Output of kubectl config current-context

    The result of minikube indicates that the kubectl context is correctly configured to the minikube cluster.

  4. Check cluster-info with the following command:

    kubectl cluster-info

    Figure 3.15: Output of kubectl cluster-info

    This output lists the critical cluster components and their IP addresses. This is helpful output to find out whether there are any broken cluster components.

  5. Get the client and server version with the following command:

    kubectl version

Figure 3.16: Output of kubectl version

This command lists the versions of the Kubernetes API server and the kubectl client, and it is essential to check whether you found any inconsistencies between API requests and responses.

Get the supported API resources by using the following command:

kubectl api-resources -o name

Figure 3.17: Output of kubectl api-resources

This long list shows all the resources supported by the minikube Kubernetes cluster that is running. In the following section, the core building block resources from this list will be presented. Furthermore, throughout this book, most of these resources will be explicitly or implicitly used and discussed.

In this exercise, we've seen how to configure and use kubectl to interact with the Kubernetes API. Both the dashboard and kubectl are essential parts of the DevOps toolset to make daily life more comfortable, and automate operations. Therefore, it is suggested that you play around with kubectl and create aliases and shortcuts to incorporate this tool into your environment. In the following section, Kubernetes will be used to manage workloads, and the very first Kubernetes resources will be presented as the building block of complex cloud-native applications.