日志
promtail-loki-grafana 日志监控
本文档使用 MrDoc 发布
-
+
首页
promtail-loki-grafana 日志监控
# 部署包 [【附件】Loki-deploy.zip](/media/attachment/2023/04/Loki-deploy.zip) # loki ``` wget https://github.com/grafana/loki/releases/download/v2.5.0/loki-linux-amd64.zip unzip loki-linux-amd64.zip mv loki-linux-amd64 loki ``` 配置文件 loki-local-config.yaml ``` auth_enabled: false server: http_listen_port: 3100 grpc_listen_port: 9096 grpc_server_max_recv_msg_size: 10000000 grpc_server_max_send_msg_size: 10000000 ingester: wal: enabled: true dir: /data/loki/wal 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 #每张表的时间范围,1天 storage_config: boltdb_shipper: active_index_directory: /data/loki/index #索引文件存储地址 cache_location: /data/loki/cache #缓存存储地址 cache_ttl: 24h # Can be increased for faster performance over longer query periods, uses more disk space shared_store: filesystem filesystem: directory: /data/loki/trunks #块存储地址 compactor: working_directory: /data/loki/compactor shared_store: filesystem limits_config: enforce_metric_name: false reject_old_samples: true reject_old_samples_max_age: 168h ingestion_rate_mb: 50 # unordered_writes: true chunk_store_config: max_look_back_period: 2160h #最大可查询历史日期 90天 #表的保留期90天 table_manager: retention_deletes_enabled: true retention_period: 2160h ruler: storage: type: local local: directory: /data/loki/rules rule_path: /data/loki/rules-tmp alertmanager_url: http://localhost:9093 ring: kvstore: store: inmemory enable_api: true ``` `启动`/usr/local/loki/loki-linux-amd64 -config.file /usr/local/loki/conf/loki-local-config.yaml # promtail ``` wget https://github.com/grafana/loki/releases/download/v2.5.0/promtail-linux-amd64.zip unzip promtail-linux-amd64.zip mv promtail-linux-amd64 promtail ``` promtail-local-config.yaml ``` server: http_listen_port: 9080 grpc_listen_port: 0 positions: filename: /tmp/positions.yaml clients: - url: http://8.212.7.58:3100/loki/api/v1/push scrape_configs: - job_name: nginx pipeline_stages: - replace: expression: '(?:[0-9]{1,3}\.){3}([0-9]{1,3})' replace: '***' static_configs: - targets: - localhost labels: job: nginx_access_log host: "满天星" agent: promtail __path__: /opt/openresty/nginx/logs/9292.log ``` ./promtail-linux-amd64 -config.file ./promtail-local-config.yaml # grafana ``` dashboards id 12559 ``` ![](/media/202305/2023-05-02_182020_5355540.3350343371508413.png) # openresty Nginx 配置 首先修改Nginx日志为json格式: ``` log_format json_analytics escape=json '{' '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution '"connection": "$connection", ' # connection serial number '"connection_requests": "$connection_requests", ' # number of requests made in connection '"pid": "$pid", ' # process pid '"request_id": "$request_id", ' # the unique request id '"request_length": "$request_length", ' # request length (including headers and body) '"remote_addr": "$remote_addr", ' # client IP '"remote_user": "$remote_user", ' # client HTTP username '"remote_port": "$remote_port", ' # client port '"time_local": "$time_local", ' '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format '"request": "$request", ' # full path no arguments if the request '"request_uri": "$request_uri", ' # full path and arguments if the request '"args": "$args", ' # args '"status": "$status", ' # response status code '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client '"http_referer": "$http_referer", ' # HTTP referer '"http_user_agent": "$http_user_agent", ' # user agent '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for '"http_host": "$http_host", ' # the request Host: header '"server_name": "$server_name", ' # the name of the vhost serving the request '"request_time": "$request_time", ' # request processing time in seconds with msec resolution '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body '"upstream_response_length": "$upstream_response_length", ' # upstream response length '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable '"ssl_protocol": "$ssl_protocol", ' # TLS protocol '"ssl_cipher": "$ssl_cipher", ' # TLS cipher '"scheme": "$scheme", ' # http or https '"request_method": "$request_method", ' # request method '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0 '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise '"gzip_ratio": "$gzip_ratio", ' '"http_cf_ray": "$http_cf_ray",' '"geoip_country_code": "$geoip_country_code"' '}'; access_log /usr/local/nginx/logs/json_access.log json_analytics; ``` 注意这里的geoip_country_code,用于展示哪个国家的哪个城市访问。需要在Nginx添加geoip的支持。 首先通过yum安装geoip及lib库 ``` yum -y install GeoIP GeoIP-data GeoIP-devel ``` ``` # http 中添加 geoip_city /usr/local/nginx/html/ip/city.dat; geoip_country /usr/local/nginx/html/ip/country.dat; ```
admin
2023年5月2日 20:18
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码