日志收集管理-ELK_01


日志收集管理-ELK

1.Elastic search(ES)介绍

  百度百科对于Elastic search的定义和解释:Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsearch 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。

  Elasticsearch是与名为Logstash的数据收集和日志解析引擎以及名为Kibana的分析和可视化平台一起开发的。这三个产品被设计成一个集成解决方案,称为“Elastic Stack”(以前称为“ELK stack”)。   Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。Elasticsearch是分布式的,这意味着索引可以被分成分片,每个分片可以有0个或多个副本。每个节点托管一个或多个分片,并充当协调器将操作委托给正确的分片。再平衡和路由是自动完成的。相关数据通常存储在同一个索引中,该索引由一个或多个主分片和零个或多个复制分片组成。一旦创建了索引,就不能更改主分片的数量。   Elasticsearch使用Lucene,并试图通过JSON和Java API提供其所有特性。它支持facetting和percolating,如果新文档与注册查询匹配,这对于通知非常有用。另一个特性称为“网关”,处理索引的长期持久性;例如,在服务器崩溃的情况下,可以从网关恢复索引。Elasticsearch支持实时GET请求,适合作为NoSQL数据存储,但缺少分布式事务。   举个例子:github就是用ES做的搜索!  

2.Elasticsearch(ES)有关概念理解

  Elasticsearch功能:

    a.分布式搜索和分析引擎;

    b.全文检索,结构化检索,数据分析;

    c.对海量数据进行实时处理。

  Elasticsearch常见使用场景: 

    a.搜索,高亮显示
    b.商城搜索
    c.日志收集分析展示  

  Elasticsearch和mysql数据库名词术语概念一一对应:

    elasticsearch              数据库

    Document                      行

    Type                              表

    Index                             库

    filed                               字段

3.Elasticsearch(ES)安装和配置

Elasticsearch 安装部署方式:

  a.docker方式;

  b.tar包安装;

  c.ansible;

  d.rpm/deb。

  本人以rpm安装方式安装elasticsearch进行研究!

  RPM安装Elasticsearch主要步骤:

    1)安装jdk

       yum search java | grep -i --color JDK
       yum install -y java-1.8.0-openjdk.x86_64

    2)下载安装elasticsearch

       mkdir -p /data/es_soft/

       cd /data/es_soft/

      wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm

       rpm -ivh elasticsearch-6.6.0.rpm

    3)配置启动

      [root@localhost es_soft]# systemctl daemon-reload
      [root@localhost es_soft]# systemctl enable elasticsearch.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
      [root@localhost es_soft]# systemctl start elasticsearch.service
      
      [root@localhost es_soft]# systemctl status elasticsearch.service
      ● elasticsearch.service - Elasticsearch
         Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
         Active: active (running) since 二 2022-01-11 11:03:20 CST; 36s ago
           Docs: http://www.elastic.co
       Main PID: 25175 (java)
         CGroup: /system.slice/elasticsearch.service
                 ├─25175 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch...
                 └─25251 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

1      月 11 11:03:20 localhost.localdomain systemd[1]: Started Elasticsearch. 

    4)检查elasticsearch服务是否启动成功

      [root@localhost es_soft]# ps -ef|grep elastic
elastic+ 25175     1  6 11:03 ?        00:00:17 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch-3742633244208252722 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/lib/elasticsearch -XX:ErrorFile=/var/log/elasticsearch/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:/var/log/elasticsearch/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Des.path.home=/usr/share/elasticsearch -Des.path.conf=/etc/elasticsearch -Des.distribution.flavor=default -Des.distribution.type=rpm -cp /usr/share/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch -p /var/run/elasticsearch/elasticsearch.pid --quiet
elastic+ 25251 25175  0 11:03 ?        00:00:00 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller
root     30177  1528  0 11:07 pts/0    00:00:00 grep --color=auto elastic

      [root@localhost es_soft]# lsof -i:9200
COMMAND   PID          USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    25175 elasticsearch  204u  IPv6 124374      0t0  TCP localhost:wap-wsp (LISTEN)
java    25175 elasticsearch  205u  IPv6 124375      0t0  TCP localhost:wap-wsp (LISTEN)

      [root@localhost es_soft]# netstat -lntup|grep 9200
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      25175/java          
tcp6       0      0 ::1:9200                :::*                    LISTEN      25175/java       

[root@localhost es_soft]# curl 127.0.0.1:9200
{
  "name" : "Plo4-LY",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qpS8OUS5ThSzrzPNZ4dVXQ",
  "version" : {
    "number" : "6.6.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "a9861f4",
    "build_date" : "2019-01-24T11:27:09.439740Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

注意:curl命令出现如上信息,说明elasticsearch启动成功!

elasticsearch输出日志如下:

      [root@localhost es_soft]# tail -f /var/log/elasticsearch/elasticsearch.log
[2022-01-11T11:03:31,931][INFO ][o.e.g.GatewayService     ] [Plo4-LY] recovered [0] indices into cluster_state
[2022-01-11T11:03:32,111][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.triggered_watches] for index patterns [.triggered_watches*]
[2022-01-11T11:03:32,175][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.watches] for index patterns [.watches*]
[2022-01-11T11:03:32,208][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.watch-history-9] for index patterns [.watcher-history-9*]
[2022-01-11T11:03:32,230][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.monitoring-logstash] for index patterns [.monitoring-logstash-6-*]
[2022-01-11T11:03:32,297][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.monitoring-es] for index patterns [.monitoring-es-6-*]
[2022-01-11T11:03:32,326][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.monitoring-beats] for index patterns [.monitoring-beats-6-*]
[2022-01-11T11:03:32,351][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.monitoring-alerts] for index patterns [.monitoring-alerts-6]
[2022-01-11T11:03:32,374][INFO ][o.e.c.m.MetaDataIndexTemplateService] [Plo4-LY] adding template [.monitoring-kibana] for index patterns [.monitoring-kibana-6-*]
[2022-01-11T11:03:32,464][INFO ][o.e.l.LicenseService     ] [Plo4-LY] license [cf912932-b640-402a-b7fa-d80ad6aeac0d] mode [basic] - valid
        
    5)查看elasticsearch有哪些配置文件:

 [root@localhost ~]# rpm -qc elasticsearch
/etc/elasticsearch/elasticsearch.yml  ES主配置文件
/etc/elasticsearch/jvm.options            JVM虚拟机配置
/etc/elasticsearch/log4j2.properties   
/etc/elasticsearch/role_mapping.yml
/etc/elasticsearch/roles.yml
/etc/elasticsearch/users
/etc/elasticsearch/users_roles
/etc/init.d/elasticsearch                      init的启动文件
/etc/sysconfig/elasticsearch               环境变量相关参数
/usr/lib/sysctl.d/elasticsearch.conf      JVM相关配置
/usr/lib/systemd/system/elasticsearch.service   systemc启动配置 

4.ES-head插件

1)elasticsearch 支持的常用查询指令

[root@localhost ~]# curl 10.96.211.105:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates


[root@localhost ~]# curl 10.96.211.105:9200/_cat/nodes
10.96.211.105 9 80 2 0.09 0.07 0.08 mdi * node-1

2)  elasticsearch交互方式

elasticsearch有三种交互方式如下:

a.curl命令

b.es-head插件

c.kibana

3)  es-head插件安装

此插件安装赞略,不是研究重点,知道即可。

5.ES集群

  ES集群就是多台主机在同一个组里。

    1)ES配置部署集群

  ES配置部署集群,我以两台主机为例进行研究。

第一台主机主配置文件做如下配置更改:
[root@localhost ~]# grep "^[a-z]" /etc/elasticsearch/elasticsearch.yml
cluster.name: Linux                                                                             #集群名称,同一个集群内所有节点集群名称要一模一样
node.name: node-1                                                                             #节点名称,同一个集群内所有节点的节点名称不能重复
path.data: /data/elasticsearch                                                              #数据目录
path.logs: /var/log/elasticsearch                                                           #日志目录
bootstrap.memory_lock: true                                                                #内存锁定
network.host: 10.96.211.105,127.0.0.1                                                #绑定监听地址
http.port: 9200                                                                                      #默认端口号
discovery.zen.ping.unicast.hosts: ["10.96.211.105", "10.96.211.106"] #集群发现节点配置
discovery.zen.minimum_master_nodes:2                                             #选举有关参数,有公式:master/2+1
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

集群配置文件:
[root@db01 elasticsearch]# grep "^[a-z]" elasticsearch.yml
cluster.name: Linux #集群名称,同一个集群内所有节点集群名称要一模一样
node.name: node-1 #节点名称,同一个集群内所有节点的节点名称不能重复
path.data: /data/elasticsearch #数据目录
path.logs: /var/log/elasticsearch #日志目录
bootstrap.memory_lock: true #内存锁定
network.host: 10.96.211.209,127.0.0.1 #绑定监听地址
http.port: 9200 #默认端口号
discovery.zen.ping.unicast.hosts: ["10.96.211.209", "10.96.211.110"] #集群发现节点配置
discovery.zen.minimum_master_nodes: 2 #选项相关参数,有公式 master/2 +1

新增节点配置步骤:
1.安装软件
rpm -ivh elasticsearch-6.6.0.rpm
2.修改配置文件
[root@db02 elasticsearch]# cat /etc/elasticsearch/elasticsearch.yml
cluster.name: Linux
node.name: node-2
path.data: /data/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.96.211.110,127.0.0.1
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.96.211.209", "10.96.211.110"]
discovery.zen.minimum_master_nodes: 2
3.修改内存锁定
[root@db02 ~]# systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
4.创建数据目录并授权
mkidr /data/elasticsearch
chown =R elasticsearch:elasticsearch /data/elasticsearch
5.重启服务
systemctl daemon-reload
systemctl start elasticsearch
6.查看日志和端口
tail -f /var/log/elasticsearch/Linux.log
netstat -lntup:grep 9200

操作指令:
curl -XGET 'http://localhost:9200/_nodes/procese?human&pretty'
curl -XGET 'http://localhost:9200/_nodes/_all/info/jvm,process?human&pretty'
curl -XGET 'http://localhost:9200/_cat/nodes?human&pretty'
curl -XGET 'http://localhost:9200/_cluster/health?pretty'
curl -XPUT 'localhost:9200/_cat/indices?pretty'

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




     

4)日志收集介绍

  kibana本身无数据,它负责展示ES里的数据。想在哪一台主机收集日志就把filebeat安装在哪一台主机里。