Elasticsearch-ES 5.x (Head,Kibana,X-Pack) 集群的部署、配置、测试
2017年03月31日 09:55:14
阅读数:11965
Elasticsearch-ES 5.x (Head,Kibana,X-Pack) 集群的部署、配置、测试
搭建环境为公司的三台 CentOS 7.3测试服务器
版本5.2.2
拓展组件 Head,Kibana,X-Pack
1 安装前准备
1、Elasticsearch的版本选择5.2.2; 2、ES的常用组件安装,Kibana、Sense等; 3、ES主节点选择192.168.10.115,一共三个节点(192.168.10.114/115/116); 4、安装环境CentOS 7.3, JDK1.8.0_121; 一共3台服务器,安装文件放在 /home/hadoop/install_file/
注意路径 下载-解压
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.2.tar.gz tar -zxvf elasticsearch-5.2.2.tar.gz -C /home/hadoop/install_path/ |
|
2 配置ElasticSearch
2.1 修改系统相关文件
启动前需要修改配置文件(需要权限) 1. 设置内核参数
vim /etc/sysctl.conf 添加如下内容: fs.file-max=65536 vm.max_map_count=262144 之后可以使用sysctl –a查看 –p刷新 |
2. 设置资源参数
vim /etc/security/limits.conf 添加如下内容: * soft nofile 65536 * hard nofile 131072 * soft nproc 2048 * hard nproc 4096 |
3. 修改进程数
vim /etc/security/limits.d/20-nproc.conf 修改( * soft nproc )的值为2048 |
|
2.2 修改集群配置文件
一共3个节点,拥有相同的集群名,修改节点名称和对应的IP即可
# ================= Elasticsearch Configuration =================== # #配置es的集群名称, es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。 cluster.name: bigData-cluster # #节点名称 node.name: node-6 # #指定该节点是否有资格被选举成为node node.master: true # #指定该节点是否存储索引数据,默认为true。 node.data: true # #设置绑定的ip地址还有其它节点和该节点交互的ip地址 network.host: 192.168.10.116 # #指定http端口,你使用head、kopf等相关插件使用的端口 http.port: 9200 # #设置节点间交互的tcp端口,默认是9300。 transport.tcp.port: 9300 #设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。 discovery.zen.ping.unicast.hosts: ["192.168.10.114:9300","192.168.10.115:9300","192.168.10.116:9300"] #如果要使用head,那么需要增加新的参数,使head插件可以访问es http.cors.enabled: true http.cors.allow-origin: "*" |
配置项要统一,要么加空格,要么都不加.
|
2.3 启动es
elasticsearch不能用root账户启动,添加 -d 参数在后台以守护进程模式运行。
cd elasticsearch-5.2.2/bin ./elasticsearch -d |
验证安装结果:curl 'http://localhost:9200/?pretty' 关闭es
ps –ef | grep elastic kill -9 xxxx |
|
3 安装Head
3.1 安装node.js
https://nodejs.org/en/download/ 下载下来的jar包是xz格式的,还需要安装xz.
然后解压nodejs的安装包:
xz -d node*.tar.xz tar -xvf node*.tar |
解压完node的安装文件后,需要配置下环境变量,编辑/etc/profile,添加
# set node environment export NODE_HOME=/home/hadoop/install_path/node-v6.10.1-linux-x64 export PATH=$PATH:$NODE_HOME/bin |
然后执行
测试一下node是否生效
|
3.2 安装grunt
NPM是随同NodeJS一起安装的包管理工具,可以从服务器下载别人编写的第三方程序,由于国内连接NPM服务器非常不稳定,这里使用淘宝的镜像地址,使用cnpm命令替代原本的npm
npm install -g cnpm --registry=https://registry.npm.taobao.org |
任意目录安装grunt
cnpm install -g grunt cnpm install -g grunt-cli |
测试安装
|
另一种直接安装的方式,可能会因为网络不稳定而失败 npm install -g grunt-cli
|
3.3npm 安装依赖
elasticsearch-head-master目录下,运行命令:
使用国内镜像:
npm install -g cnpm --registry=https://registry.npm.taobao.org |
|
3.4 修改head源码
修改配置 目录:head/Gruntfile.js
connect: { server: { options: { port: 9100, hostname: '*', base: '.', keepalive: true } } } 增加hostname属性,设置为* |
修改连接地址: 目录:head/_site/app.js
修改head的连接地址: this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200"; 把localhost修改成当前es的服务器地址,如: this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.10.116:9200"; |
|
3.5 启动Head
后台启动grunt server
访问192.168.10.116:9100 |
4 安装Kibana
Kibana是一个强大的es图形化组件,可以通过http的方式来查看es集群的状态,操作数据等. 当前es官方推荐的拓展组件为Kibana和X-Pack,而X-Pack需要依赖于Kibana.
选择任意一台节点安装kibana kibana的版本最好和es一致(5.2.2) 下载解压 https://www.elastic.co/downloads/kibana 解压到目标路径
tar -zxvf kibana-5.2.2-linux-x86_64.tar.gz -C /home/hadoop/install_path/ |
修改配置文件
$ vim config/kibana.yml server.port: 5601 server.host: "192.168.10.116" elasticsearch.url: "http://192.168.10.116:9200" |
暂时先不启动 |
5 安装X-Pack
在5.x版本中一些es插件(Shield, Watcher, Marvel, Graph, Reporting)都集成在x-pack组件中 在Es的根目录(每个节点),运行 bin/elasticsearch-plugin进行安装。
bin/elasticsearch-plugin install x-pack
|
在Kibana根目录运行 bin/kibana-plugin 进行安装。
bin/kibana-plugin install x-pack
|
|
5.2 启动Kibana
启动之前需要禁用X-Pack 插件 security
vim config/kibana.yml vim config/elasticsearch.yml 添加以下内容 xpack.security.enabled: false |
先启动es节点,再启动Kibana 前台启动
后台启动
nohup /home/hadoop/install_path/kibana-5.2.2-linux-x86_64/bin/kibana </dev/null &>/dev/null & |
Web界面访问:http:// 192.168.10.116:5601 此时需要输入用户名和密码登录,默认分别是 elastic 和 changeme |
6 JavaApi
Maven关联
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.2.2</version> </dependency> |
具体步骤
public static void main(String[] args) throws UnknownHostException { //设置集群名称,自动节点发现 Settings settings = Settings.builder().put("cluster.name", "bigData-cluster").put("client.transport.sniff", true).build(); //创建client TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.10.116"), 9300)); matchQuery m = new matchQuery(); m.findAll(client); } //查询方法,需要索引名称和类型 public void findAll(TransportClient client) throws UnknownHostException { SearchResponse rs = client.prepareSearch("q").setTypes("q1") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setExplain(true).execute().actionGet(); System.out.println(rs.getHits().getTotalHits()); // 遍历查询结果 for (SearchHit hit : rs.getHits().getHits()){ System.out.println(hit.getId()); } } |
|
7 问题
7.1以root身份运行
root@yxjay:/opt/elasticsearch-2.3.5/bin# ./elasticsearch Exception in thread "main" Java.lang.RuntimeException: don't run elasticsearch as root.. 解决方法1: 在执行elasticSearch时加上参数-Des.insecure.allow.root=true,完整命令如下
./elasticsearch -Des.insecure.allow.root=true |
解决办法2: 用vi打开elasicsearch执行文件,在变量ES_JAVA_OPTS使用前添加以下命令
ES_JAVA_OPTS="-Des.insecure.allow.root=true" |
这个方法的好处是以后不用添加参数就能以root身份执行. |
7.2 启动es报错
修改系统配置文件之后可能需要要断开用户SSH连接,然后重新登陆!否则会报以下错误.
|
7.3 Elastic License
如果遇到授权问题 去这里注册下载免费版 https://www.elastic.co/subscriptions 然后会给你邮箱发送一个链接,可以下载一个包含你名称的json文件. 安装方式 Elasticsearch 5.x -- https://www.elastic.co/guide/en/x-pack/current/installing-license.html
curl -XPUT -u elastic 'http://<host>:<port>/_xpack/license' -H "Content-Type: application/json" -d @license.json |
|