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


一:需求及基礎:

場景:

1、開發人員不能登錄線上服務器查看詳細日志

2、各個系統都有日志,日志數據分散難以查找

3、日志數據量大,查詢速度慢,或者數據不夠實時

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

Elastic Search + LogStash + Kibana = ELK Stack

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

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

logstash3----|

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

1、安裝:

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

配置yum源或使用源碼安裝

2、啟動:

 /usr/local/elasticsearch/bin/elasticsearch  -d  #后台進程方式啟動
/etc/init.d/elasticsearch  restart

3、設置啟動腳本:

下載: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.

4、安裝啟動腳本:

[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

5、啟動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

6、java的配置文件:

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

9200:訪問的都端口

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

7、測試:

[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 概念和集群:

1、基於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
  }
}

2、安裝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

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

選選免費試用:

4、進入測試界面:

5、界面效果:

提交內容:

6、提交的代碼如下:

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

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

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

8、搜索文檔:

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:

1、安裝集群的管理插件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

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

3、在另外一台主機上配置好java和elasticsearch,配置文件只要吧node節點的名字改了就行,其他的不需要改,然后配置腳本啟動服務,再把elasticsearch啟動即可,會自動在集群顯示,推薦三台或以上的節點,因為其中一台主機掛掉不影響整個服務的訪問

綠色代表分片都正常運行,20個分片都正常,表示集群非常健康
黃色表示所有主分片都正常,但是副本分片有丟失,意味着ELS可以正常工作,但是有一定的風險,性能也不是最好的
紅色代表有主分片丟失,此部分數據就無法使用了

4、成功的集群效果:

5、以上是在監控程序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"
  }
}

 


免責聲明!

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



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