首页 > 其他分享 >ELK

ELK

时间:2024-08-01 15:38:50浏览次数:9  
标签:ELK data 192.168 elasticsearch root logstash localhost

ELK

ELK简介

什么是ELK

通俗来讲,ELK 是由 Elasticsearch、Logstash、Kibana 三个开源软件的组成的一个组合体,ELK 是 elastic 公司研发的一套完整的日志收集、分析和展示的企业级解决方案,在这三个软件当中,每个软件用于完成不同的功能,ELK 又称为ELK stack,官方域名为 elastic.co,ELK stack 的主要优点有如下几个:处理方式灵活: elasticsearch 是实时全文索引,具有强大的搜索功能配置相对简单:elasticsearch 的 API 全部使用 JSON 接口,logstash 使用模块配置,kibana 的配置文件部分更简单。检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿级数据的查询秒级响应。集群线性扩展:elasticsearch 和 logstash 都可以灵活线性扩展前端操作绚丽:kibana 的前端设计比较绚丽,而且操作简单

Elasticsearch

Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。主要负责将日志索引并存储起来,方便业务方检索查询。

Elasticsearch 使用 Java 语言开发,是建立在全文搜索引擎 Apache Lucene 基础之上的搜索引擎。

Elasticsearch 的特点:

  • 实时搜索、实时分析
  • 分布式架构、实时文件存储
  • 文档导向,所有对象都是文档
  • 高可用,易扩展,支持集群,分片与复制
  • 接口友好,支持 json

Logstash

Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。是一个日志收集、过滤、转发的中间件,主要负责将各条业务线的各类日志统一收集、过滤后,转发给 Elasticsearch 进行下一步处理。

Kibana

Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

ELK的工作原理

1.在所有需要收集日志的服务器上部署Logstash;或者先将日志进行集中化管理在日志服务器上,在日志服务器上部署 Logstash。
2.Logstash 收集日志,将日志格式化并输出到 Elasticsearch 群集中。
3.Elasticsearch 对格式化后的数据进行索引和存储。
4.Kibana 从 ES 群集中查询数据生成图表,并进行前端数据的展示。

ELK安装部署

前期准备

所以设备
[root@localhost ~]# systemctl disable  --now   firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install java
[root@localhost ~]# hostnamectl set-hostname node1/node2/apache
#更改设备名称

安装部署 Elasticsearch 软件

安装 Elasticsearch 软件

#在软件官网下载所需的安装包到/data
[root@localhost data]# ls
elasticsearch-6.7.2.rpm  elasticsearch-head-master.zip  node-v8.2.1.tar.gz  phantomjs-2.1.1-linux-x86_64.tar.bz2


#用rpm安装Elasticsearch软件
[root@localhost data]#rpm -ivh elasticsearch-6.7.2.rpm

[root@localhost data]# cd /etc/elasticsearch/
[root@localhost elasticsearch]# mkdir bak
[root@localhost elasticsearch]# cp -a *.yml  bak/
#备份要修改的文件,方便还原配置文件

修改配置文件

[root@node1 elasticsearch]# vim elasticsearch.yml
17 cluster.name: my-elk-cluster			#修改集群名字

23 node.name: node1						#设置节点名称主从之间不能一致 
24 node.master: true					#作为主节点
25 node.data: true						#作为数据节点

45 bootstrap.memory_lock: true			#内存锁开启  禁止使用  swap

59 network.host: 0.0.0.0				#监听地址
60 http.port: 9200						#默认使用端口
61 transport.tcp.port: 9300				#内部传输端口

73 discovery.zen.ping.unicast.hosts: ["192.168.10.10:9300", "192.168.10.20:9300"]					 #自动集群发现,加入主机名  使用单播 类似心跳线


#再次查看数据是否更改完成
[root@localhost elasticsearch]# grep -v "^#"  elasticsearch.yml 
cluster.name: my-elk-cluster
node.name: node1
node.master: true
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0 
http.port: 9200
transport.tcp.port: 9300
discovery.zen.ping.unicast.hosts: ["192.168.10.10:9300", "192.168.10.20:9300"]

修改系统配置

