elasticsearch项目开发-elasticsearch6.2.4环境搭建

ES开发环境搭建

背景
目前公司在做巡视整改项目,需要全文检索功能。所以把自己学习并在实践中遇到的问题记录下来,共同学习和进一步加深对es的了解。

了解Elasticsearch

你知道的,为了搜索…
推荐
Elasticsearch学习,请先看这一篇!

环境准备

我是在VMware虚拟机中操作

  • CentOS7
  • jdk1.8
  • elasticsearch6.2.4

一. CentOS7系统配置java运行环境

  1. jdk1.8 tar.gz包下载:

     https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
    
  2. 解压tar.gz包(在这我把jdk1.8的tar包放在linux目录/opt/java/下):

     tar -xzvf jdk1.8.0_201.tar.gz
    
  3. 配置环境变量:

     vi /etc/profile
     在/etc/profile文件里加入下面代码
     export JAVA_HOME=/opt/java/jdk1.8.0_201
     export PATH=.:$JAVA_HOME/bin:$PATH
     export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    
  4. 刷新刚配置好的profile文件:

     source /etc/profile
    
  5. 检查是否配置成功:

     java -version
     出现下面版本信息说明配置成功
     java version "1.8.0_201"
     Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
     Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
    

二.搭建elasticsearch6.2.4服务

  1. 下载elasticsearch6.2.4.tar.gz包:

     https://www.elastic.co/cn/downloads/past-releases/elasticsearch-6-2-4
    
  2. 解压tar.gz包(在这我把tar包放在linux目录/opt/elasticsearch/下):

     tar -xzvf elasticsearch-6-2-4.tar.gz
    
  3. 在这里先修改一下es配置文件(vi /opt/elasticsearch/elasticsearch-6-2-4/config/elasticsearch.yml)

	# ======================== Elasticsearch Configuration =========================
	#
	# NOTE: Elasticsearch comes with reasonable defaults for most settings.
	#       Before you set out to tweak and tune the configuration, make sure you
	#       understand what are you trying to accomplish and the consequences.
	#
	# The primary way of configuring a node is via this file. This template lists
	# the most important settings you may want to configure for a production cluster.
	#
	# Please consult the documentation for further information on configuration options:
	# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
	#
	# ---------------------------------- Cluster -----------------------------------
	#
	# Use a descriptive name for your cluster:
	#集群名
	cluster.name: elasticsearch
	#
	# ------------------------------------ Node ------------------------------------
	#
	# Use a descriptive name for the node:
	#当前节点名
	node.name: node-1
	#
	# Add custom attributes to the node:
	#
	#node.attr.rack: r1
	#
	# ----------------------------------- Paths ------------------------------------
	#
	# Path to directory where to store the data (separate multiple locations by comma):
	#当前节点索引数据保存的路径
	path.data: /opt/elasticsearch/data
	#
	# Path to log files:
	#保存日志的路径
	path.logs: /opt/elasticsearch/logs
	#
	# ----------------------------------- Memory -----------------------------------
	#
	# Lock the memory on startup:
	#设置为true来锁住内存。因为当jvm开始swapping时es的效率会降低,所以要保证它不swap,可以把ES_MIN_MEM和
	#ES_MAX_MEM两个环境变量设置成同一个值,并且保证机器有足够的内存分配给es。
	#同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过ulimit -l unlimited命令。
	#bootstrap.memory_lock: true
	#
	# Make sure that the heap size is set to about half the memory available
	# on the system and that the owner of the process is allowed to use this
	# limit.
	#
	# Elasticsearch performs poorly when the system is swapping the memory.
	#
	# ---------------------------------- Network -----------------------------------
	#
	# Set the bind address to a specific IP (IPv4 or IPv6):
	#虚拟机ip地址
	network.host: 192.168.22.145
	#
	# Set a custom port for HTTP:
	#端口
	http.port: 9200
	#
	# For more information, consult the network module documentation.
	#
	# --------------------------------- Discovery ----------------------------------
	#
	# Pass an initial list of hosts to perform discovery when new node is started:
	# The default list of hosts is ["127.0.0.1", "[::1]"]
	#设置集群中的主节点的初始列表,能自动发现列表中节点启动
	#我这里配置的是单机
	discovery.zen.ping.unicast.hosts: ["192.168.22.145"]
	#discovery.zen.ping.unicast.hosts: ["192.168.22.143","192.168.22.144","192.168.22.145"]
	#
	# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
	#设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。
	#默认为1,对于大的集群来说,可以设置大一点的值(2-4)
	#discovery.zen.minimum_master_nodes: 
	#
	# For more information, consult the zen discovery module documentation.
	#
	# ---------------------------------- Gateway -----------------------------------
	#
	# Block initial recovery after a full cluster restart until N nodes are started:
	#设置集群中N个节点启动时进行数据恢复
	#gateway.recover_after_nodes: 3
	#
	# For more information, consult the gateway module documentation.
	#
	# ---------------------------------- Various -----------------------------------
	#
	# Require explicit names when deleting indices:
	#
	#action.destructive_requires_name: true

  1. 简单配置完已经可以准备运行了

     cd elasticsearch-6-2-4/
     //启动
     bin/elasticsearch
    

启动报错 can not run elasticsearch as root

elasticsearch项目开发-elasticsearch6.2.4环境搭建_第1张图片

es启动时不能用root用户,所以新建一个es启动的用户,并赋权限:

	1.创建es用户
	useradd esuser
	
	2.给esuser添加密码
	passwd esuser
	
	3.给esuser赋予操作elasticsearch-6-2-4文件夹的权限(我有关es的文件都放在elasticsearch就直接赋予esuser操作elasticsearch权限)
	chown -R esuser:esuser /opt/elasticsearch
	
	4.切换esuser用户
	su esuser

做完上面操作,再次启动es

	bin/elasticsearch

elasticsearch项目开发-elasticsearch6.2.4环境搭建_第2张图片
又出现问题。

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

原因是当前操作用户最大可创建文件数太小,无法创建,给它增加就完事了。

	先切换到root用户
	su root
	然后编辑limits.conf文件
	vi /etc/security/limits.conf
	在limits.conf文件最后添加
	* hard nofile 65536  
  	* soft nofile 65536

[2]: max number of threads [3795] for user [esuser] is too low, increase to at least [4096]

原因是当前用户esuser的最大线程数为3795太小了,也给它增加

	切换到root用户
	su root
	然后编辑20-nproc.conf文件
	vi /etc/security/limits.d/20-nproc.conf
	在20-nproc.conf文件添加
	* soft nproc 4096
	* hard nproc 4096

[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

原因是当前虚拟内存太小,给它增加

	切换到root用户
	su root
	在/etc/sysctl.conf文件最后添加一行
	vm.max_map_count=262144
	重启虚拟机

以上问题解决后切换到esuser用户在启动

	bin/elasticsearch

elasticsearch项目开发-elasticsearch6.2.4环境搭建_第3张图片
这回不在报错,然后访问在配置文件设置的http://192.168.22.145:9200

elasticsearch项目开发-elasticsearch6.2.4环境搭建_第4张图片

至此es服务启动成功。

es环境简单搭建好,下一步在进行elasticsearch-head图形化界面插件安装。

参考文档
Elasticsearch: 权威指南
ELK学习系列文章第二章:elasticsearch常见错误与配置简介

你可能感兴趣的:(elasticsearch)