Alibaba server (Centos7+) deploys Elasticsearch

1. Download and install Elasticsearch

1.1, Elasticsearch does not support root privileges to start, so you need to create a new user

  • Example
adduser els  # 新增用户
passwd els  # 修改密码

1.2. Create ES data and log directories

  • Because after decompression I usually put it under /usr/local/, so I will put it under this directory
mkdir -p /usr/local/kibana/ # 创建放解压kibana的目录
mkdir -p /usr/local/els/  # 创建放解压ES 的目录
mkdir -p /usr/local/els/data/ # 创建es的数据目录
mkdir -p /usr/local/els/logs/ # 创建es的日志目录
chown -R els /usr/local/els/ # 修改拥有者

1.3, download

  • I usually download it to the /opt directory, and then unzip it to the newly created directory under the /usr/local/ directory
  • Download address (premise, your centos needs to install jdk (1.8+))
// 如果需要下载其他版本,就可以自己进去网站选择。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1.tar.gz
// Elasticsearch设计的开源分析和可视化平台
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.1-linux-x86_64.tar.gz
  • Unzip
tar -zxvf elasticsearch-6.5.1.tar.gz -C /usr/local/els/ # 解压到指定目录
tar -zxvf kibana-6.5.1-linux-x86_64.tar.gz -C /usr/local/kibana/ # 解压到指定目录
chown -R els /usr/local/els/elasticsearch-6.5.1/
mv /usr/local/kibana/kibana-6.5.1-linux-x86_64 /usr/local/kibana/kibana-6.5.1
chown -R els /usr/local/kibana/kibana-6.5.1/ # 这个可以不用

2. Modify the configuration file, there are four places that need to be modified

  • Note that this article does not describe the cluster
  • Enter the es configuration file directory
cd /usr/local/els/elasticsearch-6.5.1/config/

2.1, modify the es configuration fileelasticsearch.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: my-application
#
# ------------------------------------ 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: /usr/local/els/data 
#
# Path to log files:
# 日志存放地址
path.logs: /usr/local/els/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
# 是否设置内存锁定
bootstrap.memory_lock: true
bootstrap.system_call_filter: false
#
# 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.0.1
#
# Set a custom port for HTTP:
#
http.port: 9200
transport.tcp.port: 9300
# 设置跨域
http.cors.enabled: true
http.cors.allow-origin: "*"

#
# 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: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#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:
#
#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

2.2, modify the es configuration filejvm.options

-Xms4g      # 请根据自己机器配置调整
-Xmx4g

2.3, modify the /etc/sysctl.conffile

  • In order to avoid unnecessary disk and memory swapping and affecting efficiency, you need to modify vm.swappiness to 1 (for minimal swapping without disabling swapping) or 10 (when the system has enough memory, it is recommended to set this value to increase Performance), its default value is 60.

  • Also you need to modify the maximum virtual memory vm.max_map_countto prevent the error on startup:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]。

vm.swappiness = 10 # 这里表示是否与swap内存交换,这里表示不禁止,将进行少量交换
vm.max_map_count = 262144

2.4, modify the /etc/security/limits.conffile

  • Elasticsearch requires that its available file handle is at least 65536, and its process limit is at least 2048, which can be modified according to the following instructions.
  • max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
  • max number of threads [1024] for user [es] is too low, increase to at least [2048]
*     soft   nofile  65537
*     hard   nofile  65537
*     soft   nproc   4096
*     hard   nproc   8192
elastic soft memlock unlimited
elastic hard memlock unlimited

3. Test

  • After modifying the above content, it can generally start successfully
su els # 切换用户
cd /usr/local/els/elasticsearch-6.5.1/bin/
./elasticsearch 
./elasticsearch -d # 后台启动
  • accesshttp://ip:端口
    Insert picture description here

  • A similar interface above appears, indicating success! ! !

  • If the following error occurs during startup

OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
  • In 在jvm.optionsaddition of the
