【docker-compose】ElasticSearch安装教程

仅供学习参考 ,请勿轻易在生产环境使用

0. 目录树


1. 创建目录

mkdir -p /docker/elasticsearch/conf /docker/elasticsearch/data /docker/elasticsearch/logs
chmod -R 777 /docker/elasticsearch/conf /docker/elasticsearch/data /docker/elasticsearch/logs

2. 编写配置文件

文件名:elasticsearch.yml

# 设置Elasticsearch节点的发现类型为单节点
discovery.type: "single-node"
# 尝试锁定内存,以防止被交换到磁盘
bootstrap.memory_lock: true
# Elasticsearch在所有可用网络接口上侦听,可以从任何地址访问
network.host: 0.0.0.0
# Elasticsearch HTTP API的监听端口
http.port: 9200
# Elasticsearch节点之间通信的TCP端口
transport.tcp.port: 9300
# Elasticsearch日志文件的输出路径
path.logs: /usr/share/elasticsearch/logs
# 启用跨域资源共享(CORS)支持
http.cors.enabled: true
# 允许来自任何源的跨域请求
http.cors.allow-origin: "*"
# 启用Elasticsearch安全性功能
xpack.security.enabled: true
# 允许包含 Authorization 头的跨域请求
http.cors.allow-headers: Authorization
# 启用节点之间传输层安全性(TLS/SSL)
xpack.security.transport.ssl.enabled: true

3. 编写docker-compose

version: '3.3'
networks:
  es:
services:
  elasticsearch:
    image: elasticsearch:7.13.3
    container_name: elasticsearch
    privileged: true
    environment:
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      cluster.name: elasticsearch
      ES_JAVA_OPTS: "-Xms256m -Xmx256m" # 设置 JVM 内存大小
    volumes:
      - "./data:/usr/share/elasticsearch/data:rw"
      - "./config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
      - "./logs:/usr/share/elasticsearch/logs:rw"
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - es


4.编写启动脚本

#!/usr/bin/env bash

echo "stop container"
docker stop elasticsearch
echo "del container"
docker rm elasticsearch

docker-compose -f ./docker-compose.yml up -d
echo 'success'

附录

  1. 设置密码

    # 进入容器
    docker exec -it elasticsearch /bin/bash
    # 设置密码
    elasticsearch-setup-passwords interactive
    
  2. 重置密码

    # 修改elastic密码为123456
    curl -H "Content-Type:application/json" -XPOST -u elastic 'http://127.0.0.1:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'
    

最后

文件已经上传github,可直接复制下载:https://github.com/handsometaoa/env-deploy

posted @ 2024-01-25 21:22  帅气的涛啊  阅读(267)  评论(0编辑  收藏  举报