ELKStack - 基础:部署安装+简单使用篇 (一)

1.ELKstack - ES 简介

https://www.unixhot.com/article/59

对于日志来说,最常见的需求就是收集、存储、查询、展示,开源社区正好有相对应的开源项目:logstash(收集)、elasticsearch(存储+搜索)、kibana(展示),我们将这三个组合起来的技术称之为ELKStack,所以说ELKStack指的是Elasticsearch、Logstash、Kibana技术栈的结合。通用的架构图:
ELKStack - 基础:部署安装+简单使用篇 (一)

2.ELK安装

最佳实践:yum安装的方式。

因yum源都是国外的,需要×××才能使用。本文不做详细安装步骤了,请参考上面的链接。
我是将要安装的软件都下载到本地,然后使用yum localinstall “package name‘的方式安装。

2.1 Elasticsearch部署

elasticsearch 依赖 java环境,在这里我们用yum安装即可。

[root@elk01-node2 ~]# yum -y install java
[root@elk01-node2 tools]# yum localinstall elasticsearch-2.4.4.rpm 

2.2 Logstash部署

Logstash也依赖java环境,这里我们也使用yum安装java环境即可。

[root@elk01-node2 tools]# yum localinstall logstash-2.3.4-1.noarch.rpm

2.3 kibana部署

Kibana 是为 Elasticsearch 设计的开源分析和可视化平台。你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互。你可以很容易实现高级的数据分析和可视化,以图表的形式展现出来。

[root@elk01-node2 tools]# yum localinstall kibana-4.5.4-1.x86_64.rpm

3. ES(Elasticsearch)简介

学习软件的方法:安装 - 配置 - 启动 - 测试

3.1 修改es 配置文件

[root@elk01-node2 elk]# vim /etc/elasticsearch/elasticsearch.yml 
修改内容 如下:
17 cluster.name: myes  # 集群名,集群的时候需要用到。
23 node.name: linux-node1  # 节点名,不能重复
33 path.data: /data/es-data  # 数据存放的位置
37 path.logs: /var/log/elasticsearch/  # 日志存放的位置
43 bootstrap.memory_lock: true  # 此配置的意思是,锁住es占用的内存分区,防止被交换到swap分区,影响性能。
54 network.host: 10.0.0.204   # 监听的接口地址,默认监听的端口是9200
58 http.port: 9200 # 监听的端口

新建对应的目录。

[root@elk01-node2 elk]# mkdir /data/es-data -p 
[root@elk01-node2 elk]# chown -R elasticsearch. /data/

3.2 启动es

如果不能正常启动,看日志。

[root@elk01-node2 elk]# /etc/init.d/elasticsearch start 
[root@elk01-node2 elk]# netstat -tnlpua|grep 9200 
tcp6 0 0 10.0.0.204:9200 :::* LISTEN 2943/java

3.3 测试

[root@elk01-node2 elk]# curl http://10.0.0.204:9200                 
{
  "name" : "linux-node1",
  "cluster_name" : "myes",
  "cluster_uuid" : "CRLwDyWsSX-6q4RC4wRcqA",
  "version" : {
    "number" : "2.4.4",
    "build_hash" : "fcbb46dfd45562a9cf00c604b30849a6dec6b017",
    "build_timestamp" : "2017-01-03T11:33:16Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.2"
  },
  "tagline" : "You Know, for Search"
}

[root@elk01-node2 elk]# curl -i XGET 'http://10.0.0.204:9200/_count?'
curl: (6) Could not resolve host: XGET; Name or service not known
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 59

{"count":1,"_shards":{"total":5,"successful":5,"failed":0}}

3.4 es 安装常用的插件

因安装插件需要×××。从github下载的插件可以用。

es下载的插件放在/usr/share/elasticsearch/plugins/目录下面。

安装方法

/usr/share/elasticsearch/bin/plugin install marvel-agent 
 /usr/share/elasticsearch/bin/plugin install head

git下载的插件安装

[root@elk01-node2 plugins]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head ( this plugin is to github download.)

[root@elk01-node2 plugins]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf

测试安装的插件

{
    "user": "wf",
    "mesg": "hehe"
}

ELKStack - 基础:部署安装+简单使用篇 (一)

http://10.0.0.204:9200/_plugin/kopf/#!/cluster
(ps:图是补的)
ELKStack - 基础:部署安装+简单使用篇 (一)

4.ES集群

4.1 修改es配置文件

elk01-node2.damaiche.org-204 配置

[root@elk01-node2 elk]# grep ^[a-z] /etc/elasticsearch/elasticsearch.yml 
cluster.name: myes
node.name: linux-node1
path.data: /data/es-data
path.logs: /var/log/elasticsearch/
bootstrap.memory_lock: true
network.host: 10.0.0.204
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.0.0.203", "10.0.0.204"]  # 如不能正常加入到集群中去。10.0.0.204 需要将组播改成单播。(204 知道203 是它的兄弟,203 不知道无所谓)

