elasticsearch漫步走 - 第二章 - 安装在使用之前

安装在使用之前

  • linux下安装Elasticsearch
    • 1 安装准备
      • 1.1 关闭防火墙
      • 1.2 语言配置
      • 1.3 JDK版本
    • 2 Elasticsearch下载
  • 3 安装Elasticsearch
    • 3.1 新增Elasticsearch用户
    • 3.2 上传Elasticsearch文件
    • 3.3 解压Elasticsearch文件
  • 4 系统参数的配置
    • 4.1 JVM配置
    • 4.2 禁用swapping
    • 4.3 配置处理文件数量
    • 4.4 虚拟内存配置
    • 4.4 线程池配置
  • 5 Elasticsearch 参数配置
  • 6 启动Elasticsearch
  • Tips - Elasticsearch需要配置

安装在使用之前,这不是废话么! 反正这辈子废话也说了很多,不差这一句!早上被项目数据问题搞得头疼,内心mmp
其实官网上有很详细的安装说明,我自己以前也写过,这篇就偷个懒吧,顺便根据官网的安装说明完善一下。下面都是根据我已有经验去做的。这句话是我后面回过头来加的,现在界面改变好多了

linux下安装Elasticsearch

1 安装准备

1.1 关闭防火墙

[root@host-10-146-112-217 /]# service iptables status
未关闭,则需要手动关闭防火墙,步骤:
[root@host-10-146-112-217 /]# setup
按enter进入防火墙设置界面,将[*]的按空格键取消,再保存,如下图:
elasticsearch漫步走 - 第二章 - 安装在使用之前_第1张图片

1.2 语言配置

[root@host-10-146-112-218 ~]# vi /etc/sysconfig/i18n
将内容改成:
LANG=“zh_CN.GBK”
LANGUAGE=“zh_CN.GB18030:zh_CN.GB2312:zh_CN”
SYSFONT=“latarcyrheb-sun16”
SUPPORTED=“zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en”

1.3 JDK版本

Elasticsearch5.x以上的版本需要jdk1.8版本,则需要在安装前,确保安装环境的JDK版本为1.8版本。如果版本不是1.8版本,则自行安装。
在这里插入图片描述
jdk安装方法:

1、将安装包上传到opt文件夹下
2、解压
[root@localhost java]# tar -zxvf jdk-7u79-linux-x64.tar.gz
方法一:设置环境变量
[root@localhost java]# vi /etc/profile

  • 1、在profile中添加如下内容:
    #set java environment
    JAVA_HOME=/opt/jdk1.8.0_121
    JRE_HOME=/opt/jdk1.8.0_121/jre
    CLASS_PATH=.: J A V A H O M E / l i b / d t . j a r : JAVA_HOME/lib/dt.jar: JAVAHOME/lib/dt.jar:JAVA_HOME/lib/tools.jar: J R E H O M E / l i b P A T H = JRE_HOME/lib PATH= JREHOME/libPATH=PATH: J A V A H O M E / b i n : JAVA_HOME/bin: JAVAHOME/bin:JRE_HOME/bin
    export JAVA_HOME JRE_HOME CLASS_PATH PATH
  • 2、重启:source /etc/profile
  • 3、查看:[root@host-10-146-112-217 /]# java -version,看是否是1.8版本
    方法二:unlink /usr/bin/java
    [root@host-10-146-112-217 opt]# unlink /usr/bin/java
    [root@host-10-146-112-217 opt]# ln –s /opt/jdk1.8.0_121/bin/java /usr/bin/java
    [root@host-10-146-112-217 opt]#source /etc/profile。或者断开重连。
    [root@host-10-146-112-217 opt]#java –version

elasticsearch漫步走 - 第二章 - 安装在使用之前_第2张图片

2 Elasticsearch下载

下载地址: https://www.elastic.co/cn/downloads
点击下面图标进行下载。
elasticsearch漫步走 - 第二章 - 安装在使用之前_第3张图片
下载版本如下图
elasticsearch漫步走 - 第二章 - 安装在使用之前_第4张图片
上面只是根据此刻最新的界面截图来说明。可以选择稍微稳定一定的版本下载。>>历史版本由此进

elasticsearch漫步走 - 第二章 - 安装在使用之前_第5张图片

3 安装Elasticsearch

3.1 新增Elasticsearch用户

在root用户下新增es用户,具体操作如下:
[root@135 home]# useradd es
[root@135 home]# passwd es
以下都用该用户来进行相关的配置。

给其赋予管理员权限
先cd /home/es,进入es下,
[root@135 es]# vi /etc/sudoers
加入如下内容:
es ALL=(ALL) ALL

使用x! 退出readonly文件

3.2 上传Elasticsearch文件

把下载的Elasticsearch版本放在es用户下
[root@135 es]# pwd
/home/es
[root@135 es]# ls
elasticsearch-xxx.tar.gz

3.3 解压Elasticsearch文件

