采用二进制文件方式安装loki和promtail

1. 下载二进制文件

官方下载地址:https://github.com/grafana/loki/releases

下载如图所示的这俩文件,Grafana采用yum方式安装

cd /usr/local/src
wget https://github.com/grafana/loki/releases/download/v2.1.0/loki-linux-amd64.zip
wget https://github.com/grafana/loki/releases/download/v2.1.0/promtail-linux-amd64.zip

unzip loki-linux-amd64.zip
unzip promtail-linux-amd64.zip

chmod a+x loki-linux-amd64
chmod a+x promtail-linux-amd64

wget https://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm
sudo yum -y install grafana-7.3.6-1.x86_64.rpm

systemctl start grafana-server.service
systemctl stop grafana-server.service
systemctl status grafana-server.service

2. 下载配置文件

关于这个配置文件,应该具体分情况,不同的部署方式采用的配置文件会有一些不同之处

loki官方文档地址:https://grafana.com/docs/loki/latest/configuration/examples/

不过这里参考官方github上的地址:https://github.com/grafana/loki/tree/master/cmd/loki

loki-local-config.yaml

auth_enabled: false

server:
  http_listen_port: 3100 # 端口

ingester:
  wal:
    enabled: true
    dir: /tmp/wal
    recover: true
  lifecycler:
    address: 127.0.0.1 # 地址
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 1h       # Any chunk not receiving new logs in this time will be flushed
  max_chunk_age: 1h           # All chunks will be flushed when they hit this age, default is 1h
  chunk_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 first
  chunk_retain_period: 30s    # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
  max_transfer_retries: 0     # Chunk transfers disabled

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

storage_config:
  boltdb_shipper: 
    active_index_directory: /tmp/loki/boltdb-shipper-active
    cache_location: /tmp/loki/boltdb-shipper-cache
    cache_ttl: 24h         # Can be increased for faster performance over longer query periods, uses more disk space
    shared_store: filesystem
  filesystem:
    directory: /tmp/loki/chunks

compactor:
  working_directory: /tmp/loki/boltdb-shipper-compactor
  shared_store: filesystem

limits_config:
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: false
  retention_period: 0s

ruler:
  storage:
    type: local
    local:
      directory: /tmp/loki/rules
  rule_path: /tmp/loki/rules-temp
  alertmanager_url: http://localhost:9093 # alertmanager报警地址
  ring:
    kvstore:
      store: inmemory
  enable_api: true

promtail官方github上的地址:https://github.com/grafana/loki/blob/master/cmd/promtail
promtail-local-config.yaml

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push # 推送日志的loki地址

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: varlogs
      __path__: /var/log/*log

3. 运行

使用这个loki-local-config.yaml文件时还需要修改一下,去掉里面的这几行,

  wal:
    enabled: true
    dir: /tmp/wal
    recover: true

不然启动会报错:

failed parsing config: ./loki-local-config.yaml: yaml: unmarshal errors:
  line 7: field wal not found in type ingester.Config

修改loki数据存储路径:

# vim loki-local-config.yaml
:%s+/tmp/+/opt/loki/tmp/+
# vim promtail-local-config.yaml
:%s+/tmp/+/opt/loki/tmp/+
./loki-linux-amd64 -config.file=./loki-local-config.yaml
./promtail-linux-amd64 -config.file=./promtail-local-config.yaml

后台启动,输出日志到其他文件中:

nohup ./loki-linux-amd64 -config.file=./loki-local-config.yaml > /opt/logs/loki-3100.log 2>&1 &
访问:# curl http://localhost:3100/metrics

如需debug日志,使用以下启动命令:
nohup ./loki-linux-amd64 --log.level=debug -config.file=./loki-local-config.yaml > /opt/logs/loki-3100.log 2>&1 &



# nohup ./promtail-linux-amd64 -config.file=promtail-local-config.yaml > /opt/logs/promtail-9080.log 2>&1 &

正常启动后的信息显示:

打开浏览器访问grafana界面,配置数据源

查看loki运行日志,promtail给loki推送过来日志的话日志显示如下:

4. 进一步完善

注:这一步只是写一下教程,并没实际操作

参考这篇文章的部分内容,制作成service服务并运行:https://www.cnblogs.com/sanduzxcvbnm/p/13094099.html

vim /usr/lib/systemd/system/loki.service

[Unit]
Description=loki
Documentation=https://github.com/grafana/loki/tree/master
After=network.target

[Service]
Type=simple
User=loki
ExecStart=/usr/local/src/loki-linux-amd64 -config.file=/usr/local/src/loki-local-config.yaml &>> /opt/logs/loki-3100.log # 具体路径可以根据实际情况修改
Restart=on-failure

[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl start loki
# systemctl status loki
# systemctl enable loki
vim /usr/lib/systemd/system/promtail.service

[Unit]
Description=promtail
Documentation=https://github.com/grafana/loki/tree/master
After=network.target

[Service]
Type=simple
User=promtail
ExecStart=/usr/local/src/promtail-linux-amd64 -config.file=/usr/local/src/promtail-local-config.yaml  &>> /opt/logs/promtail-9080.log # 具体路径可以根据实际情况修改
Restart=on-failure

[Install]
WantedBy=multi-user.target
# systemctl daemon-reload
# systemctl start promtail
# systemctl status promtail
# systemctl enable promtail

验证:

curl "http://127.0.0.1:3100/api/prom/label"
curl localhost:3100/loki/api/v1/labels
posted @ 2021-01-05 11:52  哈喽哈喽111111  阅读(4534)  评论(0编辑  收藏  举报