Elasticsearch 搜索引擎部署教程

Elasticsearch 是当前最流行的分布式搜索和分析引擎,广泛用于全文搜索、日志分析、指标监控等场景。基于 Apache Lucene 构建,Elasticsearch 提供了 RESTful API 和强大的分布式架构。本文将在搬瓦工 VPS 上部署 Elasticsearch 8.x,包括安全配置和基本使用。

一、系统要求

  • 内存:最低 2GB,推荐 4GB 以上。Elasticsearch 非常消耗内存。
  • 磁盘:建议 SSD,至少 20GB 可用空间。
  • 操作系统:Ubuntu 22.04(推荐)或 CentOS 7+。

二、安装 Elasticsearch

2.1 APT 包安装

# 导入 GPG 密钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

# 添加仓库
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | tee /etc/apt/sources.list.d/elastic-8.x.list

# 安装
apt update
apt install elasticsearch -y

安装完成后会输出默认的 elastic 用户密码和安全配置信息,务必保存。

2.2 Docker 安装

docker run -d --name elasticsearch   -p 9200:9200 -p 9300:9300   -e "discovery.type=single-node"   -e "xpack.security.enabled=false"   -e "ES_JAVA_OPTS=-Xms512m -Xmx512m"   -v es_data:/usr/share/elasticsearch/data   --restart unless-stopped   elasticsearch:8.13.0

三、基础配置

编辑 /etc/elasticsearch/elasticsearch.yml

cluster.name: my-cluster
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200

# 单节点模式
discovery.type: single-node

# 安全配置(8.x 默认开启)
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

3.1 JVM 内存调优

编辑 /etc/elasticsearch/jvm.options.d/custom.options

# 设置为物理内存的 50%,但不超过 31GB
-Xms1g
-Xmx1g

3.2 系统参数调优

# 调整虚拟内存
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p

# 调整文件描述符限制
echo "elasticsearch - nofile 65535" >> /etc/security/limits.conf

3.3 启动服务

systemctl daemon-reload
systemctl enable elasticsearch
systemctl start elasticsearch

# 重置密码(如果忘记安装时的密码)
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic

四、验证和基本操作

# 检查集群健康
curl -u elastic:YourPassword -k https://localhost:9200/_cluster/health?pretty

# 查看节点信息
curl -u elastic:YourPassword -k https://localhost:9200/_cat/nodes?v

# 创建索引
curl -u elastic:YourPassword -k -X PUT "https://localhost:9200/products" -H 'Content-Type: application/json' -d '{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name": { "type": "text", "analyzer": "standard" },
      "description": { "type": "text" },
      "price": { "type": "float" },
      "category": { "type": "keyword" },
      "created_at": { "type": "date" }
    }
  }
}'

# 索引文档
curl -u elastic:YourPassword -k -X POST "https://localhost:9200/products/_doc/1" -H 'Content-Type: application/json' -d '{
  "name": "ThinkPad X1 Carbon",
  "description": "轻薄商务笔记本电脑,14英寸屏幕,i7处理器",
  "price": 9999.00,
  "category": "笔记本",
  "created_at": "2026-03-01"
}'

# 搜索
curl -u elastic:YourPassword -k -X GET "https://localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d '{
  "query": {
    "match": { "description": "笔记本" }
  }
}'

五、安装 Kibana(可视化管理)

apt install kibana -y

# 配置 Kibana
# 编辑 /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["https://localhost:9200"]
elasticsearch.username: "kibana_system"

# 设置 kibana_system 密码
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u kibana_system

systemctl enable kibana
systemctl start kibana

六、中文搜索配置

# 安装 IK 中文分词插件
/usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.13.0/elasticsearch-analysis-ik-8.13.0.zip
systemctl restart elasticsearch

# 使用 IK 分词器创建索引
curl -u elastic:YourPassword -k -X PUT "https://localhost:9200/articles" -H 'Content-Type: application/json' -d '{
  "mappings": {
    "properties": {
      "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" },
      "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }
    }
  }
}'

七、监控与维护

# 查看索引状态
curl -u elastic:YourPassword -k https://localhost:9200/_cat/indices?v

# 查看磁盘使用
curl -u elastic:YourPassword -k https://localhost:9200/_cat/allocation?v

# 查看分片状态
curl -u elastic:YourPassword -k https://localhost:9200/_cat/shards?v

# 强制合并段(减少磁盘占用)
curl -u elastic:YourPassword -k -X POST "https://localhost:9200/products/_forcemerge?max_num_segments=1"

八、常见问题

  • 启动失败 bootstrap checks:检查 vm.max_map_count 和文件描述符限制。
  • 内存不足 OOM:降低 JVM 堆内存,搬瓦工 2GB 方案建议设置 -Xms512m -Xmx512m。
  • 磁盘写满:Elasticsearch 在磁盘使用超过 85% 时会停止分配新分片。清理旧索引或扩容。
  • 搜索慢:检查分片大小(建议 10-50GB),优化查询 DSL,参考 性能优化教程

总结

Elasticsearch 是构建搜索功能和日志分析平台的首选方案。在搬瓦工 VPS 上部署时需要注意内存配置,建议至少使用 2GB 内存的方案。更多 Elasticsearch 使用教程可参考 查询 DSL 教程性能优化教程。如果你的搜索需求更简单,可以考虑 MeilisearchTypesense。选购搬瓦工 VPS 请查看 全部方案,使用优惠码 NODESEEK2026 享受 6.77% 折扣,通过 bwh81.net 进入官网。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。