Elasticsearch容器化部署

Elasticsearch容器化部署

1. 单节点

修改主机参数:

cat >> /etc/sysctl.conf << EOF
vm.max_map_count=2048000
EOF
sysctl -p

创建目录并赋予权限:

# 创建目录
mkdir -p /data/elasticsearch/{data,logs,plugins}
# 赋予777递归权限
cd /data/elasticsearch
chmod 777 -R data logs plugins

构建docker-compose.yaml

version: '3'
services:
  elasticsearch:
    image: elasticsearch:7.17.9
    container_name: elasticsearch
    volumes:
      - /data/elasticsearch/data:/usr/share/elasticsearch/data
      - /data/elasticsearch/logs:/usr/share/elasticsearch/logs
      - /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - /etc/localtime:/etc/localtime
    environment:
      - TZ="Asia/Shanghai"
      - cluster.name=my-es
      - node.name=node-1
      # 绑定网络地址
      - network.host=0.0.0.0
      - http.port=9200
      - path.data=/usr/share/elasticsearch/data
      - path.logs=/usr/share/elasticsearch/logs
      # 运行模式
      - discovery.type=single-node
      # 是否锁住内存,避免交换(swapped)带来的性能损失
      - bootstrap.memory_lock=true
      # 设置java堆栈内存
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      # 允许跨域访问
      - http.cors.enabled=true
      - http.cors.allow-origin="*"
      # 关闭geoip,避免部分报错
      - ingest.geoip.downloader.enabled=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200
      - 9300:9300
    logging:
      driver: json-file
      options:
        max-size: "100m"
        max-file: "50"
    restart: always

验证:

访问宿主机9200端口,获取es运行状态:

curl http://宿主机IP:9200

2. 双节点集群

双节点集群模式,以在不同两台服务器上部署为例:

两台主机修改主机参数:

cat >> /etc/sysctl.conf << EOF
vm.max_map_count=2048000
EOF
sysctl -p

两台主机创建目录并赋予权限:

# 创建目录
mkdir -p /data/elasticsearch/{data,logs,plugins}
# 赋予777递归权限
cd /data/elasticsearch
chmod 777 -R data logs plugins

两台主机构建docker-compose.yaml

node-1:

version: '3'
services:
  elasticsearch:
    image: elasticsearch:7.17.9
    container_name: elasticsearch
    volumes:
      - /data/elasticsearch/data:/usr/share/elasticsearch/data
      - /data/elasticsearch/logs:/usr/share/elasticsearch/logs
      - /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - /etc/localtime:/etc/localtime
    environment:
      - TZ="Asia/Shanghai"
      - cluster.name=my-es
      - node.name=node-1
      - network.host=0.0.0.0
      - network.publish_host=node-1宿主机ip
      - http.port=9200
      - discovery.seed_hosts=node-1宿主机ip,node-2宿主机ip
      - cluster.initial_master_nodes=node-1宿主机ip,node-2宿主机ip
      - path.data=/usr/share/elasticsearch/data
      - path.logs=/usr/share/elasticsearch/logs
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - http.cors.enabled=true
      - http.cors.allow-origin="*"
      - ingest.geoip.downloader.enabled=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200
      - 9300:9300
    logging:
      driver: json-file
      options:
        max-size: "100m"
        max-file: "50"
    restart: always

node-2:

version: '3'
services:
  elasticsearch:
    image: elasticsearch:7.17.9
    container_name: elasticsearch
    volumes:
      - /data/elasticsearch/data:/usr/share/elasticsearch/data
      - /data/elasticsearch/logs:/usr/share/elasticsearch/logs
      - /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins
      - /etc/localtime:/etc/localtime
    environment:
      - TZ="Asia/Shanghai"
      - cluster.name=my-es
      - node.name=node-2
      - network.host=0.0.0.0
      - network.publish_host=node-2宿主机ip
      - http.port=9200
      - discovery.seed_hosts=node-1宿主机ip,node-2宿主机ip
      - cluster.initial_master_nodes=node-1宿主机ip,node-2宿主机ip
      - path.data=/usr/share/elasticsearch/data
      - path.logs=/usr/share/elasticsearch/logs
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - http.cors.enabled=true
      - http.cors.allow-origin="*"
      - ingest.geoip.downloader.enabled=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200
      - 9300:9300
    logging:
      driver: json-file
      options:
        max-size: "100m"
        max-file: "50"
    restart: always

验证:

访问宿主机9200端口,获取es运行状态:

# 获取节点状态
curl http://node-1宿主机IP:9200
curl http://node-2宿主机IP:9200
# 获取集群状态
curl http://任意节点IP:9200/_cat/nodes?v=true

你可能感兴趣的:(elasticsearch,elasticsearch,docker)