elasticsearch官网地址为:
https://www.elastic.co/
本文档详细描述elastic search集群安装和基本使用。本例采用3台服务器,搭建四节点elastic search集群,其中一台服务器上运行两个elastic search程序。
安装环境如下:
- CPU:六核心
- 内存:8GB
- 操作系统版本centos7.6
- elasticsearch-6.5.4、kibana-6.5.4、cerebro-0.8.1
- jdk1.8.0_171
- 服务器名称分别是myserver01、myserver02、myserver03
kibana是web端的elasticsearch管理工具可以在里面很方便的写命令
cerebro可以在web端可视化查看节点、索引、分片等的信息
准备需要的软件
新建/u01/soft和/u01/app两个目录
[root@myserver01 /]#mkdir -p /u01/soft
[root@myserver01 /]#mkdir -p /u01/app
[root@myserver01 /]cd /u01
[root@myserver01 u01]# ls
app soft
本例中下载的所有软件放在/u01/soft
所有的软件解压后都放在/u01/app
java可以直接到oracle官网去下载https://www.java.com/en/download/manual.jsp
[root@myserver01 /]cd /u01/soft
[root@myserver01 /u01/soft]wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
[root@myserver01 /u01/soft]wget https://github.com/lmenezes/cerebro/releases/download/v0.8.1/cerebro-0.8.1.tgz
[root@myserver01 /u01/soft]wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.4-linux-x86_64.tar.gz
[root@myserver01 soft]# ls
cerebro-0.8.1.tgz kibana-6.5.4-linux-x86_64.tar.gz
elasticsearch-6.5.4.tar.gz jdk-8u171-linux-x64.tar.gz
解压下载的软件
[root@myserver01 /]cd /u01/app
[root@myserver01 /u01/app]tar -xzvf /u01/soft/elasticsearch-6.5.4.tar.gz
[root@myserver01 /u01/app]tar -xzvf /u01/soft/cerebro-0.8.1.tgz
[root@myserver01 /u01/app]tar -xzvf /u01/soft/kibana-6.5.4-linux-x86_64.tar.gz
[root@myserver01 /u01/app]tar -xzvf /u01/soft/jdk-8u171-linux-x64.tar.gz
[root@myserver01 app]# ls
cerebro-0.8.1 elasticsearch-6.5.4 jdk1.8.0_171 kibana-6.5.4-linux-x86_64
将elasticsearch-6.5.4和jdk1.8.0_171目录用scp拷贝到其余两个节点,另外再myserver02中额外拷贝一份elasticsearch-6.5.4。
拷贝完成后myserver02和myserver03目录为
[root@myserver02 app]# ls
elasticsearch-6.5.4-2
elasticsearch-6.5.4
jdk1.8.0_171
[root@myserver03 app]# ls
elasticsearch-6.5.4
jdk1.8.0_171
操作系统配置
操作配置,三台主机都要做
- 配置hosts
[root@myserver01 app]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.16.9.32 myserver02
192.16.9.30 myserver01
192.16.9.33 myserver03
- 添加用户
[root@localhost ~]# groupadd elastic
[root@localhost ~]# useradd elastic -g elastic
- 修改操作系统参数
[root@localhost ~]# vi /etc/security/limits.conf
* hard nofile 65536
* soft nofile 65536
[root@localhost ~]# vi /etc/sysctl.conf
vm.max_map_count = 262144
- 修改文件权限
[root@localhost app]# cd /u01/app
[root@localhost app]# chown -R elastic:elastic *
[root@localhost app]# ll
total 12
drwxr-xr-x. 5 elastic elastic 53 Jun 20 2018 cerebro-0.8.1
drwxr-xr-x. 8 elastic elastic 4096 Dec 17 16:21 elasticsearch-6.5.4
drwxr-xr-x. 6 elastic elastic 4096 Dec 15 14:45 jdk1.8.0_171
drwxrwxr-x. 11 elastic elastic 4096 Dec 17 16:36 kibana-6.5.4-linux-x86_64
- 为elastic用户添加环境变量
[root@myserver01 ~]# su - elastic
[elastic@myserver01 ~]$ vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
JAVA_HOME=/u01/app/jdk1.8.0_171
export JAVA_HOME
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin
export PATH
- 重启三台服务器
配置elasticsearch
- 编辑elasticsearch.yml
实际使用的时候还可以修改端口号等,具体可以查看官方文档
myserver01修改如下:
[root@myserver01 ~]# su - elastic
[elastic@myserver01 ~]$ cd /u01/app/elasticsearch-6.5.4/config/
[elastic@myserver01 config]$ vi elasticsearch.yml
cluster.name: my-cluster
node.name: node-0
network.host: 0.0.0.0
discovery.zen.ping.unicast.hosts: ["myserver01", "myserver02","myserver03"]
myserver02修改如下:
[root@myserver02 ~]# su - elastic
[elastic@myserver02 ~]$ cd /u01/app/elasticsearch-6.5.4/config/
[elastic@myserver02 config]$ vi elasticsearch.yml
cluster.name: my-cluster
node.name: node-1
network.host: 0.0.0.0
discovery.zen.ping.unicast.hosts: ["myserver01", "myserver02","myserver03"]
[elastic@myserver02 ~]$ cd /u01/app/elasticsearch-6.5.4-2/config/
[elastic@myserver02 config]$ vi elasticsearch.yml
cluster.name: my-cluster
node.name: node-1-1
network.host: 0.0.0.0
discovery.zen.ping.unicast.hosts: ["myserver01", "myserver02","myserver03"]
myserver03修改如下:
[root@myserver03 ~]# su - elastic
[elastic@myserver03 ~]$ cd /u01/app/elasticsearch-6.5.4/config/
[elastic@myserver03 config]$ vi elasticsearch.yml
cluster.name: my-cluster
node.name: node-2
network.host: 0.0.0.0
discovery.zen.ping.unicast.hosts: ["myserver01", "myserver02","myserver03"]
- 修改kibana.yml
只在myserver01修改
[elastic@myserver01 ~]$ cd /u01/app/kibana-6.5.4-linux-x86_64/config
[elastic@myserver01 config]$ vi kibana.yml
elasticsearch.url: "http://myserver01:9200"
- 依次启动elasticsearch
启动myserver01
[elastic@myserver01 app]$ cd /u01/app/elasticsearch-6.5.4/
[elastic@myserver01 elasticsearch-6.5.4]$ ./bin/elasticsearch -d -Ecluster.name=my_cluster -Ehttp.port=9200 -Epath.data=node_0 -Enode.name=node_0 -Enode.attr.box_type=hot
[elastic@myserver01 app]$ cd /u01/app/kibana-6.5.4-linux-x86_64/
[elastic@myserver01 kibana-6.5.4-linux-x86_64]$./bin/kibana &
log [07:38:33.812] [info][listening] Server running at http://192.16.9.30:5601
kibana端口是5601
[elastic@myserver01 app]$ cd /u01/app/kibana-6.5.4-linux-x86_64/
[elastic@myserver01 cerebro-0.8.1]$./bin/cerebro &
[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
cerebro端口是9000
启动myserver02
[elastic@myserver02 app]$ cd /u01/app/elasticsearch-6.5.4/
[elastic@myserver02 elasticsearch-6.5.4]$ ./bin/elasticsearch -d -Ecluster.name=my_cluster -Ehttp.port=9200 -Epath.data=node_0 -Enode.name=node_1 -Enode.attr.box_type=hot
[elastic@myserver02 app]$ cd /u01/app/elasticsearch-6.5.4-2/
[elastic@myserver02 elasticsearch-6.5.4-2]$ ./bin/elasticsearch -d -Ecluster.name=my_cluster -Ehttp.port=9201 -Epath.data=node_0 -Enode.name=node_1-1 -Enode.attr.box_type=cold
启动myserver03
[elastic@myserver03 elasticsearch-6.5.4]$ ./bin/elasticsearch -d -Ecluster.name=my_cluster -Ehttp.port=9200 -Epath.data=node_0 -Enode.name=node_2 -Enode.attr.box_type=hot
测试四个节点正常开启
[elastic@myserver01 elasticsearch-6.5.4]$ curl 192.16.9.30:9200
{
"name" : "node_0",
"cluster_name" : "my_cluster",
"cluster_uuid" : "irt9WwVMQ4ut-i4H6oTw1A",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
[elastic@myserver01 elasticsearch-6.5.4]$ curl 192.16.9.32:9200
{
"name" : "node_1",
"cluster_name" : "my_cluster",
"cluster_uuid" : "irt9WwVMQ4ut-i4H6oTw1A",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
[elastic@myserver01 elasticsearch-6.5.4]$ curl 192.16.9.32:9201
{
"name" : "node_1-1",
"cluster_name" : "my_cluster",
"cluster_uuid" : "irt9WwVMQ4ut-i4H6oTw1A",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
[elastic@myserver01 elasticsearch-6.5.4]$ curl 192.16.9.33:9200
{
"name" : "node_2",
"cluster_name" : "my_cluster",
"cluster_uuid" : "irt9WwVMQ4ut-i4H6oTw1A",
"version" : {
"number" : "6.5.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "d2ef93d",
"build_date" : "2018-12-17T21:17:40.758843Z",
"build_snapshot" : false,
"lucene_version" : "7.5.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
通过cerebro查看节点信息
访问cerebro http://192.16.9.30:9000
通过kibana简单运行几个命令
浏览器访问http://172.16.9.30:5601/
在Dev tool页签 在文本框输入命令,命令的作用是:
删除test索引
新建test索引并插入一个文档,文档id为1
插入id为2的文档
查询test索引的所有记录
获取test索引文档id为1的文档
获取节点的健康信息
delete /test
post /test/_doc/1
{
"title":"test"
}
post /test/_doc/2
{
"title":"test2"
}
get /test/_search
get /test/_doc/1
get /_cat/health?v
kibana截图
在kibana中运行一下语句
PUT /gjw_index
{
"settings": {
"number_of_shards": 4,
"number_of_replicas": 1,
"index.routing.allocation.include.box_type":"hot"
}
}
POST /gjw_index/_doc/1
{
"name":"shrink1"
}
POST /gjw_index/_doc/2
{
"name":"shrink2"
}
POST /gjw_index/_doc/3
{
"name":"shrink3"
}
POST /gjw_index/_doc/4
{
"name":"shrink4"
}
POST /gjw_index/_doc/5
{
"name":"shrink5"
}
POST /gjw_index/_doc/6
{
"name":"shrink6"
}
POST /gjw_index/_doc/7
{
"name":"shrink7"
}
POST /gjw_index/_doc/8
{
"name":"shrink8"
}
之后刷新cerebro页面