Deploy Promtail as a Sidecar to you Main App.
Tags: devops, kubernetes, observabillity, grafana
Published: 2022-11-12 11:48:27

Hello, in this tutorial the goal is to describe the steps needed to deploy Promtail as a Sidecar container to your app in order to ship only the logs you will need to the Log Management System in our case we will use [Grafana Loki](https://grafana.com/oss/loki/).

Before we start, I would like to explain to you the reasoning behind the use of the two Kubernetes Objects a [Configmap](https://t-velmachos.notion.site/1df9773802444753973d8c15d4047a61) and [emptyDir Volume](https://kubernetes.io/docs/concepts/storage/volumes/). So we will use a emptyDir Volume create a shared temporary space between Containers in which the app logs will reside, also we will use a Configmap to store the necessary configuration used by Promtail in order to know which files need to monitor and where we want to ship the Logs in our case the Loki Url.

So, lets Dive in…

Step1. Create the ConfigMap to store the configuration (promtail.yaml) for Promtail.

```
apiVersion: v1
kind: ConfigMap
metadata:
  name: promtail-sidecar-config-map
data:
  promtail.yaml: |
      server:
        http_listen_port: 9080
        grpc_listen_port: 0
        log_level: "debug"
      positions:
        filename: /tmp/positions.yaml
      clients: # Specify target
        - url: http://loki.monitoring.svc.cluster.local:3100/loki/api/v1/push
      scrape_configs:
        - job_name:  "<app-name>" 
          static_configs: 
            - targets: 
                - localhost 
              labels:
                app: "storage-service"
                environment: "<environment-name>" 
                __path__: /app/logs/*.log # Any file .log in the EmptyDir Volume.

```
Step2. Make the necessary changes in the Deployment Manifest.

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: <app-service>
  labels:
    app: <app-service>
spec:
  replicas: 1
  selector:
    matchLabels:
      app: <app-service>
  template:
    metadata:
      labels:
        app: <app-service>
    spec:
      containers:
        - name: <app-service>
          image: <your-name>/<app-service>
          imagePullPolicy: Always
          ports:
            - containerPort: <app-port>
          readinessProbe:
            exec:
              command: ["<your health-check>"]
            initialDelaySeconds: 5
          livenessProbe:
            exec:
              command: ["<your health-check>"]
            initialDelaySeconds: 10
          env:
            - name: <ENV-VAR-1>
              valueFrom:
                configMapKeyRef:
                  name: <app-service>-config-map
                  key: appName
            - name:  <ENV-VAR-2>
              valueFrom:
                secretKeyRef:
                  name: <app-service>-secret
                  key: <secret-key>
          volumeMounts:
           - name: shared-logs # shared space monitored with Promtail
             mountPath: /app/logs
        # Sidecar Container Promtail
        - name: promtail
          image: grafana/promtail:master
          args: 
            - "-config.file=/etc/promtail/promtail.yaml" # Found in the ConfigMap
          volumeMounts:
            - name: config
              mountPath: /etc/promtail
            - name: shared-logs # shared space
              mountPath: /app/logs
      imagePullSecrets:
        - name: <registry-secret> # if needed
      volumes:
         - name: config
           configMap:
            name: promtail-sidecar-config-map
         - name: shared-logs  # shared space monitored with Promtail
           emptyDir: 
	      sizeLimit: 500Mi
```


I hope you like the tutorial, if you do give a thumps up! and follow me in [Twitter](https://twitter.com/TVelmachos), also you can subscribe to my [Newsletter](https://dashboard.mailerlite.com/forms/167581/67759331736553243/share) in order to avoid missing any of the upcoming tutorials.


#### Media Attribution
I would like to thank [Clark Tibbs](https://unsplash.com/@clarktibbs) for designing the awesome [photo ](https://unsplash.com/photos/oqStl2L5oxI)I am using in my posts.