-XX:+AssumeMP 
# 或者
-XX:-AssumeMP 
  • Single machine multi-instance deployment and kibana installation details will be introduced in another article.
  • Detailed es configuration file
elasticsearch的config文件夹里面有两个配置文 件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是使用log4j来记录日 志的,所以logging.yml里的设置按普通log4j配置文件来设置就行了。下面主要讲解下elasticsearch.yml这个文件中可配置的 东西。

cluster.name: elasticsearch
配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。

node.name: "Franz Kafka"
节点名,默认随机指定一个name列表中名字,该列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。

node.master: true
指定该节点是否有资格被选举成为node,默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。

node.data: true
指定该节点是否存储索引数据,默认为true。

index.number_of_shards: 5
设置默认索引分片个数,默认为5片。

index.number_of_replicas: 1
设置默认索引副本个数,默认为1个副本。

path.conf: /path/to/conf
设置配置文件的存储路径,默认是es根目录下的config文件夹。

path.data: /path/to/data
设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:
path.data: /path/to/data1,/path/to/data2

path.work: /path/to/work
设置临时文件的存储路径,默认是es根目录下的work文件夹。

path.logs: /path/to/logs
设置日志文件的存储路径,默认是es根目录下的logs文件夹

path.plugins: /path/to/plugins
设置插件的存放路径,默认是es根目录下的plugins文件夹

bootstrap.mlockall: true
设置为true来锁住内存。因为当jvm开始swapping时es的效率 会降低,所以要保证它不swap,可以把ES_MIN_MEM和ES_MAX_MEM两个环境变量设置成同一个值,并且保证机器有足够的内存分配给es。 同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过`ulimit -l unlimited`命令。

network.bind_host: 192.168.0.1
设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0。


network.publish_host: 192.168.0.1
设置其它节点和该节点交互的ip地址,如果不设置它会自动判断,值必须是个真实的ip地址。

network.host: 192.168.0.1
这个参数是用来同时设置bind_host和publish_host上面两个参数。

transport.tcp.port: 9300
设置节点间交互的tcp端口,默认是9300。

transport.tcp.compress: true
设置是否压缩tcp传输时的数据,默认为false,不压缩。

http.port: 9200
设置对外服务的http端口,默认为9200。

http.max_content_length: 100mb
设置内容的最大容量,默认100mb

http.enabled: false
是否使用http协议对外提供服务,默认为true,开启。

gateway.type: local
gateway的类型,默认为local即为本地文件系统,可以设置为本地文件系统,分布式文件系统,hadoop的HDFS,和amazon的s3服务器,其它文件系统的设置方法下次再详细说。

gateway.recover_after_nodes: 1
设置集群中N个节点启动时进行数据恢复,默认为1。

gateway.recover_after_time: 5m
设置初始化数据恢复进程的超时时间,默认是5分钟。

gateway.expected_nodes: 2
设置这个集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。

cluster.routing.allocation.node_initial_primaries_recoveries: 4
初始化数据恢复时,并发恢复线程的个数,默认为4。

cluster.routing.allocation.node_concurrent_recoveries: 2
添加删除节点或负载均衡时并发恢复线程的个数,默认为4。

indices.recovery.max_size_per_sec: 0
设置数据恢复时限制的带宽,如入100mb,默认为0,即无限制。

indices.recovery.concurrent_streams: 5
设置这个参数来限制从其它分片恢复数据时最大同时打开并发流的个数,默认为5。

discovery.zen.minimum_master_nodes: 1
设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)

discovery.zen.ping.timeout: 3s
设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。

discovery.zen.ping.multicast.enabled: false
设置是否打开多播发现节点,默认是true。

discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。

下面是一些查询时的慢日志参数设置
index.search.slowlog.level: TRACE
index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms

index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug:500ms
index.search.slowlog.threshold.fetch.trace: 200ms

Guess you like

Origin blog.csdn.net/JISOOLUO/article/details/104724842