目前,我正在尝试使用promtail、loki和grafana为停靠群集群设置日志监视。从promtail到loki的日志转发和图形化的可视化都很好。
但是,使用我当前的promtail配置,所有容器日志都会被发送到loki。因此,我的问题是,如果有人知道一个promtail配置,它发送由他们所属的码头群服务聚合的容器日志?
当前的promtail config.yml如下所示:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs_prom
__path__: /var/log/*log
- job_name: containers
static_configs:
- targets:
- localhost
labels:
job: containerlogs_prom
__path__: /var/lib/docker/containers/*/*log
pipeline_stages:
- json:
expressions:
output: log
stream: stream
attrs:
- json:
expressions:
tag:
source: attrs
- regex:
expression: (?P<image_name>(?:[^|]*[^|])).(?P<container_name>(?:[^|]*[^|])
).(?P<image_id>(?:[^|]*[^|])).(?P<container_id>(?:[^|]*[^|]))
source: tag
- timestamp:
format: RFC3339Nano
source: time
- labels:
tag_prom:
stream_prom:
image_name_prom:
container_name_prom:
image_id_prom:
container_id_prom:
- output:
source: output提前谢谢!!
发布于 2022-06-19 23:48:15
我发现您可以使用码头司机而不是Promtail。您可以轻松地通过以下方式安装它:
$ docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions并以下列方式验证安装:
docker plugin ls然后,可以配置为使用默认日志配置的所有容器使用loki日志驱动程序。
{
"debug" : true,
"log-driver": "loki",
"log-opts": {
"loki-url": "https://<user_id>:<password>@logs-us-west1.grafana.net/loki/api/v1/push",
"loki-batch-size": "400"
}
}从文档中,我不确定在本例中日志是否也保存在localhost上,但是如果您只是手动将日志驱动程序设置为docker-compose.yml文件,它将被发送到Loki中,并且也保存在localhost上。来自文档
version: "3.7"
services:
logger:
image: grafana/grafana
logging:
driver: loki
options:
loki-url: "https://<user_id>:<password>@logs-prod-us-central1.grafana.net/loki/api/v1/push"此配置还保留堆栈和服务名称:
每个集群服务的堆栈名称和服务名称以及每个组合服务的项目名称和服务名称都会被自动发现并作为Loki标签发送,这样您就可以在Grafana中通过它们进行过滤。
我希望这能帮到你。
EDIT1:我在GrafanaLabs find:将容器名称添加到promtail坞日志中上找到了类似的问题
EDIT2:来自已知问题的
驱动程序将所有日志保存在内存中,如果无法访问Loki,并且超出了max_retries的数量,则将删除日志条目。为了避免日志条目的删除,将max_retries设置为零允许无限重试;驱动器将永远继续尝试,直到再次到达Loki为止。永远尝试可能会产生意想不到的后果,因为Docker守护进程将等待Loki驱动程序处理容器的所有日志,直到容器被移除。因此,如果容器被卡住,Docker守护进程可能会永远等待。
其结果是,如果停靠loki驱动程序无法连接到loki,它将被塞进一个未打印的任何日志到停靠容器的stdout。它也将无法停止容器,因此它可能会导致部署应用程序的问题。正如在doc中提到的,建议使用码头目标或更好的码头服务发现。它可以从码头获取元数据,如容器名称、id、网络、标签等。要使用它,您必须使用replabel配置,例如:
scrape_configs:
- job_name: flog_scrape
docker_sd_configs:
- host: unix:///var/run/docker.sock
refresh_interval: 5s
filters:
- name: name
values: [flog]
relabel_configs:
- source_labels: ['__meta_docker_container_name']
regex: '/(.*)'
target_label: 'container'请注意此配置,标签container,即使是一个应用程序,也可以包含大量uniq值,因为停靠者的哈希名称如下:
根据Grafana Loki标签最佳实践的说法,这是不建议的。我用relabel修复了这个问题,在上面的示例中,使用relabel过滤容器的名称nginx_nginx或minio_minio,以及副本号(如果存在):
relabel_configs:
- source_labels: ['__meta_docker_container_name']
regex: '/(.*)\.[0-9]\..*'
target_label: 'name'
- source_labels: ['__meta_docker_container_name']
regex: '/(.*)\.[0-9a-z]*\..*'
target_label: 'name'
- source_labels: ['__meta_docker_container_name']
regex: '/.*\.([0-9]{1,2})\..*'
target_label: 'replica'https://stackoverflow.com/questions/70437253
复制相似问题