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

搭建一个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版本

验证

  1. 在上述环境下,输入上面提到的那行命令,即可创建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
  1. 假设docker所在电脑的IP地址是192.168.1.101,如下图,浏览器访问此地址可以查看es信息:http://192.168.1.101:9200
    docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器)_第1张图片
  2. 如下图,浏览器访问此地址可以使用head插件:http://192.168.1.101:9100
    docker下,一行命令搭建elasticsearch6.5.0集群(带head插件和ik分词器)_第2张图片
  3. 执行以下命令来创建一个索引:
curl -X PUT http://192.168.1.101:9200/test001
  1. 执行以下命令验证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分词器都是正常的;

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

  1. 首先来看看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:
  1. 上述yml文件中,使用了镜像bolingcavalry/elasticsearch-head:6来实现head插件,关于此镜像的细节,请参考《自己动手制作elasticsearch-head的Docker镜像》;
  2. 上述yml文件中,使用了镜像bolingcavalry/elasticsearch-with-ik:6.5.0来集成ik分词器,关于此镜像的细节,请参考《自己动手制作elasticsearch的ik分词器的Docker镜像》;

希望这一行简单的命令能帮助您快速搭建所需的elasticsearch环境;

欢迎关注我的公众号:程序员欣宸

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

你可能感兴趣的:(Docker实战,elasticsearch,elasticsearch之旅)