Centos6下搭建ELK


網上關於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
    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)

 
 七、最后的效果圖
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM