【ELK】使用docker运行elasticsearch集群

本文详细介绍了如何使用docker和docker-compose快速搭建一个三节点的Elasticsearch集群,并提供了从安装docker到配置各节点的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍如何使用docker、docker-compose安装和运行一个三节点elasticsearch集群。

一、安装docker

docker安装和运行请参考我之前写的一篇博客:

【DockerCE】Docker-CE 20.10.12版本发布

二、下载elasticsearch镜像

# docker pull docker.elastic.co/elasticsearch/elasticsearch:7.16.2
7.16.2: Pulling from elasticsearch/elasticsearch
da847062c6f6: Pull complete 
f9947111a3a4: Pull complete 
5f47506629dc: Pull complete 
6728f6016cfb: Pull complete 
3ee4bcac6dc4: Pull complete 
cbb4caf74f49: Pull complete 
60e3e554a3bf: Pull complete 
64906e427669: Pull complete 
96b7ea4c4a98: Pull complete 

# docker images
REPOSITORY                                       TAG       IMAGE ID      CREATED     SIZE
docker.elastic.co/elasticsearch/elasticsearch   7.16.2    66c29cde15ce   3 weeks ago   646MB

三、创建数据持久化目录

# useradd -d /data -g root elasticsearch
# mkdir -p /data/elasticsearch/config/{es01,es02,es03}
# mkdir -p /data/elasticsearch/data/{es01,es02,es03}
# mkdir -p /data/elasticsearch/logs/{es01,es02,es03}
# chown -R elasticsearch:root /data
# vi /data/elasticsearch/data/es01/jvm.options
-Xms512m
-Xmx512m
# vi /data/elasticsearch/data/es02/jvm.options
-Xms512m
-Xmx512m
# vi /data/elasticsearch/data/es03/jvm.options
-Xms512m
-Xmx512m

四、编写剧本

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65535
        hard: 65535
    volumes:
      - /data/elasticsearch/data/es01:/usr/share/elasticsearch/data
      - /data/elasticsearch/config/es01/jvm.options:/usr/share/elasticsearch/config/jvm.options
      - /data/elasticsearch/logs/es01:/usr/share/elasticsearch/logs
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - elastic
  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
    container_name: es02
    environment:
      - node.name=es02
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65535
        hard: 65535
    volumes:
      - /data/elasticsearch/data/es02:/usr/share/elasticsearch/data
      - /data/elasticsearch/config/es02/jvm.options:/usr/share/elasticsearch/config/jvm.options
      - /data/elasticsearch/logs/es02:/usr/share/elasticsearch/logs
    networks:
      - elastic
  es03:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
    container_name: es03
    environment:
      - node.name=es03
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es02
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65535
        hard: 65535
    volumes:
      - /data/elasticsearch/data/es03:/usr/share/elasticsearch/data
      - /data/elasticsearch/config/es03/jvm.options:/usr/share/elasticsearch/config/jvm.options
      - /data/elasticsearch/logs/es03:/usr/share/elasticsearch/logs
    networks:
      - elastic
networks:
  elastic:
    driver: bridge

五、运行

# mv docker-compose /usr/local/bin/
# chmod u+x /usr/local/bin/docker-compose
---在.bash_profile中的path字段中增加/usr/local/bin/:配置
# source .bash_profile
# docker-compose up -d
[+] Running 4/4
 ⠿ Network elasticsearch_elastic  Created                                                                                                                                                                                              0.0s
 ⠿ Container es03                 Started                                                                                                                                                                                              1.4s
 ⠿ Container es01                 Started                                                                                                                                                                                              1.4s
 ⠿ Container es02                 Started  
# docker ps
CONTAINER ID   IMAGE                                                  COMMAND                  CREATED          STATUS          PORTS                                                                                  NAMES
867c9f1580b6   docker.elastic.co/elasticsearch/elasticsearch:7.16.2   "/bin/tini -- /usr/l…"   15 seconds ago   Up 13 seconds   0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp   es01
24a62972a736   docker.elastic.co/elasticsearch/elasticsearch:7.16.2   "/bin/tini -- /usr/l…"   15 seconds ago   Up 13 seconds   9200/tcp, 9300/tcp                                                                     es02
34eef8ccd068   docker.elastic.co/elasticsearch/elasticsearch:7.16.2   "/bin/tini -- /usr/l…"   15 seconds ago   Up 13 seconds   9200/tcp, 9300/tcp                                                                     es03
---查看集群的节点信息
# curl -X GET "localhost:9200/_cat/nodes?v=true&pretty"
ip          heap.percent ram.percent cpu load_1m load_5m load_15m node.role   master name
192.168.0.2           21          95  99    4.02    1.56     0.73 cdfhilmrstw -      es02
192.168.0.4           25          95  99    4.02    1.56     0.73 cdfhilmrstw *      es03
192.168.0.3           55          95  99    4.02    1.56     0.73 cdfhilmrstw -      es01

六、停集群容器

# docker-compose down
[+] Running 4/3
 ⠿ Container es01                 Removed                                                                                                                                                                                              0.8s
 ⠿ Container es03                 Removed                                                                                                                                                                                              0.8s
 ⠿ Container es02                 Removed                                                                                                                                                                                              0.8s
 ⠿ Network elasticsearch_elastic  Removed    

参考:

Install Elasticsearch with Docker | Elasticsearch Guide [7.16] | Elastic

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cnskylee

技术分享我是认真的,期待您打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值