{"id":1151,"date":"2022-03-14T13:34:41","date_gmt":"2022-03-14T05:34:41","guid":{"rendered":"https:\/\/www.yinyubo.com\/?p=1151"},"modified":"2022-05-16T18:00:34","modified_gmt":"2022-05-16T10:00:34","slug":"loki","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=1151","title":{"rendered":"\u4f7f\u7528loki+promtail+grafana\u67b6\u6784\u5206\u6790nginx\u65e5\u5fd7"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">0.\u524d\u63d0<\/h2>\n\n\n\n<p>1.\u5df2\u7ecf\u9605\u8bfb\u8fc7\u8fd9\u7bc7\u6587\u7ae0https:\/\/www.yinyubo.com\/2022\/03\/14\/nginx%E5%8A%A8%E6%80%81%E5%A2%9E%E5%8A%A0%E6%A8%A1%E5%9D%97ngx_http_geoip2_module<\/p>\n\n\n\n<p>\u5e76\u4e14\u5728nginx\u91cc\u5df2\u7ecf\u5b89\u88c5\u597d\u4e86geoip2<\/p>\n\n\n\n<p>2.\u7535\u8111\u4e0a\u5b89\u88c5\u597d\u4e86docker\u548cdocker-compose<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u8c03\u6574nginx\u7684\u8bbf\u95ee\u65e5\u5fd7\u683c\u5f0f<\/h2>\n\n\n\n<p>\u7f16\u8f91\/etc\/nginx\/nginx.conf\uff0c\u5185\u5bb9\u53c2\u8003\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nload_module modules\/ngx_http_geoip2_module.so;\n...\nhttp {\n    include       \/etc\/nginx\/mime.types;\n    geoip2 \/home\/lzw\/GeoLite2-Country_20220222\/GeoLite2-Country.mmdb {\n        auto_reload 5m;\n        $geoip2_metadata_country_build metadata build_epoch;\n        $geoip2_data_country_code default=CN source=$remote_addr country iso_code;\n        $geoip2_data_country_name country names en;\n    }\n    geoip2 \/home\/lzw\/GeoLite2-City_20220222\/GeoLite2-City.mmdb {\n        $geoip2_data_city_name default=Nanjing city names en;\n \n    }\n    vhost_traffic_status_zone;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_country_code country::*;\n    log_format json_analytics escape=json '{'\n                            '\"msec\": \"$msec\", ' # request unixtime in seconds with a milliseconds resolution\n                            '\"connection\": \"$connection\", ' # connection serial number\n                            '\"connection_requests\": \"$connection_requests\", ' # number of requests made in connection\n                    '\"pid\": \"$pid\", ' # process pid\n                    '\"request_id\": \"$request_id\", ' # the unique request id\n                    '\"request_length\": \"$request_length\", ' # request length (including headers and body)\n                    '\"remote_addr\": \"$remote_addr\", ' # client IP\n                    '\"remote_user\": \"$remote_user\", ' # client HTTP username\n                    '\"remote_port\": \"$remote_port\", ' # client port\n                    '\"time_local\": \"$time_local\", '\n                    '\"time_iso8601\": \"$time_iso8601\", ' # local time in the ISO 8601 standard format\n                    '\"request\": \"$request\", ' # full path no arguments if the request\n                    '\"request_uri\": \"$request_uri\", ' # full path and arguments if the request\n                    '\"args\": \"$args\", ' # args\n                    '\"status\": \"$status\", ' # response status code\n                    '\"body_bytes_sent\": \"$body_bytes_sent\", ' # the number of body bytes exclude headers sent to a client\n                    '\"bytes_sent\": \"$bytes_sent\", ' # the number of bytes sent to a client\n                    '\"http_referer\": \"$http_referer\", ' # HTTP referer\n                    '\"http_user_agent\": \"$http_user_agent\", ' # user agent\n                    '\"http_x_forwarded_for\": \"$http_x_forwarded_for\", ' # http_x_forwarded_for\n                    '\"http_host\": \"$http_host\", ' # the request Host: header\n                    '\"server_name\": \"$server_name\", ' # the name of the vhost serving the request\n                    '\"request_time\": \"$request_time\", ' # request processing time in seconds with msec resolution\n                    '\"upstream\": \"$upstream_addr\", ' # upstream backend server for proxied requests\n                    '\"upstream_connect_time\": \"$upstream_connect_time\", ' # upstream handshake time incl. TLS\n                    '\"upstream_header_time\": \"$upstream_header_time\", ' # time spent receiving upstream headers\n                    '\"upstream_response_time\": \"$upstream_response_time\", ' # time spend receiving upstream body\n                    '\"upstream_response_length\": \"$upstream_response_length\", ' # upstream response length\n                    '\"upstream_cache_status\": \"$upstream_cache_status\", ' # cache HIT\/MISS where applicable\n                    '\"ssl_protocol\": \"$ssl_protocol\", ' # TLS protocol\n                    '\"ssl_cipher\": \"$ssl_cipher\", ' # TLS cipher\n                    '\"scheme\": \"$scheme\", ' # http or https\n                    '\"request_method\": \"$request_method\", ' # request method\n                    '\"server_protocol\": \"$server_protocol\", ' # request protocol, like HTTP\/1.1 or HTTP\/2.0\n                    '\"pipe\": \"$pipe\", ' # \"p\" if request was pipelined, \".\" otherwise\n                    '\"gzip_ratio\": \"$gzip_ratio\", '\n                    '\"http_cf_ray\": \"$http_cf_ray\",'\n                    '\"geoip_country_code\": \"$geoip2_data_country_code\"'\n                    '}';\n    access_log   \/var\/log\/nginx\/json_access.log json_analytics;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u5b89\u88c5loki+promtail+grafana<\/h2>\n\n\n\n<p>1.\u7f16\u5199docker-compose\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: \"3\"\n \nnetworks:\n  loki:\n \nservices:\n  loki:\n    image: grafana\/loki:2.4.1\n    ports:\n      - \"3100:3100\"\n    volumes:\n      - \/home\/lzw\/loki\/loki-conf:\/etc\/loki\n    command: -config.file=\/etc\/loki\/local-config.yaml\n    networks:\n      - loki\n \n  promtail:\n    image: grafana\/promtail:2.4.1\n    volumes:\n      - \/home\/lzw\/loki\/promtail-conf:\/etc\/promtail\n      - \/var\/log\/nginx:\/var\/log\/nginx\n    command: -config.file=\/etc\/promtail\/config.yml\n    networks:\n      - loki\n \n  grafana:\n    image: grafana\/grafana:latest\n    volumes:\n      - \/home\/lzw\/loki\/grafana:\/var\/lib\/grafana\n    ports:\n      - \"3000:3000\"\n    networks:\n      - loki<\/code><\/pre>\n\n\n\n<p>2,\u7f16\u5199loki-conf\/local-config.yaml \u914d\u7f6e\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>auth_enabled: false\n \nserver:\n  http_listen_port: 3100\n \ncommon:\n  path_prefix: \/loki\n  storage:\n    filesystem:\n      chunks_directory: \/loki\/chunks\n      rules_directory: \/loki\/rules\n  replication_factor: 1\n  ring:\n    instance_addr: 127.0.0.1\n    kvstore:\n      store: inmemory\n \nschema_config:\n  configs:\n    - from: 2020-10-24\n      store: boltdb-shipper\n      object_store: filesystem\n      schema: v11\n      index:\n        prefix: index_\n        period: 24h\n \nruler:\n  alertmanager_url: http:\/\/localhost:9093<\/code><\/pre>\n\n\n\n<p>3.\u7f16\u5199promtail-conf\/config.yml\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server:\n  http_listen_port: 9080\n  grpc_listen_port: 0\n \npositions:\n  filename: \/tmp\/positions.yaml\n \nclients:\n  - url: http:\/\/loki:3100\/loki\/api\/v1\/push\n \nscrape_configs:\n- job_name: nginx\n  static_configs:\n  - targets:\n      - localhost\n    labels:\n      job: nginx\n      agent: promtail\n      __path__: \/var\/log\/nginx\/json_access.log<\/code><\/pre>\n\n\n\n<p>4.\u51c6\u5907grafana\u6302\u8f7d\u76ee\u5f55<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d -p 3002:3000 --name=grafana2 grafana\/grafana:latest\ndocker cp grafana2:\/var\/lib\/grafana \/home\/lzw\/loki\/.\ndocker rm -f grafana2\nsudo chown -R 472 \/home\/lzw\/loki\/grafana<\/code><\/pre>\n\n\n\n<p>5.\u8fd0\u884cdocker-compose<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose -f docker-compose.yaml up -d\n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u5b8c\u6210\u540e\uff0c3000\u7aef\u53e3\u53ef\u4ee5\u8bbf\u95eegrafana\u30013100\u7aef\u53e3\u8bbf\u95eeloki\u3002nginx\u7684\u65e5\u5fd7\u6587\u4ef6\u901a\u8fc7volume\u7684\u65b9\u5f0f\u6302\u8f7d\u8fdbpromtail<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u5728grafana\u91cc\u914d\u7f6e\u62a5\u8868<\/h2>\n\n\n\n<p>1.\u914d\u7f6e\u6570\u636e\u6e90http:\/\/loki:3100<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"626\" src=\"https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-2.png\" alt=\"\" class=\"wp-image-1153\" srcset=\"https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-2.png 768w, https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-2-300x245.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p>2.\u5bfc\u5165\u5b98\u7f51\u6a21\u677fhttps:\/\/grafana.com\/grafana\/dashboards\/12559<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"604\" src=\"https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-3.png\" alt=\"\" class=\"wp-image-1154\" srcset=\"https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-3.png 943w, https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-3-300x192.png 300w, https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-3-768x492.png 768w\" sizes=\"auto, (max-width: 943px) 100vw, 943px\" \/><\/figure>\n\n\n\n<p>3.\u5bfc\u5165\u540e\u7684\u6548\u679c\u5e94\u8be5\u548c\u4e0b\u56fe\u7c7b\u4f3c<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"807\" src=\"https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-4-1024x807.png\" alt=\"\" class=\"wp-image-1155\" srcset=\"https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-4-1024x807.png 1024w, https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-4-300x237.png 300w, https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-4-768x606.png 768w, https:\/\/www.yinyubo.com\/wp-content\/uploads\/2022\/03\/image-4.png 1059w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>0.\u524d\u63d0 1.\u5df2\u7ecf\u9605\u8bfb\u8fc7\u8fd9\u7bc7\u6587\u7ae0https:\/\/www.yinyubo.com\/2022\/03\/14\/ngin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1151","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1151"}],"version-history":[{"count":4,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1151\/revisions"}],"predecessor-version":[{"id":1160,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1151\/revisions\/1160"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}