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

1.ELKstack - ES 简介

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

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

2.ELK安装

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

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

2.1 Elasticsearch部署

elasticsearch 依赖 java环境,在这里咱们用yum安装便可。linux

[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环境便可。nginx

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

2.3 kibana部署

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

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

3. ES(Elasticsearch)简介

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

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 # 监听的端口

新建对应的目录。web

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

3.2 启动es

若是不能正常启动,看日志。redis

[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模式打印输出信息。
相关文章
相关标签/搜索