docker搭建单机elasticsearch

前言

如果只是为了开发测试,本地搭建一个单机ES是一个不错的选择,本文总结了如何搭建单机版的ES并测试。

环境:

mac os 10.15.7

docker Docker version 20.10.2, build 2291f61

elasticsearch : 7.6.2

搭建步骤

  • 下载镜像

    docker pull elasticsearch:7.6.2
    

    这里要注意,一定要指定具体版本。

  • 准备基本环境

    在宿主机创建 /opt/workspace/work/es/single/,路径根据自己的具体情况配置。

    export ES_SINGLE=/opt/workspace/work/es/single
    # es在宿主机的的配置文件
    mkdir ${ES_SINGLE}/config/
    # es在宿主机的数据存储目录
    mkdir ${ES_SINGLE}/data/
    vim ${ES_SINGLE}/config/es-single.yml 
    

    es-single.yml 写入以下内容,wq:

    cluster.name: elasticsearch-single
    node.name: es-single-node-1
    network.bind_host: 0.0.0.0
    # 当前主机的地址
    network.publish_host: 127.0.0.1
    http.port: 9200
    transport.tcp.port: 9300
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    node.master: true
    node.data: true
    bootstrap.system_call_filter: false
    cluster.initial_master_nodes: ["es-single-node-1"]
    
  • 启动

    docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v ${ES_SINGLE}/config/es-single.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v ${ES_SINGLE}/data:/usr/share/elasticsearch/data --name es-single elasticsearch:7.6.2
    

    ES_JAVA_OPTS 参数指定了ES启动JVM的参数,默认好像是2G吧,所以本地测试调小点就行了。然后正常映射端口,映射路径

  • 查看是否启动成功

    # 查看容器日志
    docker logs -f es-single
    
    # 请求
    curl localhost:9200 
    {
           
      "name" : "es-single-node-1",
      "cluster_name" : "elasticsearch-single",
      "cluster_uuid" : "bfNp_65OQx6-MXFZ7_79NA",
      "version" : {
           
        "number" : "7.6.2",
        "build_flavor" : "default",
        "build_type" : "docker",
        "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
        "build_date" : "2020-03-26T06:34:37.794943Z",
        "build_snapshot" : false,
        "lucene_version" : "8.4.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    

搭建过程的异常

  • Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes

    表面上是说容器目录的权限不够,实际是宿主机的权限不够,给宿主机赋予权限。

    chmod -R 777 ${ES_SINGLE}
    
  • ERROR: [1] bootstrap checks failed

    yml加入,注意es-single-node-1指的是node.name的配置值,填错了也会报错。

    bootstrap.system_call_filter: false
    cluster.initial_master_nodes: ["es-single-node-1"]
    

总结

本文总结了docker 搭建ES单机版的内容。

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