Elasticsearch5.4集群(一)安装部署

来源:互联网 发布:java weka 编辑:程序博客网 时间:2024/06/05 14:14
转载自:http://blog.csdn.net/u013673976/article/details/73883617
ES版本升级

生产环境用的是1.7,Elasticsearch5.x在性能上有了很大的提升,计划升级到5.4,先在线下部署验证,过程中发现很多配置项都改了,各种报错。
一定要看官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html,网上找到的很多都过时的低版本配置,要么不对要么不全,还需要注意下breaking changes(不兼容变更项)。

节点功能分离

master、data、client分开部署,规划存储容量、内存、CPU(权威指南:https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html)。master节点不存储数据,内存和cpu都比较低,一般部署3个master保证可靠。不建议用一块很大的磁盘,建议用多块SSD,可以并发IO。不是clien的节点可以不开启http服务的节点(http.enabled: false)同时也不要安装head, bigdesk, marvel等监控插件,这样保证data节点服务器只需处理创建/更新/删除/查询索引数据等操作。

内存分配

内存不要超过32G,java版本使用jdk1.8(We recommend installing Java version 1.8.0_131 or later),es最大分配-Xms32766m -Xmx32766m(内存对象指针压缩技术),GC使用G1不要设置-Xmn(参照垃圾回收配置:http://www.oracle.com/technetwork/articles/java/g1gc-1984535.html)。物理机上的内存之分一半给ES,另一半给Lucene,否则性能会受影响。128G内存的机器最多部署2个es实例。

系统配置修改

禁用swap(在一些内核版本,swappness=0会引发OOM)。vim /etc/sysctl.conf 
修改 vm.swappiness=1,设置vm.max_map_count=262144(否则启动会报错:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144])

[plain] view plain copy
  1. vm.swappiness=1  
  2. vm.max_map_count=262144  
立即生效需执行 sysctl -p
修改es用户限制,不能用root启动Elasticsearch,比如使用tomcat用户(用户重新登录生效)
vim /etc/security/limits.conf
[plain] view plain copy
  1. tomcat soft memlock unlimited  
  2. tomcat hard memlock unlimited  

安装Elasticsearch5.4.1