10.0.0.203配置

ps: es 安装方法相同

[root@web01-node1 ~]#  grep ^[a-z] /etc/elasticsearch/elasticsearch.yml 
cluster.name: myes
node.name: linux-node2
path.data: /data/es-data
path.logs: /var/log/elasticsearch/
bootstrap.memory_lock: true
network.host: 10.0.0.203
http.port: 9200

4.2 启动es

etc/init.d/elasticsearch restart

10.0.0.204的日志,以今天加入到集群里去了。
/var/log/elasticsearch/my-es.log
ELKStack - 基础:部署安装+简单使用篇 (一)

4.3 图

ELKStack - 基础:部署安装+简单使用篇 (一)

说明:

  • 1 当主分片丢失时,会从副本分片中选举出一个作为主分片。
  • 2 查询过程。当从某个节点进行查询,此节点就是一个汇总节点。当此节点没有对应的数据时,此节点会将其他节点的数据拉过汇总给你。
  • 3 当主分片丢失,那么主分片所在的文档就不可以用了。
  • 4 es部署完成之后,需要修改系统内核的max_file,否则以后修改es配置文件就只能重启es了。
    https://www.elastic.co/guide/en/elasticsearch/guide/current/_file_descriptors_and_mmap.html

4.4 集群监控

es官方文档

监测集群健康状态

例子:

https://www.elastic.co/guide/en/elasticsearch/guide/current/_cluster_health.html

[root@web01-node1 ~]# curl -i XGET http://10.0.0.204:9200/_cluster/health?pretty=True
curl: (6) Could not resolve host: XGET; Name or service not known
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 458

{
  "cluster_name" : "myes",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 5,
  "active_shards" : 10,
  "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
}

5. Logstash

https://www.elastic.co/guide/en/logstash/2.3/index.html

5.1 基础用法

手动的输入内容,并且打印出来。

/opt/logstash/bin/logstash -e \
'input { 
    stdin {}
} 
output {
    stdout{}
}'

ELKStack - 基础:部署安装+简单使用篇 (一)

手动的输入内容,并且打印出来,格式更加的好看。

/opt/logstash/bin/logstash -e \
'input { 
    stdin {}
} 
output {
    stdout{codec => rubydebug}
}'

ELKStack - 基础:部署安装+简单使用篇 (一)

将输出的内容打印出来同时存入到es里。

input插件

官方参考链接 https://www.elastic.co/guide/en/logstash/current/input-plugins.html

logstash

官方参考链接 https://www.elastic.co/guide/en/logstash/current/index.htm

output插件

官方参考链接 https://www.elastic.co/guide/en/logstash/current/output-plugins.html

/opt/logstash/bin/logstash -e \
'input { 
    stdin {}
} 

output {
    stdout {
        codec => rubydebug
    }
    elasticsearch {
        hosts => ["http://10.0.0.204:9200"]
        index =>  "test-log-%{+YYYY.MM.dd}"
    }
}'

ELKStack - 基础:部署安装+简单使用篇 (一)
ELKStack - 基础:部署安装+简单使用篇 (一)

5.2 logstash - input - file

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html

写一个输出到前台,并且将日志存储到es里面的配置文件,启动在前台。

[root@elk01-node2 ~]# cd /etc/logstash/conf.d/
[root@elk01-node2 conf.d]# cat demo.conf 
input{
    stdin {}
}

filter{
}

output{ 
     stdout {
        codec => rubydebug
    } 

    elasticsearch {
        hosts => ["10.0.0.204:9200"]
        index => "demo-log-%{+YYYY.MM.dd}"
    }
}

