BlueXIII's Blog

热爱技术,持续学习

0%

Loki部署

参考

官网

入门教程

动态标签

日志改造

配置

安装

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
# 添加chart库
helm repo add grafana https://grafana.github.io/helm-charts

# 单体模式部署
helm upgrade --install loki \
--namespace=loki-stack grafana/loki-stack --create-namespace \
--set grafana.enabled=true \
--set grafana.image.tag="9.3.2"

# 单体模式+指定PVC
vi pvc-values.yaml
loki:
persistence:
enabled: true
size: 500Gi

helm upgrade --install loki --namespace=loki-stack grafana/loki-stack --create-namespace \
--set grafana.enabled=true \
--set grafana.image.tag="9.3.2" \
-f pvc-values.yaml

# 查看Grafana密码
kubectl get secret --namespace loki-stack loki-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
blwUDUjqhl997xofek3dp06w8Xmc4ZhvwucQnMXw

# 打开Grafana
kubectl port-forward --namespace loki-stack service/loki-grafana 3000:80
open http://localhost:3000
open http://10.193.36.41:30016 # nodeport

# 部署示例应用
kubectl apply -f https://ghproxy.com/https://raw.githubusercontent.com/lyzhang1999/kubernetes-example/main/loki/deployment.yaml
kubectl logs -l app=log-example

Query

Grafana 9.X支持图形化查询,TIDB自带的Grafana版本为7.5.11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Label查询
{app="log-example"} |= ``
# 使用logfmt解析器
{app="log-example"} | logfmt | status = `200`
# 使用JSON解析器
{app="log-example"} | json | status = `200`
# 查询状态码不等于200的日志
{app="log-example"} |= `status=` | logfmt | status != `200`
# 查询状态码在400和500区间的日志
{app="log-example"} |= `status=` | logfmt | status >= 400 | status <= 500
# 查询接口返回时间超过3us的日志
{app="log-example"} |= `status=` | logfmt | duration > 3us
# 重新格式化输出日志
{app="log-example"} |= `status=` | logfmt | line_format `{{ .duration }} {{ .uri }}`
# 分组统计 10s 内 Pod 的日志数量
sum by (pod) (count_over_time({app="log-example"} |= `status=` | logfmt | duration > 4us [10s]))
# 分组统计 1 分钟内的请求状态码数量
sum by (status) (
count_over_time({app="log-example"} |= `status=` | logfmt | __error__=""[1m])
)

{app="dubhe-metadata"} | json | level = `INFO`

JSON日志改造

pom.xml

1
2
3
4
5
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.0.1</version>
</dependency>

logback-spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<pattern>
<pattern>
{
"timestamp": "%date{\"yyyy-MM-dd HH:mm:ss.SSS\"}",
"level": "%level",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"method": "%method",
"line": "%line",
"message": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>