DEV Community

Cover image for Deploy Promtail as a Sidecar to you Main App.
Thodoris Velmachos
Thodoris Velmachos

Posted on

Deploy Promtail as a Sidecar to you Main App.

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.

Before we start, I would like to explain to you the reasoning behind the use of the two Kubernetes Objects a Configmap and emptyDir Volume. 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.

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

I hope you like the tutorial, if you do give a thumps up! and follow me in Twitter, also you can subscribe to my Newsletter in order to avoid missing any of the upcoming tutorials.

Media Attribution

I would like to thank Clark Tibbs for designing the awesome photo I am using in my posts.

Top comments (0)