性能调优参数
[root@localhost elasticsearch]# vim /etc/security/limits.conf
......
#在最后添加
*  soft    nofile          65536
*  hard    nofile          65536
*  soft    nproc           32000
*  hard    nproc           32000
*  soft    memlock         unlimited
*  hard    memlock         unlimited
修改systemd 服务管理器

/etc/systemd/system.conf 文件是用于配置 systemd 的,这是一种用于 Linux 操作系统的系统和服务管理器。通过这个文件,你可以自定义与系统操作、性能和行为相关的各种设置

  • DefaultTimeoutStartSec=:设置启动服务的默认等待时间
  • DefaultTimeoutStopSec=:设置停止服务的默认等待时间
  • DefaultRestartSec=:设置在重新启动服务之前的默认休眠时间
  • DefaultLimitNOFILE=:设置打开文件数量的默认限制
  • DefaultLimitNPROC=:设置进程数量的默认限制
  • DefaultLimitCORE=:设置核心文件大小的默认限制
  • DefaultEnvironment=:指定服务的默认环境变量

实际修改

[root@localhost elasticsearch]# vim /etc/systemd/system.conf
DefaultLimitNOFILE=65536
DefaultLimitNPROC=32000
DefaultLimitMEMLOCK=infinity
修改内核参数
[root@localhost elasticsearch]# vim /etc/sysctl.conf
#一个进程可以拥有的最大内存映射区域数,参考数据(分配 2g/262144,4g/4194304,8g/8388608)
vm.max_map_count=262144

[root@localhost elasticsearch]# sysctl -p									#读取并应用这些配置更改,无需重新启动系统效。
[root@localhost elasticsearch]# sysctl -a | grep vm.max_map_count

重启服务器启动elasticsearch

[root@localhost elasticsearch]# reboot							#重启服务器
[root@localhost ~]# systemctl start elasticsearch.service		#启动elasticsearch
[root@localhost ~]# systemctl enable elasticsearch.service		#设置开机启动
[root@localhost ~]# ss -natp | grep 9200					#查看端口号

查看节点信息

浏览器访问  
http://192.168.10.10:9200  
http://192.168.10.20:9200 
查看节点 Node1、Node2 的信息。
![](/i/l/?n=24&i=blog/3405553/202408/3405553-20240801135325938-765153284.png)



浏览器访问 
http://192.168.10.10:9200/_cluster/health?pretty  
http://192.168.10.20:9200/_cluster/health?pretty
查看群集的健康情况,可以看到 status 值为 green(绿色), 表示节点健康运行。


浏览器访问 http://192.168.10.10:9200/_cluster/state?pretty  检查群集状态信息

编译安装 Elasticsearch-head 插件 主从都可以安装

Elasticsearch 在 5.0 版本后,Elasticsearch-head 插件需要作为独立服务进行安装,需要使用npm工具(NodeJS的包管理工具)安装。
安装 Elasticsearch-head 需要提前安装好依赖软件 node phantomjs
node:是一个基于 Chrome V8 引擎的 JavaScript 运行环境。
phantomjs:是一个基于 webkit 的JavaScriptAPI,可以理解为一个隐形的浏览器,任何基于 webkit 浏览器做的事情,它都可以做到。

编译安装node组件

[root@localhost ~]# yum install gcc gcc-c++ make -y
#下载安装和解压环境
[root@localhost ~]# cd /data/
[root@localhost data]# tar zxvf node-v8.2.1.tar.gz
[root@localhost data]# cd node-v8.2.1/
[root@localhost node-v8.2.1]# ./configure
[root@localhost node-v8.2.1]# make && make install

安装 phantomjs

[root@localhost node-v8.2.1]# cd /data
[root@localhost data]# tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@localhost data]# cd /data/phantomjs-2.1.1-linux-x86_64/bin
[root@localhost bin]# ln -s  /opt/phantomjs-2.1.1-linux-x86_64/bin/phantomjs   /usr/bin

安装 Elasticsearch-head 数据可视化工具

[root@localhost bin]# cd /data
[root@localhost data]# unzip elasticsearch-head-master.zip
[root@localhost data]# cd /data/elasticsearch-head-master/
[root@localhost elasticsearch-head-master]#npm install		 #安装依赖包
[root@localhost elasticsearch-head-master]#npm install --registry=https://registry.npmmirror.com    				  #指定源

