Kubernetes

Get the Filter and Monitor the Kubernetes Events

The events in Kubernetes cannot be stored, accessed, or forwarded for a long period because there is no in-built support for events in Kubernetes. The occurrences are only kept on file for a brief time before being cleared up. However, they can be accessed or watched directly using the kubectl commands on the cluster or any other resources. In this guide, you will learn about the events in Kubernetes and specifically how to filter and monitor the Kubernetes events.

What Is an Event in Kubernetes?

Any action that is performed or any change that is made to some resource is recorded in logs. These logs are known as events in Kubernetes. These events help in debugging and managing the Kubernetes environment and also help in understanding how the decisions are made for the resources. There are many events that are produced for the changes or work that are done on the object like nodes, clusters, pods, etc. of Kubernetes. Moreover, the events in Kubernetes help in understanding what is happening in any Kubernetes objects. In general, there are two ways to watch the events on Kubernetes. They are:

  • Kubectl get events
  • Kubectl describe pod/pod-name

In the next section, we will demonstrate both methods with the help of a simple example. But first, you need to make sure that your system meets all the basic needs to check and watch the events in Kubernetes.

Prerequisites

Before you begin to learn how to get the filter and monitor the events in Kubernetes, make sure that your system has the following tools installed:

  • Ubuntu version 20.04 or any other latest version
  • Minikube cluster
  • Kubectl command line tool

Assuming that you have all these tools installed, we move forward to learn how to get the filter and monitor the events in Kubernetes.

Start the Minikube Cluster

To use the kubectl commands or execute any function in Kubernetes, the first thing that you need is to start the minikube cluster. The minikube cluster allows you to run any kubectl command and perform any function that you need. We use the “start” command to get the minikube cluster started:

> minikube start

This starts the minikube cluster and your system is ready to execute any kubectl command.

As we previously discussed, there are two ways to get the events. Here, we will explain both methods one by one.

Get the Kubernetes Events with Kubectl Get Events Method

The “kubectl get events” is a kubectl command that lists all the events that happened in the Kubernetes environment. Just enter the following command on your terminal and get the full list of events in Kubernetes:

> kubectl get events

This is a very generic way to get the list of events which is related to a specific resource or the entire cluster.

Text Description automatically generated

Get the Kubernetes Events with Kubectl Describe Pod/Pod-Name

The second method to list down all the events is to use the “describe” command. The “kubectl describe pod/pod-name” is a kubectl command that allows you to get the events in Kubernetes which are related to a specific pod. The “pod-name” represents the name of the pod of which the events are listed. Enter the following command on your terminal and get the events for the specific pod:

> kubectl describe pod/dependent-envars-demo

Text Description automatically generated

From the given output, the “dependent-envars-demo” is the name of the pod for which we want to list the events in Kubernetes and the rest is the details of events for that specific node.

How to Watch the Events in Kubernetes Using Kubectl Commands

Kubernetes does not provide an in-built support for storing, accessing, or forwarding the events for a long period. So, we need to use the third-party logging tools to retain the events for a longer period. To follow the Kubernetes events, a variety of free and open-source third-party solutions are available. These tools allow us to report the events in Kubernetes and have visibility to all the Kubernetes cluster resources. Hence, we can use the Kubectl commands to directly watch or collect the events in Kubernetes. Use the following command to directly watch the events in deployment:

> kubectl get events --watch

Text Description automatically generated

Get the Filter and Monitor the Events in Kubernetes Using the Kubewatch Tool

As previously discussed, there are many free and open-source tools available to filter and monitor the events in Kubernetes and Kubewatch is one of those tools. In this section, we will explain how to install the Kubewatch using the kubectl command to watch and track the events in Kubernetes. It is written in Golang and is used to monitor and report the events by sending notifications to Flock, Webhook, Hipchat, Slack, etc.

The installation of Kubewatch is very easy and can be done in just two steps. A configuration file must first be made, and then it must be deployed. Follow the given steps:

Step 1: Create a YAML Configuration File

At first, we create a YAML file that contains the Kubewacth configuration. To create the YAML file, we can use the “nano” command. The “nano” command is used to open or create a file in the Kubernetes environment. Hence, we use the nano command to create the “kubewatch.yaml” file. See the following command:

> nano kubewatch.yaml

When you execute this command, a YAML file named “kubewatch” is created where you can store the Kubewatch configuration details. The Kubewatch configuration is given in the following snapshot:

Once you store the Kubewatch configuration in your “kubewatch.yaml” file, you can configure it to install the Kubewatch tool.

Step 2: Configure the YAML File

The next step is to deploy the configuration file which we created in the previous step. For that, we use the following command:

>kubectl create -f kubewatch.yaml

From the given output, you can observe that the Kubewatch configuration is created successfully. Now, your Kubewatch tool is ready to send the event notifications through your configured notification channel.

Conclusion

In this article, we explored the events in Kubernetes while specifically focusing on the filter and monitor events in Kubernetes. We learned two ways, kubectl describe pod/pod-name and kubectl get events, to watch the events in Kubernetes. We also learned how to install the Kubewatch tool to watch and monitor the events in Kubernetes.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content