Docker安裝ElasticSearc並實現掛載


一、環境配置

1.查看max_map_count的值 默認是65530

cat /proc/sys/vm/max_map_count

2.設置max_map_count的值,因為ES 的默認分配的內存太較大,進程會被自動殺死。

sysctl -w vm.max_map_count=262144

二、下載鏡像、創建掛載目錄及文件

1.拉取鏡像: https://hub.docker.com/_/elasticsearch?tab=tags

 docker pull elasticsearch:7.16.3

2.創建掛載目錄

# 創建目錄
mkdir -p /usr/local/elasticsearch/data
mkdir -p /usr/local/elasticsearch/config
# 授予權限
chmod 777 /usr/local/elasticsearch/data
chmod 777 /usr/local/elasticsearch/config

3.創建配置文件

cd /usr/local/elasticsearch/config
touch elasticsearch.yml
vim elasticsearch.yml
添加配置:
# ======================== Elasticsearch Configuration =========================
cluster.name: my-application
cluster.initial_master_nodes: ["node-1"]
node.name: node-1
network.host: 0.0.0.0
http.port: 9200

三、創建容器

3.1 單節點

docker run -d --restart=always --name elasticsearch \
-p 9200:9200 -p 9300:9300 \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-v /usr/local/elasticsearch/data:/usr/share/elasticsearch/data \
-v /usr/local/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /etc/localtime:/etc/localtime:ro \
elasticsearch:7.16.3

3.2 集群

3.2.1 節點配置

# es1.yml
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 192.168.252.160
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts: ["192.168.252.160:9300","192.168.252.160:9301"]
discovery.zen.minimum_master_nodes: 1
cluster.initial_master_nodes: es-node1

# es2.yml
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.252.160
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts: ["192.168.252.160:9300","192.168.252.160:9301"]
discovery.zen.minimum_master_nodes: 1
cluster.initial_master_nodes: es-node1

3.2.2 創建容器

# 節點1
docker run -d --restart=always --name es1 \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m"  \
-p 9200:9200 -p 9300:9300 \
-v /etc/localtime:/etc/localtime:ro \
-v /usr/local/elasticsearch/node1/data:/usr/share/elasticsearch/data \
-v /usr/local/elasticsearch/node1/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
elasticsearch:7.16.3

# 節點2
docker run -d --restart=always --name es2 \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m"  \
-p 9201:9201 -p 9301:9301 \
-v /etc/localtime:/etc/localtime:ro \
-v /usr/local/elasticsearch/node2/data:/usr/share/elasticsearch/data \
-v /usr/local/elasticsearch/node2/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
elasticsearch:7.16.3

如無報錯,打開瀏覽器輸入:http://IP:9200/看到入如下信息表示安裝成功:

{
  "name" : "node-1",
  "cluster_name" : "my-application",
  "cluster_uuid" : "1eGfPnFuR0-_rdn5aP9CAw",
  "version" : {
    "number" : "7.16.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "4e6e4eab2297e949ec994e688dad46290d018022",
    "build_date" : "2022-01-06T23:43:02.825887787Z",
    "build_snapshot" : false,
    "lucene_version" : "8.10.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

四、配置跨域

4.1 進入容器

由於要進行配置,因此需要進入容器當中修改相應的配置信息。

docker exec -it 容器id /bin/bash

4.2 進行配置

# 顯示文件
ls
結果如下:
LICENSE.txt  README.textile  config  lib   modules
NOTICE.txt   bin             data    logs  plugins

# 進入配置文件夾
cd config

# 顯示文件
ls
結果如下:
elasticsearch.keystore  ingest-geoip  log4j2.properties  roles.yml  users_roles
elasticsearch.yml       jvm.options   role_mapping.yml   users

# 修改配置文件
vi elasticsearch.yml

# 加入跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

安裝配置完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM