安装 Elastic Search
Quick start guide - Elasticsearch 8.14
在本地运行 ElasticSearch
下载和安装
Installing Elasticsearch
Download and install archive for Linux
Run Elasticsearch from the command line
下载压缩包:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.17.3-linux-x86_64.tar.gz
校验 SHA512:(可选)
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.17.3-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-8.17.3-linux-x86_64.tar.gz.sha512
校验正确应当输出:elasticsearch-8.17.3-linux-x86_64.tar.gz: OK
解压缩到 HOME 目录,其文件名默认为 elasticsearch-8.17.3
:
tar -xzf elasticsearch-8.17.3-linux-x86_64.tar.gz -C ~
启动 Elasticsearch
cd ~/elasticsearch-8.17.3
./bin/elasticsearch
首次启动 Elasticsearch 时,默认情况下会启用并配置安全功能。以下安全配置会自动发生:
- 启用身份验证和授权,并为内置超级用户
elastic
生成密码。- elastic 用户的密码和 Kibana 的注册令牌会在终端输出。
- 为传输层和 HTTP 层生成 TLS 的证书和密钥,并使用这些密钥和证书启用和配置 TLS。
- 为 Kibana 生成注册令牌,有效期为 30 分钟。
命令行输出形如:
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.
ℹ️ Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
********************
ℹ️ HTTP CA certificate SHA-256 fingerprint:
****************************************************************
ℹ️ Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
********************************************************************************
ℹ️ Configure other nodes to join this cluster:
• On this node:
⁃ Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`.
⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml.
⁃ Restart Elasticsearch.
• On other nodes:
⁃ Start Elasticsearch with `bin/elasticsearch --enrollment-token <token>`, using the enrollment token that you generated.
ElasticSearch 默认运行端口为 9200
。
添加环境变量
将下列内容添加到 .bashrc
或 .zshrc
:
export ES_HOME=~/elasticsearch-8.17.3
export ELASTIC_PASSWORD="<your_password>"
export PATH=$ES_HOME/bin:$PATH
重新加载配置:
zsh
检查 Elasticsearch 是否运行
curl --cacert $ES_HOME/config/certs/http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
输出形如:
{
"name" : "<your_hostname>",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "**********************",
"version" : {
"number" : "8.17.3",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "****************************************",
"build_date" : "2024-06-10T23:35:17.114581191Z",
"build_snapshot" : false,
"lucene_version" : "9.10.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
重置密码
elasticsearch-reset-password -u elastic
在集群中注册新节点
首先在运行 Elasticsearch 的终端中执行:
elasticsearch-create-enrollment-token -s node
然后在新节点的目录下执行:
elasticsearch --enrollment-token <enrollment-token>
这里的 <enrollment-token>
是上一步生成的注册令牌。 Elasticsearch 会自动在 config/certs
目录下生成证书和密钥。
命令行选项:
elasticsearch-create-enrollment-token --help
Creates enrollment tokens for elasticsearch nodes and kibana instances
Option (* = required) Description
--------------------- -----------
-E <KeyValuePair> Configure a setting
-f, --force Use this option to force execution of the command
against a cluster that is currently unhealthy.
-h, --help Show help
* -s, --scope The scope of this enrollment token, can be either "node"
or "kibana"
--url the URL where the elasticsearch node listens for
connections.
-v, --verbose Show verbose output
复制证书到其他目录
cp $ES_HOME/config/certs/http_ca.crt ~/repos/bili-scraper/configs/elastic_ca.crt
安装 Python client
pip install elasticsearch --upgrade
Python Client Examples
Python Client Helpers
安装插件
安装 Smart Chinese analysis 插件
elasticsearch-plugin install analysis-smartcn
Smart Chinese analysis plugin | Elasticsearch Plugins and Integrations [8.14] | Elastic
安装 IK 分词插件
elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.17.3
注意:analysis-ik
插件需要与 Elasticsearch 版本匹配。
默认的 stopword.dic
文件中包含了一些停用词(stop word),分词的时候会被过滤。如果不想过滤这些词,可以将该 dic 文件清空:
cd $ES_HOME/config/analysis-ik
cp stopword.dic stopword.dic.bak && rm stopword.dic && touch stopword.dic
重启 Elasticsearch 以使清空配置的操作生效。
infinilabs/analysis-ik: 🚌 The IK Analysis plugin integrates Lucene IK analyzer into Elasticsearch and OpenSearch, support customized dictionary.
Elasticsearch 中文分词器-阿里云开发者社区
安装 pinyin 插件
elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-pinyin/8.17.3
注意:analysis-pinyin
插件需要与 Elasticsearch 版本匹配。
infinilabs/analysis-pinyin: 🛵 This Pinyin Analysis plugin is used to do conversion between Chinese characters and Pinyin.
安装 stconvert 插件
elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-stconvert/8.17.3
注意:analysis-stconvert
插件需要与 Elasticsearch 版本匹配。
infinilabs/analysis-stconvert: 中文简繁體互相转换.
重启 Elasticsearch 以使插件生效
cd ~/elasticsearch-8.17.3/
./bin/elasticsearch
或者:(可执行程序已经添加到环境变量)
elasticsearch
查看已安装插件
elasticsearch-plugin list
输出形如:
analysis-ik
analysis-pinyin
analysis-smartcn
analysis-stconvert
升级 ElasticSearch
Elastic Installation and Upgrade Guide [8.17] | Elastic
- 备份或清空数据
- 关停所有数据库读写任务
- 关停所有 ElasticSearch 和 Kibana 的服务
- 下载新版本:(例如
8.14.1
->8.17.3
)
- 由于上面的过程都是在独立的目录下进行,所以可以直接下载新版本并解压缩
- 只需要把所有步骤中的
8.14.1
替换为8.17.3
即可
- 升级 Kibana,详见:升级 Kibana
在 Docker 中运行 ElasticSearch
Start a multi-node cluster with Docker Compose | Elastic Docs
下载配置
下载 .env
和 docker-compose.yml
文件到当前目录:
mkdir elasticsearch-docker && cd elasticsearch-docker
wget https://githubfast.com/elastic/elasticsearch/raw/refs/heads/main/docs/reference/setup/install/docker/.env
wget https://githubfast.com/elastic/elasticsearch/raw/refs/heads/main/docs/reference/setup/install/docker/docker-compose.yml
修改配置
修改 .env
和 docker-compose.yml
,参考样例配置。
- 挂载
data
和certs
目录到 host - 设置 PASSWORD 和 PORT 等环境变量
- 注释掉
docker-compose.yml
中的chown
命令
启动 Docker
设置 vm.max_map_count
, 以确保 Elasticsearch 有足够的内存映射:
sudo sysctl -w vm.max_map_count=262144
# cat /proc/sys/vm/max_map_count
docker 启动时报错:
ERROR: Elasticsearch died while starting up, with exit code 78
Increase virtual memory | Elastic Docs
Elasticsearch Container Stopped with Exit 78
state in Ubuntu 18.04 · Issue #1699 · laradock/laradock
运行下列命令,启动:
docker compose build && docker compose down && docker compose up
Using the Docker images in production | Elastic Docs
首次启动时,默认以 root 身份在 host 中创建 mount 的目录,会报权限错误。
所以需要设置目录权限,以使内部的 elasticsearch 进程可以访问这些目录:
sudo chown -R 1000:1000 data certs plugins
同时注释掉 docker-compose.yml
中的下面一行:(注意,首次启动时不要注释)
echo "Setting file permissions"
chown -R root:root config/certs;
注意:如果 host 中的环境变量已经设置了 ELASTIC_PASSWORD
,那么在容器中也会自动设置该变量。
此时 .env
中的设置会被忽略。
重新启动:
docker compose build && docker compose down && docker compose up
检测 Elasticsearch 是否运行:
curl --cacert ./certs/ca/ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:19200
样例配置
.env
.env
# Password for the 'elastic' user (at least 6 characters)
ELASTIC_PASSWORD=******
# Password for the 'kibana_system' user (at least 6 characters)
KIBANA_PASSWORD=******
# Version of Elastic products
STACK_VERSION=9.0.0
# Set the cluster name
CLUSTER_NAME=es-docker-cluster
# Set to 'basic' or 'trial' to automatically start the 30-day trial
LICENSE=basic
#LICENSE=trial
# Port to expose Elasticsearch HTTP API to the host
ES_PORT=19200
#ES_PORT=127.0.0.1:9200
# Port to expose Kibana to the host
KIBANA_PORT=15601
#KIBANA_PORT=80
# Increase or decrease based on the available host memory (in bytes)
MEM_LIMIT=107374182400
# Project namespace (defaults to the current folder name if not set)
#COMPOSE_PROJECT_NAME=myproject
docker-compose.yml
docker-compose.yml
version: "2.2"
services:
setup:
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
volumes:
- ./certs:/usr/share/elasticsearch/config/certs
user: "0"
command: >
bash -c '
if [ x${ELASTIC_PASSWORD} == x ]; then
echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
exit 1;
elif [ x${KIBANA_PASSWORD} == x ]; then
echo "Set the KIBANA_PASSWORD environment variable in the .env file";
exit 1;
fi;
if [ ! -f config/certs/ca.zip ]; then
echo "Creating CA";
bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
unzip config/certs/ca.zip -d config/certs;
fi;
if [ ! -f config/certs/certs.zip ]; then
echo "Creating certs";
echo -ne \
"instances:\n"\
" - name: es01\n"\
" dns:\n"\
" - es01\n"\
" - localhost\n"\
" ip:\n"\
" - 127.0.0.1\n"\
> config/certs/instances.yml;
bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key;
unzip config/certs/certs.zip -d config/certs;
fi;
echo "Setting file permissions"
# chown -R root:root config/certs;
find . -type d -exec chmod 750 \{\} \;;
find . -type f -exec chmod 640 \{\} \;;
echo "Waiting for Elasticsearch availability";
until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
echo "Setting kibana_system password";
until curl -s -X POST --cacert config/certs/ca/ca.crt -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" https://es01:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
echo "All done!";
'
healthcheck:
test: ["CMD-SHELL", "[ -f config/certs/es01/es01.crt ]"]
interval: 1s
timeout: 5s
retries: 120
es01:
depends_on:
setup:
condition: service_healthy
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
volumes:
- ./certs:/usr/share/elasticsearch/config/certs
- ./data/es01:/usr/share/elasticsearch/data
ports:
- ${ES_PORT}:9200
environment:
- node.name=es01
- cluster.name=${CLUSTER_NAME}
- cluster.initial_master_nodes=es01
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=certs/es01/es01.key
- xpack.security.http.ssl.certificate=certs/es01/es01.crt
- xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.key=certs/es01/es01.key
- xpack.security.transport.ssl.certificate=certs/es01/es01.crt
- xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.license.self_generated.type=${LICENSE}
- xpack.ml.use_auto_machine_memory_percent=true
mem_limit: ${MEM_LIMIT}
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test:
[
"CMD-SHELL",
"curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
]
interval: 10s
timeout: 10s
retries: 120
kibana:
depends_on:
es01:
condition: service_healthy
image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
volumes:
- ./certs:/usr/share/kibana/config/certs
- ./data/kibana:/usr/share/kibana/data
ports:
- ${KIBANA_PORT}:5601
environment:
- SERVERNAME=kibana
- ELASTICSEARCH_HOSTS=https://es01:9200
- ELASTICSEARCH_USERNAME=kibana_system
- ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
- ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
mem_limit: ${MEM_LIMIT}
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'",
]
interval: 10s
timeout: 10s
retries: 120
volumes: {}
See: https://github.com/Hansimov/blog/blob/main/docs/notes/configs/elastic-docker/docker-compose.yml
复制本地配置到笔记
cp ~/elasticsearch-docker/.env ~/repos/blog/docs/notes/configs/elastic-docker/.env
cp ~/elasticsearch-docker/docker-compose.yml ~/repos/blog/docs/notes/configs/elastic-docker/docker-compose.yml