dDDgW6.jpg

近期有需求在kubernetes安装loki,此文档用于记录一下

环境说明

kubernetes1.14.3 helm3.x loki1.6 promtail1.6

loki是什么

Loki是一个水平可扩展,高可用性,多租户的日志聚合系统,受到Prometheus的启发。它的设计非常经济高效且易于操作,因为它不会为日志内容编制索引,而是为每个日志流编制一组标签。官方介绍说到:Like Prometheus, but for logs. 具体的可以参考以前写的Grafana日志聚合工具Loki搭建使用这篇博客。

安装loki

添加helm的chart库

1
2
3
4
5
6
7
8
9
$ helm repo add loki https://grafana.github.io/loki/charts
"loki" has been added to your repositories
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "loki" chart repository
Update Complete. ⎈ Happy Helming!⎈
$ helm repo list
NAME	URL
loki	https://grafana.github.io/loki/charts

安装loki及promtail

安装方式分为多种,我是下载相应的chart包修改了些配置再安装的

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 查找loki的相关chart 其中loki-stack是包含了loki和promtail
$ helm search repo loki
NAME           	CHART VERSION	APP VERSION	DESCRIPTION
loki/loki      	0.31.0       	v1.6.0     	Loki: like Prometheus, but for logs.
loki/loki-stack	0.40.0       	v1.6.0     	Loki: like Prometheus, but for logs.
loki/fluent-bit	0.3.0        	v1.6.0     	Uses fluent-bit Loki go plugin for gathering lo...
loki/promtail  	0.24.0       	v1.6.0     	Responsible for gathering logs and sending them...


# 安装参考
## 默认参考
$ helm upgrade --install loki loki/loki-stack
# 自定义安装
$ helm upgrade --install loki -n loki loki/loki-stack
# 只安装loki或者promtail
$ helm upgrade --install loki loki/loki
$ helm upgrade --install promtail loki/promtail --set "loki.serviceName=loki"
  • 修改配置及安装

下载相应的chart包,修改一些配置再安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 需要给loki做持久化
$ kubectl create ns loki

$ helm pull loki/loki
$ tar -xvf loki-0.31.0.tgz
$ vim loki/values.yaml
......
# 由于获取的日志都存放在loki中,所以给loki做一下持久化
persistence:
  enabled: true #由原来的false改为true
  storageClassName: loki #添加这一行 前提是你需要创建好名为loki的storageclasses
  accessModes:
  - ReadWriteOnce
  size: 50Gi
  annotations: {}
  # subPath: ""
  # existingClaim:
......
# 安装loki
$ helm upgrade --install loki loki/ -n loki

# 给promtail做一些配置
$ helm pull loki/promtail
$ ls
promtail-0.24.0.tgz
$ tar -xvf promtail-0.24.0.tgz
$ vim promtail/values.yaml
......
# 如果你想要promtail对接外部的loki,这个地方需要修改,我这里不是对接外部的loki 所以没有修改
loki:
  serviceName: ""  # Defaults to "${RELEASE}-loki" if not set
  servicePort: 3100
  serviceScheme: http
  
# 如果你的docker存储位置修改过,这里就要做对应的修改 否则promtail拉取日志时会报错的
# Extra volumes to scrape logs from
volumes:
- name: docker
  hostPath:
    path: /home/docker-data/docker/containers
- name: pods
  hostPath:
    path: /var/log/pods

# Custom volumes together with the default ones
extraVolumes: []

volumeMounts:
- name: docker
  mountPath: /home/docker-data/docker/containers
  readOnly: true
- name: pods
  mountPath: /var/log/pods
  readOnly: true
......


# 安装
$ helm upgrade --install promtail promtail/ --set "loki.serviceName=loki" -n loki
$ kubectl get pods -n loki
NAME             READY   STATUS    RESTARTS   AGE
loki-0           1/1     Running   0          5m20s
promtail-82czd   1/1     Running   0          2m10s
promtail-d5xjp   1/1     Running   0          2m10s
promtail-h9rc9   1/1     Running   0          2m10s
promtail-n99vv   1/1     Running   0          2m10s
promtail-pfs78   1/1     Running   0          2m10s
promtail-shwr9   1/1     Running   0          2m10s
promtail-wxp4t   1/1     Running   0          2m10s

grafana的安装配置不再做过过多赘述。

需要注意的事

  • 如果你grafana是和loki安装在同一namespaces下,配置loki源时写入http://loki:3100。
  • 如果你的grafana是和loki安装在不同namespaces下,配置loki源时写入http://loki.loki.svc.cluster.local:3100(.svc前面为你namespaces名字)
  • 如果你的grafana是安装在外部机器上的,需要你的loki暴露出来端口或解析为域名在配置loki源,ip+port和域名两种方式都可以配置

参考链接

https://www.cnblogs.com/ssgeek/p/11584870.html