@cdmonkey
2017-08-30T03:39:45.000000Z
字数 6624
阅读 1337
ELK
https://www.elastic.co/
中文指南:http://kibana.logstash.es/
学习课程:http://blog.csdn.net/jiuqiyuliang/article/details/51245335
运维人员面对的实际情况:
Solution:ELK Stack = Elastic Search + Logstash + Kibana
官方生产部署文档:https://www.elastic.co/guide/en/elasticsearch/guide/current/deploy.html
首先需要安装JDK,安装过程省略,请参见相关文档。
[root@Node-A1 tools]# java -versionjava version "1.8.0_25"Java(TM) SE Runtime Environment (build 1.8.0_25-b17)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。
安装非常的简单,解压后即可使用:
[root@Node-A1 tools]# tar zxvf elasticsearch-1.7.2.tar.gz[root@Node-A1 tools]# mv elasticsearch-1.7.2 /usr/local/elasticsearch--------------------[root@Node-A1 tools]# tree /usr/local/elasticsearch//usr/local/elasticsearch/config├── elasticsearch.yml # Main configuration file└── logging.yml # Log configuration file
[root@Node-A1 ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml# Cluster:# 将集群的名字改为自定义的名字,只要改名称相同,那么就是为同一集群内的节点。cluster.name: elasticsearch --> cluster.name: cdmonkey# Node:node.name: "Franz Kafka" --> node.name: "Node-A1" # 该处是设置节点的名称。node.master: true # 设置该节点能否通过选举成为主节点。如果不允许其成为主节点则意味着它只能存储数据。node.data: true # 设置该节点是否存储数据。当然可以设置为不存储数据只作为主节点来使用。# Index:index.number_of_shards: 5 # 默认会将索引分为五片。index.number_of_replicas: 1 # 默认分片的副本数量。# Paths:# 设置配置及数据文件的存放路径,建议制定而不是用默认值。path.conf: /path/to/conf --> path.conf: /usr/local/elasticsearch/confpath.data: /path/to/data --> path.data: /usr/local/elasticsearch/data# 数据的存放路径可以配置为多个,使用逗号分隔。path.work: /path/to/work --> path.work: /usr/local/elasticsearch/work # 临时文件的存放路径。path.logs: /path/to/logs --> path.logs: /usr/local/elasticsearch/logs # 日志文件的存放路径。# 设置插件目录:path.plugins: /path/to/plugins --> path.plugins: /usr/local/elasticsearch/plugins# Memory:bootstrap.mlockall: true # 设置锁住内存,确保系统分配给ES足够的内存。# Network And HTTP:
http://www.cnblogs.com/muzhiye/p/elasticsearch_set_cluster.html
升级到2.2版本后,必须创建一个其他的账号用于启动elasticsearch,不能使用根用户进行启动,否则会报错。
[root@es-node1 ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml# Cluster:cluster.name: my-es# Node:node.name: es-node1# Add custom attributes to the node:# node.rack: r1# Paths:path.data: /usr/local/elasticsearch/data # 设置索引数据的存储路径,可以设置多个存储路径,用逗号隔开。path.logs: /usr/local/elasticsearch/logs ## Memory:# 是否锁住内存,指长期占用该内存,不产生内存交换,防止内存交换导致性能降低,配合内存设置使用。# 服务启动时即使用足够大的内存,提高效率。bootstrap.mlockall: true# Network:network.host: 0.0.0.0http.port: 9200# Discovery:# discovery.zen.ping.unicast.hosts: ["host1", "host2"]# Gateway:# gateway.recover_after_nodes: 3# Various:node.max_local_storage_nodes: 1 # 于一台主机上最多启动的节点数。# 当移除一个索引的时候,需要指定具体索引的名称:action.destructive_requires_name: true
注意:其他节点的设定中需要把“Discovery”进行设定,否则集群是无法生效的:
[root@es-node2 ~]# vim /usr/local/elasticsearch/config/elasticsearch.yml...discovery.zen.ping.unicast.hosts: ["172.16.1.21"]
启动服务:
# 因为要使用普通用户启动服务,因而需要改变目录的属主及属组:[root@es-node1 ~]# chown -R app:app /usr/local/elasticsearch/[root@es-node1 ~]# su - app[app@es-node1 ~]$ /usr/local/elasticsearch/bin/elasticsearch# 这里需要注意:如果内存过小则无法启动(当前虚拟机内存:1G)。# 以守护进程的方式启动服务:[app@es-node1 ~]$ /usr/local/elasticsearch/bin/elasticsearch -d----------------------[app@es-node1 ~]$ netstat -lntp|egrep "(9200|9300)"tcp 0 0 ::ffff:172.16.1.21:9300 :::* LISTEN 3010/javatcp 0 0 ::ffff:172.16.1.21:9200 :::* LISTEN 3010/java
进行测试(访问节点的对外端口):
[app@es-node1 ~]$ curl http://172.16.1.21:9200{"name" : "es-node1","cluster_name" : "my-es","version" : {"number" : "2.3.3","build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde","build_timestamp" : "2016-05-17T15:40:04Z","build_snapshot" : false,"lucene_version" : "5.5.0"},"tagline" : "You Know, for Search"}
至此,ES服务就已经正常运行了。只是运行起来是不够的,通常我们需要将其安装成系统服务,设置成开机自启动。
[root@es-node1 ~]# git clone https://github.com/elasticsearch/elasticsearch-servicewrapper.git[root@es-node1 ~]# mv elasticsearch-servicewrapper/service/ /usr/local/elasticsearch/bin/[root@es-node1 ~]# chown -R app.app /usr/local/elasticsearch/bin/service[root@es-node1 ~]# /usr/local/elasticsearch/bin/service/elasticsearch installDetected RHEL or Fedora:Installing the Elasticsearch daemon..# 如此的话就可以同系统服务一样开机启动了:[root@es-node1 ~]# chkconfig --list elasticsearchelasticsearch 0:off 1:off 2:on 3:on 4:on 5:on 6:off# Start service:[root@es-node1 ~]# su - app[app@es-node1 ~]$ /etc/init.d/elasticsearch start
需要注意的是,在小内存机器上运行时,需要限制下内存大小,否则服务会无法启动,出现上面的警告信息:
Starting Elasticsearch...Waiting for Elasticsearch...................WARNING: Elasticsearch may have failed to start.
解决方法是需要设置ES能够分配的“JVM”内存大小。一般情况下,设置成总内存的50%比较好。
[app@es-node1 ~]$ vim /usr/local/elasticsearch/bin/service/elasticsearch.confset.default.ES_HEAP_SIZE=512
如果要限制
ES_MIN_MEM、ES_MAX_MEM,建议设置成一样大,以避免出现频繁的内存分配。
我们通常使用curl工具来与ES进行通信,其格式为:
# curl -X<verb> '<PROTOCOL>://<HOST>/<path>?<QUERY_STRING>' -d '<BODY>'-----------------# 查询实例:[root@Node-A1 ~]# curl -i -XGET 'http://172.16.1.21:9200/_count?pretty' -d '> {> "query":{> "match_all":{}> }> }'
主从节点的作用:
[root@Node-A1 ~]# /usr/local/elasticsearch/bin/plugin -i elasticsearch/marvel/latest# 2.x:[app@es-node1 ~]$ /usr/local/elasticsearch/bin/plugin install elasticsearch/marvel/latest
安装完成后即可访问:http://172.16.1.21:9200/_plugin/marvel
对于ES集群的管理有许多可用的插件,下面是“head”集权管理插件:
#如果集群的数量非常庞大,那么就不建议使用该插件。[root@Node-A1 ~]# /usr/local/elasticsearch/bin/plugin -i mobz/elasticsearch-head[app@es-node1 ~]$ /usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head-> Installing mobz/elasticsearch-head...Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...Downloading .................................................................................DONE...Installed head into /usr/local/elasticsearch/plugins/head
安装完成后即可访问:http://172.16.1.21:9200/_plugin/head
部署集群非常的简单,设定文件中只需要确保集群名相同、节点名称不同即可。
[root@es-node2 tools]# vim /usr/local/elasticsearch/config/elasticsearch.ymlcluster.name: my-esnode.name: es-node2path.data: /usr/local/elasticsearch/datapath.logs: /usr/local/elasticsearch/logsbootstrap.mlockall: truenetwork.host: 172.16.1.22http.port: 9200discovery.zen.ping.unicast.hosts: ["172.16.1.21"]node.max_local_storage_nodes: 1action.destructive_requires_name: true
[app@es-node1 ~]$ curl -XGET http://172.16.1.21:9200/_cluster/health?pretty{"cluster_name" : "my-es","status" : "green","timed_out" : false,"number_of_nodes" : 2,"number_of_data_nodes" : 2,"active_primary_shards" : 0,"active_shards" : 0,"relocating_shards" : 0,"initializing_shards" : 0,"unassigned_shards" : 0,"delayed_unassigned_shards" : 0,"number_of_pending_tasks" : 0,"number_of_in_flight_fetch" : 0,"task_max_waiting_in_queue_millis" : 0,"active_shards_percent_as_number" : 100.0}
https://www.elastic.co/guide/en/elasticsearch/guide/current/_changing_settings_dynamically.html
动态的改变设定。