修改 Elasticsearch 主配置文件

[root@localhost elasticsearch-head-master]# vim /etc/elasticsearch/elasticsearch.yml
......
--末尾添加以下内容--
http.cors.enabled: true					#开启跨域访问支持,默认为 false
http.cors.allow-origin: "*"				#指定跨域访问允许的域名地址为所有


[root@localhost elasticsearch-head-master]# systemctl restart elasticsearch
#重启elasticsearch  服务

启动 elasticsearch-head 服务

必须在解压后的 elasticsearch-head 目录下启动服务,进程会读取该目录下的 gruntfile.js 文件,否则可能启动失败。

[root@localhost elasticsearch-head-master]#cd /data/elasticsearch-head-master/
[root@localhost elasticsearch-head-master]# npm run start &
> [email protected] start /usr/local/src/elasticsearch-head
> grunt server

Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

#elasticsearch-head 监听的端口是 9100
[root@localhost elasticsearch-head-master]# ss -natp |grep 9100
#检测9100 端口是否开启

测试

网页访问:192.168.10.10:9100 或 192.168.10.20:9100

插入索引测试
[root@node2 elasticsearch-head-master]# curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

在应用服务器部署 Logstash

安装服务

[root@apache ~]# yum -y install java
[root@apache ~]# java -version
[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl start httpd

安装logstash

[root@apache ~]# cd /data
[root@apache data]# ls
kibana-6.7.2-x86_64.rpm  logstash-6.7.2.rpm
[root@apache data]# rpm -ivh logstash-6.7.2.rpm
#安装logstash
[root@apache data]# systemctl enable --now logstash.service
#开启logstash.service
[root@apache data]# ln -s  /usr/share/logstash/bin/logstash   /usr/bin/
# 做软连接

使用logstash 收集日志

Logstash 命令常用选项:
-f:通过这个选项可以指定 Logstash 的配置文件,根据配置文件配置 Logstash 的输入和输出流。
-e:从命令行中获取,输入、输出后面跟着字符串,该字符串可以被当作 Logstash 的配置(如果是空,则默认使用 stdin 作为输入,stdout 作为输出)。
-t:测试配置文件是否正确,然后退出。

[root@localhost opt]# logstash -e 'input { stdin{} } output { stdout{} }'
# 等待时间较长
WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults
Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console
[INFO ] 2024-08-01 15:29:30.012 [main] writabledirectory - Creating directory {:setting=>"path.queue", :path=>"/usr/share/logstash/data/queue"}
[INFO ] 2024-08-01 15:29:30.031 [main] writabledirectory - Creating directory {:setting=>"path.dead_letter_queue", :path=>"/usr/share/logstash/data/dead_letter_queue"}
[WARN ] 2024-08-01 15:29:30.392 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
[INFO ] 2024-08-01 15:29:30.399 [LogStash::Runner] runner - Starting Logstash {"logstash.version"=>"6.7.2"}
[INFO ] 2024-08-01 15:29:30.425 [LogStash::Runner] agent - No persistent UUID file found. Generating new UUID {:uuid=>"a499dafc-ae55-41da-b12d-36425108a95e", :path=>"/usr/share/logstash/data/uuid"}
[INFO ] 2024-08-01 15:29:35.985 [Converge PipelineAction::Create<main>] pipeline - Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>2, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
[INFO ] 2024-08-01 15:29:36.390 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x3a2c2f9e run>"}
The stdin plugin is now waiting for input:
[INFO ] 2024-08-01 15:29:36.458 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-08-01 15:29:36.734 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}


# 此处输入需要的信息
hello world
#############
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
{
       "message" => "hello world",
          "host" => "localhost.localdomain",
      "@version" => "1",
    "@timestamp" => 2024-07-15T16:42:54.195Z
}

使用 Logstash 将信息写入 Elasticsearch 中

logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.10.10:9200","192.168.10.20:9200"]} }'
//结果不在标准输出显示,而是发送至 Elasticsearch 中,可浏览器访问 http://192.168.10.10:9100/或http://192.168.10.20:9100 查看索引信息和数据浏览。

