准备
需要有k8s集群
本机安装有helm,kubectl,docker
git clone https://github.com/wwq-2020/observability.git
cd observability/logging/loki
kubectl create ns loki
安装
安装minio
用于存储loki的index和chunk
helm install minio ./minio -n loki
通过如下命令获取admin密码
kubectl get secrets -n loki minio data.root-password}' | base64 -d
访问k8sip:30086,k8sip为k8s节点的ip
输入admin,和上面获取的密码
然后先后点击identity,users,create user,输入access key,secret key,勾选所有policy,点击save,如下图
这边access key填入mykey,secret key填入mysecret(因为部署loki的values.yaml里面写好了,也可以配合着一起改)

点击buckets,点击create bucket

输入bucket name,点击create bucket

安装redis
用于存储loki的query,index,chunk以及dedup缓存
helm install redis ./redis-n loki
查看密码
kubectl get secrets -n loki redis -ojsonpath='{.data.redis-password}'|base64 -d
安装alertmanager
用于接收loki的日志告警
helm install alertmanager ./alertmanager -n loki
安装loki
helm install loki-distributed ./loki-distributed -n loki
安装promtail
用于采集日志
helm install promtail ./promtail -n loki
安装grafana
helm install grafana ./grafana -n loki
账号admin
密码通过如下获取
kubectl get secrets -n loki grafana -ojsonpath='{.data.admin-password}'|base64 -d
安装demo应用
cd demo
docker build -t yourregistry/mydemo:v1 .
docker push yourregistry/mydemo:v1
修改deployment.yaml中的image为yourregistry/mydemo:v1,然后执行
kubectl apply -f deployment.yaml
配置日志查看
获取密码
kubectl get secrets -n loki grafana -ojsonpath='{.data.admin-password}'|base64 -d
访问k8sip:30084,输入账号admin,密码获取到的密码


url输入http://loki-distributed-gateway.loki.svc.cluster.local
点击save & test
点击explore

依次点击log browser,app,demo,show logs

得到如下页面

配置告警
创建文件rule,内容如下
{
"name": "demo",
"interval": "10s",
"rules": [{
"alert": "demo_to_much_error",
"expr": "sum(rate({app=\"demo\"} |= \"error\" [5m]))/sum(rate({app=\"demo\"}[5m]))> 0.05",
"for": "5s",
"labels": {
"serverity": "warning"
},
"annotations": {
"summary": "demo to much error"
}
}]
}
curl -X POST k8sip:30087/api/prom/rules/loki -d@rule
等待5秒后
访问 k8sip:30083,得到如下页面

网友评论