ElasticSearch入门我爱编程elasticsearch

Elasticsearch 6.2搜索集群环境搭建(二)

2018-05-25  本文已影响75人  wangfs

环境介绍:

1.png

搭建过程

前提是安装java环境,ELK6.2版本需要jdk为1.8,官方推荐安装OracleJDK 最好不要安装OpenJDK。oracle官网下载jdk-8u171-linux-x64.tar.gz安装即可。

Elasticsearch安装
  1. 解压并放置指定目录
[root@elastic-redis-01 softwares]# ls -lh
total 210M
-rw-r--r-- 1 root root  28M May 25 15:35 elasticsearch-6.2.1.tar.gz
-rw-r--r-- 1 root root 183M Apr  4 02:05 jdk-8u171-linux-x64.tar.gz
[root@elastic-redis-01 softwares]# tar -xf elasticsearch-6.2.1.tar.gz
[root@elastic-redis-01 softwares]# mv elasticsearch-6.2.1 /usr/local/elasticsearch
  1. 创建数据存放路径(应将设置配置为在Elasticsearch主目录之外定位数据目录,以便在不删除数据的情况下删除主目录!)
[root@elastic-redis-01 softwares]# mkdir /usr/local/elasticsearch/data
  1. 建立用户并授权(es不能用root运行)
[root@elastic-redis-01 softwares]# groupadd elastic && useradd elastic -g elastic -s /bin/bash
[root@elastic-redis-01 softwares]# chown -R elastic:elastic /usr/local/elasticsearch
  1. 修改elasticsearch的配置文件
    vim /usr/local/elasticsearch/config/elasticsearch.yml 将配置文件以下内容进行修改
    #三台服务器此配置文件除了node.name和network.host不一样外其他无需再改
[root@elastic-redis-01 softwares]# cat /usr/local/elasticsearch/config/elasticsearch.yml 
#集群的名称
cluster.name: flow-es6.2
#节点名称,其余两个节点分别为node-2和node-3
node.name: node-1
#指定该节点是否有资格被选举为master节点,默认是true,es是默认集群中的第一台为master,如果这台机器挂了就会重新选举master
node.master: true
#允许该节点存储数据(默认开启)
node.data: true
#索引数据的存储路径
path.data: /usr/local/elasticsearch/data
#日志文件的存储路径
path.logs: /usr/local/elasticsearch/logs
#设置为true来锁住内存。因为内存交换到磁盘对服务器性能影响较大,当jvm开始swapping时es的效率会降低,所以要保证它不swap
bootstrap.memory_lock: true
#绑定IP地址
network.host: 0.0.0.0
#设置对外服务的http端口,默认是9200
http.port: 9200
#设置节点间交互的tcp端口,默认是9300
transport.tcp.port: 9300
#Elasticsearch将绑定到可用的环回地址,并将扫描端口9300~9305以尝试连接到运行在同一台服务器上的其他节点。
#这提供了自动集群体验,而无需进行任何配置。数组设置或逗号分隔的设置,每个值的形式应该是host:port或host
#(如果没有设置,port默认设置会transport.profiles.default.port会落到transport.tcp.port)
#请注意,IPv6主机必须放在括号内。默认为127.0.0.1,[::1]
discovery.zen.ping.unicast.hosts: ["172.31.15.172:9300","172.31.15.173:9300","172.31.15.174:9300"]
#如果没有这种设置,遭受网络故障的集群就有可能被分成两个独立的集群-脑裂-这将导致数据丢失。
discovery.zen.minimum_master_nodes: 2
#探查的超时时间,默认是3秒,提高一点以应对网络状况不好的时候,防止脑裂
discovery.zen.ping_timeout: 8s
#开启跨域访问支持
http.cors.enabled: true  
http.cors.allow-origin: "*"  
http.cors.allow-credentials: true 

系统配置需要修改的地方

我选择锁住swapping因此需要在这个配置文件下再增加两行代码

