ELK基本部署

时间:2022-07-24
本文章向大家介绍ELK基本部署,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Elasticsearch

elasticsearch是一个高度可扩展全文搜索和分析引擎,基于Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作,可以处理大规模日志数据,比如Nginx、Tomcat、系统日志等功能。

Logstash 数据收集引擎。它支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置;支持普通log、自定义json格式的日志解析。

Kibana 数据分析和可视化平台。通常与 Elasticsearch 配合使用,对其中数据进行搜索、分析和以统计图表的方式展示。

ELK部署环境准备

两台机器关闭防火墙和selinux

systemctl stop firewalld
setenforce 0

编辑/etc/hosts文件

node1:
[root@node1 /]# vim /etc/hosts
172.16.0.3 node1
172.16.0.12 node2

node2:
[root@node2 /]# vim /etc/hosts
172.16.0.3 node1
172.16.0.12 node2

安装Elasticsearch

因为elasticsearch服务运行需要java环境,所以首先安装jdk

[root@node1 /]#yum localinstall jdk-8u211-linux-x64.rpm -y
[root@nodel /]#java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

将下载的elasticsearch包上传到服务器进行安装

[root@node1 /]# yum -y localinstall elasticsearch-6.8.1.rpm
[root@node2 /]# yum -y localinstall elasticsearch-6.8.1.rpm

配置elasticsearch,node2配置一个相同的节点,通过组播进行通信

[root@node1 /]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: ELK-Cluster    #ELK的集群名称,名称相同即属于是同一个集群
node.name: node1         #本机在集群内的节点名称
path.data: /var/lib/elasticsearch    #数据存放目录
path.logs: /var/log/elasticsearch    #日志保存目录
bootstrap.memory_lock: true  #服务启动的时候锁定足够的内存,防止数据写入swap
network.host: 0.0.0.0   #监听的IP地址
http.port: 9200              #服务监听的端口
discovery.zen.ping.unicast.hosts: ["node1", "node2"]    #单播配置一台即可

修改内存限制,内存锁定需要进行配置需要2g以上内存,否则会导致无法启动elasticsearch。

[root@node1 /]# vim /usr/lib/systemd/system/elasticsearch.service  #在[Service]下加入下面这行内容
LimitMEMLOCK=infinity
[root@node1 /]# vim /etc/elasticsearch/jvm.options  #最小和最大内存限制
-Xms2g
-Xmx2g 

启动elasticsearch及检查端口是否处于监听状态

[root@node1 /]# systemctl sart etlasticsearch
[root@node1 /]# netstat -tunpl |grep java
tcp6       0      0 :::9200                 :::*                    LISTEN      1014/java           
tcp6       0      0 :::9300                 :::*                    LISTEN      1014/java  

修改node2上面的etlasticsearch.yml配置文件

[root@node2 /]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: ELK-Cluster    #ELK的集群名称,名称相同即属于是同一个集群
node.name: node2         #本机在集群内的节点名称
path.data: /var/lib/elasticsearch    #数据存放目录
path.logs: /var/log/elasticsearch    #日志保存目录
bootstrap.memory_lock: true  #服务启动的时候锁定足够的内存,防止数据写入swap
network.host: 0.0.0.0   #监听的IP地址
http.port: 9200              #服务监听的端口
discovery.zen.ping.unicast.hosts: ["node1", "node2"]    #单播配置一台即可

[root@node2 /]# vim /usr/lib/systemd/system/elasticsearch.service  #在[Service]下加入下面这行内容
LimitMEMLOCK=infinity
[root@node2 /]# vim /etc/elasticsearch/jvm.options  #最小和最大内存限制
-Xms2g
-Xmx2g 

[root@node2 /]# systemctl sart etlasticsearch
[root@node2 /]# netstat -tunpl |grep java
tcp6       0      0 :::9200                 :::*                    LISTEN      1014/java           
tcp6       0      0 :::9300                 :::*                    LISTEN      1014/java 

通过浏览器访问172.16.0.3:9200和172.16.0.11:9200 请输入图片描述 请输入图片描述

查看elasticsearch集群状态

通过shell命令获取集群状态,这里获取到的是一个json格式的返回值,例如对status进行分析,如果等于green(绿色)就是运行在正常,等于yellow(黄色)表示副本分片丢失,red(红色)表示主分片丢失

[root@node1 /]# curl http://172.16.0.3:9200/_cluster/health?pretty=true
{
  "cluster_name" : "ELK-Cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 19,
  "active_shards" : 38,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
[root@node1 /]# curl http://172.16.0.12:9200/_cluster/health?pretty=true
{
  "cluster_name" : "ELK-Cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 19,
  "active_shards" : 38,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

安装elasticsearch插件head

安装npm

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install npm -y

上传软件并解压

tar xf elasticsearch-head.tar.gz -C /usr/local/

启动head查看端口

[root@node1 /]# cd /usr/local/elasticsearch-head/
[root@node1 /]# npm run start &
[root@node1 /]# netstat -nltp |grep 9100
tcp        0      0 127.0.0.1:9100          0.0.0.0:*               LISTEN      3229/node_exporter  

修改elasticsearch配置文件,开启跨域访问支持,并重启elasticsearch

node1:
[root@node1 /]# vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"

node2:
[root@node2 /]# vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"

重启

systemctl restart elasticsearch

浏览器访问elasticsearch-head

请输入图片描述

Logstash部署

上传软件包安装

yum -y localinstall logstash-6.8.1.rpm

配置logstash的配置文件收集系统日志

[root@node1 /]# vim /etc/logstash/conf.d/system.conf
input {
    file {
        path => "/var/log/messages"  #日志路径
        type => "systemlog"     #类型,自定义,在进行多个日志收集存储时可以通过该项进行判断输出
        start_position => "beginning"  #logstash 从什么位置开始读取文件数据
        stat_interval => "3"  #logstash 每隔多久检查一次被监听文件状态(是否有更新),默认是 1 秒
    }
}
output {
        elasticsearch {
            hosts => ["172.16.0.3:9200"]  #elasticsearch服务器地址
            index => "system-log-%{+YYYY.MM.dd}"  #索引名称
        }
    }

启动logstash

systemctl start logstash

浏览器访问172.16.0.3:9100查看 请输入图片描述

kibana部署

上传软件包安装

[root@node1 /]# yum -y localinstall kibana-6.8.1-x86_64.rpm
[root@node1 /]# vim /etc/kibana/kibana.yml
server.port: 5601   #监听端口
server.host: "172.16.0.3"  #监听地址
elasticsearch.hosts: ["http://172.16.0.3:9200"]   #elasticsearch服务器地址
i18n.locale: "zh-CN"  #修改为中文

启动kibana并验证

[root@node1 /]# systemctl start kibana
[root@nodel /]# netstat -anpl |grep 5601
tcp        0      0 172.16.0.3:5601         0.0.0.0:*               LISTEN      3792/node
访问web界面添加索引172.16.0.3:5601
![请输入图片描述][5]
![请输入图片描述][6]
![请输入图片描述][7]
![请输入图片描述][8]


  [1]: https://www.endvv.com/image/52/1.png
  [2]: https://www.endvv.com/image/52/2.png
  [3]: https://www.endvv.com/image/52/3.png
  [4]: https://www.endvv.com/image/52/4.png
  [5]: https://www.endvv.com/image/52/5.png
  [6]: https://www.endvv.com/image/52/6.png
  [7]: https://www.endvv.com/image/52/7.png
  [8]: https://www.endvv.com/image/52/8.png