使用配置文件

Logstash 配置文件基本由三部分组成:input、output 以及 filter(可选,根据需要选择使用)

  • input:表示从数据源采集数据,常见的数据源如Kafka、日志文件等
    file beats kafka redis stdin

  • filter:表示数据处理层,包括对数据进行格式化处理、数据类型转换、数据过滤等,支持正则表达式
    grok 对若干个大文本字段进行再分割成一些小字段 (?<字段名>正则表达式) 字段名: 正则表达式匹配到的内容
    date 对数据中的时间格式进行统一和格式化
    mutate 对一些无用的字段进行剔除,或增加字段
    mutiline 对多行数据进行统一编排,多行合并或拆分

  • output:表示将Logstash收集的数据经由过滤器处理之后输出到Elasticsearch。
    elasticsearch stdout

#格式如下:
input {...}
filter {...}
output {...}

#在每个部分中,也可以指定多个访问方式。例如,若要指定两个日志来源文件,则格式如下:
input {
	file { path =>"/var/log/messages"   type =>"syslog"}
	file { path =>"/var/log/httpd/access.log" type =>"apache"}
	
vim system.conf
input {
    file{
        path =>"/var/log/messages"
        type =>"system"
        start_position =>"beginning"
		# ignore_older => 604800
        sincedb_path => "/etc/logstash/sincedb_path/log_progress"
        add_field => {"log_hostname"=>"${HOSTNAME}"}
    }
}
#path表示要收集的日志的文件位置
#type是输入ES时给结果增加一个叫type的属性字段
#start_position可以设置为beginning或者end,beginning表示从头开始读取文件,end表示读取最新的,这个要和ignore_older一起使用
#ignore_older表示了针对多久的文件进行监控,默认一天,单位为秒,可以自己定制,比如默认只读取一天内被修改的文件
#sincedb_path表示文件读取进度的记录,每行表示一个文件,每行有两个数字,第一个表示文件的inode,第二个表示文件读取到的位置(byteoffset)。默认为$HOME/.sincedb*
#add_field增加属性。这里使用了${HOSTNAME},即本机的环境变量,如果要使用本机的环境变量,那么需要在启动命令上加--alow-env

output {
    elasticsearch {												#输出到 elasticsearch
        hosts => ["192.168.91.100:9200","192.168.91.101:9200"]	#指定 elasticsearch 服务器的地址和端口
        index =>"system-%{+YYYY.MM.dd}"							#指定输出到 elasticsearch 的索引格式
    }
}

实列
[root@apache log]# vim /etc/logstash/conf.d/system-log.conf 

input {
    file {
      path => "/var/log/messages"
      type => "system"
      start_position => "beginning"
    }

    file {
      path => "/var/log/yum.log"
      type => "yum"
      start_position => "beginning"
    }

}

output {
  if  type]
  elasticsearch {
  hosts => [ "192.168.91.100:9200","192.168.91.101:9200" ]
  index => "system-%{+YYYY.MM.dd}"
  }

}



[root@apache conf.d]# chmod +r /var/log/messages 
#添加权限


[root@apache conf.d]# logstash   -f  system-log.conf 
#启动logstash
........................................................................
[INFO ] 2024-07-16 01:02:41.716 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9601}

安装 kibana 无所谓哪台服务器

安装

[root@apache log]# cd /data/
[root@apache data]# rpm -ivh kibana-6.7.2-x86_64.rpm

修改配置

[root@apache data]# cd /etc/kibana/
[root@localhost kibana]# cp kibana.yml  kibana.yml.bak -a
# 做好主要配置文件的备份
[root@apache kibana]# vim kibana.yml
# 修改以下行
2    server.port: 5601          		#打开端口
7    server.host: "0.0.0.0"      	#监听端口
28   elasticsearch.hosts: ["http://192.168.91.100:9200", "http://192.168.91.101:9200"]  #el服务器地址
37   kibana.index: ".kibana"         #打开索引
96   logging.dest: /var/log/k.log      #指定日志文件, 需要手动建立文件
114  i18n.locale: "zh-CN"              #中文设置

