【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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章