Elasticsearch-5.6.x安装配置

系统环境

CentOS-7

准备两台服务器,做elasticsearch集群

1
yum update -y

hosts静态解析

1
2
3
127.0.0.1 localhost
172.16.80.201 elasticsearch-1
172.16.80.202 elasticsearch-2

Oracle JDK-8u201

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 切换工作目录
cd /usr/local/
# 下载JDK并解压
wget --no-check-certificate \
-O - \
-c \
--header "Cookie: oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz" \
| tar xz
# 创建软链接
ln -sv /usr/local/jdk1.8.0_201 /usr/local/jdk
# 设置环境变量
cat > /etc/profile.d/java.sh <<EOF
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
EOF
source /etc/profile.d/java.sh

创建elasticsearch用户

1
useradd esuser

修改limits

1
2
3
4
cat > /etc/security/limits.d/esuser.conf <<EOF
esuser - nofile 262144
esuser - memlock unlimited
EOF

修改内核参数

1
2
3
4
5
6
sysctl -w vm.max_map_count=262144
cat > /etc/sysctl.d/elasticsearch.conf <<EOF
vm.max_map_count=262144
# 内存耗尽才使用swap分区
vm.swappiness = 0
EOF

安装elasticsearch

下载elasticsearch

1
2
3
4
5
cd /usr/local/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.15.tar.gz
tar xzf elasticsearch-5.6.15.tar.gz
ln -sv /usr/local/elasticsearch-5.6.15 /usr/local/elasticsearch
chown -R esuser:esuser /usr/local/elasticsearch /usr/local/elasticsearch-5.6.15

配置elasticsearch

  • 修改config/elasticsearch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 定义节点名字
node.name: elasticsearch-1
# 定义数据目录
path.data: /home/esuser/data
# 定义日志目录
path.logs: /home/esuser/logs
# 定义节点为master
node.master: true
# 定义节点为data
node.data: true
# 启动时锁定内存,避免内存被系统swap
# 看情况开启,默认是关闭的
#bootstrap.memory_lock: true
# 定义监听端口
network.host: 0.0.0.0
# 定义服务端口
http.port: 9200
# 定义单播主机
discovery.zen.ping.unicast.hosts:
- elasticsearch-1
- elasticsearch-2
http.cors.enabled: true
http.cors.allow-origin: "*"
  • 修改config/jvm.options

这里只需要修改-Xms-Xmx,默认是2g,最大不超过32g,两个选项的值保持一致

  • 设置elasticsearch系统变量
1
2
3
cat > /home/esuser/elasticsearch <<EOF
JAVA_HOME=/usr/local/jdk
EOF
  • 添加PATH
1
2
3
cat > /etc/profile.d/elasticsearch.sh <<EOF
export ES_HOME="/usr/local/elasticsearch"
export PATH=$ES_HOME/bin:$PATH
  • 创建systemd服务脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=ElasticSearch Service
After=network.target
[Service]
Type=simple
EnvironmentFile=-/home/esuser/elasticsearch
ExecStart=/usr/local/elasticsearch/bin/elasticsearch
PIDFile=/usr/local/elasticsearch/run/elasticsearch.pid
User=esuser
LimitNOFILE=262144
LimitMEMLOCK=infinity
Restart=on-failure
[Install]
WantedBy=default.target

启动elasticsearch

  • 通过systemd命令启动
1
2
systemctl daemon-reload
systemctl enable --now elasticsearch
  • 命令行启动
1
su -s /bin/sh -c "/usr/local/elasticsearch/bin/elasticsearch -d" esuser
  • 访问elasticsearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
curl http://127.0.0.1:9200/
{
"name" : "elasticsearch-1",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "JN0WSrAZQo2qWCaZrbDGjg",
"version" : {
"number" : "5.6.15",
"build_hash" : "fe7575a",
"build_date" : "2019-02-13T16:21:45.880Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}

安装elasticsearch-head

  • 直接用容器启动
1
docker run --net=host --restart=always -d --name elasticsearch-head mobz/elasticsearch-head:5-alpine
  • 访问elasticsearch-head

curl -I http://127.0.0.1:9100