安装配置elasticsearch 7.5.1群集

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

一、安装前准备

环境如下:

系统版本 IP 角色
centos 7.5 192.168.20.2 ES 01、logstash、ES-Head、logstash、kibana
centos 7.5 192.168.20.7 ES 02
centos 7.5 192.168.20.8 ES 03

注:文中用到的所有软件包,均可在我的网盘链接中下载,或者去es中文社区下载es相关组件。

1、配置域名解析

[root@node01 src]# cat > /etc/hosts << EOF
> 192.168.20.2    node01
> 192.168.20.7    node02
> 192.168.20.8    node03
> EOF
> #将hosts文件发送到其他节点
[root@node01 src]# scp /etc/hosts root@node02:/etc/hosts
[root@node01 src]# scp /etc/hosts root@node03:/etc/hosts

2、配置java环境

注:以下操作需要在所有节点上进行配置。

JDK官方下载地址

#卸载自带的java环境
[root@node01 src]# rpm -qa | grep jdk
java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64
copy-jdk-configs-3.3-2.el7.noarch
java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64
java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64
[root@node01 src]# rpm -e java-1.8.0-openjdk-headless --nodeps
[root@node01 src]# rpm -e java-1.7.0-openjdk-headless --nodeps
#配置jdk环境
[root@node01 src]# tail -4 /etc/profile   #追加内容如下
export JAVA_HOME=/usr/local/jdk1.8.0_241
export JRE_HOME=/usr/local/jdk1.8.0_241/jre
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
#刷新配置并查看配置是否正确
[root@node01 src]# . /etc/profile
[root@node01 src]# java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

二、配置ELK群集

1、下载es的rpm安装包

注:下载安装的操作需要再所有节点上执行

#下载并安装elasticsearch
[root@node01 src]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-x86_64.rpm
[root@node01 src]# rpm -ivh elasticsearch-7.5.1-x86_64.rpm
#设置开机自启动
[root@node01 src]# systemctl daemon-reload
[root@node01 src]# systemctl enable elasticsearch.service

2、优化es(可跳过)

1)修改其默认使用内存大小

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

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@node01 elasticsearch]# vim /etc/security/limits.conf
*               soft    nofile          65536
*               hard    nofile          65536
*               soft    nproc           4096
*               hard    nproc           4096
#注:修改上述配置后,需要退出当前用户再重新登录才可生效
#重新登录后,可以使用以下命令查看是否生效
#查看最大线程个数
[root@node01 elasticsearch]# ulimit -Hu
4096
[root@node01 elasticsearch]# ulimit -Su
4096
#查看每个进程最大同时打开文件数
[root@node01 elasticsearch]# ulimit -Sn
65536
[root@node01 elasticsearch]# ulimit -Hn
65536

3、配置es集群

1)node01配置如下

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

2)配置其他节点

#node02节点配置如下(修改节点名称即可)
[root@node02 ~]# sed -i 's#node.name: node01#node.name: node02#g' /etc/elasticsearch/elasticsearch.yml
[root@node02 ~]# systemctl start elasticsearch
#node03节点配置如下
[root@node03 ~]# sed -i 's#node.name: node01#node.name: node03#g' /etc/elasticsearch/elasticsearch.yml
[root@node03 ~]# systemctl start elasticsearch

3)查看群集是否配置成功

访问各个节点的9200端口,即可看到如下页面:

安装配置elasticsearch 7.5.1群集

4、配置elasticsearch Head

谷歌提供的有elasticsearch head插件,在应用商店搜索如下即可安装:

安装配置elasticsearch 7.5.1群集

虽然可以使用chrome的插件,但是每个使用者都需要安装chrome,并安装ElasticSearch head插件。由于目前google被国内限制的缘故,安装chrome比较麻烦。作为替代方案,我们可以在linux服务器安装ElasticSearch Head应用。

该插件在github存在,可以参考安装。

