docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器)

Posted 程序员欣宸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器)相关的知识,希望对你有一定的参考价值。

欢迎访问我的GitHub

  • 搭建一个elasticsearch6.5.0集群环境,再把elasticsearch-head插件和ik分词器装好,在docker环境下完成这些工作需要多久?
  • 答案是:只需下面这一行命令:

    wget https://raw.githubusercontent.com/zq2599/blog_demos/master/elasticsearch_docker_compose/docker-compose.yml && \\
    docker-compose up -d
  • 接下来就试试这一行命令吧;

环境信息

  1. 操作系统:CentOS 7.6
  2. docker:17.03.2-ce
  3. docker-compose:version 1.23.2
  4. elasticsearch:这里选用的是6.5.0版本的elasticsearch,因为目前ik分词器官方最高只支持到6.5.0版本

验证

  • 在上述环境下,输入上面提到的那行命令,即可创建elasticsearch6.5.0集群环境,如下:
[root@hedy 003]# wget https://raw.githubusercontent.com/zq2599/blog_demos/master/elasticsearch_docker_compose/docker-compose.yml && \\
> docker-compose up -d
--2019-01-27 20:38:10--  https://raw.githubusercontent.com/zq2599/blog_demos/master/elasticsearch_docker_compose/docker-compose.yml
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.24.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.24.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1227 (1.2K) [text/plain]
正在保存至: “docker-compose.yml”

100%[=======================================================================================================================================================================>] 1,227       --.-K/s 用时 0s      

2019-01-27 20:38:11 (61.9 MB/s) - 已保存 “docker-compose.yml” [1227/1227])

Creating network "003_esnet" with the default driver
Creating head           ... done
Creating elasticsearch2 ... done
Creating elasticsearch  ... done
  • 假设docker所在电脑的IP地址是192.168.1.101,如下图,浏览器访问此地址可以查看es信息:http://192.168.1.101:9200

  • 如下图,浏览器访问此地址可以使用head插件:http://192.168.1.101:9100

  • 执行以下命令来创建一个索引:
curl -X PUT http://192.168.1.101:9200/test001
  • 执行以下命令验证ik分词器效果:
curl -X POST \\
http://192.168.1.101:9200/test001/_analyze?pretty=true \\
-H Content-Type: application/json \\
-d "text":"我们是软件工程师","tokenizer":"ik_smart"
  • 收到的响应如下,可见ik分词器已经生效:

  "tokens" : [
    
      "token" : "我们",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    ,
    
      "token" : "是",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 1
    ,
    
      "token" : "软件",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    ,
    
      "token" : "工程师",
      "start_offset" : 5,
      "end_offset" : 8,
      "type" : "CN_WORD",
      "position" : 3
    
  ]
  • 验证完毕,集群、head插件、ik分词器都是正常的;

一行命令如何能实现上述功能

  • 首先来看看docker-compose.yml文件的内容,这里面决定了整个构成整个功能的容器:
version: 2.2
services:
  elasticsearch:
    image: bolingcavalry/elasticsearch-with-ik:6.5.0
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  elasticsearch2:
    image: bolingcavalry/elasticsearch-with-ik:6.5.0
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata2:/usr/share/elasticsearch/data
    networks:
      - esnet
  head:
    image: bolingcavalry/elasticsearch-head:6
    container_name: head
    ports:
      - 9100:9100
    networks:
      - esnet
volumes:
  esdata1:
    driver: local
  esdata2:
    driver: local

networks:
  esnet:

欢迎关注51CTO博客:程序员欣宸

以上是关于docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器)的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch安装(以Docker的方式)

搭建docker私有镜像仓库

Docker 环境下搭建nexus私服

动手搭建docker并在docker容器中建spark集群

在Docker下搭建Apache+PHP+mysql环境的过程记录

Centos搭建Docker环境