实验环境
loki 负责存储日志和处理查询
promtail 负责收集日志并将其发送给 loki
Grafana ui界面展示
服务器ip |
部署服务 |
服务端口号 |
192.168.10.100 |
loki+grafana |
3100/300 |
192.168.10.101 |
promtail |
80/9080 |
软件安装
cat /etc/redhat-release && uname -a
CentOS Linux release 7.8.2003 (Core)
Linux centos7-1 3.10.0-1127.el7.x86_64
systemctl stop firewalld && systemctl disable firewalld
echo SELINUX=disabled > /etc/sysconfig/selinux
cp -pv /etc/sysctl.conf /etc/sysctl.conf.bak
echo net.ipv4.tcp_syncookies = 1 >> /etc/sysctl.conf
echo net.ipv4.tcp_tw_reuse = 1 >> /etc/sysctl.conf
echo net.ipv4.tcp_tw_recycle = 1 >> /etc/sysctl.conf
echo net.ipv4.tcp_fin_timeout = 10 >> /etc/sysctl.conf
echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
sysctl -p
loki/grafana服务器端配置
docker pull grafana/loki:2.4.1
docker pull grafana/promtail:2.4.1
docker pull grafana/grafana:master
yum install -y nginx 测试监控nginx日志 被监控端部署
docker run -itd --name loki -p 3100:3100 --restart=always \
-v /etc/localtime:/etc/localtime --net=host grafana/loki:2.4.1
docker logs -f loki | grep 3100
caller=server.go:260 http=[::]:3100 grpc=[::]:9095 msg="server listening on addresses" 启动容器查看日志
docker run -itd --name grafan -p 3000:3000 --restart=always \
-v /etc/localtime:/etc/localtime --net=host grafana/grafana:master 启动grafana容器
docker logs -f grafan | grep 3000 查看容器 端口监听状态
HTTP Server Listen logger=http.server address=[::]:3000 protocol=http
promtai客户端配置
systemctl start chronyd nginx && systemctl enable chronyd nginx && hwclock -w
cat /usr/local/docker/promtail/config.yml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://loki服务器ip:3100/loki/api/v1/push 定义loki服务器ip
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: nginxlogs 定义日志收集模块名字
__path__: /var/log/nginx/*.log 定义日志目录
docker run -itd --name prtail \
-p 9080:9080 --restart=always
-v /etc/localtime:/etc/localtime \
-v /var/log/nginx/:/var/log/nginx/ \
-v /usr/local/docker/promtail/config.yml:/etc/promtail/config.yml \
--net=host grafana/promtail:2.4.1 --config.file=/etc/promtail/config.yml 启动promtail服务
docker logs -f prtail
caller=server.go:260 http=[::]:9080 grpc=[::]:41965 msg="server listening on addresses"
caller=main.go:119 msg="Starting Promtail" version="(version=2.4.1, branch=HEAD, revision=f61a4d261)"
caller=filetargetmanager.go:255 msg="Adding target" key="/var/log/nginx/*log:{job=\"varlogs\"}"
caller=log.go:168 level=info msg="Seeked /var/log/nginx/access.log - &{Offset:0 Whence:0}"
caller=tailer.go:126 component=tailer msg="tail routine: started" path=/var/log/nginx/access.log
caller=log.go:168 level=info msg="Seeked /var/log/nginx/error.log - &{Offset:0 Whence:0}"
caller=tailer.go:126 component=tailer msg="tail routine: started" path=/var/log/nginx/error.log
netstat -tuplna | grep LISTEN
tcp6 0 0 :::3100 :::* LISTEN 14504/loki
tcp6 0 0 :::9095 :::* LISTEN 14504/loki
tcp6 0 0 :::3000 :::* LISTEN 12750/grafana-serve
tcp6 0 0 :::80 :::* LISTEN 12657/nginx: master
tcp6 0 0 :::9080 :::* LISTEN 14781/promtail
http://grafana服务器ip:3000/login
用户名 admin 密码 admin