Centos6下搭建ELK

一、概念
ELK由三个部分组成。
logstash: 分析和收集日志,logstash有agent和indexer两个角色.
elasticsearch: 搜索功能。

kibana: 提供Web功能


二、搭建ElasticSearch
    2.1  基本环境搭建
        关闭防火墙或者放行

# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9292 -j ACCEPT
# service iptables restart

 2.2 安装JDK
    2.3 安装ElasticSearch
 ElasticSearch默认的对外服务的HTTP端口是9200,节点间交互的TCP端口是9300。
    1. 下载elasticsearch  存放路径为 /usr/local
    2. tar xfz elasticsearch-1.4.2.tar.gz
    3. ln -s elasticsearch-2.1.0 elasticsearch
    4. 修改配置文件
      vi /usr/local/elasticsearch/config/elasticsearch.yml
      http.cors.enabled: true   #233行
    5. 安装elasticsearch-servicewrapper,并启动ElasticSearch服务
   wget https://github.com/elasticsearch/elasticsearch-servicewrapper/archive/master.tar.gz
   mv elasticsearch-servicewrapper-master/service/   /usr/local/elasticsearch/bin/
   /usr/local/elasticsearch/bin/service/elasticsearch start
    curl -X GET http://localhost:9200

三、搭建logstash
       3.1 下载地址:
    wget https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz
      3.2 tar xfz logstash-1.4.2.tar.gz
       3.3  ln -s logstash-1.4.2 logstash
  3.4 测试
  /usr/local/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'
    3.5 mkdir /usr/local/logstash/etc
    3.6 创建配置文件
  vim /usr/local/logstash/etc/logstash_agent.conf
  input {
    file {
      type => "http.access"
      path => ["/var/log/httpd/access_log"]
    }

    file {
      type => "http.error"
      path => ["/var/log/httpd/error_log"]
    }

    file {
      type => "messages"
      path => ["/var/log/messages"]
    }
  }

  output {
     elasticsearch {
      host => "192.168.241.144"
      port => 9300
    }
  }
3.7 启动服务
  nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/logstash_agent.conf >> logstash.log &


四、安装kibana
  4.1 下载地址:
  4.2 tar xfz kibana-3.1.2.tar.gz -C /var/www/html
  4.3 mv kibana-3.1.2 kibana
  4.4 修改config.js
     elasticsearch:  "http://192.168.241.144:9200" 
  4.5 启动服务
    /etc/init.d/httpd restart
 
五、监控客户端
  5.1 监控方式
  我们可以直接监控客户端的配置文件,也可以让客户端把日志发到redis的消息队列里面。
  5.2 配置客户端
  客户端安装logstash,编写配置文件
  vim /usr/local/logstash/etc/logstash_agent.conf
  input {
          file {
                  type => "nginx_access"
                  path => ["/usr/local/nginx/log/access.log"]
          }
  }
  output {
          redis {
                  host => "192.168.241.144"
             data_type => "list"
                  key => "logstash:redis"
                port => "9400"
        }
六、elasticsearch集群搭建实例

  集群存放路径:/export/search/elasticsearch-cluster

  6.1. 解压tar包,创建集群节点

  #进入到集群路径
  [root@localhost elasticsearch-cluster]# pwd
  /export/search/elasticsearch-cluster
  #重命名解压包
  [root@localhost elasticsearch-cluster]# ls
  elasticsearch-1.4.1
  [root@localhost elasticsearch-cluster]# mv elasticsearch-1.4.1 elasticsearch-node1#进入到节点配置路径
  [root@localhost elasticsearch-cluster]# cd elasticsearch-node1/config/
  [root@localhost config]# ls
  elasticsearch.yml  logging.yml

  6.2.创建集群配置信息:

  # elasticsearch-node1配置# 配置集群名称
  cluster.name: elasticsearch-cluster-centos
  # 配置节点名称
  node.name: "es-node1"# 为节点之间的通信设置一个自定义端口(默认为9300)     
  transport.tcp.port: 9300 
  # 设置监听HTTP传输的自定义端(默认为9200)
  http.port: 9200              

  elasticsearch配置文件说明见: http://blog.csdn.net/an74520/article/details/10175603

 3.安装head插件

#进入到节点bin路径
[root@localhost bin]# pwd
/export/search/elasticsearch-cluster/elasticsearch-node1/bin
安装插件
[root@localhost bin]# ./plugin -install mobz/elasticsearch-head

安装完插件之后会在es节点bin路径同级创建一个plugins目录,存放安装的插件

 4.复制一份配置好的节点为elasticsearch-node2

[root@localhost elasticsearch-cluster]# ls
elasticsearch-node1  elasticsearch-node2

 5.修改节点2中的集群配置信息

# elasticsearch-node2配置# 配置集群名称
cluster.name: elasticsearch-cluster-centos
# 配置节点名称
node.name: "es-node2"# 为节点之间的通信设置一个自定义端口(默认为9300)     
transport.tcp.port: 9301 
# 设置监听HTTP传输的自定义端(默认为9200)
http.port: 9201             

说明:

上面配置表示集群中有2个节点,节点名为别为,"es-node1"和  "es-node2",同属于集群"elasticsearch-cluster-centos"

节点二中端口可以不用配置,es在启动时会去检测,如果目标端口被占用,会检测下一个端口.因为两节点部署在同一天虚拟机上为了更好的说明问题,这里手动配置了对应的端口.

我们可以从es对应日志中()查看对应的启动信息,以及端口绑定信息。

 6.分别启动节点

[root@localhost bin]# pwd
/export/search/elasticsearch-cluster/elasticsearch-node1/bin
[root@localhost bin]# ./elasticsearch -d -Xms2048m -Xmx2048m

如上,为启动节点1的命令,es启动配置相关日志查看elasticsearch-cluster- centos.log即可.

[root@localhost logs]# pwd
/export/search/elasticsearch-cluster/elasticsearch-node2/logs
[root@localhost logs]# ls
elasticsearch-cluster-centos_index_indexing_slowlog.log  elasticsearch-cluster-centos.log  elasticsearch-cluster-centos_index_search_slowlog.log

 7. 至此我们的简易集群配置完成.查看集群

因为我们安装了head插件,所以可以通过该插件查看,虚拟机ip为192.168.19.56.

http://192.168.19.56:9200/_plugin/head/ (对应节点1)
http://192.168.19.56:9201/_plugin/head/ (对应节点2)



猜你喜欢

转载自blog.csdn.net/lwlcoco/article/details/78771664