学习Elasticsearch之2:在Ubuntu 14.04上安装Elasticsearch

Elasticsearch的安装有两种方式

通过tar.gz压缩包安装步骤

  • SSH远程连接Ubuntu Server,ssh ethancai@###.###.###.###
  • 下载Elasticsearch
    • mkdir ~/Downloads && cd ~/Downloads && curl -L -O https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.1/elasticsearch-2.3.1.tar.gz
  • 解压
    • tar -xzvf elasticsearch-2.3.1.tar.gz && sudo mv elasticsearch-2.3.1 /var/
  • 运行
    • cd /var/elasticsearch-2.3.1/bin
    • ./elasticsearch,或者以指定clusternode名称的方式运行./elasticsearch --cluster.name my_cluster_name --node.name my_node_name

通过apt命令安装

  • Download and install the Public Signing Key
    • wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
  • Save the repository definition
    • echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
  • Install elasticsearch, 注意这里需要指定安装版本
    • sudo apt-get update && sudo apt-get install elasticsearch=2.3.2
  • Configure Elasticsearch to automatically start during bootup
    • sudo update-rc.d elasticsearch defaults 95 10

验证安装是否成功

By default, Elasticsearch uses port 9200 to provide access to its REST API. 你可以通过下面命令验证是否启动成功:

  • curl 'localhost:9200/_cat/health?v'
  • curl 'localhost:9200/_cat/nodes?v'
  • 验证后记得重启,sudo shutdown -r 0

默认目录

不同安装方式,默认的目录位置不一样,参考ElasticSearch Reference 2.3 - Directory Layout

通过tar.gz方式安装的默认目录如下:

Type Description Location Debian/Ubuntu
home Home of elasticsearch installation. {extract.path}
bin Binary scripts including elasticsearch to start a node. {extract.path}/bin
conf Configuration files elasticsearch.yml and logging.yml. {extract.path}/config
data The location of the data files of each index / shard allocated on the node. {extract.path}/data
logs Log files location {extract.path}/logs
plugins Plugin files location. Each plugin will be contained in a subdirectory. {extract.path}/plugins
repo Shared file system repository locations. Not configured
script Location of script files. {extract.path}/config/scripts

通过apt方式安装的默认目录如下:

Type Description Location Debian/Ubuntu
home Home of elasticsearch installation. /usr/share/elasticsearch
bin Binary scripts including elasticsearch to start a node. /usr/share/elasticsearch/bin
conf Configuration files elasticsearch.yml and logging.yml. /etc/elasticsearch
conf Environment variables including heap size, file descriptors. There isn’t this folder when install using tar.gz /etc/default/elasticsearch
data The location of the data files of each index / shard allocated on the node. /var/lib/elasticsearch
logs Log files location /var/log/elasticsearch
plugins Plugin files location. Each plugin will be contained in a subdirectory. /usr/share/elasticsearch/plugins
repo Shared file system repository locations. Not configured
script Location of script files. /etc/elasticsearch/scripts

安装Elasticsearch插件

Plugins are a way to enhance the core Elasticsearch functionality in a custom manner. They range from adding custom mapping types, custom analyzers, native scripts, custom discovery and more.

There are three types of plugins:

  • Java Plugins: These plugins contain only JAR files, and must be installed on every node in the cluster. After installation, each node must be restarted before the plugin becomes visible.
  • Site Plugins: These plugins contain static web content like Javascript, HTML, and CSS files, that can be served directly from Elasticsearch. Site plugins may only need to be installed on one node, and do not require a restart to become visible.
    • The content of site plugins is accessible via a URL like: http://yournode:9200/_plugin/[plugin name]
  • Mixed Plugins: Mixed plugins contain both JAR files and web content.

安装Site Plugins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ sudo su -
$ cd /

# 查看已安装插件
$ /usr/share/elasticsearch/bin/plugin list

# 安装elasticsearch-head,访问http://localhost:9200/_plugin/head
$ /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

# 安装elasticsearch-kopf,访问http://localhost:9200/_plugin/kopf
$ /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf

# 安装elastichq,访问http://localhost:9200/_plugin/hq
$ /usr/share/elasticsearch/bin/plugin install royrusso/elasticsearch-HQ

# 安装elasticsearch-inquisitor,访问http://localhost:9200/_plugin/elasticsearch-inquisitor
$ /usr/share/elasticsearch/bin/plugin install polyfractal/elasticsearch-inquisitor

安装Jar Plugins

安装elasticsearch-analysis-ik

安装前请注意和Elasticsearch的适配版本,安装elasticsearch-analysis-ik有两种方式:

  • 通过源代码手动编译安装
    • 请先安装apache maven
    • 下载Apache Maven的源代码
      • git clone https://github.com/medcl/elasticsearch-analysis-ik.git
    • 编译打包
      • cd [elasticsearch-analysis-ik所在目录]
      • git checkout v1.9.2
      • mvn package
    • 解压编译生成的打包文件target/releases/elasticsearch-analysis-ik-{version}.zip,并拷贝解压后的文件到Elasticsearch的plugins目录
      • unzip elasticsearch-analysis-ik-{version}.zip -d elasticsearch-analysis-ik-{version}
      • scp -r elasticsearch-analysis-ik-{version} ubuntu@xxx.xxx.xxx.xxx:/var/tmp/
      • cp -R /var/tmp/elasticsearch-analysis-ik-{version} /usr/share/elasticsearch/plugins
      • chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-{version}
    • 重启elasticsearch服务
      • service elasticsearch restart
  • 直接下载安装程序包
    • 下载指定版本的打包文件
    • 后续步骤同上

注意:

  • v2.2.1版本以后安装elasticsearch-analysis-ik并不需要修改elasticsearch.yml配置文件

安装elasticsearch-analysis-pinyin

安装前请注意和Elasticsearch的适配版本, 安装elasticsearch-analysis-pinyin有两种方式:

  • 通过源代码手动编译安装
    • 具体操作参考elasticsearch-analysis-ik的安装步骤
  • 直接下载安装程序包

安装elasticsearch-analysis-stconvert

安装前请注意和Elasticsearch的适配版本, 安装elasticsearch-analysis-stconvert有两种方式:

  • 通过源代码手动编译安装
    • 具体操作参考elasticsearch-analysis-ik的安装步骤
  • 直接下载安装程序包

总结

建议Oracle JDK和Elasticsearch均使用apt安装方式安装,原因如下:

  • 安装方便
  • 默认目录的配置更规范
  • 和Elasticsearch官方的Docker镜像的目录结构一致,未来切换到Docker方式部署障碍少,参考Dockerfile