基于docker的ELK环境搭建

  • Post author:
  • Post category:其他




elk介绍

  • elk是elastic公司提供的一套完整的日志收集和展示的解决方案,它是由三个产品的首字母缩写而成,分别是elasticSearch、logstash、kibana

    • elasticSearch: 简称 Es,它是一个分布式搜索引擎,可用于全文搜索、结构化搜索以及分析。它是一个建立在全文搜索引擎(Apache Lucene)基础上的一个搜索引擎。使用java语言编写
    • logstash:一个具有实时传输的数据收集引擎,用来进行数据收集(例如:读取文本文件)、解析、并将数据发送给es
    • Kibana:为es提供分析和可视化的web平台,它可以在es的索引中查找、交互数据、并生成各种维度的表格图形



elk 用途

传统意义上,Elk是作为代替splunk的一个开源解决方案。Splunk是日志分析领域的领导者。日志分析不仅包括系统产生的错误日志、异常,也包括业务逻辑、任何文本类的分析,而基于日志分的分析,能够产生非常多的解决方案;比如:

  1. 问题排查:我们常说,运维和开发这一辈子无非就是和问题在战斗,所以这个说起来很朴实的四个字,其实是沉甸甸的。很多公司其实不缺钱,就要稳定,而要稳定,就要运维和开发能够快速的定位问题,甚至防微杜渐,把问题杀死在摇篮里。日志分析技术显然问题排查的基石。基于日志做问题排查,还有一个很帅的技术,叫全链路追踪,比如阿里的eagleeye 或者Google的dapper,也算是日志分析技术里的一种
  2. 监控和预警:日志,监控,预警是相辅相成的。基于日志的监控,预警使得运维有自己的机械战队,大大节省人力以及延长运维的寿命
  3. 关联事件:多个数据源产生的日志进行联动分析,通过某种分析算法,就能够解决生活中各个问题。比如金融里的风险欺诈等。这个可以可以应用到无数领域了,取决于你的想象力
  4. 数据分析:这个对于数据分析师,还有算法工程师都是有所裨益的

在这里插入图片描述



安装脚本下载

  • docker Registry Mirrors推荐

    {
      "registry-mirrors": ["https://e2iytqf0.mirror.aliyuncs.com/"]
    }
    

    下载链接: https://pan.baidu.com/s/1duGzCPdiuBO6z9r8iGaq5w 提取码: p5yr

  • 执行安装

chmod a+rwx run
./run
  • 设置es密码(此命令后会初始化es所有账户的密码)

    内置用户
docker exec -it es elasticsearch-setup-passwords interactive
#执行结果:密码:root123456
[root@cd36 env]# docker exec -it es elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]: 
Reenter password for [elastic]: 
Enter password for [apm_system]: 
Reenter password for [apm_system]: 
Enter password for [kibana_system]: 
Reenter password for [kibana_system]: 
Enter password for [logstash_system]: 
Reenter password for [logstash_system]: 
Enter password for [beats_system]: 
Reenter password for [beats_system]: 
Enter password for [remote_monitoring_user]: 
Reenter password for [remote_monitoring_user]: 
Changed password for user [apm_system]
Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
  • es配置文件添加[下载的文件里已配置;无需操作]
xpack.security.enabled: true
  • 配置kibana访问连接
#xpack配置参考:https://www.elastic.co/guide/en/kibana/7.9/security-settings-kb.html
#kibana配置参考:https://www.elastic.co/guide/en/kibana/7.9/settings.html
server.name: kibana
server.host: "0.0.0.0"
elasticsearch.hosts: [ "http://es:9200" ]
i18n.locale: zh-CN
#elasticsearch.username: "kibana_system"
#elasticsearch.password: "root123456"
#
#
#
#安全配置:xpack.security.encryptionKey在kibana.yml 配置文件中设置属性。您可以使用32个字符或更长的任何文本字符串作为加密密钥
#参考地址:https://www.elastic.co/guide/en/kibana/7.9/using-kibana-with-security.html
xpack.security.encryptionKey: "8884aea3bdfb65e3083a9280f4758086"
#
#
#报告配置:要设置用于报告的静态加密密钥,请xpack.reporting.encryptionKey在kibana.yml 配置文件中设置 属性。您可以使用任何至少32个字符的字母数字文本字符串作为加密密钥。
#参考地址:https://www.elastic.co/guide/en/kibana/7.9/configuring-reporting.html
xpack.reporting.encryptionKey: "8884aea3bdfb65e3083a9280f4758086"
#
#参考地址:https://www.elastic.co/guide/en/security/7.9/detections-permissions-section.html
xpack.encryptedSavedObjects.encryptionKey: "8884aea3bdfb65e3083a9280f4758086"
  • 添加密码到Kibana密钥库
