4

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

 1 year ago
source link: https://www.bboy.app/2022/12/30/%E4%BD%BF%E7%94%A8promtail%E5%8E%BB%E6%94%B6%E9%9B%86%E5%AE%B9%E5%99%A8%E5%86%85%E9%83%A8%E7%9A%84%E6%97%A5%E5%BF%97/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

2022-12-30 17:03:00 kubernetes

20221230-2.webp

如果直接使用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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK