k3s 基础 —— 快捷搭建 loki

官方文档

核心组件 3 个 chart:

  • promtail 这是一个 agent 代理客户端,用于收集日志,将日志传送给 loki
  • loki 核心组件,主要功能是日志数据的写入与分析。包含 gateway、read、write 3个组件。
  • kube-prometheus-stack 这个技术栈包含 prometheus,grafana,prometheus operator 等组件。prometheus 主要用于数据监控与告警。grafana 图形界面展示。Prometheus Operator 用于在 k8s 中能够帮助用户自动化的创建以及管理 Prometheus Server 以及其相应的配置(监听一些 crd 资源的配置)。

部署 loki

新增配置文件 /var/lib/rancher/k3s/server/manifests/loki.yaml

创建 smtp secret

kubectl create secret generic smtp-secret --from-literal=user=<SMTP_USER> --from-literal=password=<SMTP_PASSWORD> -n monitoring

配置文件

apiVersion: v1
kind: Namespace
metadata:
  name: monitoring
---
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: kube-prometheus-stack
  namespace: kube-system
spec:
  repo: https://prometheus-community.github.io/helm-charts
  chart: kube-prometheus-stack
  version: 57.0.3
  targetNamespace: monitoring
  valuesContent: |-
    grafana:
      adminUser: <adminName>
      adminPassword: <adminPassword>
      persistence:
        enabled: true
        size: 4Gi
      smtp:
        existingSecret: smtp-secret
        userKey: user
        passwordKey: password
      grafana.ini:
        server:
          root_url: https://<your_grafana_domain>/
        smtp:
          enabled: true
          host: smtp.xxxx.com:465
          from_address: noreply@xxxx.com
          from_name: xxxx
          #user: <smtpUser>
          #password: <smtpPassword>
---
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: loki
  namespace: kube-system
spec:
  repo: https://grafana.github.io/helm-charts
  chart: loki
  version: 5.45.0
  targetNamespace: monitoring
  valuesContent: |-
    loki:
      auth_enabled: false
      commonConfig:
        replication_factor: 2
    backend:
      replicas: 2
    read:
      replicas: 2
    write:
      replicas: 2
    minio:
      enabled: true
    compactor:
      working_directory: /data/retention
      shared_store: s3
      compaction_interval: 10m
      retention_enabled: true
      retention_delete_delay: 2h
      retention_delete_worker_count: 150
    schema_config:
      configs:
        - from: "2023-01-03"
          index:
            period: 24h
            prefix: index_
          object_store: s3
          schema: v12
          store: boltdb-shipper
    storage_config:
      boltdb_shipper:
        active_index_directory: /data/index
        cache_location: /data/boltdb-cache
        shared_store: s3
      s3:
        bucket_name: loki
    limits_config:
      retention_period: 744h
---
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: promtail
  namespace: kube-system
spec:
  repo: https://grafana.github.io/helm-charts
  chart: promtail
  version: 6.15.5
  targetNamespace: monitoring
  valuesContent: |-
    daemonset:
      enabled: true

注:grafana alert 触发警报时邮件提醒,需要填写发信邮箱 SMTP 相关配置。
更多配置参考:
https://github.com/grafana/helm-charts/blob/main/charts/grafana/values.yaml
https://github.com/prometheus-community/helm-charts/blob/main/charts/kube-prometheus-stack/values.yaml

执行部署

kubectl apply -f loki.yaml

PS: 若要访问 minio console,可执行下面的命令进行流量转发。浏览器访问 http://server_ip:9001,默认账号 enterprise-logs/supersecret

kubectl port-forward -n monitoring service/loki-minio-console 9001:9001 --address 0.0.0.0

查询日志数据

配置 grafana 访问路由

新增配置文件 grafana-traefik.yaml

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: grafana-web
  namespace: monitoring
spec:
  entryPoints:
    - web
  routes:
    - kind: Rule
      match: Host(`grafana.example.com`)
      services:
        - name: kube-prometheus-stack-grafana
          namespace: monitoring
          port: 80

执行部署命令后,即可访问 grafana.example.com

kubectl apply -f grafana-traefik.yaml

默认的账号密码:admin/prom-operator 查看

添加 loki 数据源

打开 Data sources 页面,添加 loki 数据源。其中 url 为 http://loki-gateway

查询日志

打开 explore 页面。选择 Label Broswer

选择需要查询的标签,如 namespace,然后选择列表出现的任一值,最后点击下方的 Show logs 进行查询

2233

posted on 2023-07-04 15:27  Lemo_wd  阅读(422)  评论(0编辑  收藏  举报

导航