作者:陈剑冬  历史版本:1  最后编辑:陈剑冬  更新时间:2024-06-12 11:20

1. 物理部署

1.1. 下载部署包

# 下载 部署包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-linux-x86_64.tar.gz
# 解压 部署包
tar -zxvf elasticsearch-7.9.3-linux-x86_64.tar.gz -C /opt

1.2. 疑难杂症

1.2.1. 解决 Elasticsearch 强依赖jdk问题

由于 Elasticsearch 和 JDK 是一个强依赖的关系,所以当我们在新版本的 ElasticSearch 压缩包中包含有自带的jdk,但是当我们的Linux中已经安装了jdk之后,就会发现启动es的时候优先去找的是Linux中已经装好的jdk,此时如果jdk的版本不一致,就会造成jdk不能正常运行,报错如下:

warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
Future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_291/jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set.

解决办法

cd /opt/elasticsearch-7.9.3/bin
vim elasticsearch
############## 添加配置解决jdk版本问题 ##############
# 将jdk修改为es中自带jdk的配置目录
export JAVA_HOME=/usr/local/elasticsearch-7.9.3/jdk
export PATH=$JAVA_HOME/bin:$PATH

if [ -x "$JAVA_HOME/bin/java" ]; then
        JAVA="/usr/local/elasticsearch-7.9.3/jdk/bin/java"
else
        JAVA=`which java`
fi

1.2.2. 解决内存不足问题

Elasticsearch 默认启动内存为2g,如果服务器配置较低会报错,可以通过修改配置调低

vim /opt/elasticsearch-7.9.3/config/jvm.options
# 默认配置如下:
-Xms2g
-Xmx2g

# 默认的配置占用内存太多了,调小一些:
-Xms512m
-Xmx1g

1.2.3. vm.max_map_count [65530] is too low 问题

上面几个步骤依然没启动成功,继续解决问题:

ERROR: [1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

elasticsearch用户拥有的内存权限太小,至少需要262144,解决办法:
/etc/sysctl.conf 文件最后添加如下内容,即可永久修改

# 切换到root用户
su root

# 执行命令
vim /etc/sysctl.conf

# 添加如下内容
vm.max_map_count=262144

# 保存退出,刷新配置文件
sysctl -p

1.2.4. 可能遇到的 max file descriptors [4096] 问题

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
su root
vim /etc/security/limits.conf
# 添加以下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

1.3. 创建 Elasticsearch 专用用户

root用户不能直接启动Elasticsearch,所以需要创建一个专用用户,来启动ES

java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:101)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:168)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:397)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:75)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:116)
        at org.elasticsearch.cli.Command.main(Command.java:79)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:81)

创建命令如下:

#创建用户
useradd elasticsearch

#创建所属组:
chown elasticsearch:elasticsearch -R /opt/elasticsearch-7.9.3

#切换到elasticsearch用户
su elasticsearch

#进入bin目录
cd /opt/elasticsearch-7.9.3/bin

#启动elasticsearch
./elasticsearch

1.4. 修改 Elasticsearch 核心配置信息

# 执行命令修改elasticsearch.yml文件内容
vim /opt/elasticsearch-7.9.3/config/elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0
transport.host: 0.0.0.0
# 修改端口(非必须)
http.port: 9200

1.5. 启动与停止

说明:Elasticsearch 端口 9300、9200,其中:9300是tcp通讯端口,集群ES节点之间通讯使用,9200是http协议的RESTful接口

# 前台运行,Ctrl + C 则程序终止
/opt/elasticsearch-7.9.3/bin/elasticsearch

# 后台运行
/opt/elasticsearch-7.9.3/bin/elasticsearch -d
# 出现started时启动完成

# 查询pid
ps aux|grep elasticsearch
# 关闭ES服务
kill pid

1.6. 验证 ElasticSearch

访问如下地址(请将IP替换为您服务器的真实地址):
出现以下字样为部署成功

{
  "name" : "elasticsearch793-58c9556888-j8wh9",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "pc_Zn5hsT5yYNApWXkSiXw",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2. 容器部署

2.1. 创建目录及配置

创建所需的目录及配置文件

# 创建 数据目录
mkdir -p /opt/elasticsearch/data
# 创建 配置目录
mkdir -p /opt/elasticsearch/conf
# 创建 插件目录(比如 ik 分词插件之类的)
mkdir -p /opt/elasticsearch/plugins
# 授权 数据目录
chmod -R 777 /opt/elasticsearch/data
# 创建 配置文件
vim /opt/elasticsearch/conf/elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0
transport.host: 0.0.0.0
# 修改端口(非必须)
http.port: 9200

2.2. 容器描述文件

# 创建 容器描述文件
vim /opt/elasticsearch/docker-compose.yml
version: '3.3'
service:
  elasticsearch:
    container_name: elasticsearch793
    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
    restart: on-failure
    # 指定配置文件启动
    environment:
      ES_JAVA_OPTS: '-Xms2048m -Xmx4096m'
      discovery.type: 'single-node'
    ports:
      - 9200:9200
      - 9300:9300
    volumes:
      - ./data:/usr/share/elasticsearch/data
      - ./plugins:/usr/share/elasticsearch/plugins
      - ./conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro

2.3. 启动容器

docker-compose -f /opt/elasticsearch/docker-compose.yml -p elasticsearch up -d

2.4. 验证 ElasticSearch

访问如下地址(请将IP替换为您服务器的真实地址):
出现以下字样为部署成功

{
  "name" : "elasticsearch793-58c9556888-j8wh9",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "pc_Zn5hsT5yYNApWXkSiXw",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}