[关闭]
@cdmonkey 2017-08-30T03:39:45.000000Z 字数 6624 阅读 1090

ELK-Elasticsearch-1

ELK


https://www.elastic.co/
中文指南:http://kibana.logstash.es/
学习课程:http://blog.csdn.net/jiuqiyuliang/article/details/51245335

运维人员面对的实际情况:

Solution:ELK Stack = Elastic Search + Logstash + Kibana

一、Elastic Search

官方生产部署文档:https://www.elastic.co/guide/en/elasticsearch/guide/current/deploy.html

Install JDK

首先需要安装JDK,安装过程省略,请参见相关文档。

  1. [root@Node-A1 tools]# java -version
  2. java version "1.8.0_25"
  3. Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
  4. Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

权威指南:
http://www.learnes.net/index.html
http://es.xiaoleilu.com

它是一款基于Apache Lucene构建的开源分布式全文检索服务器,提供RESTful API

Version: 1.x

安装非常的简单,解压后即可使用:

  1. [root@Node-A1 tools]# tar zxvf elasticsearch-1.7.2.tar.gz
  2. [root@Node-A1 tools]# mv elasticsearch-1.7.2 /usr/local/elasticsearch
  3. --------------------
  4. [root@Node-A1 tools]# tree /usr/local/elasticsearch/
  5. /usr/local/elasticsearch/
  6. config
  7. ├── elasticsearch.yml # Main configuration file
  8. └── logging.yml # Log configuration file
  1. [root@Node-A1 ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml
  2. # Cluster:
  3. # 将集群的名字改为自定义的名字,只要改名称相同,那么就是为同一集群内的节点。
  4. cluster.name: elasticsearch --> cluster.name: cdmonkey
  5. # Node:
  6. node.name: "Franz Kafka" --> node.name: "Node-A1" # 该处是设置节点的名称。
  7. node.master: true # 设置该节点能否通过选举成为主节点。如果不允许其成为主节点则意味着它只能存储数据。
  8. node.data: true # 设置该节点是否存储数据。当然可以设置为不存储数据只作为主节点来使用。
  9. # Index:
  10. index.number_of_shards: 5 # 默认会将索引分为五片。
  11. index.number_of_replicas: 1 # 默认分片的副本数量。
  12. # Paths:
  13. # 设置配置及数据文件的存放路径,建议制定而不是用默认值。
  14. path.conf: /path/to/conf --> path.conf: /usr/local/elasticsearch/conf
  15. path.data: /path/to/data --> path.data: /usr/local/elasticsearch/data
  16. # 数据的存放路径可以配置为多个,使用逗号分隔。
  17. path.work: /path/to/work --> path.work: /usr/local/elasticsearch/work # 临时文件的存放路径。
  18. path.logs: /path/to/logs --> path.logs: /usr/local/elasticsearch/logs # 日志文件的存放路径。
  19. # 设置插件目录:
  20. path.plugins: /path/to/plugins --> path.plugins: /usr/local/elasticsearch/plugins
  21. # Memory:
  22. bootstrap.mlockall: true # 设置锁住内存,确保系统分配给ES足够的内存。
  23. # Network And HTTP:

Version: 2.x

http://www.cnblogs.com/muzhiye/p/elasticsearch_set_cluster.html

升级到2.2版本后,必须创建一个其他的账号用于启动elasticsearch,不能使用根用户进行启动,否则会报错。

  1. [root@es-node1 ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml
  2. # Cluster:
  3. cluster.name: my-es
  4. # Node:
  5. node.name: es-node1
  6. # Add custom attributes to the node:
  7. # node.rack: r1
  8. # Paths:
  9. path.data: /usr/local/elasticsearch/data # 设置索引数据的存储路径,可以设置多个存储路径,用逗号隔开。
  10. path.logs: /usr/local/elasticsearch/logs #
  11. # Memory:
  12. # 是否锁住内存,指长期占用该内存,不产生内存交换,防止内存交换导致性能降低,配合内存设置使用。
  13. # 服务启动时即使用足够大的内存,提高效率。
  14. bootstrap.mlockall: true
  15. # Network:
  16. network.host: 0.0.0.0
  17. http.port: 9200
  18. # Discovery:
  19. # discovery.zen.ping.unicast.hosts: ["host1", "host2"]
  20. # Gateway:
  21. # gateway.recover_after_nodes: 3
  22. # Various:
  23. node.max_local_storage_nodes: 1 # 于一台主机上最多启动的节点数。
  24. # 当移除一个索引的时候,需要指定具体索引的名称:
  25. action.destructive_requires_name: true

注意:其他节点的设定中需要把“Discovery”进行设定,否则集群是无法生效的:

  1. [root@es-node2 ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml
  2. ...
  3. discovery.zen.ping.unicast.hosts: ["172.16.1.21"]

启动服务:

  1. # 因为要使用普通用户启动服务,因而需要改变目录的属主及属组:
  2. [root@es-node1 ~]# chown -R app:app /usr/local/elasticsearch/
  3. [root@es-node1 ~]# su - app
  4. [app@es-node1 ~]$ /usr/local/elasticsearch/bin/elasticsearch
  5. # 这里需要注意:如果内存过小则无法启动(当前虚拟机内存:1G)。
  6. # 以守护进程的方式启动服务:
  7. [app@es-node1 ~]$ /usr/local/elasticsearch/bin/elasticsearch -d
  8. ----------------------
  9. [app@es-node1 ~]$ netstat -lntp|egrep "(9200|9300)"
  10. tcp 0 0 ::ffff:172.16.1.21:9300 :::* LISTEN 3010/java
  11. tcp 0 0 ::ffff:172.16.1.21:9200 :::* LISTEN 3010/java

进行测试(访问节点的对外端口):

  1. [app@es-node1 ~]$ curl http://172.16.1.21:9200
  2. {
  3. "name" : "es-node1",
  4. "cluster_name" : "my-es",
  5. "version" : {
  6. "number" : "2.3.3",
  7. "build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
  8. "build_timestamp" : "2016-05-17T15:40:04Z",
  9. "build_snapshot" : false,
  10. "lucene_version" : "5.5.0"
  11. },
  12. "tagline" : "You Know, for Search"
  13. }

至此,ES服务就已经正常运行了。只是运行起来是不够的,通常我们需要将其安装成系统服务,设置成开机自启动。

  1. [root@es-node1 ~]# git clone https://github.com/elasticsearch/elasticsearch-servicewrapper.git
  2. [root@es-node1 ~]# mv elasticsearch-servicewrapper/service/ /usr/local/elasticsearch/bin/
  3. [root@es-node1 ~]# chown -R app.app /usr/local/elasticsearch/bin/service
  4. [root@es-node1 ~]# /usr/local/elasticsearch/bin/service/elasticsearch install
  5. Detected RHEL or Fedora:
  6. Installing the Elasticsearch daemon..
  7. # 如此的话就可以同系统服务一样开机启动了:
  8. [root@es-node1 ~]# chkconfig --list elasticsearch
  9. elasticsearch 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  10. # Start service:
  11. [root@es-node1 ~]# su - app
  12. [app@es-node1 ~]$ /etc/init.d/elasticsearch start

需要注意的是,在小内存机器上运行时,需要限制下内存大小,否则服务会无法启动,出现上面的警告信息:

  1. Starting Elasticsearch...
  2. Waiting for Elasticsearch...................
  3. WARNING: Elasticsearch may have failed to start.

解决方法是需要设置ES能够分配的“JVM”内存大小。一般情况下,设置成总内存的50%比较好。

  1. [app@es-node1 ~]$ vim /usr/local/elasticsearch/bin/service/elasticsearch.conf
  2. set.default.ES_HEAP_SIZE=512

如果要限制ES_MIN_MEMES_MAX_MEM,建议设置成一样大,以避免出现频繁的内存分配。

我们通常使用curl工具来与ES进行通信,其格式为:

  1. # curl -X<verb> '<PROTOCOL>://<HOST>/<path>?<QUERY_STRING>' -d '<BODY>'
  2. -----------------
  3. # 查询实例:
  4. [root@Node-A1 ~]# curl -i -XGET 'http://172.16.1.21:9200/_count?pretty' -d '
  5. > {
  6. > "query":{
  7. > "match_all":{}
  8. > }
  9. > }'

主从节点的作用:

Install Plugin

  1. [root@Node-A1 ~]# /usr/local/elasticsearch/bin/plugin -i elasticsearch/marvel/latest
  2. # 2.x:
  3. [app@es-node1 ~]$ /usr/local/elasticsearch/bin/plugin install elasticsearch/marvel/latest

安装完成后即可访问:http://172.16.1.21:9200/_plugin/marvel

对于ES集群的管理有许多可用的插件,下面是“head”集权管理插件:

  1. #如果集群的数量非常庞大,那么就不建议使用该插件。
  2. [root@Node-A1 ~]# /usr/local/elasticsearch/bin/plugin -i mobz/elasticsearch-head
  3. [app@es-node1 ~]$ /usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head
  4. -> Installing mobz/elasticsearch-head...
  5. Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
  6. Downloading .................................................................................DONE
  7. ...
  8. Installed head into /usr/local/elasticsearch/plugins/head

安装完成后即可访问:http://172.16.1.21:9200/_plugin/head

Cluster

部署集群非常的简单,设定文件中只需要确保集群名相同、节点名称不同即可。

  1. [root@es-node2 tools]# vim /usr/local/elasticsearch/config/elasticsearch.yml
  2. cluster.name: my-es
  3. node.name: es-node2
  4. path.data: /usr/local/elasticsearch/data
  5. path.logs: /usr/local/elasticsearch/logs
  6. bootstrap.mlockall: true
  7. network.host: 172.16.1.22
  8. http.port: 9200
  9. discovery.zen.ping.unicast.hosts: ["172.16.1.21"]
  10. node.max_local_storage_nodes: 1
  11. action.destructive_requires_name: true

Get the health status

  1. [app@es-node1 ~]$ curl -XGET http://172.16.1.21:9200/_cluster/health?pretty
  2. {
  3. "cluster_name" : "my-es",
  4. "status" : "green",
  5. "timed_out" : false,
  6. "number_of_nodes" : 2,
  7. "number_of_data_nodes" : 2,
  8. "active_primary_shards" : 0,
  9. "active_shards" : 0,
  10. "relocating_shards" : 0,
  11. "initializing_shards" : 0,
  12. "unassigned_shards" : 0,
  13. "delayed_unassigned_shards" : 0,
  14. "number_of_pending_tasks" : 0,
  15. "number_of_in_flight_fetch" : 0,
  16. "task_max_waiting_in_queue_millis" : 0,
  17. "active_shards_percent_as_number" : 100.0
  18. }

Changing Settings Dynamically

https://www.elastic.co/guide/en/elasticsearch/guide/current/_changing_settings_dynamically.html

动态的改变设定。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注