ElasticSearch CentOS 7部署及使用

554 阅读2分钟

安装部署

不同版本的ES功能特性和代码编写方法不一样,本文使用ES 7.10.X版本

部署

ES 7.x RPM安装官方指南

/etc/yum.repos.d/ 目录下新建 elasticsearch.repo 文件

[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md

然后执行安装

sudo yum install --enablerepo=elasticsearch elasticsearch

安装结束后可以看到提示:

Installed: elasticsearch.x86_64 0:7.17.6-1

执行自动启动

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service

检查ES状态: sudo systemctl status elasticsearch

 elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-10-04 10:36:25 CST; 2min 14s ago
     Docs: https://www.elastic.co
 Main PID: 23597 (java)
   CGroup: /system.slice/elasticsearch.service
           ├─23597 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -De...
           └─23800 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

进一步验证:curl localhost:9200

{
  "name" : "youan-ept",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "oZSgC0n4Q6WhQjPKfwhTVg",
  "version" : {
    "number" : "7.17.6",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "f65e9d338dc1d07b642e14a27f338990148ee5b6",
    "build_date" : "2022-08-23T11:08:48.893373482Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

开放外网访问

进入/etc/elasticsearch/目录,编辑elasticsearch.yml文件,修改如下(默认只能局域网内访问)

network.host: 0.0.0.0

同时注意开启防火墙9200端口允许访问

设置访问权限

最小安全性设置官方参考

编辑elasticsearch.yml文件如下:

xpack.security.enabled: true
discovery.type: single-node

执行以下命令:

/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive

  • After you set a password for the elastic user, you cannot run the elasticsearch-setup-passwords command a second time.
  • 重置密码方法
  1. 停止服务
  2. ./elasticsearch-users useradd esadmin -p new_password -r superuser
  3. 启动服务
  4. curl -u esadmin -XPUT 'http://127.0.0.1:9200/_xpack/security/user/elastic/_password' -H 'Content-Type: application/json' -d ' { "password" : "new_password"}'
  5. 重启服务
  6. 验证: curl -u elastic:new_password 'http://localhost:9200/'

FAQ

Q:There is insufficient memory for the Java Runtime Environment to continue.

A:设置ES堆内存

进入/etc/elasticsearch/jvm.options.d,新建任意文件,如a.options,编辑如下:

-Xms1g
-Xmx1g

指定堆大小为1个G

Q:Got notification message from PID 24170, but reception is disabled.

Step 1

find -name elasticsearch.service,可确定文件目录

/etc/systemd/system/multi-user.target.wants/elasticsearch.service

Step2

新增如下:

[Service] 
TimeoutStartSec=180 
NotifyAccess=all

Step3

systemctl daemon-reload

systemctl restart elasticsearch.service

Q: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

A: 编辑elasticsearch.yml文件,如下:

node.name: node-1
cluster.initial_master_nodes: ["node-1"]

Q: "unable to authenticate user [elastic] for REST request [/]"

A: 检查是否是密码输入错误