https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

线上部署目前最方便的还是使用Docker,先看下官方给的docker-compose
tips: 请根据自己要使用的elasticsearch版本自行修改下面出现的版本

version: '2.2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  elasticsearch2:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "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

volumes:
  esdata1:
    driver: local
  esdata2:
    driver: local

networks:
  esnet:

里面包括了两个节点,而根据我们公司目前的业务需求,这样其实就够了

不过我们还需要在Elasticsearch里面加入中文的分词软件,所以我们要在官方镜像的基础上做一些改动,我们需要新建一个Dockerfile

FROM docker.elastic.co/elasticsearch/elasticsearch:6.7.0
RUN mkdir -p /usr/share/elasticsearch/plugins/ik
RUN cd /usr/share/elasticsearch/plugins/ik && wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.7.0/elasticsearch-analysis-ik-6.7.0.zip && unzip elasticsearch-analysis-ik-6.7.0.zip

一开始我以为只要两行,结果发现使用elasticsearch-plugin来安装的时候会发生问题,所以用这种方式来安装插件

将Dockerfile和docker-compose.yml放在同一个文件夹下,文件结构如下

elasticsearch
  -- Dockerfile
  -- docker-compose.yml 

并且最终的docker-compose.yml内容如下

version: '2.2'
services:
  elasticsearch:
    build:
      context: ./
      dockerfile: Dockerfile
    # image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  elasticsearch2:
    build:
      context: ./
      dockerfile: Dockerfile
    #image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "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

volumes:
  esdata1:
  esdata2:

networks:
  esnet:

准备好了这两个文件后,ssh到我们的服务器准备开始部署,有个东西需要注意下,官方也说了

The vm.max_map_count kernel setting needs to be set to at least 262144 for production use. Depending on your platform:

所以我们需要打开/etc/sysctl.conf这个文件,加一行

vm.max_map_count=262144

否则等会启动的时候会报错

现在我们可以进入刚才的文件夹,构建项目了

docker-compose up

装中文插件那步可能会比较慢,如果太慢ctrl + c 关掉再来一次可能会解决

一切顺利的话,现在elasticsearch就已经部署完毕了

$ curl localhost:9200

{
  "name" : "eg-GUK0",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "jTA2SV03RZCUCq-Mnvj1Nw",
  "version" : {
    "number" : "6.7.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "8453f77",
    "build_date" : "2019-03-21T15:32:29.844721Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

加入systemctl,支持开机启动以及自动重连

进到/lib/systemd/system下,新建文件elasticsearch.service

[Unit]
Description=Elasticsearch

[Service]
User=root

ExecStart=/bin/bash -c 'cd /data/dockers/elasticsearch && docker-compose up'
ExecStop=/bin/bash -c 'cd /data/dockers/elasticsearch && docker-compose down'

Restart=always
StandardOutput=syslog
StandardError=syslog

RestartSec=5s

[Install]

WantedBy=multi-user.target

然后ln过去

$ ln -s /lib/systemd/system/elasticsearch.service /etc/systemd/system/multi-user.target.wants/elasticsearch.service

先关掉elasticsearch

docker-compose down

然后之后就可以用systemd来启动了

$ systemctl start elasticsearch