安装步骤参照(https://www.elastic.co/downloads/elasticsearch),我们把es安装到/usr/local/目录下

[plain] view plain copy
  1. cd /usr/local/  
  2. wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz  
  3. sha1sum elasticsearch-5.4.1.tar.gz   
  4. tar -xzf elasticsearch-5.4.1.tar.gz  
  5. cd elasticsearch-5.4.1/  
建立软链接:
[plain] view plain copy
  1. ln -s /usr/local/elasticsearch-5.4.1 elasticsearch  
更新安装目录/usr/local/elasticsearch/bin/elasticsearch启动脚本,修改查找jvm配置文件顺序,加入"$LEIDA_ES_HOME"/config/jvm.options,让虚拟机参数优先使用实例自己的配置,因为我们一个物理机上会启用多个es实例
[plain] view plain copy
  1. if [ -z "$ES_JVM_OPTIONS" ]; then  
  2.     for jvm_options in "$LEIDA_ES_HOME"/config/jvm.options \  
  3.                        "$ES_HOME"/config/jvm.options \  
  4.                       /etc/elasticsearch/jvm.options; do  
  5.         if [ -r "$jvm_options" ]; then  
  6.             ES_JVM_OPTIONS=$jvm_options  
  7.             break  
  8.         fi  
  9.     done  
  10. fi  
修改JVM参数的堆大小和G1GC方式(vim /usr/local/elasticsearch/config/jvm.options):
[plain] view plain copy
  1. -Xms32766m  
  2. -Xmx32766m  
  3.   
  4. ## GC configuration  
  5. #-XX:+UseConcMarkSweepGC  
  6. #-XX:CMSInitiatingOccupancyFraction=75  
  7. #-XX:+UseCMSInitiatingOccupancyOnly  
  8.   
  9. ## G1GC  
  10. -XX:+UseG1GC  
  11. -XX:MaxGCPauseMillis=800  
  12. -XX:ParallelGCThreads=15  
  13. -XX:ConcGCThreads=4  

mmseg分词器

下载elasticsearch-analysis-mmseg-5.4.1.zip
下载地址:https://github.com/medcl/elasticsearch-analysis-mmseg/releases
(elasticsearch.yml不支持参数path.plugins参数,如果一台机器上启多个实例,想配置每个实例自己的插件,比如master节点不加载head插件,需要改下源码读取路径和check验证,默认是读取安装路径下的/usr/local/elasticsearch/plugins/)

[plain] view plain copy
  1. cd /usr/local/elasticsearch/plugins/  
  2. unzip elasticsearch-analysis-mmseg-5.4.1.zip -d elasticsearch-analysis-mmseg-5.4.1  

创建master

实例配置存放在/data/es/es_[tcpport]下

[plain] view plain copy
  1. cd /data/es  
  2. mkdir -p es_9300/bin  
  3. mkdir -p es_9300/config  
  4. mkdir -p es_9300/data  
  5. mkdir -p es_9300/logs  
  6.   
  7. cp /usr/local/elasticsearch/config/log4j2.properties /data/es/es_9300/config/log4j2.properties  
  8. cp /usr/local/elasticsearch/config/jvm.options /data/es/es_9300/config/jvm.options  
修改JVM堆大小,vim /data/es/es_9300/config/jvm.options,安装目录的默认-Xms32766m是给datanode用的,master的堆需要改小。

cp /usr/local/elasticsearch/config/elasticsearch.yml /data/es/es_9300/config/elasticsearch.yml
修改elasticsearch.yml如下, IP、端口和存储路径需要自己替换,其他2个master的配置类似。为避免脑裂,discovery.zen.minimum_master_nodes设为2(非生产环境验证优先保证可用这里设为1)
[plain] view plain copy
  1. bootstrap.memory_lock: true  
  2. bootstrap.system_call_filter: false  
  3.   
  4. cluster.name: loganalysis  
  5. node.attr.box_type: master  
  6. node.name: master_1  
  7. node.master: true  
  8. node.data: false  
  9.   
  10. discovery.zen.minimum_master_nodes: 1  
  11. discovery.zen.ping.unicast.hosts: ["master1ip:9300","master2ip:9300","master3ip:9300"]  
  12. discovery.zen.fd.ping_timeout: 50s  
  13. discovery.zen.fd.ping_retries: 6  
  14.   
  15. cluster.routing.allocation.node_initial_primaries_recoveries: 6  
  16. cluster.routing.allocation.node_concurrent_recoveries: 6  
  17. cluster.routing.allocation.cluster_concurrent_rebalance: 4  
  18.   
  19. network.host: master1ip  
  20. http.port: 9200  
  21. transport.tcp.port: 9300  
  22. path.conf: /data/es/es_9300/config  
  23. path.data: /data/es/es_9300/data  
  24. path.logs: /data/es/es_9300/logs  
  25.   
  26. http.cors.enabled: true  
  27. http.cors.allow-origin: /.*/  
  28. http.cors.allow-credentials: true  

启动脚本

因为一个物理机上只安装了一个es,但是要启用多个es实例,需要区分config文件的路径,把别的脚本拷贝过来改成我们的leidaES.sh,启动master

[plain] view plain copy
  1. cd /data/es/es_9300/bin  
  2. ./leidaES.sh start  
  3. elasticsearch's real path : /usr/local/elasticsearch-5.4.1  
  4. Starting elasticsearch es_9300:                            [  OK  ]  


脚本内容其实就是加了些验证和start、stop参数,停止命令./leidaES.sh stop
[plain] view plain copy
  1. #!/bin/sh  
  2. #   
  3. # init.d / servicectl compatibility (openSUSE)  
  4. #  
  5. if [ -f /etc/rc.status ]; then  
  6.     . /etc/rc.status  
  7.     rc_reset  
  8. fi  
  9.   
  10. #  
  11. # Source function library.  
  12. #  
  13. if [ -f /etc/rc.d/init.d/functions ]; then  
  14.     . /etc/rc.d/init.d/functions  
  15. fi  
  16.   
  17. MAX_OPEN_FILES=600000  
  18. # max locked memory   (kbytes, -l) 32G=1024*1024*32  
  19. MAX_LOCKED_MEMORY=33554432  
  20. # vim /etc/sysctl.conf add vm.max_map_count=262144  
  21. #MAX_MAP_COUNT=262144  
  22.   
  23. ES_USER="tomcat"  
  24. ES_GROUP="tomcat"  
  25. JAVA_HOME=/usr/local/java/jdk1.8  
  26. ES_HOME="/usr/local/elasticsearch"  
  27.   
  28. SCRIPT="$0"  
  29. # determine leida elasticsearch home  
  30. LEIDA_ES_HOME=`dirname "$SCRIPT"`/..  
  31. # make LEIDA ELASTICSEARCH_HOME absolute  
  32. LEIDA_ES_HOME=`cd "$LEIDA_ES_HOME"; pwd`  
  33. ES_NODE=`echo $LEIDA_ES_HOME|awk -F'/' '{print $NF}'`  
  34.   
  35. CONF_DIR="${LEIDA_ES_HOME}/config"  
  36. WORK_DIR="/tmp/elasticsearch"  
  37. CONF_FILE="${CONF_DIR}/elasticsearch.yml"  
  38.   
  39. #export ES_HOME  
  40. #export JAVA_HOME  
  41. export LEIDA_ES_HOME  
  42.   
  43. exec="$ES_HOME/bin/elasticsearch"  
  44. if [ -f "$exec" ]; then  
  45.     chmod 755 "$exec"  
  46. fi  
  47. prog="elasticsearch"  
  48. pidfile="$LEIDA_ES_HOME/${prog}.pid"  
  49. lockfile=/var/lock/subsys/$prog  
  50.   
  51. # backwards compatibility for old config sysconfig files, pre 0.90.1  
  52. if [ -n $USER ] && [ -z $ES_USER ] ; then  
  53.    ES_USER=$USER  
  54. fi  
  55.   
  56. checkJava() {  
  57.     if [ -x "$JAVA_HOME/bin/java" ]; then  
  58.         JAVA="$JAVA_HOME/bin/java"  
  59.     else  
  60.         JAVA=`which java`  
  61.     fi  
  62.   
  63.     if [ ! -x "$JAVA" ]; then  
  64.         echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"  
  65.         exit 1  
  66.     fi  
  67. }  
  68.   
  69. start() {  
  70.     checkJava  
  71.     [ -x $exec ] || echo "$exec script file is not executable." ||exit 5  
  72.     [ -f $CONF_FILE ] || echo "$CONF_FILE is not exists." || exit 6  
  73. #    if [ -n "$MAX_LOCKED_MEMORY" -a -z "$ES_HEAP_SIZE" ]; then  
  74. #        echo "MAX_LOCKED_MEMORY is set - ES_HEAP_SIZE must also be set"  
  75. #        return 7  
  76. #    fi  
  77.     if [ -n "$MAX_OPEN_FILES" ]; then  
  78.         ulimit -n $MAX_OPEN_FILES  
  79.     fi  
  80.     if [ -n "$MAX_LOCKED_MEMORY" ]; then  
  81.         ulimit -l $MAX_LOCKED_MEMORY  
  82.     fi  
  83.     if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then  
  84.         sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT  
  85.     fi  
  86.     if [ -n "$WORK_DIR" ]; then  
  87.         mkdir -p "$WORK_DIR"  
  88.         chown "$ES_USER":"$ES_GROUP" "$WORK_DIR"  
  89.     fi  
  90.   
  91.     # Ensure that the LEIDA_ES_HOME exists (it is cleaned at OS startup time)  
  92.     if [ -n "$LEIDA_ES_HOME" ] && [ ! -e "$LEIDA_ES_HOME" ]; then  
  93.         mkdir -p "$LEIDA_ES_HOME" && chown "$ES_USER":"$ES_GROUP" "$LEIDA_ES_HOME"  
  94.     fi  
  95.     chown -R "$ES_USER":"$ES_GROUP" "$LEIDA_ES_HOME"  
  96.     chown -R "$ES_USER":"$ES_GROUP" "${ES_HOME}"  
  97.     if [ -h "$ES_HOME" ]; then  
  98.         eslink=`ls -ld "$ES_HOME"`  
  99.         # Drop everything prior to ->  
  100.         eslink=`expr "$eslink" : '.*-> .$'`  
  101.         echo "elasticsearch's real path : $eslink"  
  102.         if expr "$eslink" : '/.*' > /dev/null; then  
  103.             chown -R "$ES_USER":"$ES_GROUP" "${eslink}"  
  104.         fi  
  105.     fi  
  106.   
  107.     if [ -n "$pidfile" ] && [ ! -e "$pidfile" ]; then  
  108.         touch "$pidfile" && chown "$ES_USER":"$ES_GROUP" "$pidfile"  
  109.     fi  
  110.   
  111.     echo -n $"Starting $prog ${ES_NODE}: "  
  112.     # if not running, start it up here, usually something like "daemon $exec" # -Djava.io.tmpdir=${WORK_DIR}  
  113.     daemon --user $ES_USER --pidfile $pidfile $exec -d -p $pidfile -Epath.conf=${CONF_DIR}  
  114.     retval=$?  
  115.     echo  
  116.     [ $retval -eq 0 ] && touch $lockfile  
  117.     return $retval  
  118. }  
  119.   
  120. stop() {  
  121.     echo -n $"Stopping $prog ${ES_NODE}: "  
  122.     # stop it here, often "killproc $prog"  
  123.     killproc -p $pidfile -d 20 $prog  
  124.     retval=$?  
  125.     echo  
  126.     [ $retval -eq 0 ] && rm -f $lockfile  
  127.     return $retval  
  128. }  
  129.   
  130. restart() {  
  131.     stop  
  132.     start  
  133. }  
  134.   
  135. reload() {  
  136.     restart  
  137. }  
  138.   
  139. force_reload() {  
  140.     restart  
  141. }  
  142.   
  143. rh_status() {  
  144.     # run checks to determine if the service is running or use generic status  
  145.     status -p $pidfile $prog  
  146. }  
  147.   
  148. rh_status_q() {  
  149.     rh_status >/dev/null 2>&1  
  150. }  
  151.   
  152. case "$1" in  
  153.     start)  
  154.         rh_status_q && exit 0  
  155.         $1  
  156.         ;;  
  157.     stop)  
  158.         rh_status_q || exit 0  
  159.         $1  
  160.         ;;  
  161.     restart)  
  162.         $1  
  163.         ;;  
  164.     reload)  
  165.         rh_status_q || exit 7  
  166.         $1  
  167.         ;;  
  168.     force-reload)  
  169.         force_reload  
  170.         ;;  
  171.     status)  
  172.         rh_status  
  173.         ;;  
  174.     condrestart|try-restart)  
  175.         rh_status_q || exit 0  
  176.         restart  
  177.         ;;  
  178.     *)  
  179.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"  
  180.         exit 2  
  181. esac  
  182. exit $?  

创建datanode

和master节点的配置类似,注意node.master:false和node.data:true;node.name在集群中唯一。生产环境我们要做冷热分离,es1.7是通过分组来实现,ES5已改成node.attr.box_type。另外最好datanode和clientnode独立,非生产环境就合二为一了。cp /usr/local/elasticsearch/config/elasticsearch.yml /data/es/es_9301/config/elasticsearch.yml修改elasticsearch.yml如下:

[plain] view plain copy
  1. bootstrap.memory_lock: true  
  2. bootstrap.system_call_filter: false  
  3.   
  4. cluster.name: loganalysis  
  5. node.attr.box_type: hot  
  6. node.name: hot_1  
  7. node.master: false  
  8. node.data: true  
  9.   
  10. discovery.zen.minimum_master_nodes: 1  
  11. discovery.zen.ping.unicast.hosts: ["master1ip:9300","master2ip:9300","master3ip:9300"]  
  12. discovery.zen.fd.ping_timeout: 50s  
  13. discovery.zen.fd.ping_retries: 6  
  14.   
  15. cluster.routing.allocation.node_initial_primaries_recoveries: 6  
  16. cluster.routing.allocation.node_concurrent_recoveries: 6  
  17. cluster.routing.allocation.cluster_concurrent_rebalance: 4  
  18.   
  19. network.host: 改成机器的IP  
  20. http.port: 9201  
  21. transport.tcp.port: 9301  
  22. path.conf: /data/es/es_9301/config  
  23. path.data: /data/es/es_9301/data  
  24. path.logs: /data/es/es_9301/logs  
  25.   
  26. http.cors.enabled: true  
  27. http.cors.allow-origin: /.*/  
  28. http.cors.allow-credentials: true  

启动数据节点

先启动3个master节点,再启动配置的多个数据节点,组成Es集群

[plain] view plain copy
  1. cd /data/es/es_9301/bin  
  2. ./leidaES.sh start  
  3. elasticsearch's real path : /usr/local/elasticsearch-5.4.1  
  4. Starting elasticsearch es_9301:                            [  OK  ]  

创建动态模版

配置mmseg分词和默认一个分片,不要副本。每个doc都有time字段和存储日志的msg字段。

[plain] view plain copy
  1. PUT /_template/default_template  
  2. {  
  3.    "template": "*",  
  4.    "settings": {  
  5.       "index.refresh_interval": "15s",  
  6.       "index.translog.flush_threshold_size": "1gb",  
  7.       "index.translog.sync_interval": "1000000ms",  
  8.       "index.codec": "best_compression",  
  9.       "index.number_of_replicas": "0",  
  10.       "index.number_of_shards": "1"  
  11.    },  
  12.    "mappings": {  
  13.       "_default_": {  
  14.          "_source": {  
  15.          },  
  16.          "dynamic_templates": [  
  17.             {  
  18.                "msg": {  
  19.                   "path_match": "msg",  
  20.                   "mapping": {  
  21.                      "analyzer": "mmseg_maxword",  
  22.                      "search_analyzer": "mmseg_maxword",  
  23.                      "omit_norms": true,  
  24.                      "type": "text"  
  25.                   }  
  26.                }  
  27.             },  
  28.             {  
  29.                "time": {  
  30.                   "path_match": "time",  
  31.                   "mapping": {  
  32.                      "type": "date"  
  33.                   }  
  34.                }  
  35.             },  
  36.             {  
  37.                "other": {  
  38.                   "mapping": {  
  39.                      "fielddata": {  
  40.                         "format": "doc_values"  
  41.                      },  
  42.                      "index": "not_analyzed",  
  43.                      "ignore_above":1000,  
  44.                      "omit_norms": true,  
  45.                      "doc_values": true  
  46.                   },  
  47.                   "match": "*"  
  48.                }  
  49.             }  
  50.          ],  
  51.          "_all": {  
  52.             "enabled": false  
  53.          }  
  54.       }  
  55.    },  
  56.    "aliases": {}  
  57. }  
测试中文分词`mmseg_maxword` ,`mmseg_complex` ,`mmseg_simple`
[plain] view plain copy
  1. curl -X GET "localhost:9200/_analyze?analyzer=mmseg_maxword&pretty=true" -d '中华人民共和国12**3朝阳群众,www.elastic.co'  

集群状态API接口测试

查看集群状态
curl 'localhost:9200/_cat/health?v'

创建索引(sense插件执行)
PUT /test

插入doc数据
POST /test/china/1
{"msg":"美国留给伊拉克的是个烂摊子吗"}
POST /test/china/2
{"msg":"公安部:各地校车将享最高路权"}
POST /test/china/3
{"msg":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
POST /test/china/4
{"msg":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

搜索数据,能搜到2条数据。
POST /test/china/_search?
{"query":{"bool":{"must":[{"match_phrase_prefix":{"msg":{"query":"中国"}}}],"must_not":[]}},"from":0,"size":40}

至此集群正常运行,还需安装监控插件,后续优化配置