准备设备
准备三台机器:{172.20.28.150, 172.20.28.151, 172.20.28.152}
,
[root@els3 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@els3 ~]# ifconfig | grep 172.20
inet 172.20.28.152 netmask 255.255.255.0 broadcast 172.20.28.255
安装JDK
由于Elasticsearch依托JAVA,分别在这三台设备上安装JDK, 安装后的目录在 /usr/java/jdk1.8.0_261-amd64/bin/java
由于现在下载JDK需要登录Oracle账号,所以每此下RPM包都需要重新验证。
[root@els3 ~]# yum install -y wget
[root@els3 ~]# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn/java/jdk/8u261-b12/a4634525489241b9a9e1aa73d9e118e6/jdk-8u261-linux-x64.rpm?AuthParam=1594954017_7f9a36bbb1235f2968e8b798d9a8cbbd"
[root@els2 ~]# mv jdk-8u261-linux-x64.rpm\?AuthParam\=1594954017_7f9a36bbb1235f2968e8b798d9a8cbbd jdk-8u261-linux-x64.rpm
[root@els3 ~]# sudo yum -y localinstall jdk-8u261-linux-x64.rpm
[root@els2 ~]# java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
[root@els2 ~]# ls -al /usr/bin/java
lrwxrwxrwx 1 root root 22 Jul 17 10:55 /usr/bin/java -> /etc/alternatives/java
[root@els2 ~]# ls -al /etc/alternatives/java
lrwxrwxrwx 1 root root 37 Jul 17 10:55 /etc/alternatives/java -> /usr/java/jdk1.8.0_261-amd64/bin/java
安装Elasticsearch
- 通过添加Elastic的包存储库,可以将Elasticsearch与包管理器一起安装。将Elasitcsearch的公共GPG加入到RPM中。
[root@els3 ~]# sudo rpm --import http://packages.elastic.co/GPG-KEY-elasticsearch
- 为Elasticsearch创建一个新的创库文件。将以下内容写入到
/etc/yum.repos.d/elasticsearch.repo
中
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
- 安装Elasticsearch, 这个过程会比较慢。
[root@els3 ~]# sudo yum -y install elasticsearch
配置Elasticsearch
分别配置三台设备的Elasticsearch,注意节点名称node.name
和network.host
[root@els2 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: es_cluster
node.name: es_node01
node.master: true
node.data: true
index.number_of_shards: 5
index.number_of_replicas: 1
index.max_result_window: 1000000
path.data: /opt/elasticsearch/data
path.logs: /opt/elasticsearch/logs
network.host: 172.20.28.150
http.port: 9200
transport.tcp.port: 9300
discovery.zen.ping.unicast.hosts: ["172.20.28.17:9300","172.20.28.18:9300","172.20.28.19:9300"]
- 启动Elasticsearch,启动日志内无任何数据。
[root@els2 jna--1985354563]# systemctl start elasticsearch
[root@els2 jna--1985354563]# /etc/init.d/elasticsearch start
Starting elasticsearch (via systemctl): [ OK ]
[root@els2 jna--1985354563]# sudo service elasticsearch status
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2020-07-17 15:04:45 CST; 14s ago
Docs: http://www.elastic.co
Process: 14429 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -Des.pidfile=${PID_DIR}/elasticsearch.pid -Des.default.path.home=${ES_HOME} -Des.default.path.logs=${LOG_DIR} -Des.default.path.data=${DATA_DIR} -Des.default.path.conf=${CONF_DIR} (code=exited, status=1/FAILURE)
排错
- 去
/usr/share/elasticsearch/bin/elasticsearch
执行脚本,提示没有config,复制配置文件后又提示不能以root用户启动。
[root@els2 elasticsearch]# su - elasticsearch
Last login: Fri Jul 17 15:43:06 CST 2020 on pts/0
This account is currently not available.
- journalctl -xe 发现权限出错,不能以root用户访问该目录。
Jul 17 17:17:25 els2 elasticsearch[28409]: Exception in thread "main" java.lang.IllegalStateException: Unable to access 'path.data' (/opt/elasticsearch/data/es_cluster)
Jul 17 17:17:25 els2 elasticsearch[28409]: Likely root cause: java.nio.file.AccessDeniedException: /opt/elasticsearch/data/es_cluster
- vim /etc/passwd 将elasticsearch栏的,/sbin/nologin 改成 /bin/bash 之后正常启动。切换用户能正常启动,但是启用的不是自己配置的文件。
[root@els2 elasticsearch]# su - elasticsearch
Last login: Fri Jul 17 17:28:51 CST 2020 on pts/1
-bash-4.2$ /etc/init.d/elasticsearch start
Starting elasticsearch (via systemctl): ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password:
- journaltcl -xe 查看日志。有个疑问点:
◇:
logs、data目录权限是:root:elasticsearch
,
Jul 18 16:08:27 els1 elasticsearch[30141]: java.io.FileNotFoundException: /opt/elasticsearch/logs/es_cluster.log (Permission denied)
Jul 18 16:08:28 els1 elasticsearch[30141]: Exception in thread "main" java.lang.IllegalStateException: Unable to access 'path.data' (/usr/share/elasticsearch/data)
Jul 18 16:08:28 els1 elasticsearch[30141]: Likely root cause: java.nio.file.AccessDeniedException: /opt/elasticsearch/data/es_cluster
- 修改目录权限,问题解决。
[root@els1 opt]# chown -R elasticsearch:elasticsearch elasticsearch/
total 4
drwxr-xr-x 4 elasticsearch elasticsearch 4096 Jul 17 14:15 elasticsearch
[root@els1 opt]# ll elasticsearch/
total 8
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Jul 17 14:15 data
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Jul 17 14:15 logs
[root@els1 opt]# /etc/init.d/elasticsearch start
[root@els1 opt]# netstat -anpt | grep 9200
tcp6 0 0 172.20.28.150:9200 :::* LISTEN 31583/java
完成
- 查看集群健康状态
[root@els1 opt]# curl -XGET 'http://172.20.28.150:9200/_cluster/health?pretty'
{
"cluster_name" : "es_cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
- 设置开机自动启动
[root@els1 opt]# systemctl enable elasticsearch
[root@els1 opt]# systemctl list-unit-files | grep elastic
elasticsearch.service enabled
安装插件
- 使用插件命令,去官网下载,报错。 去stackoverflow查,说是可以使用
--verbose
参数查看详细信息。
[root@els2 bin]# ./plugin install head
-> Installing head...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/head/2.4.6/head-2.4.6.zip ...
ERROR: failed to download out of all possible locations..., use --verbose to get detailed information
[root@els2 bin]# ./plugin install --verbose mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip
Downloading ..............................
Installed head into /usr/share/elasticsearch/plugins/head
- 另外一种方法,GitHub上下载zip包,解压到
plugins/head/_site/
目录下,重启后即可。