github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/docs/sources/clients/aws/eks/_index.md (about)

     1  ---
     2  title: EKS
     3  ---
     4  # Sending logs from EKS with Promtail
     5  
     6  In this tutorial we'll see how to set up Promtail on [EKS][eks]. Amazon Elastic Kubernetes Service (Amazon [EKS][eks]) is a fully managed Kubernetes service, using Promtail we'll get full visibility into our cluster logs. We'll start by forwarding pods logs then nodes services and finally Kubernetes events.
     7  
     8  After this tutorial you will able to query all your logs in one place using Grafana.
     9  
    10  <!-- TOC -->
    11  
    12  - [Sending logs from EKS with Promtail](#sending-logs-from-eks-with-promtail)
    13      - [Requirements](#requirements)
    14      - [Setting up the cluster](#setting-up-the-cluster)
    15      - [Adding Promtail DaemonSet](#adding-promtail-daemonset)
    16      - [Fetching kubelet logs with systemd](#fetching-kubelet-logs-with-systemd)
    17      - [Adding Kubernetes events](#adding-kubernetes-events)
    18      - [Conclusion](#conclusion)
    19  
    20  <!-- /TOC -->
    21  
    22  ## Requirements
    23  
    24  Before we start you'll need:
    25  
    26  - The [AWS CLI][aws cli] configured (run `aws configure`).
    27  - [kubectl][kubectl] and [eksctl][eksctl] installed.
    28  - A Grafana instance with a Grafana Loki data source already configured, you can use [GrafanaCloud][GrafanaCloud] free trial.
    29  
    30  For the sake of simplicity we'll use a [GrafanaCloud][GrafanaCloud] Loki and Grafana instances, you can get an free account for this tutorial on our [website][GrafanaCloud], but all the steps are the same if you're running your own Open Source version of Loki and Grafana instances.
    31  
    32  ## Setting up the cluster
    33  
    34  In this tutorial we'll use [eksctl][eksctl], a simple command line utility for creating and managing Kubernetes clusters on Amazon EKS. AWS requires creating many resources such as IAM roles, security groups and networks, by using `eksctl` all of this is simplified.
    35  
    36  > We're not going to use a Fargate cluster. Do note that if you want to use Fargate daemonset are not allowed, the only way to ship logs with EKS Fargate is to run a fluentd or fluentbit or Promtail as a sidecar and tee your logs into a file. For more information on how to do so, you can read this [blog post][blog ship log with fargate].
    37  
    38  
    39  ```bash
    40  eksctl create cluster --name loki-promtail --managed
    41  ```
    42  
    43  You have time for a coffee ☕, this usually take 15minutes. When this is finished you should have `kubectl context` configured to communicate with your newly created cluster. Let's verify everything is fine:
    44  
    45  ```bash
    46  kubectl version
    47  
    48  Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-07-04T15:01:15Z", GoVersion:"go1.14.4", Compiler:"gc", Platform:"darwin/amd64"}
    49  Server Version: version.Info{Major:"1", Minor:"16+", GitVersion:"v1.16.8-eks-fd1ea7", GitCommit:"fd1ea7c64d0e3ccbf04b124431c659f65330562a", GitTreeState:"clean", BuildDate:"2020-05-28T19:06:00Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}
    50  ```
    51  
    52  ## Adding Promtail DaemonSet
    53  
    54  To ship all your pods logs we're going to set up [Promtail](../../promtail/) as a DaemonSet in our cluster. This means it will run on each nodes of the cluster, we will then configure it to find the logs of your containers on the host.
    55  
    56  What's nice about Promtail is that it uses the same [service discovery as Prometheus][prometheus conf], you should make sure the `scrape_configs` of Promtail matches the Prometheus one. Not only this is simpler to configure, but this also means Metrics and Logs will have the same metadata (labels) attached by the Prometheus service discovery. When querying Grafana you will be able to correlate metrics and logs very quickly, you can read more about this on our [blogpost][correlate].
    57  
    58  Let's add the Loki repository and list all available charts.
    59  
    60  ```bash
    61  helm repo add loki https://grafana.github.io/loki/charts
    62  "loki" has been added to your repositories
    63  
    64  helm search repo
    65  
    66  NAME                   CHART VERSION   APP VERSION     DESCRIPTION
    67  loki/fluent-bit 0.3.0           v1.6.0          Uses fluent-bit Loki go plugin for gathering lo...
    68  loki/loki       0.31.0          v1.6.0          Loki: like Prometheus, but for logs.
    69  loki/loki-stack 0.40.0          v1.6.0          Loki: like Prometheus, but for logs.
    70  loki/promtail   0.24.0          v1.6.0          Responsible for gathering logs and sending them...
    71  ```
    72  
    73  If you want to install Loki, Grafana, Prometheus and Promtail all together you can use the `loki-stack` chart, for now we'll focus on Promtail. Let's create a new helm value file, we'll fetch the [default][default value file] one and work from there:
    74  
    75  ```bash
    76  curl https://raw.githubusercontent.com/grafana/helm-charts/main/charts/promtail/values.yaml > values.yaml
    77  ```
    78  
    79  First we're going to tell Promtail to send logs to our Loki instance, the example below shows how to send logs to [GrafanaCloud][GrafanaCloud], replace your credentials. The default value will send to your own Loki and Grafana instance if you're using the `loki-chart` repository.
    80  
    81  ```yaml
    82  loki:
    83    serviceName: "logs-prod-us-central1.grafana.net"
    84    servicePort: 443
    85    serviceScheme: https
    86    user: <userid>
    87    password: <grafancloud apikey>
    88  ```
    89  
    90  Once you're ready let's create a new namespace monitoring and add Promtail to it:
    91  
    92  ```bash
    93  kubectl create namespace monitoring
    94  namespace/monitoring created
    95  
    96  helm install promtail --namespace monitoring loki/promtail -f values.yaml
    97  
    98  NAME: promtail
    99  LAST DEPLOYED: Fri Jul 10 14:41:37 2020
   100  NAMESPACE: default
   101  STATUS: deployed
   102  REVISION: 1
   103  TEST SUITE: None
   104  NOTES:
   105  Verify the application is working by running these commands:
   106    kubectl --namespace default port-forward daemonset/promtail 3101
   107    curl http://127.0.0.1:3101/metrics
   108  ```
   109  
   110  Verify that Promtail pods are running. You should see only two since we're running a two nodes cluster.
   111  
   112  ```bash
   113  kubectl get -n monitoring pods
   114  
   115  NAME             READY   STATUS    RESTARTS   AGE
   116  promtail-87t62   1/1     Running   0          35s
   117  promtail-8c2r4   1/1     Running   0          35s
   118  ```
   119  
   120  You can reach your Grafana instance and start exploring your logs. For example if you want to see all logs in the `monitoring` namespace use `{namespace="monitoring"}`, you can also expand a single log line to discover all labels available from the Kubernetes service discovery.
   121  
   122  ![grafana logs namespace][grafana logs namespace]
   123  
   124  ## Fetching kubelet logs with systemd
   125  
   126  So far we're scrapings logs from containers, but if you want to get more visibility you could also scrape systemd logs from each of your machine. This means you can also get access to `kubelet` logs.
   127  
   128  Let's edit our values file again and `extraScrapeConfigs` to add the systemd job:
   129  
   130  ```yaml
   131  extraScrapeConfigs:
   132    - job_name: journal
   133      journal:
   134        path: /var/log/journal
   135        max_age: 12h
   136        labels:
   137          job: systemd-journal
   138      relabel_configs:
   139        - source_labels: ['__journal__systemd_unit']
   140          target_label: 'unit'
   141        - source_labels: ['__journal__hostname']
   142          target_label: 'hostname'
   143  ```
   144  
   145  > Feel free to change the [relabel_configs][relabel_configs] to match what you would use in your own environnement.
   146  
   147  Now we need to add a volume for accessing systemd logs:
   148  
   149  ```yaml
   150  extraVolumes:
   151    - name: journal
   152      hostPath:
   153        path: /var/log/journal
   154  ```
   155  
   156  And add a new volume mount in Promtail:
   157  
   158  ```yaml
   159  extraVolumeMounts:
   160    - name: journal
   161      mountPath: /var/log/journal
   162      readOnly: true
   163  ```
   164  
   165  Now that we're ready we can update the Promtail deployment:
   166  
   167  ```bash
   168  helm upgrade  promtail loki/promtail -n monitoring -f values.yaml
   169  ```
   170  
   171  Let go back to Grafana and type in the query below to fetch all logs related to Volume from Kubelet:
   172  
   173  ```logql
   174  {unit="kubelet.service"} |= "Volume"
   175  ```
   176  
   177  Filter expressions are powerful in LogQL they help you scan through your logs, in this case it will filter out all your [kubelet][kubelet] logs not having the `Volume` word in it.
   178  
   179  The workflow is simple, you always select a set of labels matchers first, this way you reduce the data you're planing to scan.(such as an application, a namespace or even a cluster).
   180  Then you can apply a set of filters to find the logs you want.
   181  
   182  Promtail also supports syslog.
   183  
   184  ## Adding Kubernetes events
   185  
   186  Kubernetes Events (`kubectl get events -n monitoring`)  are a great way to debug and troubleshoot your kubernetes cluster. Events contains information such as Node reboot, OOMKiller and Pod failures.
   187  
   188  We'll deploy a the `eventrouter` application created by [Heptio][eventrouter] which logs those events to `stdout`.
   189  
   190  But first we need to configure Promtail, we want to parse the namespace to add it as a label from the content, this way we can quickly access events by namespace.
   191  
   192  Let's update our `pipelineStages` to parse logs from the `eventrouter`:
   193  
   194  ```yaml
   195  pipelineStages:
   196  - docker:
   197  - match:
   198      selector: '{app="eventrouter"}'
   199      stages:
   200      - json:
   201          expressions:
   202            namespace: event.metadata.namespace
   203      - labels:
   204          namespace: ""
   205  ```
   206  
   207  > Pipeline stages are great ways to parse log content and create labels (which are [indexed][labels post]), if you want to configure more of them, check out the [pipeline][pipeline] documentation.
   208  
   209  Now update Promtail again:
   210  
   211  ```bash
   212  helm upgrade  promtail loki/promtail -n monitoring -f values.yaml
   213  ```
   214  
   215  And deploy the `eventrouter` using:
   216  
   217  ```bash
   218   kubectl create -f https://raw.githubusercontent.com/grafana/loki/master/docs/sources/clients/aws/eks/eventrouter.yaml
   219  
   220  serviceaccount/eventrouter created
   221  clusterrole.rbac.authorization.k8s.io/eventrouter created
   222  clusterrolebinding.rbac.authorization.k8s.io/eventrouter created
   223  configmap/eventrouter-cm created
   224  deployment.apps/eventrouter created
   225  ```
   226  
   227  Let's go in Grafana [Explore][explore] and query events for our new `monitoring` namespace using `{app="eventrouter",namespace="monitoring"}`.
   228  
   229  For more information about the `eventrouter` make sure to read our [blog post][blog events] from Goutham.
   230  
   231  ## Conclusion
   232  
   233  That's it ! You can download the final and complete [`values.yaml`][final config] if you need.
   234  
   235  Your EKS cluster is now ready, all your current and future application logs will now be shipped to Loki with Promtail. You will also able to [explore][explore] [kubelet][kubelet] and Kubernetes events. Since we've used a DaemonSet you'll automatically grab all your node logs as you scale them.
   236  
   237  If you want to push this further you can check out [Joe's blog post][blog annotations] on how to automatically create Grafana dashboard annotations with Loki when you deploy new Kubernetes applications.
   238  
   239  > If you need to delete the cluster simply run `eksctl delete  cluster --name loki-promtail`
   240  
   241  [eks]: https://aws.amazon.com/eks/
   242  [aws cli]: https://aws.amazon.com/cli/
   243  [GrafanaCloud]: https://grafana.com/signup/
   244  [blog ship log with fargate]: https://aws.amazon.com/blogs/containers/how-to-capture-application-logs-when-using-amazon-eks-on-aws-fargate/
   245  [correlate]: https://grafana.com/blog/2020/03/31/how-to-successfully-correlate-metrics-logs-and-traces-in-grafana/
   246  [default value file]: https://github.com/grafana/helm-charts/blob/main/charts/promtail/values.yaml
   247  [grafana logs namespace]: namespace-grafana.png
   248  [relabel_configs]:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
   249  [syslog]: ../../../installation/helm#run-promtail-with-syslog-support
   250  [kubelet]: https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/#:~:text=The%20kubelet%20works%20in%20terms,PodSpecs%20are%20running%20and%20healthy.
   251  [blog events]: https://grafana.com/blog/2019/08/21/how-grafana-labs-effectively-pairs-loki-and-kubernetes-events/
   252  [labels post]: https://grafana.com/blog/2020/04/21/how-labels-in-loki-can-make-log-queries-faster-and-easier/
   253  [pipeline]: https://grafana.com/docs/loki/latest/clients/promtail/pipelines/
   254  [final config]: values.yaml
   255  [blog annotations]: https://grafana.com/blog/2019/12/09/how-to-do-automatic-annotations-with-grafana-and-loki/
   256  [kubectl]: https://kubernetes.io/docs/tasks/tools/install-kubectl/
   257  [eksctl]: https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html
   258  [Promtail]: ./../../promtail/README
   259  [prometheus conf]: https://prometheus.io/docs/prometheus/latest/configuration/configuration/
   260  [eventrouter]: https://github.com/heptiolabs/eventrouter
   261  [explore]: https://grafana.com/docs/grafana/latest/features/explore/