docker exec -it kibana /usr/share/kibana/bin/kibana-keystore create
docker exec -it kibana /usr/share/kibana/bin/kibana-keystore add elasticsearch.username
docker exec -it kibana /usr/share/kibana/bin/kibana-keystore add elasticsearch.password
  • 访问kibana
http://192.168.10.191:5601
账户:elastic
密码:你自己密码
  • 访问es
http://192.168.10.191:9200
账户:elastic
密码:你自己密码
  • 创建fluentd角色

  • 创建fluentd账户



数据测试

  • 启动一个测试容器向服务器发送消息;注意修改fluentd_client.conf配置的接收地址
docker run --rm -itd -u fluent -v $(pwd)/test/fluentd_client.conf:/fluentd/etc/fluent.conf -v $(pwd)/test/json.log:/fluentd/log/json.log  --name fluent_test elk_fluentd
  • 执行生产数据命令;向测试容器【fluent_test】监听的json文件发送消息
php ./test/generate.php >> json.log



elasticSearch调试

#获取所有角色
http://192.168.10.191:9200/_security/role
#获取所有其中一个角色
http://192.168.10.191:9200/_security/role/fluentd
#获取所有用户
http://192.168.10.191:9200/_security/user
#获取所有其中一个用户
http://192.168.10.191:9200/_security/user/fluentd



安装问题

  • 问题1 提示最大虚拟内存太低
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least

解决方案:执行命令: sysctl -w vm.max_map_count=262144

  • 问题2 由于以下原因,kibana启动无法从Elasticsearch获取许可证信息:原因kibana未配置连接账户
 {"type":"log","@timestamp":"2020-08-13T01:57:03Z","tags":["warning","plugins","licensing"],"pid":6,"message":"License information could not be obtained from Elasticsearch due to [security_exception] missing authentication credentials for REST request [/_xpack], with { header={ WWW-Authenticate=\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\" } } :: {\"path\":\"/_xpack\",\"statusCode\":401,\"response\":\"{\\\"error\\\":{\\\"root_cause\\\":[{\\\"type\\\":\\\"security_exception\\\",\\\"reason\\\":\\\"missing authentication credentials for REST request [/_xpack]\\\",\\\"header\\\":{\\\"WWW-Authenticate\\\":\\\"Basic realm=\\\\\\\"security\\\\\\\" charset=\\\\\\\"UTF-8\\\\\\\"\\\"}}],\\\"type\\\":\\\"security_exception\\\",\\\"reason\\\":\\\"missing authentication credentials for REST request [/_xpack]\\\",\\\"header\\\":{\\\"WWW-Authenticate\\\":\\\"Basic realm=\\\\\\\"security\\\\\\\" charset=\\\\\\\"UTF-8\\\\\\\"\\\"}},\\\"status\\\":401}\",\"wwwAuthenticateDirective\":\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\"} error"}

解决方案:在kibana配置账户信息

  • 问题3 fluentd 启动时报403权限不允许
2020-09-14 08:37:11 +0000 [warn]: #0 failed to flush the buffer. retry_time=1 next_retry_seconds=2020-09-14 08:37:12 +0000 chunk="5af41f0aa1db3bef7795533234cac3ff" error_class=Fluent::Plugin::ElasticsearchOutput::RecoverableRequestFailure error="could not push logs to Elasticsearch cluster ({:host=>\"es\", :port=>9200, :scheme=>\"http\", :user=>\"fluentd\", :password=>\"obfuscated\"}): [403] {\"error\":{\"root_cause\":[{\"type\":\"security_exception\",\"reason\":\"action [indices:data/write/bulk] is unauthorized for user [fluentd]\"}],\"type\":\"security_exception\",\"reason\":\"action [indices:data/write/bulk] is unauthorized for user [fluentd]\"},\"status\":403}"

解决方案:创建的fluentd角色权限不对;把索引权限设置为*,操作权限为all

  • 问题4:查看fluentd日志一直抛这个警告;翻译:不推荐在批量请求中指定类型
warning: 299 Elasticsearch-7.8.1-b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89 "[types removal] Specifying types in bulk requests is deprecated."

解决方案:根据官网的教程下载的fluent-plugin-elasticsearch是3.5.2;将插件升级到4.1.1然再添加配置

suppress_type_name true



kibana使用



流文件传输配置

  • 添加索引模式 ;位置:Stack Management ==》索引模式



  • 配置流式传输



设置人员权限





  • 登录后发现页面只有分配的权限



版权声明:本文为qq_36738861原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。