ELK 之一:ElasticSearch 基礎和集羣搭建

一:需求及基礎:html

場景:java

一、開發人員不能登陸線上服務器查看詳細日誌node

二、各個系統都有日誌,日誌數據分散難以查找git

三、日誌數據量大,查詢速度慢,或者數據不夠實時github

四、一個調用會涉及到多個系統,難以在這些協調中快速定位數據web

Elastic Search + LogStash + Kibana = ELK Stackredis

logstash1----|   (redis實現鬆耦合功能)json

logstash2----|----->broker redis----->indexer logstash---->search storage<--------Web Logstashbootstrap

logstash3----|vim

ELS的概念:

1、索引:數據會放在多個索引中,索引能夠理解爲database,索引裏面存放的基本單位是文檔,LES會把索引分片,便於橫向擴展,分別能夠作備份,多個分片讀比較快,備份分片在主的掛掉以後能夠自動將本身提高爲主分片(實現橫向擴展和冗餘)
2、文檔類型:和redis同樣,key是有類型的
3、節點:一個ELS的實例是一個節點
4、集羣:多節點的集合組成集羣,相似於zookeeper會選舉出主節點,客戶端不須要關注主節點,鏈接任何一個均可以,數據會自動同步,所以應用不須要關注那個是主節點。前提是要把

配置文件:

[root@elk-server1 config]# vim elasticsearch.yml 
cluster.name: hfelk-server  #集羣的名稱,名稱相同就是一個集羣
node.name: Server1  #集羣狀況下,當前node的名字,每一個node應該不同
node.master: true  #當前節點是否能夠被選舉爲master節點,能夠不選舉master作保存數據
node.data: true #當前節點是否存儲數據,也能夠不存儲數據,只作master
bootstrap.mlockall: true #鎖住內存,不作swap,提升效率
http.port: 9200  #客戶端訪問端口
transport.tcp.port: 9300 #集羣訪問端口:
index.number_of_shards: 5 #默認每一個項目5個分片
index.number_of_replicas: 1  #每一個主分片一個副本分片,即5個主分片就有5個副本

 

 二:安裝及配置:

官網下載地址:
https://www.elastic.co/downloads
官方文檔:
https://www.elastic.co/guide/index.html

一、安裝:

安裝java環境,1.8.20或以上的版本

配置yum源或使用源碼安裝

二、啓動:

 /usr/local/elasticsearch/bin/elasticsearch  -d  #後臺進程方式啓動
/etc/init.d/elasticsearch  restart

三、設置啓動腳本:

下載:elasticsearch-servicewrapper-master.zip

[root@elk-server1 tianqi]# mv  elasticsearch-servicewrapper-master/service/ /usr/local/elasticsearch/bin/

幫助信息:

[root@elk-server1 tianqi]# /usr/local/elasticsearch/bin/service/elasticsearch
Usage: /usr/local/elasticsearch/bin/service/elasticsearch [ console | start | stop | restart | condrestart | status | install | remove | dump ]

Commands:
  console      Launch in the current console.
  start        Start in the background as a daemon process.
  stop         Stop if running as a daemon or in another console.
  restart      Stop if running and then start.
  condrestart  Restart only if already running.
  status       Query the current status.
  install      Install to start automatically when system boots.
  remove       Uninstall.
  dump         Request a Java thread dump if running.

四、安裝啓動腳本:

[root@elk-server1 tianqi]# /usr/local/elasticsearch/bin/service/elasticsearch install  #安裝腳本
Detected RHEL or Fedora:
Installing the Elasticsearch daemon..
[root@elk-server1 tianqi]# ls /etc/init.d/elasticsearch  #驗證是否安裝完成
/etc/init.d/elasticsearch
[root@elk-server1 tianqi]# chkconfig  --list | grep ela #自動設置爲開機啓動
elasticsearch      0:off    1:off    2:on    3:on    4:on    5:on    6:off

五、啓動elasticsearch服務:

[root@elk-server1 tianqi]# /etc/init.d/elasticsearch   start
Starting Elasticsearch...
Waiting for Elasticsearch......
running: PID:14183
[root@elk-server1 tianqi]# /etc/init.d/elasticsearch   status
Elasticsearch is running: PID:14183, Wrapper:STARTED, Java:STARTED

