{"id":1521,"date":"2019-03-22T23:40:06","date_gmt":"2019-03-22T23:40:06","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=1521"},"modified":"2019-04-06T01:17:57","modified_gmt":"2019-04-06T01:17:57","slug":"grafana-logging-using-loki","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/03\/22\/grafana-logging-using-loki\/","title":{"rendered":"Grafana Logging using Loki"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/blog.giantswarm.io\/assets\/2019\/03\/grafana-logging-using-loki.jpg\" alt=\"Grafana Logging using Loki\" \/><\/p>\n<p><a href=\"https:\/\/grafana.com\/loki\">Loki<\/a> is a Prometheus-inspired logging service for cloud native infrastructure.<\/p>\n<h2><strong>What is Loki?<\/strong><\/h2>\n<p>Open sourced by Grafana Labs during KubeCon Seattle 2018, Loki is a logging backend optimized for users running Prometheus and Kubernetes with great logs search and visualization in Grafana 6.0.<\/p>\n<p>Loki was built for efficiency alongside the following goals:<\/p>\n<ul>\n<li>Logs should be cheap. Nobody should be asked to log less.<\/li>\n<li>Easy to operate and scale.<\/li>\n<li>Metrics, logs (and traces later) need to work together.<\/li>\n<\/ul>\n<h2><strong>Loki vs other logging solutions<\/strong><\/h2>\n<p>As said, Loki is designed for efficiency to work well in the Kubernetes context in combination with Prometheus metrics.<\/p>\n<p>The idea is to switch easily between metrics and logs based on Kubernetes labels you already use with Prometheus.<\/p>\n<p>Unlike most logging solutions, Loki does not parse incoming logs or do full-text indexing.<\/p>\n<p>Instead, it indexes and groups log streams using the same labels you\u2019re already using with Prometheus. This makes it significantly more efficient to scale and operate.<\/p>\n<h2><strong>Loki components<\/strong><\/h2>\n<p>Loki is a TSDB (Time-series database), it stores logs as split and gzipped chunks of data.<\/p>\n<p>The logs are ingested via the API and an agent, called Promtail (Tailing logs in Prometheus format), will scrape Kubernetes logs and add label metadata before sending it to Loki.<\/p>\n<p>This metadata addition is exactly the same as Prometheus, so you will end up with the exact same labels for your resources.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/grafana.com\/blog\/assets\/img\/blog\/image1.png\" alt=\"\" \/><\/p>\n<p><a href=\"https:\/\/grafana.com\/blog\/2018\/12\/12\/loki-prometheus-inspired-open-source-logging-for-cloud-natives\/\">https:\/\/grafana.com\/blog\/2018\/12\/12\/loki-prometheus-inspired-open-source-logging-for-cloud-natives\/<\/a><\/p>\n<h2><strong>How to deploy Loki on your Kubernetes cluster<\/strong><\/h2>\n<ol>\n<li>Deploy Loki on your cluster<\/li>\n<\/ol>\n<p>The easiest way to deploy Loki on your Kubernetes cluster is by using the <a href=\"https:\/\/github.com\/grafana\/loki\/tree\/master\/production\/helm\">Helm chart<\/a> available in the official repository.<\/p>\n<p>You can follow the <a href=\"https:\/\/github.com\/grafana\/loki\/blob\/master\/production\/helm\/README.md\">setup guide<\/a> from the official repo.<\/p>\n<p>This will deploy Loki and Promtail.<\/p>\n<ol>\n<li>Add Loki datasource in Grafana (built-in support for Loki is in 6.0 and newer releases)\n<ol>\n<li>Log into your Grafana.<\/li>\n<li>Go to Configuration &gt; Data Sources via the cog icon in the left sidebar.<\/li>\n<li>Click the big + Add data source button.<\/li>\n<li>Choose Loki from the list.<\/li>\n<li>The http URL field should be the address of your Loki server: http:\/\/loki:3100<\/li>\n<\/ol>\n<\/li>\n<li>See your logs in the \u201cExplore\u201d view\n<ol>\n<li>Select the \u201cExplore\u201d view on the sidebar.<\/li>\n<li>Select the Loki data source.<\/li>\n<li>Choose a log stream using the \u201cLog labels\u201d button.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/grafana.com\/blog\/assets\/img\/blog\/loki\/loki_grafana_ui.png\" alt=\"\" \/><\/p>\n<h2><strong>Promtail configuration<\/strong><\/h2>\n<blockquote><p>Promtail is the metadata appender and log sending agent<\/p><\/blockquote>\n<p>The Promtail configuration you get from the Helm chart is already configured to get all the logs from your Kubernetes cluster and append labels on it as Prometheus does for metrics.<\/p>\n<p>However, you can tune the configuration for your needs.<\/p>\n<p>Here are two examples:<\/p>\n<ol>\n<li>Get logs only for specific namespace<\/li>\n<\/ol>\n<p>You can use the action: keep for your namespace and add a new relabel_configs for each scrape_config in <a href=\"https:\/\/github.com\/grafana\/loki\/blob\/master\/production\/helm\/templates\/promtail\/configmap.yaml\">promtail\/configmap.yaml<\/a><\/p>\n<p>For example, if you want to get logs only for the kube-system namespace:<\/p>\n<p>scrape_configs:<br \/>\n&#8211; job_name: kubernetes-pods<br \/>\nkubernetes_sd_configs:<br \/>\n&#8211; role: pod<br \/>\nrelabel_configs:<br \/>\n&#8211; source_labels: [__meta_kubernetes_namespace]<br \/>\naction: keep<br \/>\nregex: kube-system<\/p>\n<p># [&#8230;]<\/p>\n<p>&#8211; job_name: kubernetes-pods-app<br \/>\nkubernetes_sd_configs:<br \/>\n&#8211; role: pod<br \/>\nrelabel_configs:<br \/>\n&#8211; source_labels: [__meta_kubernetes_namespace]<br \/>\naction: keep<br \/>\nregex: kube-system<\/p>\n<ol>\n<li>Exclude logs from specific namespace<\/li>\n<\/ol>\n<p>For example, if you want to exclude logs from kube-system namespace:<\/p>\n<p>You can use the action: drop for your namespace and add a new relabel_configs for each scrape_config in <a href=\"https:\/\/github.com\/grafana\/loki\/blob\/master\/production\/helm\/templates\/promtail\/configmap.yaml\">promtail\/configmap.yaml<\/a><\/p>\n<p>scrape_configs:<br \/>\n&#8211; job_name: kubernetes-pods<br \/>\nkubernetes_sd_configs:<br \/>\n&#8211; role: pod<br \/>\nrelabel_configs:<br \/>\n&#8211; source_labels: [__meta_kubernetes_namespace]<br \/>\naction: drop<br \/>\nregex: kube-system<\/p>\n<p># [&#8230;]<\/p>\n<p>&#8211; job_name: kubernetes-pods-app<br \/>\nkubernetes_sd_configs:<br \/>\n&#8211; role: pod<br \/>\nrelabel_configs:<br \/>\n&#8211; source_labels: [__meta_kubernetes_namespace]<br \/>\naction: drop<br \/>\nregex: kube-system<\/p>\n<p>For more info on the configuration, you can refer to the <a href=\"https:\/\/prometheus.io\/docs\/prometheus\/latest\/configuration\/configuration\/\">official Prometheus configuration documentation<\/a>.<\/p>\n<h2><strong>Use fluentd output plugin<\/strong><\/h2>\n<p>Fluentd is a well-known and good log forwarder that is also a [CNCF project] (https:\/\/www.cncf.io\/projects\/). It has a lot of input plugins and good filtering built-in. So, if you want to for example, forward journald logs to Loki, it\u2019s not possible via Promtail so you can use the fluentd syslog input plugin with the fluentd Loki output plugin to get those logs into Loki.<\/p>\n<p>You can refer to the <a href=\"https:\/\/github.com\/grafana\/loki\/tree\/master\/fluentd\/fluent-plugin-loki\">installation guide<\/a> on how to use the fluentd Loki plugin.<\/p>\n<p>There\u2019s also <a href=\"https:\/\/github.com\/giantswarm\/fluentd-loki\">an example<\/a>, of how to forward API server audit logs to Loki with fluentd.<\/p>\n<p>Here is the fluentd configuration:<\/p>\n<p>&lt;match fluent.**&gt;<br \/>\ntype null<br \/>\n&lt;\/match&gt;<br \/>\n&lt;source&gt;<br \/>\n@type tail<br \/>\npath \/var\/log\/apiserver\/audit.log<br \/>\npos_file \/var\/log\/fluentd-audit.log.pos<br \/>\ntime_format %Y-%m-%dT%H:%M:%S.%NZ<br \/>\ntag audit.*<br \/>\nformat json<br \/>\nread_from_head true<br \/>\n&lt;\/source&gt;<br \/>\n&lt;filter kubernetes.**&gt;<br \/>\ntype kubernetes_metadata<br \/>\n&lt;\/filter&gt;<br \/>\n&lt;match audit.**&gt;<br \/>\n@type loki<br \/>\nurl &#8220;#&#8221;<br \/>\nusername &#8220;#&#8221;<br \/>\npassword &#8220;#&#8221;<br \/>\nextra_labels {&#8220;env&#8221;:&#8221;dev&#8221;}<br \/>\nflush_interval 10s<br \/>\nflush_at_shutdown true<br \/>\nbuffer_chunk_limit 1m<br \/>\n&lt;\/match&gt;<\/p>\n<h2><strong>Promtail as a sidecar<\/strong><\/h2>\n<p>By default, Promtail is configured to automatically scrape logs from containers and send them to Loki. Those logs come from stdout.<\/p>\n<p>But sometimes, you may like to be able to send logs from an external file to Loki.<\/p>\n<p>In this case, you can set up Promtail as a sidecar, i.e. a second container in your pod, share the log file with it through a shared volume, and scrape the data to send it to Loki<\/p>\n<p>Assuming you have an application simple-logger. The application logs into \/home\/slog\/creator.log<\/p>\n<p>Your kubernetes deployment will look like this :<\/p>\n<ol>\n<li>Add Promtail as a sidecar\n<p>apiVersion: apps\/v1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\nname: my-app<br \/>\nspec:<br \/>\ntemplate:<br \/>\nmetadata:<br \/>\nname: my-app<br \/>\nspec:<br \/>\ncontainers:<br \/>\n&#8211; name: simple-logger<br \/>\nimage: giantswarm\/simple-logger:latest<\/p>\n<p>apiVersion: apps\/v1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\nname: my-app<br \/>\nspec:<br \/>\ntemplate:<br \/>\nmetadata:<br \/>\nname: my-app<br \/>\nspec:<br \/>\ncontainers:<br \/>\n&#8211; name: simple-logger<br \/>\nimage: giantswarm\/simple-logger:latest<br \/>\n&#8211; name: promtail<br \/>\nimage: grafana\/promtail:master<br \/>\nargs:<br \/>\n&#8211; &#8220;-config.file=\/etc\/promtail\/promtail.yaml&#8221;<br \/>\n&#8211; &#8220;-client.url=http:\/\/loki:3100\/api\/prom\/push&#8221;<\/li>\n<li>Use a shared data volume containing the log file\n<p>apiVersion: apps\/v1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\nname: my-app<br \/>\nspec:<br \/>\ntemplate:<br \/>\nmetadata:<br \/>\nname: my-app<br \/>\nspec:<br \/>\ncontainers:<br \/>\n&#8211; name: simple-logger<br \/>\nimage: giantswarm\/simple-logger:latest<br \/>\nvolumeMounts:<br \/>\n&#8211; name: shared-data<br \/>\nmountPath: \/home\/slog<br \/>\n&#8211; name: promtail<br \/>\nimage: grafana\/promtail:master<br \/>\nargs:<br \/>\n&#8211; &#8220;-config.file=\/etc\/promtail\/promtail.yaml&#8221;<br \/>\n&#8211; &#8220;-client.url=http:\/\/loki:3100\/api\/prom\/push&#8221;<br \/>\nvolumeMounts:<br \/>\n&#8211; name: shared-data<br \/>\nmountPath: \/home\/slog<br \/>\nvolumes:<br \/>\n&#8211; name: shared-data<br \/>\nemptyDir: {}<\/li>\n<li>Configure Promtail to read your log file<\/li>\n<\/ol>\n<p>As Promtail uses the same config as Prometheus, you can use the scrape_config type static_configs to read the file you want.<\/p>\n<p>scrape_configs:<br \/>\n&#8211; job_name: system<br \/>\nentry_parser: raw<br \/>\nstatic_configs:<br \/>\n&#8211; targets:<br \/>\n&#8211; localhost<br \/>\nlabels:<br \/>\njob: my-app<br \/>\nmy-label: awesome<br \/>\n__path__: \/home\/slog\/creator.log<\/p>\n<p>And you\u2019re done.<\/p>\n<p>A running example can be found <a href=\"https:\/\/github.com\/giantswarm\/simple-logger\">here<\/a><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>So Loki looks very promising. The footprint is very low. It integrates nicely with Grafana and Prometheus. Having the same labels as in Prometheus is very helpful to map incidents together and quickly find logs related to metrics. Another big point is the simple scalability, Loki is horizontally scalable by design.<\/p>\n<p>As Loki is currently alpha software, install it and play with it. Then, join us on grafana.slack.com and add your feedback to make it better.<\/p>\n<p>Interested in finding out how Giant Swarm handles the entire cloud native stack including Loki? Request your free trial of the <a href=\"https:\/\/giantswarm.io\">Giant Swarm Infrastructure here<\/a>.<\/p>\n<p><a href=\"https:\/\/blog.giantswarm.io\/grafana-logging-using-loki\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Loki is a Prometheus-inspired logging service for cloud native infrastructure. What is Loki? Open sourced by Grafana Labs during KubeCon Seattle 2018, Loki is a logging backend optimized for users running Prometheus and Kubernetes with great logs search and visualization in Grafana 6.0. Loki was built for efficiency alongside the following goals: Logs should be &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2019\/03\/22\/grafana-logging-using-loki\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Grafana Logging using Loki&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1521","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=1521"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1521\/revisions"}],"predecessor-version":[{"id":1584,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/1521\/revisions\/1584"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=1521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=1521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=1521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}