[root@apache kibana]# chown kibana:kibana /var/log/k.log

启动 kibana

[root@apache kibana]# systemctl enable --now kibana.service
[root@apache kibana]# ss -nap |grep 5601
tcp    LISTEN     0      128       *:5601                  *:*                   users:(("node",pid=42235,fd=19))

访问测试

192.168.10.30:5601

标签:ELK,data,192.168,elasticsearch,root,logstash,localhost
From: https://www.cnblogs.com/zhj0708/p/18336510

相关文章

  • docker-compose搭建elk
    一、准备检查自己的docker和docker-compose是否安装完毕,切换docker的镜像源 二、安装本次安装的主要组件包括es、filebeat、kibana、logstash   2.1先配置组件的挂载点                       ......
  • Java中的应用监控与日志分析:ELK Stack
    Java中的应用监控与日志分析:ELKStack大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!今天我们来讨论如何使用ELKStack(Elasticsearch,Logstash,Kibana)进行Java应用的监控与日志分析。ELKStack是目前非常流行的一种解决方案,能够帮助开发者轻松地收集、......
  • Elasticsearch概念及ELK安装
    1、Elasticsearch是什么它是elastic技术栈中的一部分。完整的技术栈包括:Elasticsearch:用于数据存储、计算和搜索Logstash/Beats:用于数据收集Kibana:用于数据可视化整套技术栈被称为ELK,经常用来做日志收集、系统监控和状态分析等2、Elasticearch的安装步骤拉取镜像命......
  • ElasticSearch第1讲(4万字详解 Linux下安装、原生调用、API调用超全总结、Painless、IK
    ElasticSearch官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html非官方中文文档:https://learnku.com/docs/elasticsearch73/7.3极简概括:基于ApacheLucene构建开源的分布式搜索引擎。解决问题:MySQLlike中文全文搜索不走索引......
  • ELK Stack入门之部署EFK架构
    前言:日志分析对于现代IT系统来说至关重要,它可以帮助组织机构理解和优化其业务和技术基础设施。以下是日志分析的一些重要性方面:问题诊断与故障排除:当系统发生故障或出现异常时,通过对相关日志进行分析,可以找到问题的根源,进行准确的定位。日志数据提供了详细的上下文信息,......
  • ELK详细搭建流程
    文章目录一、ELk定义及特点二、Elasticsearch安装部署1.下载地址2.解压到指定目录。3.修改配置文件4.启动elasticsearch注意:1.临时修改 vm.max_map_count(重启后失效)2.永久修改 vm.max_map_count(重启后依然有效)5.成功重启后访问http:ip:9200端口如表示如下,及......
  • ELK用途及定义
    文章目录一、ELK的组成1.Elasticsearch2.Logstash3.Kibana二、ELK的应用场景三、ELK的扩展与优势小结ELK是Elasticsearch、Logstash、Kibana三个开源软件的缩写,它们都是开源软件,通常配合使用,并且都先后归于Elastic.co企业名下,故被简称为ELK协议栈。一、EL......
  • 在K8S中,ELK是如何实现及如何优化的ES?
    ELK栈(Elasticsearch、Logstash、Kibana)在Kubernetes(K8S)环境中是用于日志收集、分析和可视化的强大工具组合。其中,Elasticsearch作为核心存储和搜索引擎,承担着存储大量日志数据和提供高效搜索的能力。以下是如何在K8S中实现及优化Elasticsearch的详细说明:1.实现Elasticsearchin......
  • ELK日志分析系统
    一、ELK介绍ELK平台是一套完整的日志集中处理解决方案,将ElasticSearch、Logstash和Kiabana三个开源工具配合使用,完成更强大的用户对日志的查询、排序、统计需求。1.ElasticsearchElasticsearch是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索。搜索......
  • ES快速开发,ElasticsearchRestTemplate基本使用以及ELK快速部署
    最近博主有一些elasticsearch的工作,所以更新的慢了些,现在就教大家快速入门,并对一些基本的查询、更新需求做一下示例,废话不多说开始:1. ES快速上手es下载:[https://elasticsearch.cn/download/]()这里关于es所需要的链接基本都有,可以快速下载使用当你解压好了归档文件之后,Elas......