loki-promtail 指抓取所选pod的日志

版本:grafana/promtail:v0.4.0

Promtail 默认安装,会抓取所有pod的日志,如果我不想从群集中的所有Pod抓取日志,而仅抓取所选应用程序的Pod日志,需要自己修改的配置。

例如:我们想跟prometheus一样,增加 annotations: prometheus.io/scrape: "true" 来选定抓取的pod:

$ cat nginx-deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        prometheus.io/scrape: "true"
    spec:
      nodeSelector:
        kubernetes.io/hostname: 10.2.2.123
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

在promtail.yaml的scrape_config下增加:

  - action: keep
    regex: true
    source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]

完整的promtail配置文件如下: 

# cat /etc/promtail/promtail.yaml 
client:
  backoff_config:
    maxbackoff: 5s
    maxretries: 20
    minbackoff: 100ms
  batchsize: 102400
  batchwait: 1s
  external_labels: {}
  timeout: 10s
positions:
  filename: /run/promtail/positions.yaml
server:
  http_listen_port: 3101
target_config:
  sync_period: 10s

scrape_configs:
- job_name: kubernetes-pods-app
  pipeline_stages:
    - docker: {}
    
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - action: keep
    regex: true
    source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
  - action: drop
    regex: .+
    source_labels:
    - __meta_kubernetes_pod_label_name
  - source_labels:
    - __meta_kubernetes_pod_label_app
    target_label: __service__
  - source_labels:
    - __meta_kubernetes_pod_node_name
    target_label: __host__
  - action: drop
    regex: ''
    source_labels:
    - __service__
  - action: labelmap
    regex: __meta_kubernetes_pod_label_(.+)
  - action: replace
    replacement: $1
    separator: /
    source_labels:
    - __meta_kubernetes_namespace
    - __service__
    target_label: job
  - action: replace
    source_labels:
    - __meta_kubernetes_namespace
    target_label: namespace
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_name
    target_label: instance
  - action: replace
    source_labels:
    - __meta_kubernetes_pod_container_name
    target_label: container_name
  - replacement: /var/log/pods/*$1/*.log
    separator: /
    source_labels:
    - __meta_kubernetes_pod_uid
    - __meta_kubernetes_pod_container_name
    target_label: __path__

参考:

https://github.com/grafana/loki/blob/master/docs/clients/promtail/configuration.md#relabel_config

https://github.com/grafana/loki/issues/808

https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值