Bboysoul's Blog

首页 公告 RSS

使用promtail去收集容器内部的日志

December 30, 2022 本文有 376 个字 需要花费 1 分钟阅读

简介

如果直接使用promtail去收集应用标准输出的日志的确很不错,但是有的日志是打在容器内部的,所以就要把promtail部署成sidecar的形式去部署

操作

首先两个容器之间要有一个共享的volume

      - name: shared-logs
        emptyDir: 

接着要定义promtail相关的东西

      - name: promtail
        image: grafana/promtail:2.4.1
        args: 
          - "-config.file=/etc/promtail/promtail.yaml" 
        volumeMounts:
          - name: config-promtail
            mountPath: /etc/promtail
          - name: shared-logs 
            mountPath: /app/logs

下面是示例Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: app
    app.kubernetes.io/instance: app
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      imagePullSecrets:
      - name: regcred
      containers:

      - name: promtail
        image: grafana/promtail:2.4.1
        args: 
          - "-config.file=/etc/promtail/promtail.yaml" 
        volumeMounts:
          - name: config-promtail
            mountPath: /etc/promtail
          - name: shared-logs 
            mountPath: /app/logs

      - image: app:20220516-4
        imagePullPolicy: IfNotPresent
        name: app
        livenessProbe:
          failureThreshold: 10
          httpGet:
            path: /health
            port: 1883
          periodSeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          failureThreshold: 60
          httpGet:
            path: /health
            port: 1883
          periodSeconds: 5
          timeoutSeconds: 5
        ports:
          - containerPort: 1883
            name: tcp
        resources:
          limits:
            cpu: "4"
            memory: 4Gi
          requests:
            cpu: 100m
            memory: 512Mi
        volumeMounts:
        - name: config
          mountPath: "/app/conf"
          readOnly: true
        - name: shared-logs
          mountPath: /app/logs
          
      volumes:
      - name: config
        configMap:
          name: app-config
      - name: shared-logs
        emptyDir: 
      - name: config-promtail
        configMap:
          name: promtail-sidecar-config-map
      terminationGracePeriodSeconds: 10

还有就是对应的configmap

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-headless.loki:3100/loki/api/v1/push
      scrape_configs:
        - job_name:  "release/app" 
          static_configs: 
            - targets: 
                - localhost 
              labels:
                app: "app"
                __path__: /app/logs/app.log       

这里的路径要写对了

__path__: /app/logs/app.log

欢迎关注我的博客www.bboy.app

Have Fun


Tags:

本站总访问量 本站总访客数