Skip to content
ImagineWorld

Monitoring and Logging

node_idnode-book_rancher-deep-dive_chapter-10-monitoring-and-logging
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num10
chapter_titleMonitoring and Logging
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

The previous chapters covered cluster configuration, backup, and recovery. This chapter will cover Rancher monitoring and how Rancher uses Prometheus and Grafana to collect metrics for a cluster and then appoint them. Then, we will cover Rancher logging and how Rancher uses the Banzai Cloud Logging operator and Fluent Bit to collect the logs from the Kubernetes components and collect application logs, including filtering logs.

In this chapter, we’re going to cover the following main topics:

  • What is Prometheus and Grafana?
  • Deploying Rancher’s monitoring stack
  • Adding custom application metrics to Prometheus
  • Creating alert rules in Prometheus
  • Creating a Grafana dashboard
  • What is the Banzai Cloud Logging operator?
  • What is Fluent Bit?
  • Deploying Rancher logging
  • Filtering application logs
  • Writing logs to multiple log servers

In this section, we’ll be covering the most popular monitoring solution for Kubernetes clusters.

Prometheus is an open source monitoring and alerting framework that the Kubernetes community has widely adopted. Prometheus was initially created by SoundCloud back in 2012 before it was accepted by the Cloud Native Computing Foundation (CNCF) as its second incubated project after Kubernetes. Prometheus was built from the ground up to work with Kubernetes, the core idea being that everything should be discoverable via the Kubernetes API. At this point, Prometheus will pull the metrics and store them as time-series key-value pairs.

Of course, the first question that always comes up is, what are metrics? In the simplest terms, it’s a numerical measurement of a resource. For example, it can be the current memory usage of a pod or the current number of connections to a database server. It is important to note that Prometheus doesn’t support anything but an integer or floating-point number for the values. You can’t set a value to something such as the words up or down for the value of a metric. For example, if you want to check whether a job was successful or failed, you might output that value as 0 for a successful status and 1 for a failed status.

The other central point with metrics is they should be a point-in-time value. For example, you might want the average number of connections at a given time. So, you would define a metrics endpoint at the pod level. One of the traps for new players is to add the metric endpoint to a service record. This might be easier but is not recommended for the long term because you might want a different rule in the future. For example, you start with just finding an average number of connections over the last 5 minutes, and then you want to change that to 15 minutes. Do you change the value of the current metric, which might affect historical reporting, or do you add another metric, which then means you are collecting duplicate data? The best approach is to output the raw data as a metric and then process it inside Prometheus and Grafana.

The next question that comes up is, how does Prometheus get its data? Because Prometheus uses a pull instead of a push model, this is done by running a web server that exports the metrics as a simple text output of key-value pairs. This is commonly called an exporter in Prometheus. These exporters can be built directly into your application, as in the case of most of the core components of Kubernetes. For example, etcd has a built-in metrics exporter that runs on a different port, 2379. It is common to run metrics on a different port than the main application because Prometheus, by default, will try making a GET request without authentication. Prometheus can query an endpoint that requires authentication, but setting up the tokens or credentials requires additional work and maintenance. So, most users will avoid it and use the fact that metrics are only exposed internally to the cluster and not to the public as good enough security.

Of course, Prometheus uses exporters to collect metrics, so the question of what exporters are available comes up. And luckily, because of the open source community, there are a significant number of third-party exporters for most standard applications. For example, almost all major open source databases have an exporter such as MySQL, CouchDB, MongoDB, MSSQL, Oracle DB, and PostgreSQL. You can find the official list at https://prometheus.io/docs/instrumenting/exporters/#databases. It’s the same with standard web servers such as Apache and NGINX, with the complete list available at https://prometheus.io/docs/instrumenting/exporters/#http. And, of course, almost all Kubernetes native applications such as CoreDNS, Longhorn, Linkerd, and OPA Gatekeeper have Prometheus exporters built right into the application.

For application developers, there are several libraries available for Go, Java/JVM, Python, and Node.js that allow even custom applications to add built-in support for Prometheus. Of course, if you can’t find a premade exporter for your application, upstream Prometheus provides excellent resources for writing your exporter, including naming standards, example code, and different technical aspects for handling use cases. All this can be found at https://prometheus.io/docs/instrumenting/writing_exporters/.

