How to Access the Kubernetes Events
What Is an Event in Kubernetes?
A Kubernetes event is an object which is generated automatically against any change that happens with resources like nodes, containers, clusters, or pods in the Kubernetes system. It tells the user what is happening with the resources in the system, i.e. a container is killed, a pod is scheduled, deployment is updated, etc. These events help in maintaining the Kubernetes system and performing debugging in the Kubernetes environment. In this blog, we will discover and discuss how to access the events in the Kubernetes environment.
Before you begin to learn how to watch the events in Kubernetes, make sure that your system meets all the basic needs. You must have the following:
- Ubuntu 20.04 or any other latest Ubuntu version
- Virtual machine installed in your Linux/Unix system
- Minikube cluster
- Kubectl command line tool
Now, let us move on to learn the different methods of accessing the Kubernetes events.
Start the Minikube Environment
To use the Kubernetes environment and access the events which are created in it, we need to have access to minikube. Therefore, let us first start the minikube using the following command:
This starts at the minikube terminal where you can access the Kubernetes events. Now, we can access or get the events in Kubernetes
How to Watch the Events in Kubernetes
There are many different ways to access or watch the events in Kubernetes. Here, we will describe these methods to help you understand how they are used to access the events in Kubernetes. The first and basic method is to use the simple kubectl get event command. The “get” command in Kubernetes is used to access one or more resources from the Kubernetes system. All the parameters are followed by the “get” command to get the events in Kubernetes according to the requirement. Hence, we first get the events with the basic command which is given in the following:
You can leverage the resource API to get the recent events using this command. It shows all the recent events that happened across the entire system.
Moving forward, we will show you how you can display the result of the “get event” command in the form of JSON format. The kubectl allows you to print the output in your desired format. All you have to do is to define the output type. Here, we access the event in Kubernetes with the “get” command and display the result in JSON format with the help of the following command:
As you can observe from the given output, the events are listed from the Kubernetes environment in the JSON format. This is pretty simple, and you can easily use it just by executing the previous command in your kubectl command line tool.
The next method that we will show you is how to get the filtered events from Kubernetes. So far, we learned how to get all the events in Kubernetes using the “get events” kubectl command and how to display the output in JSON format. Now, let us see how we can filter the events according to our needs and only see the required events. Filtering the events is very simple; as discussed previously, you need to use the parameter according to your needs followed by the “get events” kubectl command. We use the following command to filter the events according to our needs and only display the required events:
When you use this command, you only see the events that do not have a “normal” type. Since the events with the “normal” type are mostly just noise and do not provide any meaningful information, we can skip them. The following given output shows the events that do not have the “normal” type:
How to Get the Events for a Specific Pod
Just like we can filter the only required events, we can also access the events only for a specific pod. To do that, let us first list down all the pods from the Kubernetes environment with the help of the following command:
This command lists down all the pods that are created in the Kubernetes environment so far:
Now, we have a list of all the pods. We can access the events for a specific pod using the pod name. We use the “describe pod” command followed by the pod name to get the events that are related to that pod. The sample command to access the events for a specific command is as follows:
Here, the “pod-name” represents the name of the pod for which you need to see the events in Kubernetes.
Here is a sample of a complete command that displays all the events for a specific pod:
From the given output, the name of the first pod is “dependent-envars-demo” and we access the events for that pod. The following given output shows you the events for the dependent-envars-demo pod:
In this article, we learned about the events in Kubernetes. We explored what an event is in the Kubernetes environment and how it can be accessed in the Kubernetes system. We learned that there are many open-source free methods to access the events in Kubernetes. We also learned how to implement those methods using the kubectl commands.