A Kubernetes event is an object that shows what’s happening inside a cluster, node, pod, or container. These objects are usually generated in response to changes that occur inside your K8s system. The Kubernetes API Server enables all core components to create these events. Generally, each event is accompanied by a log message as well.
An event in Kubernetes is automatically generated in response to changes with other resources—like nodes, pods, or containers. State changes lie at the center of this. For example, phases across a pod’s lifecycle—like a transition from pending to running, or statuses like successful or failed may trigger a K8s event. The same goes for reallocations and scheduling.
Event objects are not regular log events, therefore the Kubernetes logs do not include them. Kubernetes has no builtin support to store or forward these events on the long term, and they are cleaned up after a short retention time defaulting to just 1 hour.
To overcome this a dedicated application inside the Kubernetes cluster can watch these events on the API Server and write them to the standard output, where you can collect them using Loki.
In this tutorial you will learn how to configure the Kubernetes Events Exporter and collect and perist those events using Loki
. The event exporter allows exporting the often missed Kubernetes events to various outputs so that they can be used for observability or alerting purposes.
- Introduction
- Prerequisites
- Step 1 - Installing Event Exporter
- Step 2 - Viewing Events in Grafana
- Conclusion
To complete this tutorial, you will need:
- A Git client, for cloning the
Starter Kit
repository. - Helm, for installing the
Loki
stack chart. - Kubectl, for
Kubernetes
interaction. - A text editor with
YAML
lint support, for example: Visual Studio Code. - Emojivoto Sample App deployed in the cluster. Please follow the steps in its repository README.
- Prometheus Stack and Loki installed and configured.
In this step, you will learn how to deploy Event Exporter
to your DOKS
cluster, using kubectl
.
-
First, clone the
Starter Kit
repository, and then change directory to your local copy:git clone https://github.com/digitalocean/Kubernetes-Starter-Kit-Developers.git cd Kubernetes-Starter-Kit-Developers
-
Next, open and inspect the
04-setup-observability/assets/manifests/event-exporter/event-exporter-roles.yaml
file from theStarter Kit
repository, and adjust according to your needs. -
Next, create the
ServiceAccount
,ClusterRole
andClusterRoleBinding
by applying the04-setup-observability/assets/manifests/event-exporter/event-exporter-roles.yaml
values file:kubectl apply -f 04-setup-observability/assets/manifests/event-exporter/event-exporter-roles.yaml
The output looks similar to the following:
namespace/event-exporter created serviceaccount/event-exporter created clusterrolebinding.rbac.authorization.k8s.io/event-exporter created
-
Next create the
event exporter
config by applying the04-setup-observability/assets/manifests/event-exporter/event-exporter-config.yaml
values file. Configuration is done via aConfigMap
. The tool watches all the events and user has to option to filter out some events, according to their properties. The events will be dumped to stdout where they will be picked up byLoki
with no other additional configurationr required. Please see the Event Exporter Repository for any additional configuration.kubectl apply -f 04-setup-observability/assets/manifests/event-exporter/event-exporter-config.yaml
You can inspect the config map by exporting it to a local
yaml
file via:kubectl get cm event-exporter-cfg -n event-exporter -o yaml > event-exporter-config.yaml
Then, open the
event-exporter-config.yaml
file using a text editor of your choice (preferably withYAML
support). For example you can use VS Code:code event-exporter-config.yaml
-
Finally, create the
event exporter
deployment by applying the04-setup-observability/assets/manifests/event-exporter/event-exporter-deployment.yaml
kubectl apply -f 04-setup-observability/assets/manifests/event-exporter/event-exporter-deployment.yaml
Inspect all the
Kubernetes
resources created forEvent Exporter
:kubectl get all -n event-exporter
The output looks similar to:
NAME READY STATUS RESTARTS AGE pod/event-exporter-6988454f57-9xlrb 1/1 Running 0 7s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/event-exporter 1/1 1 1 9s NAME DESIRED CURRENT READY AGE replicaset.apps/event-exporter-6988454f57 1 1 1 9s
If the installation went well and no errors were reported you should start seeing event start to flow in Grafana.
-
Connect to
Grafana
(using default credentials:admin/prom-operator
) by port forwarding to local machine:kubectl --namespace monitoring port-forward svc/kube-prom-stack-grafana 3000:80
-
Open a web browser on localhost:3000. Once in, you can go to
Explore
menu and select theLoki
as a datasource. -
In the
Log browser
input enter the following:{app="event-exporter"}
To simulate a realistic event, you can downscale the number of replicas for the emojivoto
deployment and check out the events in Grafana
:
-
From your terminal run the following command to bring the number of replicas for the
emoji
deployment to 0:kubectl scale --replicas=0 deployment/emoji -n emojivoto
-
From the
Log browser
input in Grafana proceed to enter the following query:{app="event-exporter"} |= "emojivoto"
Using this filter the results will only include the
emojivoto
namespace. You should see the filtered events in theLogs
section. -
Expand on a log to see its details. The log contains a lot of useful information such as the node the pod was deployed on, the message and the namespace to name a few. These fields can be further used to perform queries.
Notes:
Any of the fields in the Detected fields
section of a log detail view can be used to query. For example you can perform a query using the pod name and view specific logs for a certain pod.
{app="event-exporter"} |= "emoji-56d5cc4566-rggs7"
Due to the fact that storage and retention has been configured for Loki the events will be persisted and will not be wiped after 1 hour.
In this tutorial, you learned how to install Event Exporter
for event forwarding in your DOKS
cluster. Then, you used Grafana
together with the Loki
data source to view those events.
Next go to Alerts and Notification.