docker部署ES

Docker Compose 部署 Elasticsearch 7.10 集群 + Kibana 完整指南

一、版本确认与下载地址

Elasticsearch 和 Kibana 必须使用相同的 7.10.x 版本。推荐使用 7.10.2 版本。

 
组件版本Docker 镜像官方下载地址
Elasticsearch 7.10.2 docker.elastic.co/elasticsearch/elasticsearch:7.10.2 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.2-linux-x86_64.tar.gz
Kibana 7.10.2 docker.elastic.co/kibana/kibana:7.10.2 https://artifacts.elastic.co/downloads/kibana/kibana-7.10.2-linux-x86_64.tar.gz

Elasticsearch 7.10.2 Docker 镜像可从官方仓库拉取,支持 amd64 和 arm64 架构,镜像大小约 456 MB。Kibana 7.10.2 的 Docker 镜像同样来自官方 Elastic 仓库。如果需要 OSS(纯开源版),将镜像名中的 elasticsearch 替换为 elasticsearch-osskibana 替换为 kibana-oss 即可。

二、前置条件

  • Linux 服务器(CentOS 7+/Ubuntu 18.04+)

  • 至少 4GB 内存(3个 ES 节点 + Kibana 的最低要求,建议 8GB+)

  • Docker 已安装

  • Docker Compose 已安装

2.1 安装 Docker 和 Docker Compose

bash
# 安装 Docker(以 CentOS 为例)
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io

# 启动 Docker 并设置开机自启
systemctl start docker
systemctl enable docker

# 验证 Docker
docker --version

# 安装 Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

三、系统参数调整

ES 运行期间会创建大量线程和文件句柄,必须调整系统限制,否则会启动失败。

3.1 修改虚拟内存映射数

bash
# 临时生效
sysctl -w vm.max_map_count=655360

# 永久生效
echo "vm.max_map_count=655360" >> /etc/sysctl.conf
sysctl -p

3.2 关闭 Swap

bash
swapoff -a
# 永久关闭:注释掉 /etc/fstab 中包含 swap 的行
sed -i '/swap/s/^/#/' /etc/fstab

3.3 修改最大文件句柄数和进程数

bash
cat >> /etc/security/limits.conf << EOF
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
EOF

# 重启使配置生效
reboot

四、创建目录结构

bash
# 创建 ES 集群根目录
mkdir -p /usr/local/es-cluster

# 创建 3 个 ES 节点的挂载目录
mkdir -p /usr/local/es-cluster/node-{1..3}/{config,data,plugins,logs}

# 创建 Kibana 挂载目录
mkdir -p /usr/local/es-cluster/kibana/config

# 赋予权限
chmod -R 777 /usr/local/es-cluster

五、编写 Elasticsearch 配置文件

5.1 节点1配置(es-node1)

创建 /usr/local/es-cluster/node-1/config/elasticsearch.yml

yaml
cluster.name: es-docker-cluster
node.name: es-node1
node.master: true
node.data: true

network.host: 0.0.0.0
network.publish_host: 你的宿主机IP       # 例如 192.168.1.21
http.port: 9200
transport.tcp.port: 9300

# 集群发现配置
discovery.seed_hosts: ["你的宿主机IP:9300", "你的宿主机IP:9301", "你的宿主机IP:9302"]
cluster.initial_master_nodes: ["es-node1", "es-node2", "es-node3"]

# 跨域配置(便于elasticsearch-head等工具访问)
http.cors.enabled: true
http.cors.allow-origin: "*"

# 关闭安全认证(7.10默认不开启,显式关闭以防干扰)
xpack.security.enabled: false

# 内存锁定
# bootstrap.memory_lock: true

5.2 节点2配置(es-node2)

创建 /usr/local/es-cluster/node-2/config/elasticsearch.yml

yaml
cluster.name: es-docker-cluster
node.name: es-node2
node.master: true
node.data: true

network.host: 0.0.0.0
network.publish_host: 你的宿主机IP
http.port: 9200
transport.tcp.port: 9301

discovery.seed_hosts: ["你的宿主机IP:9300", "你的宿主机IP:9301", "你的宿主机IP:9302"]
cluster.initial_master_nodes: ["es-node1", "es-node2", "es-node3"]

http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: false
#bootstrap.memory_lock: true

5.3 节点3配置(es-node3)

创建 /usr/local/es-cluster/node-3/config/elasticsearch.yml

yaml
cluster.name: es-docker-cluster
node.name: es-node3
node.master: true
node.data: true

network.host: 0.0.0.0
network.publish_host: 你的宿主机IP
http.port: 9200
transport.tcp.port: 9302

discovery.seed_hosts: ["你的宿主机IP:9300", "你的宿主机IP:9301", "你的宿主机IP:9302"]
cluster.initial_master_nodes: ["es-node1", "es-node2", "es-node3"]

http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.enabled: false
#bootstrap.memory_lock: true

注意network.publish_host 必须填写宿主机的实际 IP 地址,不能用 127.0.0.1localhost,否则集群间无法通信