六、java的配置文件:

[root@elk-server1 service]# ls /usr/local/elasticsearch/bin/service/elasticsearch.conf

9200:訪問的都端口

9300:服務器之間通訊的端口

七、測試:

[root@elk-server1 elasticsearch]# curl  -i -XGET http://192.168.0.251:9200
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 335

{
  "status" : 200,
  "name" : "Server1",
  "cluster_name" : "HFELK-Server1",
  "version" : {
    "number" : "1.7.0",
    "build_hash" : "929b9739cae115e73c346cb5f9a6f24ba735a743",
    "build_timestamp" : "2015-07-16T14:31:07Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

 

 三:ES 概念和集羣:

一、基於http的RESTful API

 以jsop返回查詢結果:

[root@elk-server1 config]# curl  -XGET  'http://192.168.0.251:9200/_count?pretty' -d '
> {
>     "query":{
>            "match_all":{}
>       }
> }
> 
> '
{
  "count" : 1,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  }
}

curl -i:

[root@elk-server1 config]# curl  -i -XGET  'http://192.168.0.251:9200/_count?pretty' -d '
{
    "query":{
           "match_all":{}
      }
}

'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 95

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

二、安裝ELS監控管理插件:

[root@elk-server1 service]# /usr/local/elasticsearch/bin/plugin  -i elasticsearch/marvel/latest/
-> Installing elasticsearch/marvel/latest/...
Trying http://download.elasticsearch.org/elasticsearch/marvel/marvel-latest.zip...
Downloading ......................................................................................................................................................................................................................................................DONE
Installed elasticsearch/marvel/latest/ into /usr/local/elasticsearch/plugins/marvel

三、web訪問:http://xx.chinacloudapp.cn:9200/_plugin/marvel/

選選免費試用:

四、進入測試界面:

五、界面效果:

提交內容:

六、提交的代碼以下:

POST  /index-demo/test
{
  "user":"jack",
  "message":"hello word"
  }
}

七、 查看和刪除指定文檔內容:

GET  /index-demo/test/AVP0y8ANAZWiuuxBK3mq/_source
DELETE  /index-demo/test/AVP0y8ANAZWiuuxBK3mq/_source

八、搜索文檔:

GET  /index-demo/test/_search?q=hello

{
   "took": 97,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.15342641,
      "hits": [
         {
            "_index": "index-demo",
            "_type": "test",
            "_id": "AVP0y8ANAZWiuuxBK3mq",
            "_score": 0.15342641,
            "_source": {
               "user": "jack",
               "message": "hello word"
            }
         }
      ]
   }
}

 

四:elasticsearch集羣管理程序之head:

一、安裝集羣的管理插件head:

集羣更換了虛擬機環境,因此主機名不同,安裝的時候要多安裝幾回,有的時候會由於網絡問題沒法一次 安裝完成。

[root@node6 local]#  /usr/local/elasticsearch/bin/plugin  -i mobz/elasticsearch-head/
-> Installing mobz/elasticsearch-head/...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip...
Downloading ....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Installed mobz/elasticsearch-head/ into /usr/local/elasticsearch/plugins/head

二、打開web管理端,查看是否已經有本機被管理了:

三、在另一臺主機上配置好java和elasticsearch,配置文件只要吧node節點的名字改了就行,其餘的不須要改,而後配置腳本啓動服務,再把elasticsearch啓動便可,會自動在集羣顯示,推薦三臺或以上的節點,由於其中一臺主機掛掉不影響整個服務的訪問

綠色表明分片都正常運行,20個分片都正常,表示集羣很是健康
黃色表示全部主分片都正常,可是副本分片有丟失,意味着ELS能夠正常工做,可是有必定的風險,性能也不是最好的
紅色表明有主分片丟失,此部分數據就沒法使用了

四、成功的集羣效果:

五、以上是在監控程序marvel的界面建立了兩個項目:

打開鏈接:http://192.168.10.206:9200/_plugin/marvel/sense/index.html

POST  /index-demo/hi
{
  "user":"tom1",
  "message":"hello word"
  }
}

POST  /index-hello/hi
{
  "user":"tom1",
  "message":"hello word"
  }
}
相關文章
相關標籤/搜索