执行解压指令对elasticsearch文件进行解压。
[root@135 es]# tar -xvf elasticsearch-xxx.tar.gz
[root@135 es]# ls
elasticsearch-xxx elasticsearch-xxx.tar.gz

4 系统参数的配置

有很多参数可以配置,可以参考官网。这里介绍基本的一些参数配置,保证可以正常启动ELasticsearch即可。

Elasticsearch对系统具有较高的要求,具体一些系统参数的配置,详情见:官网系统参数配置

4.1 JVM配置

配置文件/home/es/elasticsearch-xxx/config/jvm.options,按照如下修改
elasticsearch漫步走 - 第二章 - 安装在使用之前_第6张图片
将Xms8g 中的数字修改,目前系统64G内存,建议改成Xms20g。
详细说明:jvm配置

4.2 禁用swapping

配置文件/home/es/elasticsearch-xxx/config/elasticsearch.yml,将
Bootstrap . memory_lock: true 前的#去掉,保存。

且配置/etc/security/limits.conf
es soft memlock unlimited
es hard memlock unlimited
如下图:
elasticsearch漫步走 - 第二章 - 安装在使用之前_第7张图片
注:上述配置中es soft nofile 65536 是 4.3的配置。
通过下里面的指令可以判断是否已经禁用swapping。
详细说明

4.3 配置处理文件数量

配置elasticsearch可处理的文件数量,必须为65536或者更多。
配置/etc/security/limits.conf
es soft nofile 65536
es hard nofile 65536
详细说明

4.4 虚拟内存配置

配置 /etc/sysctl.conf文件,添加行 vm.max_map_count=262144
并且执行命令: sysctl -p

详细说明

4.4 线程池配置

vim /etc/security/limits.d/90-nproc.conf.
将soft nproc 后的数字改成2048.

5 Elasticsearch 参数配置

1、先在es下创建logs和data文件夹。
[root@host-10-146-112-217 es]#mkdir /esdata/data
[root@host-10-146-112-217 es]#mkdir /esdata/logs

2、配置文件/home/es/elasticsearch-5.2.2/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.
#
http.cors.enabled: true
http.cors.allow-origin: "*"
# 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: es_cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: es217
#
# 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: /home/es/esdata/data
#
# Path to log files:
#
path.logs: /home/es/esdata/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
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):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 19200-19300
transport.tcp.port: 19300-19400
#
# 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: ["10.146.112.111", "10.146.112.222", "10.146.112.333", "10.146.112.444", "10.146.112.555"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
# discovery.zen.minimum_master_nodes: 5
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#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

以下对上述参数进行详细的说明:
bootstrap.memory_lock: 禁用交换内存。
cluster.name:集群名称,当有多个ES节点时,相同集群名称的节点则会自动组合成一个集群。
node.name: 节点名称,如果不设置会自动生成一个
path.data: 设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:path.data: /path/to/data1,/path/to/data2
path.logs: 日志文件存储的位置,最好与data放在相同路径下。
network.host:节点启动绑定的主机IP
http.port: es对外提供服务的端口
transport.tcp.port: 设置节点间交互的tcp端口
discovery.zen.ping.unicast.hosts:设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。
下面两个配置使安装的插件生效。
http.cors.enabled: true
http.cors.allow-origin: “*”

注意:path.data与path.logs对应的文件需是es用户权限,在home文件夹下执行指令chown -R -v es:es esdata

[root@host-10-146-112-217 home]# chown -R -v es:es es

详细说明

6 启动Elasticsearch

执行指令su - es,切换至es用户下。
在路径/home/es/elasticsearch-xxx下,执行bin/elasticsearch -d 启动。 -d表示在后台启动,也可以使用&符号替代。
查看Elasticsearch是否已经启动:

[es@135 elasticsearch-xxx]$ ps -ef | grep elasticsearch
es       1734     1 99 09:29 pts/1    00:00:23 /opt/jdk1.8.0_111//bin/java -Xms8g -Xmx8g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/home/es/elasticsearch-xxx -cp /home/es/elasticsearch-xxx/lib/elasticsearch-5.0.2.jar:/home/es/elasticsearch-xxx/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
es       1801  1635  0 09:30 pts/1    00:00:00 grep --color=auto elasticsearch

在地址栏输入启动的地址即可,出现下面的状态表示启动成功
elasticsearch漫步走 - 第二章 - 安装在使用之前_第8张图片

就写到这里吧,想到下一章写什么了。see UUU…

Tips - Elasticsearch需要配置

如果只是测试的话,一台机器,2G内存够了

比较好的集群配置:
推荐服务器配置如下,且最好有5台相同配置的服务器组成一个Elasticsearch集群。
CPU:24cores Intel® Xeon® CPU E5-2620 v3 @ 2.40GHz
内存:64GB
操作系统:Centos 7 64位
磁盘空间:1T

你可能感兴趣的:(elasticsearch)