安装JDK
下载
wget https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz
解压
tar -xvf jdk-8u201-linux-x64.tar.gz
配置java环境变量
vi /etc/profile
在文件的末尾添加 把JAVA_HOME添加到PATH中
export JAVA_HOME=/usr/java/jdk1.8.0_201
export PATH=${JAVA_HOME}/bin:${PATH}
// wq 保存退出
// 使配置的环境变量生效
source /etc/profile
// 检测Java命令是否可用,出现以下内容可用
java -vesion
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)
安装ElasticSearch
[root@Centos-52 home]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz
--2019-11-25 13:41:54-- https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz
Resolving artifacts.elastic.co (artifacts.elastic.co)... 151.101.230.222, 2a04:4e42:36::734
Connecting to artifacts.elastic.co (artifacts.elastic.co)|151.101.230.222|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 288775500 (275M) [application/x-gzip]
Saving to: ‘elasticsearch-7.4.2-linux-x86_64.tar.gz’
100%[=======================================================================================================================================================================================>] 288,775,500 4.42MB/s in 2m 5s
2019-11-25 13:43:59 (2.21 MB/s) - ‘elasticsearch-7.4.2-linux-x86_64.tar.gz’ saved [288775500/288775500]
[root@Centos-52 home]# tar -xvf elasticsearch-7.4.2-linux-x86_64.tar.gz
配置文件
config目录下有三个文件分别是:
elasticsearch.yml es的配置
jvm.options jvm参数控制
log4j2.properties 日志规则配置
只需要修改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: es7-cluster
node.master: true
node.data: true
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: es7-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: /home/elastic_data/es/data
#
# Path to log files:
#
path.logs: /home/elastic_data/es/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: 192.168.0.51
#
# 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 this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
# 写入候选主节点的设备地址,在开启服务后可以被选为主节点
discovery.seed_hosts: ["192.168.0.51", "192.168.0.52", "192.168.0.53"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["es7-node-1", "es7-node-2", "es7-node-3"]
#
# For more information, consult the discovery and cluster formation 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
# 是否支持跨域,在使用head插件时需要此配置
http.cors.enabled: true
# “*” 表示支持所有域名
http.cors.allow-origin: "*"
其他两个节点的配置需要修改node.name、network.host,node.name 分别为:
es7-node-2,es7-node-3,network.host分别为主机的ip
创建es用户
[root@Centos-51 home]# groupadd es
[root@Centos-51 home]# useradd es -g es -p es
[root@Centos-51 home]# groups es
es : es
[root@Centos-51 home]#
切换目录权限
[root@Centos-51 home]# chown -R es:es /home/elasticsearch-7.4.2
[root@Centos-51 home]# chown -R es:es /home/elastic_data/
启动es
./elasticsearch
./elasticsearch -d
es 默认会前台运行,第一次启动可以直接用命令运行,这样可以看到输出日志,能够比较及时的发现问题,能够正常运行后可以在命令后面加上 -d ,让es在 后台运行
启动异常-1
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_181/jre] does not meet this requirement
jdk版本太低,下载jdk11,进行安装
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz
修改Elasticsearch的启动文件,使它指向我们下载的JDK11
修改elasticsearch启动文件,增加下面红色部分配置
启动异常-2
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
这是提醒你 cms 垃圾收集器在 jdk9 就开始被标注为 @deprecated
修改jvm.options
将 -XX:+UseConcMarkSweepGC 改为 -XX:+UseG1GC
启动异常-3
Exception in thread "main" org.elasticsearch.bootstrap.BootstrapException: java.nio.file.AccessDeniedException: /home/elasticsearch-7.4.2/config/elasticsearch.keystore
Likely root cause: java.nio.file.AccessDeniedException: /home/elasticsearch-7.4.2/config/elasticsearch.keystore
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at
发现elasticsearch.keystore文件所属用户是root,不知道为什么在切换目录所有权中没有切换过来
切换目录的所属用户,重新启动
启动异常-4
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
原因:新建es账户后,系统分配的内存太低
解决:切换到root用户 执行命令:
sysctl -w vm.max_map_count=262144
查看结果:
sysctl -a|grep vm.max_map_count
显示:
vm.max_map_count = 262144
上述方法修改之后,如果重启虚拟机将失效,所以:
在/etc/sysctl.conf文件最后添加一行,即可永久修改
vm.max_map_count=262144
执行命令sysctl -p重启
集群环境验证
在浏览器访问
http://192.168.0.51:9200/_cat/nodes
如下表示集群搭建成功