Finally, one of the newer features added to Prometheus is alerting. Because Prometheus is already collecting your environment and applications data, it makes sense to add alerting into Prometheus using AlertManager. The core concept is that you define a set of queries that will run inside the Prometheus server that, if violated, will trigger an alert, which will be sent to Alertmanager, which will forward that alert to several external services such as email, Slack, and PagerDuty. Later in this chapter, we’ll cover creating alerts in Prometheus along with some examples.

The one main feature that Prometheus is missing is a way to visualize your data. This is where Grafana comes into the picture. Grafana allows you to visualize data stored in Prometheus and other data sources such as MySQL, Loki, and InfluxDB. The main idea behind Grafana is that you create a dashboard that will query a data source (Prometheus, in this case) and then use this data to develop a range of graphs, charts, gauges, and so on. It is important to note that Gradana doesn’t store any data outside of caching query results. Grafana also supports exploring logs from sources such as Loki and Elasticsearch. It also has a notification system that can trigger alerts based on queries, as Prometheus does. This can be helpful for application teams to create custom alerts.

With Rancher, there are two main versions of monitoring, v1 and v2. The original v1 monitoring that came with Rancher 2.0 to 2.4 is based on Prometheus and Grafana. But with the Rancher server and UI managing the deployment and configuration of the monitoring stack, the basic idea is to deploy Prometheus at the cluster level and additional Prometheus servers for each Rancher project. This approach was fine if you had a small number of projects that didn’t need to be controlled via automation. This was mainly done because, initially, all the configurations of the Prometheus server were done by changing configmap. This required a great deal of work to manage the monitoring settings as clusters and applications grew in size and complexity.

With the creation of the Prometheus operator, it all changed. The core idea is that the Prometheus operator monitors a set of Custom Resource Definitions (CRDs). This includes the description of the Prometheus server and its related services such as node-exporter and Alertmanager. It is important to note that monitoring v1 and v2 has built-in rules and dashboards but, most importantly, the configuration of probes, alerts, and other related Prometheus settings, with Prometheus operator handling, creating, and updating the configuration files used by Prometheus.

In October 2020, Rancher 2.5 migrated from monitoring v1 to v2, with v2 being built on the operator model. It is important to note that both Prometheus and Grafana moved to this new model. This also included Rancher using the standard upstream Prometheus and Grafana image instead of the Rancher customized images.

If you are currently running the old v1 monitoring, migrating to the new v2 monitoring is recommended. The official process can be found at https://rancher.com/docs/rancher/v2.5/en/monitoring-alerting/guides/migrating/, but the process can be summarized as follows:

  1. You need to delete all the current settings and configurations.
  2. Then, uninstall the old Prometheus server and its components.
  3. At this point, you can install v2 monitoring and reconfigure all the settings.

