Loki日志系统,比ELK轻量多了( 二 )


架构图:

Loki日志系统,比ELK轻量多了

文章插图
 
收集日志的架构图:
Loki日志系统,比ELK轻量多了

文章插图
 
一套轻量级日志收集方案:
  • Promtail:日志收集工具
  • Loki:日志聚合系统
  • Grafana:可视化工具
部署 Loki官网地址:
https://github.com/grafana/loki ①loki
编辑 loki 配置文件:loki-config.yaml,参考:
https://grafana.com/docs/loki/latest/configuration/examples/ https://grafana.com/docs/loki/latest/installation/docker/ --- apiVersion: v1 kind: ConfigMap metadata:name: loki-configlabels:name: loki data:loki-config.yaml: |-auth_enabled: falseserver:http_listen_port: 3100grpc_listen_port: 9096ingester:lifecycler:address: 127.0.0.1ring:kvstore:store: inmemoryreplication_factor: 1final_sleep: 0schunk_idle_period: 5mchunk_retain_period: 30schunk_target_size: 1048576# Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached firstmax_transfer_retries: 0# Chunk transfers disabledschema_config:configs:- from: 2021-08-18store: boltdbobject_store: filesystemschema: v11index:prefix: index_period: 168hstorage_config:boltdb:directory: /tmp/loki/indexfilesystem:directory: /tmp/loki/chunkslimits_config:enforce_metric_name: falsereject_old_samples: truereject_old_samples_max_age: 168hingestion_rate_mb: 15chunk_store_config:max_look_back_period: 0stable_manager:retention_deletes_enabled: falseretention_period: 0s kubectl Apply -floki-config.yaml 创建 Service 和 StatefulSet,loki,.yaml:
---- apiVersion: v1 kind: Service metadata:name: lokiannotations:k8s.kuboard.cn/displayName: lokik8s.kuboard.cn/workload: lokilabels:name: loki spec:ports:- name: httpport: 3100protocol: TCPtargetPort: 3100selector:name: loki--- apiVersion: apps/v1 kind: StatefulSet metadata:name: loki spec:serviceName: lokiselector:matchLabels:name: lokitemplate:metadata:labels:name: lokispec:volumes:- name: loki-configconfigMap:#defaultMode: 0640name: loki-configcontainers:- name: loki#image: grafana/loki:2.3.0image: grafana/loki:masterargs:- -config.file=/etc/loki/loki-config.yamlports:- containerPort: 3100name: lokiprotocol: TCPvolumeMounts:- name: loki-configmountPath: /etc/loki/readOnly: true 执行命令创建:
kubectl apply -f loki.yaml ②grafana
根据自己时间情可对存储那块进行更改,不改的话是 emptyDir,你懂的 。账号密码为 admin/admin123.可自行修改 。
apiVersion: v1 kind: Service metadata:name: grafanalabels:k8s-app: grafana spec:type: NodePortports:- name: httpport: 3000targetPort: 3000selector:k8s-app: grafana --- apiVersion: apps/v1 kind: Deployment metadata:name: grafanalabels:k8s-app: grafana spec:selector:matchLabels:k8s-app: grafanatemplate:metadata:labels:k8s-app: grafanaspec:# initContainers:## 初始化容器,用于修改挂载的存储的文件夹归属组与归属用户# - name: init-file#image: busybox:1.28#imagePullPolicy: IfNotPresent#securityContext:#runAsUser: 0#command: ['chown', '-R', "472:0", "/var/lib/grafana"]#volumeMounts:#- name: data#mountPath: /var/lib/grafana#subPath: grafanacontainers:- name: grafana## Grafana 容器#image: grafana/grafanaimage: grafana/grafana:7.4.3#securityContext:## 容器安全策略,设置运行容器使用的归属组与用户#fsGroup: 0#runAsUser: 472ports:- name: httpcontainerPort: 3000protocol: TCPenv:## 配置环境变量,设置 Grafana 的默认管理员用户名/密码- name: GF_SECURITY_ADMIN_USERvalue: "admin"- name: GF_SECURITY_ADMIN_PASSwordvalue: "admin123"readinessProbe:## 就绪探针failureThreshold: 10httpGet:path: /api/healthport: 3000scheme: HTTPinitialDelaySeconds: 10periodSeconds: 10successThreshold: 1timeoutSeconds: 30livenessProbe:## 存活探针failureThreshold: 10httpGet:path: /api/healthport: 3000scheme: HTTPinitialDelaySeconds: 10periodSeconds: 10successThreshold: 1timeoutSeconds: 1volumeMounts:## 容器挂载配置- name: datamountPath: /var/lib/grafanasubPath: grafanavolumes:## 共享存储挂载配置- name: dataemptyDir: {}#persistentVolumeClaim:#claimName: grafana## 指定使用的 PVC ③promtail
应用结合 promtail,进行日志收集 。这里使用 Sidecar 模式 。一个 pod 中跑两个容器,一个为业务容器,一个为 promtail,两个容器挂载同一个存储目录,promtail 即可收集日志 。
编辑 promtail-config.yaml,可根据不同业务设置标签 。参考:
https://grafana.com/docs/loki/latest/clients/promtail/installation/ --- apiVersion: v1 kind: ConfigMap metadata:name: promtail-configlabels:k8s-app: promtail data:promtail.yaml: |-server:http_listen_port: 9080grpc_listen_port: 0positions:filename: ./positions.yaml # This location needs to be writeable by Promtail.#filename: /tmp/positions.yaml # This location needs to be writeable by Promtail.client:url: http://loki:3100/loki/api/v1/pushscrape_configs:- job_name: system#- job_name: busyboxstatic_configs:- targets:- localhostlabels:job: varlog#自定义host: busybox#自定义__path__: /tmp/*log收集日志的目录


推荐阅读