[root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f demo.conf 

写一个收集系统日志的配置文件

[root@elk01-node2 conf.d]# cat file.conf 
input{
     file {
        path => ["/var/log/messages", "/var/log/secure"]   # path 指定日志收集的位置
        start_position => "beginning"  # 指定从日志文件什么地方开始读。
        type => "system-log" # 指定日志的类型,这个是自定义的。用来做if判断。
    }
}

filter {
}

output{
        if [type] == "system-log" {  # 这里的等于不是=> 而是== , 需要注意一下。
                elasticsearch {
                        hosts => ["10.0.0.204:9200"]
                        index => "system-log-%{+YYYY.MM}"
                }
        }
}

[root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f file.conf 

ELKStack - 基础:部署安装+简单使用篇 (一)

5.3 logstash - input - if

这里需要注意下:如果使用了type来做if判断,那么在日志里就不能出现type字段了。

input{
    file {
        path => ["/var/log/messages", "/var/log/secure"]
        start_position => "beginning"
        type => "system-log"
    }

    file {
        path  => ["/var/log/elasticsearch/myes.log"]
        start_position => "beginning"
        type => "myes-log"
    }
}

filter {
}

output{
        if [type] == "system-log" {
            elasticsearch {
                hosts => ["10.0.0.204:9200"]
                index => "system-log-%{+YYYY.MM}"
            }
        }       

      if [type] == "myes-log" {
            elasticsearch {
                hosts => ["10.0.0.204:9200"]
                index => "myes-log-%{+YYYY.MM.dd}"
            }

       }
} 

[root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f file.conf 

5.4 logstash - codec - multiline

在上面配置文件基础上,添加对java日志文件的收集。
需要注意java日志的日志格式。java日志内容 一行会有很多的内容。需要用到multiline模块
https://www.elastic.co/guide/en/logstash/2.3/plugins-codecs-multiline.html

[root@elk01-node2 conf.d]# cat codec.conf 
input{
    file {
        path  => ["/var/log/elasticsearch/myes.log"]
        start_position => "beginning"
        type => "myes"
        codec => multiline {
             # 下面这三行的意思是,匹配到以[开头的文件之前的内容,就合并到上一行。
             pattern => "^\[" # 匹配的表达式
             negate => true # 匹配的结果,这是一个bool类型。
             what => "previous" # 动作,合并到上一行。previous合并到上一行,next是合并到下一行。
        }
    }
}

filter {
}

output{
    if [type] == 'myes' {
        elasticsearch {
             hosts => ["10.0.0.204:9200"]
             index => "myes_log-%{+YYYY.MM.dd}"    
        }
    }
}

可以将其添加到kibana里面去,能清楚看到效果。

注:

  • 1 对相同索引的日志进行操作,在调试过后;然后写到es里,需要将之前调试生成的索引缓存删除掉。
  • 2 前台调试的缓存:当前家目录下.since开头的文件。
  • 3 后台启动的缓存:/var/lib/logstash/目录下的文件。

5.5 LogStash-codec-json

https://www.elastic.co/guide/en/logstash/2.3/plugins-codecs-json.html

为什么要用json来收集日志?

获取日志里面的参数信息,es不能直接做到的(如果你会ruby就可以自己写了),用json就可以轻松的获取到。(用app客户端的访问日志来举例,客户端,uid等等。)

获取方式

  • 1 将日志存入到reids,然后logstash从redis读,进行处理成json各式。
  • 2 将python直接从redis里面读取,然后进行处理。搞成json写到es里面去。
5.5.1 收集json格式的nginx日志

ps: nginx可以将日志写成json格式的。

修改nginx配置文件

[root@elk01-node2 nginx]# vim nginx.conf        

    log_format  access_log_json'{"user_ip":"$http_x_real_ip","lan_ip":"$remote_addr","log_time":"$time_iso8601","user_req":"$request","http_code":"$status","body_bytes_sent":"$body_bytes_sent","req_time":"$request_time","user_ua":"$http_user_agent"}';
    access_log  /var/log/nginx/access_json.log  access_log_json;

[root@elk01-node2 nginx]#  systemctl restart nginx

编写收集nginx日志的配置文件,启动在前台进行测试。

[root@elk01-node2 conf.d]# cat nginx.conf 
input{
    file {
        path  => ["/var/log/nginx/access_json.log"]
        type => "nginx-log"
        codec => "json"
    }
}

filter {
}

output{
    if [type] == 'nginx-log' {
        stdout { codec => rubydebug}
        }
    }

[root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f nginx.conf 

# 模拟数据
[root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/aaa/
[root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/

效果

ELKStack - 基础:部署安装+简单使用篇 (一)

确认没问题后写入到es里

[root@elk01-node2 conf.d]# cat nginx.conf 
input{
    file {
      path => "/var/log/nginx/access_json.log"
      codec => "json"
      type => "nginx-log"
    }
}

filter{
}

output{
    if [type] == 'nginx-log' {
        elasticsearch {
             hosts => ["10.0.0.204:9200"]
             index => "nginxlog-%{+YYYY.MM.dd}"
        }
    }
}

[root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f nginx.conf 

# 模拟数据
[root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/aaa/
[root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/

ELKStack - 基础:部署安装+简单使用篇 (一)

遇到的问题:

  • 1 要有新的日志产生,我们在es里面才能看到这个新加的索引。so 要造日志。
  • 2 如果前面用debug进行了调试,那么我们就删除.since开头的缓存文件。
  • 3 如果在测试的时候,没有在es上面看到我们添加的索引。那么我们可以用debug模式打印输出信息。

转载于:https://blog.51cto.com/damaicha/2122633

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。该课程从ELK组件中的两大组件和常用的应用日志收集进行剖析讲解。(1)ElasticSearch:从单台的安装到集群的搭建、从基础的入门使用到高级的搜索和聚合都进行了较好的阐述。(2) Logstash:通过收集apache日志,使用grok插件和geoip插件先让学习人员对整体框架做到了了解于胸。然后再深入的对LogStash重要的知识点进行了剖析。(3)应用日志收集:对常见的Apache、Nginx、MySQL、Syslog进行日志收集和分析。虽说没有讲解太多,但是我想应该能做到让大家对应用日志收集了解的更加深入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值