You must make sure that nothing is left behind from v1 monitoring before installing v2. Luckily, one of the engineers at Rancher named Bastian Hofmann created a script that handles the process of collecting all the alerts and dashboards and migrating them over to v2 (https://github.com/bashofmann/rancher-monitoring-v1-to-v2). It is important to note that it is not an official script, and you should take an etcd backup before starting this process.

For deploying monitoring v1, log into the Rancher UI, go to Tools | Monitoring, and click the Enable button. At this point, the Rancher server will take over deploying the Prometheus server and node exporters. Then, all the monitoring configuration will be done via the Rancher UI. For example, if you wanted to view the CPU usage of a pod, you would browse to the pod in the Rancher UI, and Grafana graphs will be displayed right inside the UI. For additional details about the workload metrics that can be collected, please see the official Rancher documentation at https://rancher.com/docs/rancher/v2.0-v2.4/en/cluster-admin/tools/cluster-monitoring/cluster-metrics/.

It is important to note that cluster monitoring is only designed to be used by users that have full view access to the cluster. If you want to scope monitoring to a single project, you’ll need to enable project monitoring by going to the project and selecting Monitoring from the Tools menu. This will cause the Rancher server to deploy an additional Prometheus server with its own namespace inside the project. This Prometheus server is scoped to the project and its namespace:

Figure 10.1 – Rancher monitoring v1

For deploying monitoring v2, you have a couple of different options. The first one is to go to Cluster explorer | Cluster Tools and click Install next to Monitoring:

Figure 10.2 – Rancher monitoring v2

This will deploy Rancher’s monitoring chart via the app catalog. This chart is just a repackage of upstream images with no code changes. The only real difference is to use Rancher’s Docker Hub repositories in place of upstreams. Also, the default namespace is set to cattle-monitoring-system, but this can be customized if you so choose. Because monitoring v2 is a Helm chart, you can choose to deploy it directly via the helm command, which can be very helpful when managing clusters at scale, using tools such as Rancher’s fleet. The following is an example command:

helm repo add rancher-charts https://git.rancher.io/charts
helm repo update
helm upgrade –install -n cattle-monitoring-system rancher-monitoring rancher-charts/rancher-monitoring

You can find the complete command and values.yaml for installing Rancher monitoring via the helm command at https://github.com/PacktPublishing/Rancher-Deep-Dive/tree/main/ch10/rancher-monitoring-v2.

The second option is to deploy the upstream Helm chart, commonly called kube-prometheus-stack:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm upgrade –install -n monitoring monitoring prometheus-community/kube-prometheus-stack

It is important to note that, at the time of writing, this chart is still in beta and is subject to change, and Rancher doesn’t support all the versions available in the upstream charts. So, it recommends reviewing Rancher’s support matrix at https://www.suse.com/suse-rancher/support-matrix/all-supported-versions/. You can also find a complete list of the configuration options by viewing the chart value at https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack#configuration.

At this point, you should have Prometheus and Grafana installed on your cluster. It is important to note that it can take approximately 5–10 minutes for all the pods and services that Prometheus needs to start entirely. It is also important to note that, at the time of writing, Rancher does not fully support Prometheus federation – the idea being that you can have a central Prometheus server that scans all other Prometheus servers across your other clusters. If you would like to learn more about this, I recommend looking at the official documentation at https://prometheus.io/docs/prometheus/latest/federation/, but I would note that this is still a new feature and still evolving.

Adding custom application metrics to Prometheus

Section titled “Adding custom application metrics to Prometheus”

Of course, now that you have Prometheus and Grafana all installed and working, the question becomes, how do you get metrics from our applications into Prometheus? In this section, we will cover two main ways of doing this.

The easiest way is to use a community-created chart such as Bitnami’s MariaDB chart, including the metrics.enabled=true option. This option enables a sidecar that adds mysqld-exporter to the deployment, with many community-created charts using this model of having the exporter be a sidecar container to the main container. It is important to note that you should read the documentation for the Helm chart to see if any additional steps need to be taken when enabling metrics in your chart, as some applications will require a service account or permissions to be set for the exporter to work correctly.

Besides enabling the metrics, you’ll also see an annotation section with the prometheus.io/scrape=true and prometheus.io/port=9000 key pair. The port might be different, but it is a standard practice to set it to something in this range. These two annotations are significant, as they are what Prometheus uses when discovering all the different pods that should be scraped.

But let’s assume that you are using a custom-made application and want to capture metrics from this application. The following are a couple of examples of different applications where the metrics exporter is installed.

With GoLang, Prometheus provides an official library located at https://github.com/prometheus/client_golang/. This library handles most of the heavy lifting when generating the metrics output. You can find an example Go application and deployment at https://github.com/PacktPublishing/Rancher-Deep-Dive/tree/main/ch10/examples/go. You need to run the kubectl apply -f deploy.yaml command in order to deploy the example application. If you curl the pod IP address with the path/metrics, you’ll find that the application returns a list of different metrics (for example, curl 10.42.7.23:8080/metrics). Once the application is up and running, you can send a GET request to /ping, which will return the word pong. Then, inside the application, it will increase a counter called ping_request_count, which is a custom metric that is being exposed.

Here is an example of the metrics output:

# HELP ping_request_count No of request handled by Ping handler
# TYPE ping_request_count counter
ping_request_count 4
….
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 2
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

The full output can be found at https://github.com/PacktPublishing/Rancher-Deep-Dive/blob/main/ch10/examples/go/output.txt.

With Apache2, we need to take the sidecar option to add the exporter to the deployment. In our example, we are going to use a popular third-party exporter called apache_exporter. You can read more about this project at https://github.com/Lusitaniae/apache_exporter. The basic idea behind this project is to act as a translation layer between the Apache mod_status module and Prometheus. We need to install/enable the mod_status module to the primary web server container in the example deployment. Then, we need to expose the server-status page to the sidecar container that hosts the exporter. You can find the example and deployment file at https://github.com/PacktPublishing/Rancher-Deep-Dive/tree/main/ch10/examples/apache. You need to run the kubectl apply -f deploy.yaml command to deploy the example application.

Here is an example of the metrics output:

# HELP apache_accesses_total Current total apache accesses (*)
# TYPE apache_accesses_total counter
apache_accesses_total 6
# HELP apache_connections Apache connection statuses
# TYPE apache_connections gauge
apache_connections{state="closing"} 0
apache_connections{state="keepalive"} 0
apache_connections{state="total"} 1
apache_connections{state="writing"} 0
# HELP apache_cpu_time_ms_total Apache CPU time
# TYPE apache_cpu_time_ms_total counter
apache_cpu_time_ms_total{type="system"} 30
apache_cpu_time_ms_total{type="user"} 0
# HELP apache_cpuload The current percentage CPU used by each worker and in total by all workers combined (*)
# TYPE apache_cpuload gauge
apache_cpuload

The full output can be found at https://github.com/PacktPublishing/Rancher-Deep-Dive/blob/main/ch10/examples/apache/output.txt.

With NGINX, we will use a similar process as we did with Apache, but this time, we will use an exporter provided by NGINX. You can find the example and deployment file at https://github.com/PacktPublishing/Rancher-Deep-Dive/tree/main/ch10/examples/nginx. You simply need to run the kubectl apply -f deploy.yaml command.

Here is an example of the metrics output:

# HELP nginx_connections_accepted Accepted client connections
# TYPE nginx_connections_accepted counter
nginx_connections_accepted 1
# HELP nginx_connections_active Active client connections
# TYPE nginx_connections_active gauge
nginx_connections_active 1
# HELP nginx_connections_handled Handled client connections
# TYPE nginx_connections_handled counter
nginx_connections_handled 1
# HELP nginx_connections_reading Connections where NGINX is reading the request header
# TYPE nginx_connections_reading gauge
nginx_connections_reading 0
# HELP nginx_connections_waiting Idle client connections
# TYPE nginx_connections_waiting gauge
nginx_connections_waiting 0
# HELP nginx_connections_writing Connections where NGINX is writing the response back to the client
# TYPE nginx_connections_writing gauge
nginx_connections_writing 1
# HELP nginx_http_requests_total Total http requests
# TYPE nginx_http_requests_total counter
nginx_http_requests_total 9
# HELP nginx_up Status of the last metric scrape
# TYPE nginx_up gauge
nginx_up 1

The full output can be found at https://github.com/PacktPublishing/Rancher-Deep-Dive/blob/main/ch10/examples/nginx/output.txt..

At this point, we can monitor the metrics of our different applications, but we’re missing the ability to create alerts based on these metrics. In the next section, we’ll dive into alert rules for Prometheus.

The Prometheus operator defines alert rules via the CRD PrometheusRule. At its core, all an alert is is an expression with a trigger. Let’s look at the following example alert. This alert is from Longhorn, which we’ll cover in the next chapter. As you can see, the expression is denoted by the expr field, which has a formula to take the actual size of the volume, divided by the capacity, and convert it to a percentage. Then, if that value is greater than 90%, the expression is true, which will trigger an alert. The description section is mainly for the end user. Still, it’s important to note that you can have variables inside the description because the alert will contain the same explanation as the summary, typically used for the subject line. For example, when sending an email alert, the email’s subject will be set to the subject of the alert, with the body of the email being the description.

Here is an example of an alert:

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
prometheus: longhorn
role: alert-rules
name: prometheus-longhorn-rules
namespace: monitoring
spec:
groups:
- name: longhorn.rules
rules:
- alert: LonghornVolumeActualSpaceUsedWarning
annotations:
description: The actual space used by Longhorn volume {{$labels.volume}} on {{$labels.node}} is at {{$value}}% capacity for
more than 5 minutes.
summary: The actual used space of Longhorn volume is over 90% of the capacity.
expr: (longhorn_volume_actual_size_bytes / longhorn_volume_capacity_bytes) * 100 > 90
for: 5m
labels:
issue: The actual used space of Longhorn volume {{$labels.volume}} on {{$labels.node}} is high.
severity: warning

It is important to note that Prometheus will only find rules located in the same namespace as the server by default. This can cause issues, as application teams might need access to the namespace to add/edit/remove their alerts. To work around this issue, you’ll need to add the following settings in your values.yaml file.

Here is an example of values.yaml:

prometheusSpec:
podMonitorNamespaceSelector: {}
serviceMonitorNamespaceSelector: {}
ruleNamespaceSelector: {}

At this point, we have Prometheus up and running. It’s collecting all the data about our cluster, but the built-in UI doesn’t give you a way to visualize this data in a useful way. In the next section, we’ll be diving into Grafana to bring dashboards to our data.

At this point, we should have Prometheus and Grafana up and running, with the server collecting all the data about our cluster. Still, you can’t see most of the data unless you use Rancher’s monitoring charts, including some prebuilt dashboards that are mostly related to the cluster and its core services such as etcd, kube-apiserver, and CoreDNS. But, of course, a question comes up: How do I create my own?

The most straightforward answer is to find a premade dashboard and let someone else do all the hard work for you. The Grafana Labs dashboard repository is the most extensive resource, located at https://grafana.com/grafana/dashboards/. Their search tool lets you filter results by applications, data sources, and so on. But the coolest part is their dashboard ID system. All dashboards on the official site have an ID number – for example, the NGINX Ingress controller dashboard has an ID of 9614, and all you need to do to use this dashboard is copy that ID number and go to the Grafana UI. Browse to Dashboards | Manage | Import. Note that you might need to log into Grafana using the default login, which is admin/prom-operator. Then, paste the ID number in, and you’re done.

Of course, Rancher monitoring provides some example dashboards bundled into the rancher-monitoring chart. You can find the raw JSON files at https://github.com/rancher/system-charts/tree/dev-v2.6/charts/rancher-monitoring/v0.3.1/charts/grafana/dashboards. Additionally, you can add baseboards by ID too, with some of the most important ones being the component and etcd dashboards, which can be used to provide a great deal of insight into cluster performance issues.

But let’s say the application you are deploying is a community-created application that doesn’t have a dashboard on the official site. Most repositories will have the dashboard defined as a JSON file. You’ll copy and paste it into the UI and import it with this file. But let’s say you are deploying a custom in-house application and want to start from zero. I recommend watching the Getting started with Grafana dashboard design video at https://grafana.com/go/webinar/guide-to-dashboard-design/. I use a lot of community-created dashboards and tune them to my needs. You can click the share button at the top and export a dashboard as a JSON file, at which point you can copy and paste the parts you like. It is also imperative that you save your work when making changes to your dashboard by clicking the save icon at the top-right corner. If you close the page without clicking that icon, all your changes will be lost.

So far in this chapter, we have been talking about monitoring and alerting. In the next section, we will shift gears and focus on the other half of the equation, logging.

What is the Banzai Cloud Logging operator?

Section titled “What is the Banzai Cloud Logging operator?”

Along with migrating to monitoring v2 in Rancher 2.5, Rancher also migrated to logging v2 for mostly the same reasons. In v1, logging is built on Fluentd and uses plugins to ship logs to different logging services such as Elasticsearch, Splunk, Kafka, and Syslog. With v1, the Rancher server was in complete control of the logging deployments, which made customizing and tuning the logging solution complicated. Most of the settings were hard coded inside Rancher. This is where Banzai’s Logging operator comes into the picture.

The Logging operator uses the CRD model, just like the Prometheus operator, wherein you’ll define your Fluent Bit deployment and its setting via a CRD. The operator takes over pushing out your changes. Because everything is a CRD, including your settings, you can let your application teams define their logging settings. For example, one team might want their logs sent to a cloud log service such as Splunk, while another team might have the legal requirement for everything to stay running on RKE or another K8s cluster hosted on-premises, and you can do this because of the Logging operator. The idea is that you have logging flows that are a set of application pods going to an output, which can be any number of logging servers/services.

When talking about logging, two of the questions are, what is Fluent Bit? and what is Fluentd?

Before diving into Fluent Bit, let’s talk about Fluentd, which came first. Fluentd is an open source project written in Ruby by the Treasure Data team back in 2011. Its core idea was that all logs should be JSON objects. With Fluentd and Docker, the basic process used to collect logs from the containers is to use the default Docker log driver that writes the container logs to a file on the disk. Then, Fluentd will read the whole log file and bring forward the events onto the server, at which point Fluentd will open a tail file handler that will hold the log file open and read all writes to it.

It is important to note that Fluentd has a process for handling log rotation, so it is recommended to enable log rotation in Docker Engine. The following is an example configuration. You can find the complete documentation at https://docs.docker.com/config/containers/logging/configure/. Docker will wait until the logs file is 100 MB before rotating the file in the following example. This is done to prevent the loss of events for applications that create large amounts of events. Fluentd needs to read all the events and forward them to the log server before the rotation.

In the following example, we are defining the log options for the json-file log driver, which is built into Docker Engine by default.

Here is an example at /etc/docker/daemon.json:

{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
}
}

