Docker安装Elasticsearch7.9.3步骤

353 阅读2分钟

拉取 Elasticsearch 镜像

首先,确保已经安装了 Docker。然后,在命令行中执行以下命令来拉取 Elasticsearch 官方镜像。以 Elasticsearch 7.9.3 版本为例:

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.9.3

配置 Elasticsearch

Elasticsearch 需要一些配置才能正常运行,此步骤必不可少。可以创建一个配置目录,在本地创建一个名为es_config的目录,用于存放配置文件。

mkdir /path/es_config

创建配置文件elasticsearch.ymljvm.optionslog4j2.properties

cd /path/es_config
touch elasticsearch.yml
touch jvm.options
touch log4j2.properties

配置相关文件内容

elasticsearch.yml配置示例如下:

cluster.name: my-es -cluster
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["localhost"]
cluster.initial_master_nodes: ["node-1"]

jvm.options配置示例如下:

-Xms1g
-Xmx1g

log4j2.properties配置示例如下:

status = error
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}] [%p] [%c] - %m%n
rootLogger.level = info
rootLogger.appenderRef.console.ref = console

启动 Elasticsearch 容器

使用以下命令启动 Elasticsearch 容器:

docker run -d --name es - container - p 9200:9200 - p 9300:9300 - v /path/to/es_config:/usr/share/elasticsearch/config docker.elastic.co/elasticsearch/elasticsearch:7.9.3

其中-d表示以守护进程模式运行,--name是容器名称,-p用于将容器内的端口映射到主机端口,-v用于将主机上的配置目录挂载到容器内的配置目录,这样容器内的 Elasticsearch 就可以使用主机上配置好的elasticsearch.yml文件。

验证安装

等待一段时间(可能需要几分钟,因为 Elasticsearch 在启动时需要进行一些初始化操作),然后在浏览器中访问http://localhost:9200,如果看到类似如下的信息,就说明 Elasticsearch 已经成功安装:

{
  "name": "node-1",
  "cluster_name": "my-es-cluster",
  "cluster_uuid": "EA4rkMdKTXeLcZtQEF2yVw",
  "version": {
  "number": "7.9.3",
  "build_flavor": "default",
  "build_type": "docker",
  "build_hash": "c4138e51121ef06a6404866cddc601906fe5c868",
  "build_date": "2020-10-16T13:34:25.304557Z",
  "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"
}