注:以下配置在其中一个节点进行即可

[root@node01 ~]# git clone git://github.com/mobz/elasticsearch-head.git
[root@node01 ~]# cd elasticsearch-head/
#安装epel源及更新openssl
[root@node01 elasticsearch-head]# yum -y install epel-release 
[root@node01 elasticsearch-head]# yum -y update openssl
#安装npm(注:必须安装epel源才可安装npm)
[root@node01 elasticsearch-head]# yum -y install npm
[root@node01 elasticsearch-head]# npm install

注:如果在执行npm install命令时,长时间停留在如下界面,或者报错,只需Ctrl+c终止后重新运行该命令即可。

安装配置elasticsearch 7.5.1群集

后台启动elasticsearch head,否则会一直占用当前终端:

[root@node01 elasticsearch-head]# npm run start &   #后台启动
[root@node01 ~]# ss -lnp | grep 9100          #确定9100端口在监听

浏览器访问elasticsearch head所在主机的9100端口,并连接到es群集的9200端口,即可在浏览器查看群集状态,如下:

安装配置elasticsearch 7.5.1群集

注:Elasticsearch默认不允许第三方接入,可以修改Elasticsearch的配置文件elasticsearch.yml,添加如下所示配置(我在第一次修改配置文件时,已经增加了如下配置):

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

5、安装kibana

1)下载并安装kibana

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

2)配置kibana

[root@node01 ~]# vim /etc/kibana/kibana.yml       #编辑主配置文件
server.host: "0.0.0.0"         #指定kibana监听地址
elasticsearch.hosts: ["http://192.168.20.2:9200","http://192.168.20.7:9200","http://192.168.20.8:9200"]
#指定es监听地址,可以写群集中的某一个节点监听地址
kibana.index: ".kibana"             #在elasticsearch中添加.kibana索引
#设置开机自启并启动kibana
[root@node01 ~]# systemctl enable kibana
[root@node01 ~]# systemctl start kibana
[root@node01 ~]# ss -lnp | grep 5601          #确定kibana端口在监听
tcp    LISTEN     0      128       *:5601                  *:*                   users:(("node",pid=2593,fd=28))

2)汉化kibana(可选)

由于kibana 7之前的版本,官方并没有支持中文,需要另外下载补丁包。
地址:https://github.com/anbai-inc/Kibana_Hanization

kibana 7的版本,官方加入了中文的选项,只需要修改kibana的配置文件即可,如下:

[root@node01 bin]# sed -i 's/#i18n.locale: "en"/i18n.locale: "zh-CN"/g' /etc/kibana/kibana.yml

注:修改配置文件后,记得重启生效哦!

启动完成后,访问主机的5601端口,即可看到如下界面:

安装配置elasticsearch 7.5.1群集

6、安装logstash收集日志

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

注:下面收集的有nginx的日志,请自行配置nginx。


#下载logstash并安装
[root@node01 src]# wget https://artifacts.elastic.co/downloads/logstash/logstash-7.5.1.rpm
[root@node01 src]# rpm -ivh logstash-7.5.1.rpm
#设置开机自启
[root@node01 src]# systemctl daemon-reload
[root@node01 src]# systemctl enable logstash.service
#编辑配置文件,收集日志
[root@node01 src]# cd /etc/logstash/conf.d/
[root@node01 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.20.2:9200"]
            index => "system-%{+YYYY.MM.dd}"
            }
            }
    if [type] == "access" {
        elasticsearch {
            hosts => ["192.168.20.2:9200"]
            index => "access-%{+YYYY.MM.dd}"
            }
            }

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

7、登录es查看是否创建索引

显示如下,表示正常:

安装配置elasticsearch 7.5.1群集

好了,至于kibana如何添加索引,自行研究吧,没有耐心写了,可以参考我之前的博文来进行添加kibana上的索引。

猜你喜欢

转载自blog.51cto.com/14154700/2468544