elastic soft memlock unlimited
elastic hard memlock unlimited

注意:之后需要执行一句命令sysctl -p使系统配置生效(使用root用户)。

  1. 调整jvm内存(服务器内存大小为16GB)
    vim /usr/local/elasticsearch/config/jvm.options
    此处我调整为如下所示:
-Xms5g
-Xmx5g
  1. 使用普通用户elastic来启动三台es服务器(三台服务器分别执行)
[root@elastic-redis-01 ~]# su - elastic
Last login: Fri May 25 18:35:28 CST 2018 on pts/0
[elastic@elastic-redis-01 ~]$ /usr/bin/nohup /usr/local/elasticsearch/bin/elasticsearch &
[1] 22994

应该先在前台启动,观察是否有报错。确认无误后再放入后台执行。

head插件安装

在使用Elasticsearch的过程中,必不可少需要通过一些工具查看es的运行状态以及数据。如果都是通过rest请求,未免太过麻烦,而且也不够人性化。
此时,head可以完美的帮助你快速学习和使用es。

此处将把head插件作为独立的webapp运行
先安装必要插件,否则报-bash: npm: 未找到命令

  1. 安装node.js
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash  
source ~/.nvm/nvm.sh
nvm install node 
nvm use node
  1. 安装npm
npm install -g cnpm --registry=https://registry.npm.taobao.org
  1. 使用npm安装grunt
    grunt是基于Node.js的项目构建工具,可以进行打包压缩、测试、执行等等的工作,head插件就是通过grunt启动
[root@elastic-redis-01 ~]# npm install -g grunt
[root@elastic-redis-01 ~]# npm install -g grunt-cli  --registry=https://registry.npm.taobao.org 
[root@elastic-redis-01 ~]# grunt -version
grunt-cli v1.2.0

grunt -version检查是否安装成功。

4.下载head插件源码

cd /usr/local/elk/elasticsearch/  
git clone git://github.com/mobz/elasticsearch-head.git  
cd elasticsearch-head/  
npm install  #这一步可能会执行不成功,可以尝试国内镜像安装
#npm install -g cnpm --registry=https://registry.npm.taobao.org
[root@elastic-redis-01 ~]# cnpm install
npminstall WARN package.json not exists: /root/package.json
✔ Installed 0 packages
✔ Linked 0 latest versions
✔ Run 0 scripts
✔ All packages installed (used 13ms, speed 0B/s, json 0(0B), tarball 0B)
             connect: {
                        server: {
                                options: {
                                        port: 9100,
                                        hostname: '*',
                                        base: '.',
                                        keepalive: true
                                }
                        }
                }
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://47.105.xxx.xxx:9200";
[root@elastic-redis-01 elasticsearch-head]# pwd
/usr/local/elasticsearch/elasticsearch-head
[root@elastic-redis-01 elasticsearch-head]# /usr/bin/nohup grunt server &

重启es,然后访问如下地址:


2.png

异常:点击 连接 按钮连接集群,发现无论如何点击都没有反应,还需要在es上进行以下设置,开启跨域访问支持
vim /usr/local/elasticsearch/config/elasticsearch.yml 在最后添加以下三条属性:

http.cors.enabled: true  
http.cors.allow-origin: "*"  
http.cors.allow-credentials: true 

备注:
elasticsearch安装x-pack插件之后,head插件就无法使用了,因为x-pack中加入了安全模块(security机制),这个时候需要在elasticseach.yml中再增加下面一行配置即可解决。

http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

然后在每次使用head插件的时候,按照如下的格式输入,其中auth_user是es的用户名,auth_password是es的密码:
http://172.20.1.187:9100/?auth_user=elastic&auth_password=123456

参考地址:
https://blog.csdn.net/qq_34021712/article/details/79329919
https://blog.csdn.net/fgf00/article/details/79571940

上一篇下一篇

猜你喜欢

热点阅读