<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
  <title>BigBinary Blog: Prometheus</title>
  <subtitle>Deep technical writing on Ruby on Rails, performance, scaling, monitoring and engineering.</subtitle>
  <link rel="self" type="application/atom+xml" href="https://www.bigbinary.com/blog/categories/prometheus/feed.xml"/>
  <link rel="alternate" type="text/html" href="https://www.bigbinary.com/blog/categories/prometheus"/>
  <id>https://www.bigbinary.com/blog/categories/prometheus</id>
  <updated>2024-05-07T00:00:00.000Z</updated>
  <author>
    <name>BigBinary</name>
    <uri>https://www.bigbinary.com/</uri>
  </author>
  <icon>https://www.bigbinary.com/favicon.ico</icon>
  <logo>https://www.bigbinary.com/images/bigbinary_square_big_logo.png</logo>
  <rights>Copyright 2024 Big Binary LLC</rights>
  <generator uri="https://nextjs.org">Next.js</generator>
  <entry>
      <title type="text"><![CDATA[Grafana Loki and Kubernetes Event exporter]]></title>
      <link rel="alternate" type="text/html" href="https://www.bigbinary.com/blog/k8s-event-exporter-and-grafana-loki-integration"/>
      <link rel="enclosure" type="image/jpeg" href="https://www.bigbinary.com/blog/images/social_preview_images/grafana_loki_and_kubernetes_event_exporter.jpg"/>
      <id>https://www.bigbinary.com/blog/k8s-event-exporter-and-grafana-loki-integration</id>
      <published>2024-05-07T00:00:00.000Z</published>
      <updated>2024-05-07T00:00:00.000Z</updated>
      <author>
        <name>Vishal Yadav</name>
        <uri>https://www.bigbinary.com/blog/authors/vishal76342</uri>
      </author>
      <category term="NeetoCI"/>
      <category term="Prometheus"/>
      <category term="Grafana"/>
      <category term="Kubernetes"/>
      <media:thumbnail url="https://www.bigbinary.com/blog/images/social_preview_images/grafana_loki_and_kubernetes_event_exporter.jpg" width="1200" height="627"/>
      <summary type="text"><![CDATA[In this blog, we'll explore how to integrate the Kubernetes Event exporter & Grafana Loki into your Kubernetes Cluster using a helm chart.]]></summary>
      <content type="html" xml:base="https://www.bigbinary.com/blog/k8s-event-exporter-and-grafana-loki-integration"><![CDATA[<p>In the previous
<a href="https://www.bigbinary.com/blog/prometheus-and-grafana-integration">blog</a>, we
discussed integrating <a href="https://prometheus.io/">Prometheus</a> and
<a href="https://grafana.com/">Grafana</a> in the Kubernetes Cluster. In this blog, we'll
explore how to integrate the
<a href="https://github.com/resmoio/kubernetes-event-exporter">Kubernetes Event exporter</a>
&amp; <a href="https://grafana.com/oss/loki/">Grafana Loki</a> into your Kubernetes Cluster
using a helm chart.</p>
<p>Additionally, you’ll also learn how to add Grafana Loki as a data source to your
Grafana Dashboard. This will help you visualize the Kubernetes events.</p>
<p>Furthermore, we'll delve into the specifics of setting up the Event exporter and
Grafana Loki, ensuring you understand each step of the process. From downloading
and configuring the necessary helm charts to understanding the Grafana Loki
dashboard, we'll cover it all.</p>
<p>By the end of this blog, you'll be able to fully utilize Grafana Loki and
Kubernetes Event Exporter, gaining insights from your Kubernetes events.</p>
<h2>How Kubernetes event exporter can help us in monitoring health</h2>
<p>Objects in Kubernetes, such as Pod, Deployment, Ingress, Service publish events
to indicate status updates or problems. Most of the time, these events are
overlooked and their 1-hour lifespan might cause missing important updates. They
are also not searchable and cannot be aggregated.</p>
<p>For instance, they can alert you to changes in the state of pods, errors in
scheduling, and resource constraints. Therefore, exporting these events and
visualizing them can be crucial for maintaining the health of your cluster.</p>
<p>Kubernetes event exporter allows exporting the often missed Kubernetes events to
various outputs so that they can be used for observability or alerting purposes.
We can have multiple receivers to export the events from the Kubernetes cluster.</p>
<ul>
<li><a href="https://www.opsgenie.com/">Opsgenie</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#webhookshttp">Webhooks/HTTP</a></li>
<li><a href="https://www.elastic.co/">Elasticsearch</a></li>
<li><a href="https://opensearch.org/">OpenSearch</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#slack">Slack</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#kinesis">Kinesis</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#firehose">Firehose</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#sns">SNS</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#sqs">SQS</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#file">File</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#stdout">Stdout</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#kafka">Kafka</a></li>
<li><a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/OpsCenter.html">OpsCenter</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#customizing-payload">Customize Payload</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#pubsub">Pubsub</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#teams">Teams</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#syslog">Syslog</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#bigquery">Bigquery</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#pipe">Pipe</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#aws-eventbridge">Event Bridge</a></li>
<li><a href="https://github.com/resmoio/kubernetes-event-exporter#loki">Grafana Loki</a></li>
</ul>
<h2>Setting up Grafana Loki &amp; Kubernetes event exporter using Helm chart</h2>
<p>We will once again use <a href="https://artifacthub.io/">ArtifactHub</a>, which provides a
helm chart for installing Grafana Loki onto a Kubernetes Cluster. If you need
instructions on how to install Helm on your system, you can refer to this blog.</p>
<p>In this blog post, we will install a Helm
<a href="https://artifacthub.io/packages/helm/grafana/loki">chart</a> that sets up Loki in
scalable mode, with separate read-and-write components that can be independently
scaled. Alternatively, we can install Loki in monolithic mode, where the Helm
Chart installation runs the Grafana Loki <em>single binary</em> within a Kubernetes
cluster. You can learn more about this
<a href="https://grafana.com/docs/loki/latest/setup/install/helm/install-monolithic/#install-the-monolithic-helm-chart">here</a>.</p>
<h3>1. Create S3 buckets</h3>
<ul>
<li>
<p>grafana-loki-chunks-bucket</p>
</li>
<li>
<p>grafana-loki-admin-bucket</p>
</li>
<li>
<p>grafana-loki-ruler-bucket</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/loki-s3-buckets.png" alt="The four Grafana Loki buckets listed in the AWS S3 console"></p>
</li>
</ul>
<h3>2. Create a policy for Grafana Loki</h3>
<p>Create a new policy under IAM on Amazon AWS using the below snippet.</p>
<pre><code>{
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
        {
            &quot;Sid&quot;: &quot;LokiStorage&quot;,
            &quot;Effect&quot;: &quot;Allow&quot;,
            &quot;Action&quot;: [
                &quot;s3:ListBucket&quot;,
                &quot;s3:PutObject&quot;,
                &quot;s3:GetObject&quot;,
                &quot;s3:DeleteObject&quot;
            ],
            &quot;Resource&quot;: [
                &quot;arn:aws:s3:::grafana-loki-chunks-bucket&quot;,
                &quot;arn:aws:s3:::grafana-loki-chunks-bucket/*&quot;,
                &quot;arn:aws:s3:::grafana-loki-admin-bucket&quot;,
                &quot;arn:aws:s3:::grafana-loki-admin-bucket/*&quot;,
                &quot;arn:aws:s3:::grafana-loki-ruler-bucket&quot;,
                &quot;arn:aws:s3:::grafana-loki-ruler-bucket/*&quot;
            ]
        }
    ]
}
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/grafana-loki-policy.png" alt="Terminal output confirming the Grafana Loki IAM policy was created"></p>
<h3>3. Create a Role with the above permission</h3>
<p>Create a role with a custom trust policy &amp; use the below snippet</p>
<pre><code>{
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
        {
            &quot;Effect&quot;: &quot;Allow&quot;,
            &quot;Principal&quot;: {
                &quot;Federated&quot;: &quot;arn:aws:iam::account_id:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/open_id&quot;
            },
            &quot;Action&quot;: &quot;sts:AssumeRoleWithWebIdentity&quot;,
            &quot;Condition&quot;: {
                &quot;StringEquals&quot;: {
                    &quot;oidc.eks.us-east-1.amazonaws.com/id/open_id:aud&quot;: &quot;sts.amazonaws.com&quot;,
                    &quot;oidc.eks.us-east-1.amazonaws.com/id/open_id:sub&quot;: &quot;system:serviceaccount:default:grafana-loki-access-s3-role-sa&quot;
                }
            }
        }
    ]
}
</code></pre>
<p>Note: Please update the account_id and open_id in the above given snippet.</p>
<p><strong>grafana-loki-access-s3-role-sa</strong> is the service account name that we will
mention in the Loki values.</p>
<h3>4. Add Grafana using the helm chart</h3>
<p>To get this Helm chart, run this command:</p>
<pre><code class="language-jsx">helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/chart-add-output.png" alt="Terminal output from adding and updating the Grafana Helm chart repository"></p>
<p>We have downloaded the latest version of the Grafana.</p>
<h3>5. Install grafana/loki stack using the helm chart</h3>
<p>Create a <strong>loki-values yaml</strong> file with the below snippet</p>
<pre><code>loki:
  readinessProbe: {}
  auth_enabled: false
  storage:
    bucketNames:
      chunks: grafana-loki-chunks-bucket
      ruler: grafana-loki-rules-bucket
      admin: grafana-loki-admin-bucket
    type: s3
    s3:
      endpoint: null
      region: us-east-1
      secretAccessKey: null
      accessKeyId: null
      s3ForcePathStyle: false
      insecure: false
monitoring:
  lokiCanary:
      enabled: false
  selfMonitoring:
    enabled: false
test:
  enabled: false
serviceAccount:
  create: true
  name: grafana-loki-access-s3-role-sa
  imagePullSecrets: []
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::account_id:role/loki-role
  automountServiceAccountToken: true
</code></pre>
<p>To install Loki using the Helm Chart on Kubernetes Cluster, run
this <code>helm install</code> command:</p>
<pre><code class="language-jsx">helm install my-loki grafana/loki --values loki-values.yaml
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/chart-installation-output.png" alt="Terminal output from installing the Loki Helm chart"></p>
<p>We have successfully installed Loki on the Kubernetes Cluster.</p>
<p>Run the following command to view all the resources created by the Loki Helm
Chart in your Kubernetes cluster:</p>
<pre><code class="language-jsx">kubectl get all -l app.kubernetes.io/name=loki
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/all-resources-output.png" alt="Output of kubectl get all for the Loki release, listing its pods, services and deployments"></p>
<p>Helm chart created the following components:</p>
<ul>
<li><strong>Loki read and write:</strong> Loki is installed in scalable mode by default, which
includes a read-and-write component. These components can be independently
scaled out.</li>
<li><strong>Gateway:</strong> Inspired by Grafana’s
<a href="https://github.com/grafana/loki/blob/main/production/ksonnet/loki">Tanka setup</a>,
the chart installs a gateway component by default. This NGINX component
exposes Loki’s API and automatically proxies requests to the appropriate Loki
components (read or write, or a single instance in the case of filesystem
storage). The gateway must be enabled to provide an Ingress since the Ingress
only exposes the gateway. If enabled, Grafana and log shipping agents, such as
Promtail, should be configured to use the gateway. If NetworkPolicies are
enabled, they become more restrictive when the gateway is active.</li>
<li><strong>Caching:</strong> In-memory caching is enabled by default. If this type of caching
is unsuitable for your deployment, consider setting up memcache.</li>
</ul>
<p>Run this command to view all the Kubernetes Services for Prometheus &amp; Grafana:</p>
<pre><code class="language-jsx">kubectl get service -l app.kubernetes.io/name=loki
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/all-services-output.png" alt="Output of kubectl get service for the Loki release, listing its services"></p>
<p>Listed services for Loki are:</p>
<ul>
<li>loki-backend</li>
<li>loki-backend-headless</li>
<li>loki-gateway</li>
<li>loki-memberlist</li>
<li>loki-read</li>
<li>loki-read-headless</li>
<li>loki-write</li>
<li>loki-write-headless</li>
<li>query-scheduler-discovery</li>
</ul>
<p>The <code>loki-gateway</code> service will be used to add Loki as a Datasource into
Grafana.</p>
<h3>6. Adding Loki data source in Grafana</h3>
<p>On the main page of Grafana, click on &quot;<strong>Home</strong>&quot;. Under &quot;<strong>Connections</strong>&quot;, you
will find the &quot;<strong>Data sources</strong>&quot; option.</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/grafana-dashboard.webp" alt="The Grafana home page, with the Data sources option listed under Connections"></p>
<p>On the Data Sources page, click on the &quot;Add new data source&quot; button.</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/data-sources-page.png" alt="The Grafana Data sources page with the Add new data source button"></p>
<p>In the search bar, type &quot;Loki&quot; and search for it.</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/add-data-source.png" alt="Search results for Loki on the Add data source page"></p>
<p>Clicking on &quot;Loki&quot; will redirect you to the dedicated page for the Loki data
source.</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/loki-data-source.png" alt="The dedicated settings page for the Loki data source in Grafana"></p>
<p>To read the metrics from Loki, we will use the <code>loki-gateway</code> service. Add the
URL of the service as <code>http://loki-gateway</code>.</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/loki-form.png" alt="The Loki data source form with the connection URL set to http://loki-gateway"></p>
<p>After clicking on the &quot;Save &amp; test&quot; button, you will receive a toastr message
shown in the image below. This message is received because no clients have been
created for Loki yet.</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/loki-addon-output.png" alt="A Grafana toast reporting that no clients exist yet for the Loki data source"></p>
<h3>7. Install Kubernetes event exporter using the helm chart</h3>
<p>Create an <strong>event-exporter-values yaml</strong> file with the below snippet</p>
<pre><code class="language-jsx">config:
  leaderElection: {}
  logLevel: debug
  logFormat: pretty
  metricsNamePrefix: event_exporter_
  receivers:
    - name: &quot;dump&quot;
      file:
        path: &quot;/dev/stdout&quot;
        layout: {}
    - name: &quot;loki&quot;
      loki:
        url: &quot;http://loki-gateway/loki/api/v1/push&quot;
        streamLabels:
          source: kubernetes-event-exporter
          container: kubernetes-event-exporter
  route:
    routes:
      - match:
          - receiver: &quot;dump&quot;
          - receiver: &quot;loki&quot;
</code></pre>
<p>With the use of the above snippet, run this command to install the Kubernetes
event exporter in your Kubernetes Cluster.</p>
<pre><code class="language-jsx">helm repo add bitnami [https://charts.bitnami.com/bitnami](https://charts.bitnami.com/bitnami)
helm install event-exporter bitnami/kubernetes-event-exporter --values event-exporter.yaml
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/event-exporter-installation-output.png" alt="Terminal output from installing the kubernetes-event-exporter Helm chart"></p>
<p>To view all the resources created by the above helm chart, run this command:</p>
<pre><code class="language-jsx">kubectl get all -l app.kubernetes.io/name=kubernetes-event-exporter
</code></pre>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/event-exporter-all-resources.png" alt="Output of kubectl get all for the event exporter release, listing its pod and deployment"></p>
<p>To view the logs of the event exporter POD, run this command:</p>
<pre><code class="language-jsx">kubectl logs -f pod/kubernetes-event-exporter-586455bbdd-sqlqc
</code></pre>
<p>Note: Replace <strong>kubernetes-event-exporter-586455bbdd-sqlqc</strong> with your pod name.</p>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/event-exporter-logs.webp" alt="event-exporter-logs"></p>
<p>As you can see in the above image, the event exporter is working and running
fine. Event logs are being sent to both the receivers that we’d configure in the
values YAML file.</p>
<p>Once the POD is created &amp; running, we can go back to the Loki data source under
<strong>Connections</strong> &gt; <strong>Data Sources</strong> page.</p>
<p>Again click on the “Save &amp; test” button &amp; this time you’ll receive a success
toastr message.</p>
<p>Output:</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/loki-data-source-added-output.png" alt="loki-data-source-added-output"></p>
<h3>8. Kubernetes event exporter dashboard</h3>
<p>We will import this
<a href="https://grafana.com/grafana/dashboards/17882-kubernetes-event-exporter/">dashboard</a>
into Grafana to monitor and track the events received from the Kubernetes
cluster. You can go through this blog if you want to learn how to import an
existing dashboard into Grafana.</p>
<p>After successfully importing the dashboard, you can view all the events from the
cluster, as shown in the image below. Additionally, you can filter the events
based on any value within any interval.</p>
<p>Kubernetes Event Exporter</p>
<p><img src="https://www.bigbinary.com/blog/images/images_used_in_blog/2024/k8s-event-exporter-and-grafana-loki-integration/event-exporter-dashboard.png" alt="Kubernetes Event Exporter"></p>
<h2>Conclusion</h2>
<p>In this blog post, we discussed the process of setting up Grafana Loki and
Kubernetes Event exporter. We covered various steps, such as creating a policy
for Grafana Loki, creating a role with the necessary permissions, and adding
Grafana using the Helm chart, installing the Loki stack, and adding Loki as a
data source in Grafana, installing Kubernetes event exporter using the Helm
chart, and finally, setting up the Kubernetes event exporter dashboard in
Grafana.</p>
<p>By following the steps outlined in this blog post, you can effectively monitor
and track events from your Kubernetes cluster using Grafana Loki and Kubernetes
Event exporter. This setup provides valuable insights and helps in
troubleshooting and analyzing events in your cluster.</p>
<p>If you have any questions or feedback, please feel free to reach out. Happy
monitoring!</p>
]]></content>
    </entry>
</feed>