{"id":1658,"date":"2022-05-04T22:13:54","date_gmt":"2022-05-04T14:13:54","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=1658"},"modified":"2022-05-07T23:22:41","modified_gmt":"2022-05-07T15:22:41","slug":"7-elk%e5%bc%95%e5%85%a5redis%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%e8%a7%a3%e8%80%a6","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=1658","title":{"rendered":"7. ELK\u5f15\u5165Redis\u6d88\u606f\u961f\u5217\u89e3\u8026"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.ELK\u5f15\u5165Redis\u4ecb\u7ecd<\/h2>\n\n\n\n<p><strong><em>1.EFLK\u67b6\u6784\u9762\u4e34\u95ee\u9898\u4e00\uff1a\u8026\u5408\u5ea6\u592a\u9ad8<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u6bd4\u5982\u76ee\u524d\u7cfb\u7edf\u65e5\u5fd7\u8f93\u51fa\u5f88\u9891\u7e41\uff0c\u5341\u5206\u949f\u5927\u7ea65G\uff0c\u90a3\u4e48\u4e00\u4e2a\u5c0f\u65f6\u5c31\u662f50G\u3002\u800c\u5e94\u7528\u670d\u52a1\u5668\u7684\u5b58\u50a8\u7a7a\u95f4\u4e00\u822c\u9ed8\u8ba440Gb\uff0c\u6240\u4ee5\u901a\u5e38\u4f1a\u5bf9\u5e94\u7528\u670d\u52a1\u5668\u65e5\u5fd7\u6309\u5c0f\u65f6\u8f6e\u8f6c\u3002\u5982\u679c\u6211\u4eec\u7684Logstash\u6545\u969c\u4e861\u5c0f\u65f6\uff0c\u90a3\u4e48Filebeat\u5c31\u65e0\u6cd5\u5411Logstash\u53d1\u9001\u65e5\u5fd7\uff0c\u4f46\u6211\u4eec\u7684\u5e94\u7528\u670d\u52a1\u5668\u6bcf\u5c0f\u65f6\u4f1a\u5bf9\u65e5\u5fd7\u8fdb\u884c\u5207\u5272\uff0c\u90a3\u4e48\u4e5f\u5c31\u610f\u5473\u7740\u6211\u4eec\u4f1a\u4e22\u59311\u5c0f\u65f6\u7684\u65e5\u5fd7\u6570\u636e\u3002\r\n\u89e3\u51b3\u529e\u6cd5\uff1a\u4f7f\u7528\u961f\u5217\uff0c\u53ea\u8981\u4f60\u7684filebeat\u80fd\u591f\u6536\u96c6\u65e5\u5fd7\uff0c\u961f\u5217\u80fd\u591f\u5b58\u50a8\u8db3\u591f\u957f\u65f6\u95f4\u7684\u6570\u636e\uff0c\u90a3\u540e\u9762logstash\u6216Elasticsearch\u6545\u969c\u4e86\uff0c\u4e5f\u4e0d\u7528\u62c5\u5fc3\uff0c\u4fee\u590d\u540e\uff0c\u65e5\u5fd7\u4f9d\u7136\u80fd\u6b63\u5e38\u5199\u5165\uff0c\u4e5f\u4e0d\u4f1a\u9020\u6210\u6570\u636e\u4e22\u5931\uff0c\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u89e3\u8026\u3002\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.\u6b64\u524d\u67b6\u6784\u9762\u4e34\u95ee\u9898\u4e8c\uff1a\u6027\u80fd\u74f6\u9888<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u4f7f\u7528filebeat\u6216logstash\u76f4\u63a5\u5199\u5165ES\uff0c\u90a3\u4e48\u65e5\u5fd7\u9891\u7e41\u7684\u88ab\u5199\u5165ES\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u9020\u6210ES\u51fa\u73b0\u8d85\u65f6\u3001\u4e22\u5931\u7b49\u60c5\u51b5\u3002\u56e0\u4e3aES\u9700\u8981\u5904\u7406\u6570\u636e\uff0c\u6027\u80fd\u4f1a\u53d8\u7f13\u3002\r\n\r\n\u89e3\u51b3\u529e\u6cd5\uff1a\u4f7f\u7528\u6d88\u606f\u961f\u5217\uff0cfilebeat\u6216Logstash\u76f4\u63a5\u5199\u5165\u6d88\u606f\u961f\u5217\u4e2d\u5c31\u53ef\u4ee5\u4e86\uff0c\u56e0\u4e3a\u961f\u5217\u53ef\u4ee5\u8d77\u5230\u4e00\u4e2a\u7f13\u51b2\u4f5c\u7528\uff0c\u6700\u540e\u6211\u4eec\u7684logstash\u6839\u672c\u81ea\u5df1\u7684\u80fd\u529b\u8fdb\u884c\u6570\u636e\u6d88\u8d39\u548c\u5904\u7406\uff0c\u7136\u540e\u5199\u5165ES\uff0c\u8fd9\u6837\u80fd\u6709\u6548\u7f13\u89e3ES\u5199\u5165\u6027\u80fd\u7684\u74f6\u9888\u3002\r<\/code><\/pre>\n\n\n\n<p><strong><em>3.redis\u961f\u5217\u7684\u4f18\u52bf\u53ca\u81f4\u547d\u7f3a\u70b9<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u4f18\u70b9<\/strong>\n\u5b9e\u73b0\u6700\u7b80\u5355\n<strong>\u7f3a\u70b9<\/strong>\n1.filebeat\u4e0d\u652f\u6301\u5199\u5165redis\u96c6\u7fa4\u3002\r\n2.redis\u662f\u9700\u8981\u5185\u5b58\u652f\u6301\uff0c\u5927\u91cf\u7684\u6570\u636e\u5199\u5165redis\u9700\u8981\u8db3\u591f\u5927\u7684\u5185\u5b58\uff0c\u6210\u672c\u8f83\u9ad8\u3002\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.ELK\u5bf9\u63a5Redis\u5b9e\u64cd<\/h2>\n\n\n\n<p><strong><em>1.\u5b89\u88c5\u5e76\u914d\u7f6eredis<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@es-node2 ~]# yum -y install redis \r\n&#91;root@es-node2 ~]# grep \"^&#91;a-Z]\" \/etc\/redis.conf\r\nbind 10.0.0.151\r\nrequirepass 123456\r\n....\r\n&#91;root@es-node2 ~]# systemctl restart redis \r\n&#91;root@es-node2 ~]# systemctl enable redis\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.\u4fee\u6539filebeat\u8f93\u51fa\u6570\u636e\u81f3Redis<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@es-node3 ~]# cat \/etc\/filebeat\/filebeat.yml\r\nfilebeat.inputs:\r\n- type: log\r\n  enabled: true\r\n  paths:\r\n    - \/var\/log\/access.log\r\n  tags: &#91;\"access\"]\r\n\r\n- type: log\r\n  enabled: true\r\n  paths:\r\n    - \/var\/log\/error.log\r\n  tags: &#91;\"error\"]\r\n\r\noutput.redis:\r\n  hosts: &#91;\"10.0.0.141:6379\"]    #redis\u5730\u5740\r\n  password: 123456              #redis\u5bc6\u7801\r\n  timeout: 5                    #\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\r\n  db: 0                         #\u5199\u5165db0\u5e93\u4e2d\r\n  keys:                         #\u5b58\u50a8\u7684key\u540d\u79f0\r\n    - key: \"nginx_access\"\r\n      when.contains:\r\n        tags: \"access\"\r\n    - key: \"nginx_error\"\r\n      when.contains:\r\n        tags: \"error\"\r\n&#91;root@es-node3 ~]# systemctl restart filebeat.service\r\n&#91;root@es-node3 ~]# head access.log > \/var\/log\/nginx.log\r<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"290\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-49.png\" alt=\"\" class=\"wp-image-1661\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-49.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-49-300x101.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-49-768x257.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><strong><em>3.\u4fee\u6539logstash\u4eceRedis\u4e2d\u8bfb\u53d6\u6570\u636e<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@es-node3 ~]# cat \/etc\/logstash\/conf.d\/redis_nginx_es.conf \r\ninput {\r\n    redis {\r\n        host => &#91;\"10.0.0.151\"]\r\n        port => \"6379\"\r\n        password => \"123456\"\r\n        data_type => \"list\"\r\n        key => \"nginx_access\"\r\n        db => \"0\"\r\n    }\r\n\r\n    redis {\r\n        host => &#91;\"10.0.0.151\"]\r\n        port => \"6379\"\r\n        password => \"123456\"\r\n        data_type => \"list\"\r\n        key => \"nginx_error\"\r\n        db => \"0\"\r\n    }\r\n}\r\n\r\nfilter {\r\n\tif \"access\" in &#91;tags]&#91;0] {\r\n\t\tgrok {\r\n\t\t\tpatterns_dir => \"\/usr\/local\/logstash\/patterns\"\r\n\t\t\tmatch => {\r\n\t\t\t\t\"message\" => \"%{NGINXACCESS}\"\r\n\t\t\t}\r\n\t\t}\r\n\t\tuseragent {\r\n\t\t\tsource => \"user_agent\"\r\n\t\t\ttarget => \"agent\"\r\n\t\t}\r\n\t\tgeoip {\r\n\t\t\tsource => \"http_x_forwarded_for\"\r\n\t\t\tfields => &#91;\"country_name\",\"country_code2\",\"timezone\",\"longitude\",\"latitude\",\"continent_code\"]\r\n\t\t}\r\n\t\tdate {\r\n\t\t\t# 09\/Nov\/2020:08:51:50 +0800\r\n\t\t\tmatch => &#91;\"log_timestamp\", \"dd\/MMM\/yyyy:HH:mm:ss Z\" ]\r\n\t\t\ttarget => \"@timestamp\"\r\n\t\t\ttimezone => \"Asia\/Shanghai\"\r\n\t\t}\r\n\t\tmutate {\r\n\t\t\tconvert => &#91; \"body_bytes_sent\", \"integer\" ]\r\n\t\t\tadd_field => { \"target_index\" => \"logstash-nginx-access-%{+YYYY.MM.dd}\" }\r\n\t\t}\r\n    } else if \"error\" in &#91;tags]&#91;0] {\r\n\r\n        date {\r\n                match => &#91;\"timestamp\",\"dd\/MMM\/yyyy:HH:mm:ss Z\"]\r\n                target => \"@timestamp\"\r\n                timezone => \"Asia\/Shanghai\"\r\n        }\r\n\r\n\r\n\r\n\t\tmutate {\r\n\t\t\tadd_field => { \"target_index\" => \"logstash-nginx-error-%{+YYYY.MM.dd}\" }\r\n\t\t}\r\n\t}\r\n\r\n}\r\n\r\noutput {\r\n    elasticsearch {\r\n        hosts => &#91;\"10.0.0.151:9200\",\"10.0.0.152:9200\",\"10.0.0.150:9200\"]\r\n        index => \"%{&#91;target_index]}\"\r\n        template_overwrite => true\r\n\tuser => \"elastic\"\r\n\tpassword => \"iEj4tsQy7iFrdXdJjNY6\"\r\n    }\r\n\r\n    stdout {\r\n        codec => rubydebug\r\n    }\r\n}\r\nLogstash\u542f\u52a8\u540e\uff0c\u53d1\u73b0\u6d88\u606f\u5df2\u7ecf\u6d88\u8d39\u5b8c\u4e86\r<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"319\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-50.png\" alt=\"\" class=\"wp-image-1662\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-50.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-50-300x111.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/05\/image-50-768x283.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1658"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1658"}],"version-history":[{"count":2,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions"}],"predecessor-version":[{"id":1663,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1658\/revisions\/1663"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}