您的位置:首页 > 其它

Elasticsearch 7.5.1群集部署

2020-05-08 07:48 357 查看

注:本博文只用于实现简单群集配置,更深入的资料可以参考官方文档
Elasticsearch官方文档
kibana官方文档

一、准备工作

环境如下

系统 IP 服务
Centos7.3 192.168.171.131 ES1、logstash、ES-Head、logstash、kibana
Centos7.3 192.168.171.134 ES2
Centos7.3 192.168.171.135 ES3

1、配置域名解析

[root@node1 ~]# cat > /etc/hosts << EOF
> 192.168.171.131  node1
> 192.168.171.134  node2
> 192.168.171.135  node3
> EOF
[root@node1 ~]# scp /etc/hosts root@192.168.171.134:/etc/hosts
[root@node1 ~]# scp /etc/hosts root@192.168.171.135:/etc/hosts

2、配置java环境
注:以下操作需要在所有节点上进行配置

JDK官方下载,由于登录才可以下载,所以这里没有使用wget

#卸载自带的java环境
[root@node1 ~]# rpm -qa | grep jdk
copy-jdk-configs-1.2-1.el7.noarch
java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64
java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
[root@node1 ~]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
[root@node1 ~]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
#配置jdk环境
[root@node1 ~]# tar zxf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@node1 ~]# vim /etc/profile
..........
export  JAVA_HOME=/usr/local/jdk1.8.0_211
export  JRE_HOME=/usr/local/jdk1.8.0_211/jre
export  CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export  PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
#刷新配置并查看配置是否正确
[root@node1 ~]# . /etc/profile
[root@node1 ~]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

二、配置ELK群集

1、下载并安装es的rpm安装包
注:下载安装的操作需要再所有节点上执行
可在es中文社区下载

[root@node1 ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-x86_64.rpm
[root@node1 ~]# rpm -ivh elasticsearch-7.5.1-x86_64.rpm
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable elasticsearch.service

2、优化es(可跳过)
1)修改其默认使用内存大小

#查看elasticsearch的配置文件目录
[root@node1 elasticsearch]# pwd
/etc/elasticsearch
[root@node1 elasticsearch]# ls
elasticsearch.keystore  jvm.options        role_mapping.yml  users
elasticsearch.yml       log4j2.properties  roles.yml         users_roles
#elasticsearch默认内存使用为1G,可以更改如下配置,修改其默认使用内存
[root@node1 elasticsearch]# cat jvm.options
-Xms1g
-Xmx1g

注:生产环境中建议将Xms和Xmx两个值设置为一致,一般设置为物理内存的一半,但最高最好不要超过30G
2)修改其打开文件数的大小
如果服务器文件数上线和线程上线较低,就会产生如下异常:

1. max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]每个进程最大同时打开文件数太小

2. max number of threads [3818] for user [es] is too low, increase to at least [4096]最大线程个数太低

可以进行以下修改,以便修改可打开文件数的大小

[root@node1 elasticsearch]# vim /etc/security/limits.conf
*               soft    nofile          65536
*               hard    nofile          65536
*               soft    nproc           4096
*               hard    nproc           4096

#注:修改上述配置后,需要退出当前用户再重新登录才可生效
#重新登录后,可以使用以下命令查看是否生效
#查看最大线程个数
[root@node1 elasticsearch]# ulimit -Hu
4096
[root@node1 elasticsearch]# ulimit -Su
4096
#查看每个进程最大同时打开文件数
[root@node1 elasticsearch]# ulimit -Sn
65536
[root@node1 elasticsearch]# ulimit -Hn
65536

3、配置es集群
1)node1配置如下

[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: my-es       #群集名称
node.name: node1        #es节点名称
bootstrap.memory_lock: false      #启动时不锁定内存
network.host: 0.0.0.0           #监听地址
http.port: 9200                 #监听端口
discovery.seed_hosts: ["node1", "node2", "node3"]    #这里指定参与集群的主机节点
cluster.initial_master_nodes: ["node1", "node2", "node3"]     #同上
#在配置文件末尾添加以下内容,后面es-head连接es群集时需要
http.cors.enabled: true                #添加该行,开启跨域访问支持
http.cors.allow-origin: "*"            #添加该行,跨域访问允许的域名地址
#启动服务,并将修改好的配置文件发送到其他节点
[root@node1 ~]# systemctl start elasticsearch
[root@node1 ~]# scp /etc/elasticsearch/elasticsearch.yml root@192.168.171.134:/etc/elasticsearch/elasticsearch.yml
[root@node1 ~]# scp /etc/elasticsearch/elasticsearch.yml root@192.168.171.135:/etc/elasticsearch/elasticsearch.yml

2)配置其他节点

[root@node2 ~]# sed -i 's#node.name: node1#node.name: node2#g' /etc/elasticsearch/elasticsearch.yml
[root@node2 ~]# systemctl start elasticsearch
#node03节点配置如下
[root@node3 ~]# sed -i 's#node.name: node1#node.name: node3#g' /etc/elasticsearch/elasticsearch.yml
[root@node3 ~]# systemctl start elasticsearch