In this case, we are allowing the log file for all containers to reach a maximum size of 100 MB before rotating the file. Then, we’ll only be keeping the last three rotated files.

Now, let’s go over Fluent Bit. Basically, Fluentd was designed to be a simple tool that is fast and lightweight. Fluent Bit is built on top of Fluentd and is intended to provide the additional filtering and routing that Banzai logging needs. You can find more about the differences at https://docs.fluentbit.io/manual/about/fluentd-and-fluent-bit.

With Rancher logging, it is recommended to deploy via the Apps and Marketplace in the Rancher UI by going to Cluster Tools and clicking Logging app. This will deploy the operator in the cattle-logging-system namespace. It is important to note that you’ll see two applications, rancher-logging and rancher-logging-crd, in the Installed section after the installation is complete. Also, depending on the size of the cluster, it might take 5 to 15 minutes for all the pods to start up and go into the ready state. Once Rancher logging is installed on the cluster, we will be able to configure filtering and log flows, which we’ll cover in the next two sections.

Because logging v2 is a Helm chart, you can choose to deploy it directly via the helm command, which can be very helpful when managing clusters at scale, using tools such as Rancher’s Fleet. The following is an example command:

helm repo add rancher-charts https://git.rancher.io/charts
helm repo update
helm upgrade –install -n cattle-logging-system rancher-logging rancher-charts/rancher-logging

