Skip to content

Commit dd34ed6

Browse files
committed
Add workshop files
Signed-off-by: Richard Hartmann <[email protected]>
1 parent f3bfae8 commit dd34ed6

File tree

8 files changed

+381
-2
lines changed

8 files changed

+381
-2
lines changed

workshops/k8s-monitoring/finish.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clone the tutorial repository by running the following command:

workshops/k8s-monitoring/index.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"title": "Why team Faro Mimir Loki should obviously win",
3+
"description": "They. Are. Awesome.",
4+
"details": {
5+
"intro": {
6+
"text": "intro.md"
7+
},
8+
"steps": [
9+
{
10+
"text": "step1.md"
11+
},
12+
{
13+
"text": "step2.md"
14+
},
15+
{
16+
"text": "step3.md"
17+
}
18+
],
19+
"finish": {
20+
"text": "finish.md"
21+
}
22+
},
23+
"backend": {
24+
"imageid": "kubernetes-kubeadm-2nodes"
25+
}
26+
}

workshops/k8s-monitoring/intro.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Things to know
2+
3+
Before you begin, here are some things you should know:
4+
5+
- **Loki**: Loki can run in a single binary mode or as a distributed system. In this tutorial, we will deploy Loki as a single binary otherwise known as monolithic mode. Loki can be vertically scaled in this mode depending on the amount of logs you are collecting. It is recommended to run Loki in a distributed/microservice mode for production use cases to monitor high volumes of logs.
6+
7+
- **Deployment**: We will deploy Loki, Grafana and Alloy (As part of the Kubernetes Monitoring Helm) in the `meta`{{copy}} namespace of your Kubernetes cluster. Make sure you have the necessary permissions to create resources in this namespace. These pods will also require resources to run so consider the amount of capacity your nodes have available. It also possible to just deploy the Kubernetes monitoring helm (since it has a minimal resource footprint) within your cluster and write logs to an external Loki instance or Grafana Cloud.
8+
9+
- **Storage**: In this tutorial, Loki will use the default object storage backend provided in the Loki Helm; [MinIO](https://min.io/docs/minio/kubernetes/upstream/index.html). You should migrate to a more production-ready storage backend like [S3](https://aws.amazon.com/s3/getting-started/), [GCS](https://cloud.google.com/storage/docs), [Azure Blob Storage](https://learn.microsoft.com/en-us/azure/storage/blobs/) or a MinIO Cluster for production use cases.
10+
11+
# Prerequisites
12+
13+
Before you begin, you will need the following:
14+
15+
- A Kubernetes cluster running version `1.23`{{copy}} or later.
16+
17+
- [kubectl](https://kubernetes.io/docs/tasks/tools/) installed on your local machine.
18+
19+
- [helm](https://helm.sh/docs/intro/install/) installed on your local machine.
20+
21+
> **Tip:**
22+
> Alternatively, you can try out this example in our interactive learning environment: [Kubernetes Monitoring with Loki](https://killercoda.com/grafana-labs/course/loki/k8s-monitoring).
23+
>
24+
> It’s a fully configured environment with all the dependencies already installed.
25+
>
26+
> ![Interactive](https://grafana.com/media/docs/loki/loki-ile.svg)
27+
>
28+
> Provide feedback, report bugs, and raise issues in the [Grafana Killercoda repository](https://github.com/grafana/killercoda).
+282
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
---
2+
title: Kubernetes Monitoring with Loki
3+
menuTitle: Kubernetes Monitoring with Loki
4+
weight: 300
5+
description: Learn how to collect and store logs from your Kubernetes cluster using Loki.
6+
killercoda:
7+
title: Why team Faro Mimir Loki should obviously win
8+
description: They. Are. Awesome.
9+
backend:
10+
imageid: kubernetes-kubeadm-2nodes
11+
---
12+
13+
# Kubernetes Monitoring with Loki
14+
15+
One of the primary use cases for Loki is to collect and store logs from your [Kubernetes cluster](https://kubernetes.io/docs/concepts/overview/). These logs fall into three categories:
16+
17+
1. [**Pod logs**](https://kubernetes.io/docs/concepts/cluster-administration/logging/#basic-logging-in-kubernetes): Logs generated by containers otherwise known as logs running in your cluster.
18+
2. [**Kubernetes Events**](https://kubernetes.io/docs/reference/kubernetes-api/cluster-resources/event-v1/): Logs generated by the Kubernetes API server.
19+
3. [**Node logs**](https://kubernetes.io/docs/concepts/cluster-administration/logging/#using-a-node-logging-agent): Logs generated by the nodes in your cluster.
20+
21+
{{< figure max-width="75%" src="/media/docs/loki/loki-k8s-logs.png" caption="Scraping Kubernetes Logs" alt="Scraping Kubernetes Logs" >}}
22+
23+
In this tutorial, we will deploy [Loki](https://grafana.com/docs/loki/latest/get-started/overview/) and the [Kubernetes Monitoring Helm chart](https://grafana.com/docs/grafana-cloud/monitor-infrastructure/kubernetes-monitoring/) to collect two of these log types: Pod logs and Kubernetes events. We will also deploy [Grafana](https://grafana.com/docs/grafana/latest/) to visualize these logs.
24+
25+
<!-- INTERACTIVE page intro.md START -->
26+
27+
## Things to know
28+
29+
Before you begin, here are some things you should know:
30+
31+
* **Loki**: Loki can run in a single binary mode or as a distributed system. In this tutorial, we will deploy Loki as a single binary otherwise known as monolithic mode. Loki can be vertically scaled in this mode depending on the amount of logs you are collecting. It is recommended to run Loki in a distributed/microservice mode for production use cases to monitor high volumes of logs.
32+
* **Deployment**: We will deploy Loki, Grafana and Alloy (As part of the Kubernetes Monitoring Helm) in the `meta` namespace of your Kubernetes cluster. Make sure you have the necessary permissions to create resources in this namespace. These pods will also require resources to run so consider the amount of capacity your nodes have available. It also possible to just deploy the Kubernetes monitoring helm (since it has a minimal resource footprint) within your cluster and write logs to an external Loki instance or Grafana Cloud.
33+
* **Storage**: In this tutorial, Loki will use the default object storage backend provided in the Loki Helm; [MinIO](https://min.io/docs/minio/kubernetes/upstream/index.html). You should migrate to a more production-ready storage backend like [S3](https://aws.amazon.com/s3/getting-started/), [GCS](https://cloud.google.com/storage/docs), [Azure Blob Storage](https://learn.microsoft.com/en-us/azure/storage/blobs/) or a MinIO Cluster for production use cases.
34+
35+
## Prerequisites
36+
37+
Before you begin, you will need the following:
38+
39+
* A Kubernetes cluster running version `1.23` or later.
40+
* [kubectl](https://kubernetes.io/docs/tasks/tools/) installed on your local machine.
41+
* [helm](https://helm.sh/docs/intro/install/) installed on your local machine.
42+
43+
> **Tip:**
44+
> Alternatively, you can try out this example in our interactive learning environment: [Kubernetes Monitoring with Loki](https://killercoda.com/grafana-labs/course/loki/k8s-monitoring).
45+
>
46+
> It's a fully configured environment with all the dependencies already installed.
47+
>
48+
> ![Interactive](/media/docs/loki/loki-ile.svg)
49+
>
50+
> Provide feedback, report bugs, and raise issues in the [Grafana Killercoda repository](https://github.com/grafana/killercoda).
51+
52+
<!-- INTERACTIVE page intro.md END -->
53+
<!-- INTERACTIVE page step1.md START -->
54+
55+
```bash
56+
57+
echo 'rm -rf /'
58+
59+
```
60+
61+
## Step 1: Create the `meta` and `prod` namespaces
62+
63+
The K8s Monitoring Helm chart will monitor two namespaces: `meta` and `prod`:
64+
- `meta` namespace: This namespace will be used to deploy Loki, Grafana, and Alloy.
65+
- `prod` namespace: This namespace will be used to deploy the sample application that will generate logs.
66+
67+
Create the `meta` and `prod` namespaces by running the following commands:
68+
69+
```bash
70+
kubectl create namespace meta && kubectl create namespace prod
71+
```
72+
73+
<!-- INTERACTIVE page step1.md END -->
74+
<!-- INTERACTIVE page step2.md START -->
75+
76+
## Step 2: Add the Grafana Helm repository
77+
78+
All three helm charts (Loki, Grafana, and the Kubernetes Monitoring Helm) are available in the Grafana Helm repository. Add the Grafana Helm repository by running the following command:
79+
80+
```bash
81+
helm repo add grafana https://grafana.github.io/helm-charts && helm repo update
82+
```
83+
84+
It's recommended to also run `helm repo update` to ensure you have the latest version of the charts.
85+
86+
<!-- INTERACTIVE page step2.md END -->
87+
<!-- INTERACTIVE page step3.md START -->
88+
89+
## Step 3: Clone the tutorial repository
90+
91+
<!-- INTERACTIVE page finish.md START -->
92+
Clone the tutorial repository by running the following command:
93+
<!-- INTERACTIVE page finish.md END -->
94+
95+
```bash
96+
git clone https://github.com/grafana/alloy-scenarios.git && cd alloy-scenarios/k8s-logs
97+
```
98+
99+
As well as cloning the repository, we have also changed directories to `alloy-scenarios/k8s-logs`. **The rest of this tutorial assumes you are in this directory.**
100+
101+
<!-- INTERACTIVE page step3.md END -->
102+
103+
## Step 4: Deploy Loki
104+
105+
Grafana Loki will be used to store our collected logs. In this tutorial we will deploy Loki with a minimal footprint and use the default storage backend provided by the Loki Helm (MinIO).
106+
107+
> **Note**: Due to the resource constraints of the Kubernetes cluster running in the playground, we are deploying Loki using a custom values file. This values file reduces the resource requirements of Loki. This turns off features such as; cache, Loki Canary, and runs Loki with limited resources. This can take up to **1 minute** to complete.
108+
109+
To deploy Loki run the following command:
110+
111+
```bash
112+
helm install --values loki-values.yml loki grafana/loki -n meta
113+
```
114+
115+
```bash
116+
helm install --values killercoda/loki-values.yml loki grafana/loki -n meta
117+
```
118+
119+
120+
This command will deploy Loki in the `meta` namespace. The command also includes a `values` file that specifies the configuration for Loki. For more details on how to configure the Loki Helm refer to the Loki Helm [documentation](https://grafana.com/docs/loki/<LOKI_VERSION>/setup/install/helm).
121+
122+
## Step 5: Deploy Grafana
123+
124+
Next we will deploy Grafana to the meta namespace. Grafana will be used to visualize the logs stored in Loki. To deploy Grafana run the following command:
125+
126+
```bash
127+
helm install --values grafana-values.yml grafana grafana/grafana --namespace meta
128+
```
129+
130+
As before the command also includes a `values` file that specifies the configuration for Grafana. There are two important configurations attributes to take note of:
131+
132+
1. `adminUser` & `adminPassword`: These are the credentials you will use to log in to Grafana. The values are `admin` and `adminadminadmin` respectively. The recommended practice is to either use a Kubernetes secret or allow Grafana to generate a password for you. For more details on how to configure the Grafana Helm refer to the Grafana Helm [documentation](https://grafana.com/docs/grafana/latest/installation/helm/).
133+
134+
2. `datasources`: This section of the configuration allows for the definition of data sources that Grafana will use. In this tutorial, we will define a data source for Loki. The data source is defined as follows:
135+
136+
```yaml
137+
datasources:
138+
datasources.yaml:
139+
apiVersion: 1
140+
datasources:
141+
- name: Loki
142+
type: loki
143+
access: proxy
144+
orgId: 1
145+
url: http://loki-gateway.meta.svc.cluster.local:80
146+
basicAuth: false
147+
isDefault: false
148+
version: 1
149+
editable: false
150+
```
151+
This configuration defines a data source named `Loki` that Grafana will use to query logs stored in Loki. The `url` attribute specifies the URL of the Loki gateway. The Loki gateway is a service that sits in front of the Loki API and provides a single endpoint for ingesting and querying logs. The URL is in the format `http://loki-gateway.meta.svc.cluster.local:80`. The `loki-gateway` service is created by the Loki Helm chart and is used to query logs stored in Loki. **If you choose to deploy Loki in a different namespace or with a different name, you will need to update the `url` attribute accordingly.**
152+
153+
## Step 6: Deploy the Kubernetes Monitoring Helm
154+
155+
The Kubernetes Monitoring Helm chart is used for gathering, scraping, and forwarding Kubernetes telemetry data to a Grafana Stack. This includes the ability to collect; metrics, logs, traces & continuous profiling data. The scope of this tutorial is to deploy the Kubernetes Monitoring Helm chart to collect pod logs and Kubernetes events.
156+
157+
To deploy the Kubernetes Monitoring Helm chart run the following command:
158+
159+
```bash
160+
helm install --values ./k8s-monitoring-values.yml k8s grafana/k8s-monitoring -n meta
161+
```
162+
Within the configuration file `k8s-monitoring-values.yml` we have defined the following:
163+
164+
```yaml
165+
---
166+
cluster:
167+
name: meta-monitoring-tutorial
168+
169+
destinations:
170+
- name: loki
171+
type: loki
172+
url: http://loki-gateway.meta.svc.cluster.local/loki/api/v1/push
173+
174+
175+
clusterEvents:
176+
enabled: true
177+
collector: alloy-logs
178+
namespaces:
179+
- meta
180+
- prod
181+
182+
nodeLogs:
183+
enabled: false
184+
185+
podLogs:
186+
enabled: true
187+
gatherMethod: kubernetesApi
188+
collector: alloy-logs
189+
namespaces:
190+
- meta
191+
- prod
192+
193+
# Collectors
194+
alloy-singleton:
195+
enabled: false
196+
197+
alloy-metrics:
198+
enabled: false
199+
200+
alloy-logs:
201+
enabled: true
202+
203+
alloy-profiles:
204+
enabled: false
205+
206+
alloy-receiver:
207+
enabled: false
208+
```
209+
210+
To break down the configuration file:
211+
* Define the cluster name as `meta-monitoring-tutorial`. This a static label that will be attached to all logs collected by the Kubernetes Monitoring Helm chart.
212+
* Define a destination named `loki` that will be used to forward logs to Loki. The `url` attribute specifies the URL of the Loki gateway. **If you choose to deploy Loki in a different namespace or in a different location entirley, you will need to update the `url` attribute accordingly.**
213+
* Enable the collection of cluster events and pod logs:
214+
* `collector`: specifies which collector to use to collect logs. In this case, we are using the `alloy-logs` collector.
215+
* `namespaces`: specifies the namespaces to collect logs from. In this case, we are collecting logs from the `meta` and `prod` namespaces.
216+
* Disable the collection of node logs for the purpose of this tutorial as it requires the mounting of `/var/log/journal`. This is out of scope for this tutorial.
217+
* Lastly, define the role of the collector. The Kubernetes Monitoring Helm chart will deploy only what you need and nothing more. In this case, we are telling the Helm chart to only deploy Alloy with the capability to collect logs. If you need to collect K8s metrics, traces, or continuous profiling data, you can enable the respective collectors.
218+
219+
220+
## Step 7: Accessing Grafana
221+
222+
To access Grafana, you will need to port-forward the Grafana service to your local machine. To do this, run the following command:
223+
224+
```bash
225+
export POD_NAME=$(kubectl get pods --namespace meta -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}") && \
226+
kubectl --namespace meta port-forward $POD_NAME 3000 --address 0.0.0.0
227+
```
228+
229+
> **Tip:**
230+
> This will make your terminal unusable until you stop the port-forwarding process. To do this, press `Ctrl + C`.
231+
232+
This command will port-forward the Grafana service to your local machine on port `3000`. You can access Grafana by navigating to [http://localhost:3000](http://localhost:3000) in your browser. The default credentials are `admin` and `adminadminadmin`. One of the first places you should visit is Explore Logs which will provide a no-code view of the logs being stored in Loki:
233+
234+
[http://localhost:3000/a/grafana-lokiexplore-app](http://localhost:3000/a/grafana-lokiexplore-app)
235+
236+
{{< figure max-width="100%" src="/media/docs/loki/k8s-logs-explore-logs.png" caption="Explore Logs view of K8s logs" alt="Explore Logs view of K8s logs" >}}
237+
238+
## Step 8 (Optional): View the Alloy UI
239+
240+
The Kubernetes Monitoring Helm chart deploys Grafana Alloy to collect and forward telemetry data from the Kubernetes cluster. The Helm is designed to abstract you from creating an Alloy configuration file. However if you would like to understand the pipeline you can view the Alloy UI. To access the Alloy UI, you will need to port-forward the Alloy service to your local machine. To do this, run the following command:
241+
242+
```bash
243+
export POD_NAME=$(kubectl get pods --namespace meta -l "app.kubernetes.io/name=alloy-logs,app.kubernetes.io/instance=k8s" -o jsonpath="{.items[0].metadata.name}") && \
244+
kubectl --namespace meta port-forward $POD_NAME 12345 --address 0.0.0.0
245+
```
246+
247+
> **Tip:**
248+
> This will make your terminal unusable until you stop the port-forwarding process. To do this, press `Ctrl + C`.
249+
250+
This command will port-forward the Alloy service to your local machine on port `12345`. You can access the Alloy UI by navigating to [http://localhost:12345](http://localhost:12345) in your browser.
251+
252+
{{< figure max-width="100%" src="/media/docs/loki/k8s-logs-alloy-ui.png" caption="Grafana Alloy UI" alt="Grafana Alloy UI" >}}
253+
254+
## Step 9: Adding a sample application to `prod`
255+
256+
Lastly, lets deploy a sample application to the `prod` namespace that will generate some logs. To deploy the sample application run the following command:
257+
258+
```bash
259+
helm install tempo grafana/tempo-distributed -n prod
260+
```
261+
262+
This will deploy a default version of Grafana Tempo to the `prod` namespace. Tempo is a distributed tracing backend that is used to store and query traces. Normally Tempo would sit alongside Loki and Grafana in the `meta` namespace, but for the purpose of this tutorial, we will pretend this is the primary application generating logs.
263+
264+
Once deployed lets expose Grafana once more:
265+
266+
```bash
267+
export POD_NAME=$(kubectl get pods --namespace meta -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=grafana" -o jsonpath="{.items[0].metadata.name}") && \
268+
kubectl --namespace meta port-forward $POD_NAME 3000 --address 0.0.0.0
269+
```
270+
271+
and navigate to [http://localhost:3000/a/grafana-lokiexplore-app](http://localhost:3000/a/grafana-lokiexplore-app) to view Grafana Tempo logs.
272+
273+
{{< figure max-width="100%" src="/media/docs/loki/k8s-logs-tempo.png" caption="Label view of Tempo logs" alt="Label view of Tempo logs" >}}
274+
275+
276+
## Conclusion
277+
278+
In this tutorial, you learned how to deploy Loki, Grafana, and the Kubernetes Monitoring Helm chart to collect and store logs from a Kubernetes cluster. We have deployed a minimal test version of each of these helm charts to demonstrate how quickly you can get started with Loki. It now worth exploring each of these helm charts in more detail to understand how to scale them to meet your production needs:
279+
280+
* [Loki Helm](https://grafana.com/docs/loki/latest/setup/install/helm/)
281+
* [Grafana Helm](https://grafana.com/docs/grafana/latest/installation/helm/)
282+
* [Kubernetes Monitoring Helm](https://grafana.com/docs/grafana-cloud/monitor-infrastructure/kubernetes-monitoring/)

workshops/k8s-monitoring/step1.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```bash
2+
3+
echo 'rm -rf /'
4+
5+
```{{exec}}
6+
7+
# Step 1: Create the `meta`{{copy}} and `prod`{{copy}} namespaces
8+
9+
The K8s Monitoring Helm chart will monitor two namespaces: `meta`{{copy}} and `prod`{{copy}}:
10+
11+
- `meta`{{copy}} namespace: This namespace will be used to deploy Loki, Grafana, and Alloy.
12+
13+
- `prod`{{copy}} namespace: This namespace will be used to deploy the sample application that will generate logs.
14+
15+
Create the `meta`{{copy}} and `prod`{{copy}} namespaces by running the following commands:
16+
17+
```bash
18+
kubectl create namespace meta && kubectl create namespace prod
19+
```{{exec}}

workshops/k8s-monitoring/step2.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Step 2: Add the Grafana Helm repository
2+
3+
All three helm charts (Loki, Grafana, and the Kubernetes Monitoring Helm) are available in the Grafana Helm repository. Add the Grafana Helm repository by running the following command:
4+
5+
```bash
6+
helm repo add grafana https://grafana.github.io/helm-charts && helm repo update
7+
```{{exec}}
8+
9+
It’s recommended to also run `helm repo update`{{copy}} to ensure you have the latest version of the charts.

workshops/k8s-monitoring/step3.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Step 3: Clone the tutorial repository
2+
3+
<!-- raw HTML omitted -->
4+
5+
Clone the tutorial repository by running the following command:
6+
7+
<!-- raw HTML omitted -->
8+
9+
```bash
10+
git clone https://github.com/grafana/alloy-scenarios.git && cd alloy-scenarios/k8s-logs
11+
```{{exec}}
12+
13+
As well as cloning the repository, we have also changed directories to `alloy-scenarios/k8s-logs`{{copy}}. **The rest of this tutorial assumes you are in this directory.**

0 commit comments

Comments
 (0)