六、编写 docker-compose.yml

/usr/local/es-cluster/ 目录下创建 docker-compose.yml

yaml
version: '2.2'

services:
  # ==================== Elasticsearch 集群 ====================
  es-node1:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    container_name: es-node1
    restart: always
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 131072
    volumes:
      - ./node-1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ./node-1/data:/usr/share/elasticsearch/data
      - ./node-1/plugins:/usr/share/elasticsearch/plugins
      - ./node-1/logs:/usr/share/elasticsearch/logs
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - es-net

  es-node2:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    container_name: es-node2
    restart: always
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 131072
    volumes:
      - ./node-2/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ./node-2/data:/usr/share/elasticsearch/data
      - ./node-2/plugins:/usr/share/elasticsearch/plugins
      - ./node-2/logs:/usr/share/elasticsearch/logs
    ports:
      - "9201:9200"
      - "9301:9300"
    networks:
      - es-net

  es-node3:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    container_name: es-node3
    restart: always
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 131072
    volumes:
      - ./node-3/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ./node-3/data:/usr/share/elasticsearch/data
      - ./node-3/plugins:/usr/share/elasticsearch/plugins
      - ./node-3/logs:/usr/share/elasticsearch/logs
    ports:
      - "9202:9200"
      - "9302:9300"
    networks:
      - es-net

  # ==================== Kibana ====================
  kibana:
    image: docker.elastic.co/kibana/kibana:7.10.2
    container_name: kibana
    restart: always
    environment:
# 三个 节点都 写上 - ELASTICSEARCH_HOSTS=["http://es-node1:9200","",""] - SERVER_HOST=0.0.0.0 ports: - "5601:5601" volumes: - ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml networks: - es-net networks: es-net: driver: bridge

说明:Docker Compose 配置中 ES_JAVA_OPTS 设置的 512m 适合开发/测试环境。生产环境建议根据服务器内存调整,一般设置为物理内存的 50% 且不超过 32GB。Kibana 通过容器名 es-node1 连接 ES,因为它们在同一个 Docker 网络中。

七、创建 Kibana 配置文件

创建 /usr/local/es-cluster/kibana/config/kibana.yml

yaml
server.name: kibana
server.host: "0.0.0.0"
server.port: 5601
elasticsearch.hosts: ["http://es-node1:9200"]
i18n.locale: "zh-CN"

八、启动集群

bash
cd /usr/local/es-cluster

# 确保宿主机IP已替换到 elasticsearch.yml 中
# 启动所有服务
docker-compose up -d

# 查看容器状态
docker-compose ps

# 查看启动日志(等待约1-2分钟)
docker-compose logs -f es-node1

九、验证集群状态

9.1 检查集群健康

bash
curl http://localhost:9200/_cluster/health?pretty

期望结果:

json
{
  "cluster_name" : "es-docker-cluster",
  "status" : "green",
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  ...
}

如果 statusyellow,稍等片刻,集群正在分片分配中。number_of_nodes 应为 3

9.2 查看节点列表

bash
curl http://localhost:9200/_cat/nodes?v

应看到 es-node1、es-node2、es-node3 三个节点。

9.3 访问 Kibana

浏览器打开 http://你的宿主机IP:5601,能看到 Kibana 界面即部署成功。

十、后续操作

10.1 安装 IK 中文分词器(可选)

需要为每个 ES 节点安装 IK 分词器。以 7.10.2 为例:

bash
# 下载 IK 分词器
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.10.2/elasticsearch-analysis-ik-7.10.2.zip

# 分别放入三个节点的 plugins 目录
mkdir -p /usr/local/es-cluster/node-{1..3}/plugins/ik
unzip elasticsearch-analysis-ik-7.10.2.zip -d /usr/local/es-cluster/node-1/plugins/ik
unzip elasticsearch-analysis-ik-7.10.2.zip -d /usr/local/es-cluster/node-2/plugins/ik
unzip elasticsearch-analysis-ik-7.10.2.zip -d /usr/local/es-cluster/node-3/plugins/ik

# 重启 ES 集群
docker-compose restart es-node1 es-node2 es-node3

IK 分词器下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases 

10.2 常用运维命令

bash
# 停止集群
docker-compose stop

# 停止并删除容器(数据保留在挂载目录中)
docker-compose down

# 重新启动
docker-compose up -d

# 查看某个节点日志
docker-compose logs -f es-node1

十一、常见问题

Q1:ES 启动失败,报 max virtual memory areas vm.max_map_count [65530] is too low

执行 sysctl -w vm.max_map_count=655360 并写入 /etc/sysctl.conf

Q2:集群只有一个节点加入,其他节点一直重试

检查每个节点的 network.publish_host 是否填写了正确的宿主机 IP,以及 discovery.seed_hosts 中的地址和端口是否与实际配置一致。

Q3:Kibana 无法连接 ES

确认 Kibana 的 elasticsearch.hosts 使用的是容器名 http://es-node1:9200 而非 localhost,因为容器间通过 Docker 网络通信。

Q4:数据持久化是否安全?

所有 ES 数据、配置、插件和日志都通过 volumes 挂载到了宿主机 /usr/local/es-cluster/node-*/ 下,容器重启或删除不会丢失数据。

 

一、Nginx 镜像与版本

推荐使用官方 Alpine 版,体积小:

 
组件版本Docker 镜像下载地址
Nginx 1.25.4-alpine nginx:1.25.4-alpine https://hub.docker.com/_/nginx

拉取镜像:

bash
docker pull nginx:1.25.4-alpine

二、创建 Nginx 目录和配置

bash
mkdir -p /usr/local/es-cluster/nginx/html

创建配置文件:

bash
vi /usr/local/es-cluster/nginx/nginx.conf

内容如下,作用:

  • / 反向代理到 Kibana

  • /es/ 反向代理到 Elasticsearch 集群

nginx
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout 65;

    upstream kibana {
        server kibana:5601;
    }

    upstream es_cluster {
        server es-node1:9200;
        server es-node2:9200;
        server es-node3:9200;
    }

    server {
        listen 80;
        server_name localhost;

        # Kibana
        location / {
            proxy_pass http://kibana;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 300s;
        }

        # Elasticsearch
        location /es/ {
            proxy_pass http://es_cluster/;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

说明:kibana:5601es-node1:9200 这些主机名能解析,是因为 Nginx 容器会和 ES/Kibana 在同一个 Docker 自定义网络中。

三、方式一:单独 docker run 启动 Nginx

先确认 ES/Kibana 的 Docker 网络名:

bash
docker network ls

通常你之前用 /usr/local/es-cluster/docker-compose.yml 启动,网络名大概是:

text
es-cluster_es-net

然后启动 Nginx:

bash
docker run -d \
  --name nginx \
  --restart always \
  -p 80:80 \
  -v /usr/local/es-cluster/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /usr/local/es-cluster/nginx/html:/usr/share/nginx/html:ro \
  --network es-cluster_es-net \
  nginx:1.25.4-alpine

如果你的网络名不是 es-cluster_es-net,把上面 --network 后面的名字换成 docker network ls 查到的实际网络名。

查看日志:

bash
docker logs -f nginx

重载配置:

bash
docker exec nginx nginx -s reload

停止删除:

bash
docker stop nginx && docker rm nginx

四、方式二:集成到原有 docker-compose.yml

进入原目录:

bash
cd /usr/local/es-cluster

编辑 docker-compose.yml,在 services: 下追加 Nginx 服务:

yaml
  nginx:
    image: nginx:1.25.4-alpine
    container_name: nginx
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/html:/usr/share/nginx/html:ro
    depends_on:
      - kibana
    networks:
      - es-net

注意:Nginx 服务要放在原来的 services: 下面,并且和 ES、Kibana 一样使用 es-net 网络。

然后启动:

bash
docker-compose up -d nginx

或者整体重新启动:

bash
docker-compose up -d

查看状态:

bash
docker-compose ps

五、验证 Nginx 反向代理

1. 验证 Kibana

浏览器访问:

text
http://你的宿主机IP/

如果能看到 Kibana 页面,说明 Nginx 代理 Kibana 成功。

2. 验证 Elasticsearch

命令行访问:

bash
curl http://你的宿主机IP/es/_cluster/health?pretty

如果返回类似:

json
{
  "cluster_name" : "es-docker-cluster",
  "status" : "green",
  "number_of_nodes" : 3
}

说明 Nginx 代理 ES 集群成功。


六、常见问题

Q1:80 端口被占用怎么办?

把端口映射改成 8080:

yaml
ports:
  - "8080:80"

单独 docker run 则改成:

bash
-p 8080:80

访问时用:

text
http://你的宿主机IP:8080/

Q2:Nginx 报 host not found in upstream "kibana"

说明 Nginx 容器没有和 Kibana 在同一个 Docker 网络里。
检查:

bash
docker network ls
docker inspect nginx | grep Network
docker inspect kibana | grep Network

确保它们都在 es-netes-cluster_es-net 中。

Q3:Kibana 页面能打开,但样式或接口异常

Kibana 7.10 通过 Nginx 根路径代理一般没问题。如果你改成子路径,例如 /kibana/,则需要额外配置 Kibana 的 server.basePath,这里不展开。

Q4:Nginx 配置修改后如何生效?

bash
docker exec nginx nginx -s reload

如果是 docker-compose:

bash
docker-compose restart nginx

七、最终访问入口

部署完成后:

 
服务地址
Kibana http://宿主机IP/
Elasticsearch http://宿主机IP/es/
Kibana 原端口 http://宿主机IP:5601
ES 原端口 http://宿主机IP:9200

推荐统一走 Nginx:

text
http://宿主机IP/       -> Kibana
http://宿主机IP/es/    -> Elasticsearch 集群

这样你的整套环境就是:

text
Nginx -> Kibana -> Elasticsearch 7.10.2 三节点集群
posted @ 2026-09-13 00:19  Binky.Lee  阅读(2)  评论(0)    收藏  举报