3)查看群集是否配置成功
访问各个节点的9200端口,即可看到如下页面:
三个节点的UUID为一样的!!!
三个节点的UUID为一样的!!!
三个节点的UUID为一样的!!!


4、配置elasticsearch Head
可以使用谷歌自带的elasticsearch head插件,在应用商店搜索如下即可安装
也可以在linux服务器安装ElasticSearch Head应用
这里我们选择在linux服务器上安装
该插件可在github上找到
注:以下配置在其中一个节点进行即可
[root@node1 ~]# git clone git://github.com/mobz/elasticsearch-head.git
[root@node1 ~]# cd elasticsearch-head/
[root@node1 elasticsearch-head]# yum -y install epel-release
[root@node1 elasticsearch-head]# yum -y update openssl
#安装npm(注:必须安装epel源才可安装npm)
[root@node1 elasticsearch-head]# yum -y install npm
[root@node1 elasticsearch-head]# npm install
注:如果在执行npm install命令时,长时间停留在安装界面,或者报错,只需Ctrl+c终止后重新运行该命令即可
后台启动elasticsearch head,否则会一直占用当前终端:

[root@node1 elasticsearch-head]# npm run start &
[root@node1 elasticsearch-head]# netstat -anput | grep 9100
tcp        0      0 0.0.0.0:9100            0.0.0.0:*               LISTEN      41915/grunt

浏览器访问elasticsearch head所在主机的9100端口,并连接到es群集的9200端口,即可在浏览器查看群集状态,如下:
注:Elasticsearch默认不允许第三方接入,可以修改Elasticsearch的配置文件elasticsearch.yml,添加如下所示配置(我在第一次修改配置文件时,已经增加了如下配置):

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


5、安装kibana
1)下载并安装kibana

[root@node1 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.5.1-x86_64.rpm
[root@node1 ~]# rpm -ivh kibana-7.5.1-x86_64.rpm

2)配置kibana

[root@node1 ~]# vim /etc/kibana/kibana.yml
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://192.168.171.131:9200","http://192.168.171.134:9200","http://192.168.171.135:9200"]
kibana.index: ".kibana"
[root@node1 ~]# systemctl enable kibana.service
[root@node1 ~]# systemctl start kibana
[root@node1 ~]# ss -anput | grep 5601
tcp    LISTEN     0      128       *:5601                  *:*                   users:(("node",pid=42355,fd=18))

2)汉化kibana(可选)
由于kibana 7之前的版本,官方并没有支持中文,需要另外下载补丁包
kibana 7的版本,官方加入了中文的选项,只需要修改kibana的配置文件即可,如下:

[root@node1 ~]# sed -i 's/#i18n.locale: "en"/i18n.locale: "zh-CN"/g' /etc/kibana/kibana.yml
[root@node1 ~]# systemctl restart kibana

注:修改配置文件后,记得重启生效
注:修改配置文件后,记得重启生效
注:修改配置文件后,记得重启生效
启动完成后,访问主机的5601端口,即可看到如下界面:

6、安装logstash收集日志
在实际生产环境中,大多中小架构可能是这样的:filebeat>>kafka>>logstash>>elasticsearch>>kibana,我这里只是想将新版的某些组件安装配置一下,所以省略了前两个,直接用logstash来收集主机日志了。
注:下面收集的有nginx的日志,请自行配置nginx。

#下载logstash并安装
[root@node1 ~]# wget https://artifacts.elastic.co/downloads/logstash/logstash-7.5.1.rpm
[root@node1 ~]# rpm -ivh  logstash-7.5.1.rpm
#设置开机自启
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable logstash.service
#编辑配置文件,收集日志
[root@node1 ~]# cd /etc/logstash/conf.d/
[root@node1 conf.d]# vim nginx_log.conf
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file{
path => "/var/log/nginx/access.log"
type => "access"
start_position => "beginning"
}
file{
path => "/var/log/nginx/error.log"
type => "error"
start_position => "beginning"
}
}

output {
if [type] == "system" {
elasticsearch {
hosts => ["192.168.171.131:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "access" {
elasticsearch {
hosts => ["192.168.171.131:9200"]
index => "access-%{+YYYY.MM.dd}"
}
}

if [type] == "error" {
elasticsearch {
hosts => ["192.168.171.131:9200"]
index => "error-%{+YYYY.MM.dd}"
}
}
}
[root@node1 conf.d]# chmod +r /var/log/messages             #赋予该目录其他人的读权限
[root@node1 conf.d]# ln -sf /usr/share/logstash/bin/logstash  /usr/local/bin/                  #对命令做软连接
[root@node1 conf.d]# systemctl start logstash
[root@node1 conf.d]# logstash -f nginx_log.conf &             #放入后台运行

7、登录es查看是否创建索引
显示如下,表示正常:

8、进入kibana添加索引





接下来再添加一个error的索引,我比较懒,就不添加了哈

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: