Docker部署Elasticsearch单节点 | Eddie'Blog
Docker部署Elasticsearch单节点

Docker部署Elasticsearch单节点

eddie 960 2020-09-06

目录

版本说明

docker-compose version 1.26.2
elasticsearch:6.8.12
elasticsearch-head:6
analysis-ik-6.8.12

一、准备工作

1.1 创建文件夹

mkdir -p /data/elasticsearch/conf
mkdir -p /data/elasticsearch/data
mkdir -p /data/elasticsearch/logs

1.2 创建 elasticsearch.yml

bootstrap.memory_lock: false
cluster.name: "es-server"
node.name: node-1
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
path.logs: /usr/share/elasticsearch/logs
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.security.audit.enabled: true

1.3 创建 jvm.options

  • 低配云服务器,建议定义 Xms 和 Xmx
-Djna.nosys=true

# turn off a JDK optimization that throws away stack traces for common
# exceptions because stack traces are important for debugging
-XX:-OmitStackTraceInFastThrow

-Xms128m  
-Xmx128m

# flags to configure Netty
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0

# log4j 2
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true

-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging

8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise
# time/date parsing will break in an incompatible way for some date patterns and locals
9-:-Djava.locale.providers=COMPAT

# temporary workaround for C2 bug with JDK 10 on hardware with AVX-512
10-:-XX:UseAVX=2

1.4 赋予执行权限

chown 1000:1000 /data/elasticsearch/data

1.5.1 查看服务器文件句柄数

[root@eddie ~]# sysctl -a|grep vm.max_map_count
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.br-12b3af4da15f.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.docker0.stable_secret"
sysctl: reading key "net.ipv6.conf.eth0.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.veth669472e.stable_secret"
sysctl: reading key "net.ipv6.conf.vethe3d5ade.stable_secret"
vm.max_map_count = 65530

1.5.2 修改服务器文件句柄数

# 临时修改
sysctl -w vm.max_map_count=262144

# 永久修改
vim /etc/sysctl.conf
vm.max_map_count=262144
sysctl -p

二、部署 Elasticsearch And Head

2.1 docker-compose.yaml

version: '3'
services:
  elasticsearch:
    image:  elasticsearch:6.8.12
    container_name: elasticsearch
    restart: always
    volumes:
      - /data/elasticsearch/data:/usr/share/elasticsearch/data:rw
      - /data/elasticsearch/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /data/elasticsearch/conf/jvm.options:/usr/share/elasticsearch/config/jvm.options
      - /data/elasticsearch/logs:/user/share/elasticsearch/logs:rw
    ports:
        - "9200:9200"
        - "9300:9300"
    environment:
        - discovery.type=single-node
  es-head:
    image: tobias74/elasticsearch-head:6
    container_name: es-head
    restart: always
    ports:
      - "9100:9100"

2.2 运行

[root@eddie ~]# docker-compose up -d

[root@eddie ~]# docker-compose ps
    Name                   Command               State                       Ports                     
-------------------------------------------------------------------------------------------------------
elasticsearch   /usr/local/bin/docker-entr ...   Up      0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp
es-head         /bin/sh -c grunt server          Up      0.0.0.0:9100->9100/tcp    

2.3 查看端口

[root@eddie ~]# netstat -ntlp | grep 00
tcp6       0      0 :::9100                 :::*                    LISTEN      1597/docker-proxy-c 
tcp6       0      0 :::9200                 :::*                    LISTEN      1619/docker-proxy-c 
tcp6       0      0 :::9300                 :::*                    LISTEN      1608/docker-proxy-c 

三、检查

3.1 检查 elasticsearch

[root@eddie ~]# curl 127.0.0.1:9200
{
  "name" : "node-1",
  "cluster_name" : "es-server",
  "cluster_uuid" : "45mg9ikDQwCrAEORIMuOwA",
  "version" : {
    "number" : "6.8.12",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "7a15d2a",
    "build_date" : "2020-08-12T07:27:20.804867Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.3",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

3.2 检查 elasticsearch-head

图片.png

四、IK分词器

4.1 下载对应版本

下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

4.2 拷贝到容器

docker cp /data/elasticsearch/elasticsearch-analysis-ik-6.8.12.zip elasticsearch:/usr/share/elasticsearch/plugins/

4.3 解压

mkdir -p plugins/ik
unzip elasticsearch-analysis-ik-6.8.12.zip

4.4 重启服务

docker restart elasticsearch

4.5 测试分词

没有使用分词器:

http://127.0.0.1:9200/book/

图片.png

使用分词器:

http://127.0.0.1:9200/book_ik/

图片.png