You can find the full commands and values.yaml to install Rancher monitoring via the helm command at https://github.com/PacktPublishing/Rancher-Deep-Dive/tree/main/ch10/rancher-logging-v2.

The first setting that most people configure with Rancher logging is ClusterFlows and ClusterOutput. ClusterFlow is designed to be scoped to all namespaces and can set the default logging policy for the cluster as a whole. To configure this setting, you’ll go to the Rancher UI and browse to Logging and then ClusterFlows. From there, you’ll fill out the form. Then, once that is done, you’ll want to define ClusterOutput, where you define the target location for your logs, ElasticSearch, Splunk, Syslog, and so on. For an example of each of the different logging providers, please see Rancher’s official documentation at https://rancher.com/docs/rancher/v2.5/en/logging/custom-resource-config/outputs/.

Once you have ClusterFlows and ClusterOutput configured, you can call it done. But if you want to customize the logging for an application, you need to repeat the process. Still, this time, you’ll be configuring the Flows and Outputs, with the main difference being setting the selector rules or what the documentation calls matches, which are a set of include or exclude labels that you can use to limit the scope of the Flows and Outputs. The following is an example YAML for a NGINX application in the default namespace. It is important to note that Flows are namespace-scoped:

Here is a Flow example:

apiVersion: logging.banzaicloud.io/v1beta1
kind: Flow
metadata:
name: flow-sample
namespace: default
spec:
filters:
- parser:
remove_key_name_field: true
parse:
type: nginx
- tag_normaliser:
format: ${namespace_name}.${pod_name}.${container_name}
localOutputRefs:
- s3-output
match:
- select:
labels:
app: nginx

At this point, we should have logging configured on our cluster and be forwarding the logs to a log server. In the next section, we’ll cover a more advanced setup that a number of users use in their environments to log to multiple servers.

Because you can define as many outputs as you would like, suppose you wanted to send logs to multiple log servers, such as sending to a local Syslog and Splunk server. It is imperative to note that this will duplicate your logs, so this is not recommended as an High Availability (HA) solution – that is, sending to a production log server or a Disaster Recovery (DR) log server. It’s also vital that you do not dispatch logs to an offline server. Fluent Bit does cache failed to send logs, so you can run into memory pressure issues if you have a misconfigured log server or the log server is offline for long periods.

You can find YAML examples in Rancher’s official documentation, located at https://rancher.com/docs/rancher/v2.6/en/logging/custom-resource-config/outputs/. It’s important to note that, as of Rancher 2.6.3, the logging settings in the Rancher UI are still buggy (for example, https://github.com/rancher/rancher/issues/36516, where the ClusterOutput field is failing to update in the UI), so it’s recommended to use YAML files as much as possible.

In this chapter, we learned about Rancher monitoring and logging. This includes how Prometheus, Grafana, Fluentd, and Fluent Bit work. We learned how to install Rancher monitoring and logging. We finally went into detail about some example dashboards. We ended the chapter by talking about customizing application logging and its flows.

The next chapter will cover Rancher’s storage project to provide storage to Kubernetes clusters.