Rancher中使用promtail+loki+grafna收集k8s日志并展示

本文介绍了在Rancher中使用promtail、loki和grafana集成收集和展示k8s日志的步骤。首先,通过添加Chart仓库安装promtail,然后配置并更新promtail以指向loki地址。接着,外部部署loki和grafana,通过docker-compose启动服务。最后,通过grafana的Explore功能,利用Loki进行日志过滤和查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rancher中使用promtail+loki+grafna收集k8s日志并展示

根据应用需求和日志数量级别选择对应的日志收集、过滤和展示方式,当日志量不太大,又想简单集中管理查看日志时,可使用promtail+loki+grafna的方式。本文找那个loki和grafana外置在了k8s集群之外。

1、添加Chart Repo

方式一:

helm repo add grafana https://grafana.github.io/helm-charts

方式二:

登录rancher,选择集群→应用→Chart仓库→创建,配置仓库
在这里插入图片描述

2、安装promtail

选择集群→应用→Chart,搜索promtail,然后安装

注意,

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

配置promtail

选择集群→应用→已安装应用,开始配置

在这里插入图片描述

在这里插入图片描述

修改loki地址,然后更新
在这里插入图片描述

在这里插入图片描述

更新完成后会自动弹出打印日志。

3、安装loki+grafana

loki+grafana外置,没有放到k8s集群中,我这里直接在其他机器用docker-compose方式部署

编辑docker-compose.yaml 文件如下

version: "3"

networks:
  loki:

services:
  loki:
    image: grafana/loki:2.8.3
    ports:
      - "4100:3100"
    volumes:
      - /data/loki/loki/chunks:/loki/chunks
      - /data/loki/loki/rules:/loki/rules
      - /etc/localtime:/etc/localtime
      - /data/loki/loki/loki-local-config.yaml:/etc/loki/local-config.yaml
    command: -config.file=/etc/loki/local-config.yaml
    networks:
      - loki

  grafana:
    environment:
      - GF_PATHS_PROVISIONING=/etc/grafana/provisioning
      - GF_AUTH_ANONYMOUS_ENABLED=false
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
    entrypoint:
      - sh
      - -euc
      - |
        mkdir -p /etc/grafana/provisioning/datasources
        cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
        apiVersion: 1
        datasources:
        - name: Loki
          type: loki
          access: proxy 
          orgId: 1
          url: http://loki:3100
          basicAuth: false
          isDefault: true
          version: 1
          editable: false
        EOF
        /run.sh
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    volumes:
      - /etc/localtime:/etc/localtime
    networks:
      - loki

创建对应文件存储目录并编写loki配置文件

mkdir -p /data/loki/loki/{chunks,rules}
chmod 777 /data/loki/loki/{chunks,rules}

cat /data/loki/loki/loki-local-config.yaml 
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

common:
  instance_addr: 127.0.0.1
  path_prefix: /tmp/loki
  storage:
    filesystem:
      chunks_directory: /tmp/loki/chunks
      rules_directory: /tmp/loki/rules
  replication_factor: 1
  ring:
    kvstore:
      store: inmemory

query_range:
  results_cache:
    cache:
      embedded_cache:
        enabled: true
        max_size_mb: 100

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

ruler:
  alertmanager_url: http://localhost:9093

# By default, Loki will send anonymous, but uniquely-identifiable usage and configuration
# analytics to Grafana Labs. These statistics are sent to https://stats.grafana.org/
#
# Statistics help us better understand how Loki is used, and they show us performance
# levels for most users. This helps us prioritize features and documentation.
# For more information on what's sent, look at
# https://github.com/grafana/loki/blob/main/pkg/usagestats/stats.go
# Refer to the buildReport method to see what goes into a report.
#
# If you would like to disable reporting, uncomment the following lines:
#analytics:
#  reporting_enabled: false

启动服务

docker-compose up -d

4、使用

访问http://IP:3000,登录grafana,默认账户密码:admi/admin

也可以直接进容器查看

#进入容器
docker exec -it loki_grafana_1 bash
#查看密码
cat /usr/share/grafana/conf/defaults.ini |grep -C 10 admin_user

# Controls if the UI contains any links to user feedback forms
feedback_links_enabled = true

#################################### Security ############################
[security]
# disable creation of admin user on first start of grafana
disable_initial_admin_creation = false

# default admin user, created on startup
admin_user = admin

# default admin password, can be changed before first start of grafana, or in profile settings
admin_password = admin

# default admin email, created on startup
admin_email = admin@localhost

# used for signing
secret_key = SW2YcwTIb9zpOOhoPsMm

在这里插入图片描述

登录后选择Explore
在这里插入图片描述

选择Loki,根据Lable过滤命名空间、app、容器名、文件名等条件,然后Run query 运行

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

正在输入中…………

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值