日志分析系统 [ 1 ] --- Elastic Stack 单节点部署,集群部署


小知识:
环境变量目录 /etc/profile
修改以后需要更新 source /etc/profile

Elastic Stack 单节点部署

框架结构
192.168.116.159 ela1
192.168.116.155 ela2
192.168.116.166 ela3

一、二进制压缩包方式下载和安装

不同的版本依赖不同版本的 Java
官方链接:https://www.elastic.co/cn/support/matrix#matrix_jvm

1.下载二进制压缩包

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.0-linux-x86_64.tar.gz

总结几点:

5.x 版本和更高的版本都可以使用 JDK 1.8
JDK 的版本不是越高越好,大部分都不支持 JDK 9 和 10
从6.5 版本开始支持 JDK11

2.解压安装

[root@ela1 ~]# ls
elasticsearch-7.10.0-linux-x86_64.tar.gz 
[root@ela1 ~]# tar -xf elasticsearch-7.10.0-linux-x86_64.tar.gz -C /usr/local/

目录结构介绍

目录 配置文件 描述
bin 脚本文件,包括启动 elasticsearch,安装插件。运行统计数据等
config elasticsearch.yml 集群配置文件,user,role based 相关配置
JDK Java 运行环境
data path.data 数据文件
lib Java 类库
logs path.log 日志文件
modules 包含所有 ES 模块
plugins 包含所有已安装插件

3.运行 修改节点权限

[root@ela1 ~]# useradd elastic
[root@ela1 ~]# chown -R elastic.elastic /usr/local/elasticsearch-7.10.0

4.切换用户并启动

[root@ela1 ~]# su - elastic
[elastic@ela1 root]$ /usr/local/elasticsearch-7.10.0/bin/elasticsearch
# 启动后会卡在那里不动,需要重新打开一个页面
# 或者后台运行 则不需要重新开页面
[elastic@ela1 root]$ /usr/local/elasticsearch-7.10.0/bin/elasticsearch -d

# or指定到文件放进程
[elastic@ela1 root]$ /usr/local/elasticsearch-7.10.0/bin/elasticsearch -d -p /tmp/elasticsearch.pid

注:如果误以 root 账户启动会生成文件 需要删掉才可继续执行

[root@ela1 ~]# ls -l /usr/local/elasticsearch-7.10.0/config/
总用量 40
-rw-rw---- 1 root root   199 12月 27 22:11 elasticsearch.keystore
[root@ela1 ~]# rm -rf /usr/local/elasticsearch-7.10.0/config/elasticearch.keystore

5.查看状态

[elastic@ela1 root]$ ss -ntal
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128       [::ffff:127.0.0.1]:9200                            [::]:*                  
LISTEN      0      128       [::ffff:127.0.0.1]:9300                            [::]:*                  
LISTEN      0      128                 [::]:22                              [::]:* 
[elastic@ela1 root]$ curl http://localhost:9200/
{
    
    
  "name" : "Ela",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "o8iFoxkGRhWR7kyUIcpZxw",
  "version" : {
    
    
    "number" : "7.10.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "51e9d6f22758d0374a0f3f5c6e8f3a7997850f96",
    "build_date" : "2020-11-09T21:30:33.964949Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

到目前为止,已经成功了运行了一个节点的 Elasticsearch。

注意: 这里只是启动了一个节点,Elasticsearch 本身是支持集群的,集群至少三个节点。后面章节如何启动一个集群。

默认端口号是 :
9200 用于外部访问的监听端口,比如查看集群状态,向其传输数据,查询数据等
9300 用户集群中节点之间的互相通信,比如主节点的选举,集群节点信息的通告等。

二、集群部署

1.条件:

集群最少 3 个节点, 集群的每个节点都需要使用非 root 用户启动。
如果root启动会报错

2.节点上设置系统内核参数

设置内存映射

[root@ela1 ~]# sysctl -w vm.max_map_count=262144 > /etc/sysctl.conf
[root@ela1 ~]# sysctl -p
vm.max_map_count = 262144

还需要设置关于这个进程可以打开的文件描述符数量

[root@ela1 ~]# tail /etc/security/limits.conf
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

3.设置集群参数

编译配置文件 /usr/local/elasticsearch-7.10.0/config/elasticsearch.yml

# ela1 设置

cluster.name: elk
node.name: ela1
node.data: true
network.host: 0.0.0.0
http.port: 9200

discovery.seed_hosts:

   - 192.168.116.159
   - 192.168.116.155
   - 192.168.116.166
     cluster.initial_master_nodes: ["ela1", "ela2", "ela3"]
# ela2 设置

cluster.name: elk
node.name: ela2
node.data: true
network.host: 0.0.0.0
http.port: 9200

discovery.seed_hosts:

   - 192.168.116.159
   - 192.168.116.155
   - 192.168.116.166
     cluster.initial_master_nodes: ["ela1", "ela2", "ela3"]
# ela3设置

cluster.name: elk
node.name: ela3
node.data: true
network.host: 0.0.0.0
http.port: 9200

discovery.seed_hosts:

   - 192.168.116.159
   - 192.168.116.155
   - 192.168.116.166
     cluster.initial_master_nodes: ["ela1", "ela2", "ela3"]

discovery.seed_hosts: 的参数可以换成ela1 2 3 或者 ip 后面加 端口9300(因为默认为9300)
cluster.initial_master_nodes 的参数 都可以换成ip

参数说明

cluster.name 集群名称,各节点配成相同的集群名称。
node.name 节点名称,各节点配置不同。
node.data 指示节点是否为数据节点。数据节点包含并管理索引的一部分。
network.host 绑定节点IP。
http.port 监听端口。
path.data 数据存储目录。
path.logs 日志存储目录。
discovery.seed_hosts 指定集群成员,一般主动发现他们
cluster.initial_master_nodes 指定有资格成为 master 的节点
http.cors.enabled 用于允许head插件访问ES。
http.cors.allow-origin 允许的源地址。

注意:
当您为提供自定义设置时 network.host,Elasticsearch会假设您正在从开发模式过渡到生产模式,并将许多系统启动检查从警告升级到异常。
cluster.initial_master_nodes 中的节点名称需要和 node.name 的名称一致。

4 启动集群

在每个节点上启动 elasticsearch 进程

切换到普通用户 elastic

su - elastic

执行如下命令

cd /usr/local/elasticsearch-7.10.0

./bin/elasticsearch -d -p /tmp/elasticsearch.pid

-d 后台运行
-p 指定一个文件,用于存放进程的 pid

5 日志

[elastic@ela1 ~]$ cat /usr/local/elasticsearch-7.10.0/logs/elk.log 
[2020-12-28T02:35:50,116][INFO ][o.e.n.Node               ] [ela1] version[7.10.0], pid[10362], build[default/tar/51e9d6f22758d0374a0f3f5c6e8f3a7997850f96/2020-11-09T21:30:33.964949Z], OS[Linux/3.10.0-1127.19.1.el7.x86_64/amd64], JVM[AdoptOpenJDK/OpenJDK 64-Bit Server VM/15.0.1/15.0.1+9]
[2020-12-28T02:35:50,121][INFO ][o.e.n.Node               ] [ela1] JVM home [/usr/local/elasticsearch-7.10.0/jdk], using bundled JDK [true]

6 查看集群健康状态

[elastic@ela1 ~]$ curl -X GET "localhost:9200/_cat/health?v"
epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1609141145 07:39:05  elk     green           1         1      0   0    0    0        0             0                  -                100.0%

7 查看集群节点信息

[elastic@ela1 ~]$ curl -X GET "localhost:9200/_cat/nodes?v"
ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role  master name
192.168.116.155           15          99   6    0.44    0.33     0.46 cdhilmrstw *      ela2
192.168.116.166           13          96   8    0.67    0.89     0.85 cdhilmrstw -      ela3
192.168.116.159           18          97  10    0.25    0.19     0.27 cdhilmrstw -      ela1

8 出现的问题及解决办法

1.如果本机有多个ip地址 需要指定监听ip地址 不能写0.0.0.0 ,否则会出错
或者只留一个ip地址 就没错了

2.报错503 没有发现master,无法返回信息
查看自己 没有发现错误

[root@prod ~]# curl  http://localhost:9200/
{
    
    
  "name" : "prod",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "nYkwCL9YQEinaJk2dstK0w",
  "version" : {
    
    
    "number" : "7.10.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "51e9d6f22758d0374a0f3f5c6e8f3a7997850f96",
    "build_date" : "2020-11-09T21:30:33.964949Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

但是

[root@prod ~]#http://localhost:9200/_cat/nodes?pretty

{
    
       "error" : {
    
         "root_cause" : [       {
    
             "type" : "master_not_discovered_exception",         "reason" : null       }     ],     "type" : "master_not_discovered_exception",     "reason" : null   },   "status" : 503 }

解决

#删除默认配置文件之前的配置信息

rm -rf  /var/log/elasticsearch/*
rm -rf  /var/lib/elasticsearch/* #尤其是这个下面的nodes文件

3.集群问题 查看节点信息只能看到自己 (自己是自己的master)

# 找到进程
[elastic@ela1 ~]$  cd /usr/local/elasticsearch-7.10.0/
[elastic@ela1 elasticsearch-7.10.0]$ jdk/bin/jps
8244 Jps
7526 Elasticsearch

# 杀死进程
[elastic@ela1 elasticsearch-7.10.0]$ kill -9 7526

# 删除零时文件 
[elastic@ela1 elasticsearch-7.10.0]$ rm -rf /tmp/elasticsearch-*

# 删除数据目录中的所有文件 生产环境不要随便删除
[elastic@ela1 elasticsearch-7.10.0]$ rm -rf data/*

# 删除 keystore 文件
[elastic@ela1 elasticsearch-7.10.0]$ rm -rf config/elasticsearch.keystore

# 重新启动进程
[elastic@ela1 elasticsearch-7.10.0]$ ./bin/elasticsearch -d -p /tmp/elk.pid

9 关闭 Elasticsearch 进程

pkill -F /tmp/elasticsearch.pid

10 重要的集群参数

10.1 设置堆内存大小

Elasticsearch将通过在 jvm.options中指定Xms(最小堆大小 )和 Xmx(最大堆大小)的大小来设置 整个堆 。这两个设置必须彼此相等。

这些设置的值取决于服务器上可用的RAM数量:
设置Xmx和Xms 的大小应该不超过你物理内存的50%。
而且最大不能超过32G

[root@ela1 ~]# vim /usr/local/elasticsearch-7.10.0/config/jvm.options
-Xms1g
-Xmx1g

10.2 设置JVM堆转储路径

默认情况下,Elasticsearch将JVM配置为将内存不足异常上的堆转储到默认数据目录。
在RPM和 Debian软件包中,数据目录为 /var/lib/elasticsearch
使用二进制文件部署时,该data目录位于Elasticsearch安装目录的根目录下。
如果该路径不适合于接收堆转储,应该通过在 jvm.options 中设置条目 XX:HeapDumpPath=... 来改变此值。

[root@ela1 ~]# vim /usr/local/elasticsearch-7.10.0/config/jvm.options
-XX:HeapDumpPath=data

10.3设置 GC日志记录

默认情况下,Elasticsearch启用垃圾收集(GC)日志。
它们在 jvm.options中配置,并输出到和 Elasticsearch 日志相同的默认目录下。默认配置每64 MB轮换一次日志,最多可消耗2 GB磁盘空间。

# JDK 9+ GC logging
[root@ela1 ~]# vim /usr/local/elasticsearch-7.10.0/config/jvm.options
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m

修改默认值
这里的示例是关闭默认的日志配置参数
并将日志输出到 /opt/my-app/gc.log

这里通过创建$ES_HOME/config/jvm.options.d/gc.options 子配置文件的方式设置新的 gc 日志输出参数

# 关闭所有以前的日志配置
-Xlog:disable

# Default settings from JEP 158, but with `utctime` instead of `uptime` to match the next line
-Xlog:all=warning:stderr:utctime,level,tags

# Enable GC logging to a custom location with a variety of options
-Xlog:gc*,gc+age=trace,safepoint:file=/opt/my-app/gc.log:utctime,pid,tags:filecount=32,filesize=64m

10.4 设置临时目录

默认情况下,Elasticsearch使用启动脚本在系统临时目录下立即创建的私有临时目录。这个零时目录通常在系统的 /tmp 目录下。

在某些Linux发行版中,如果最近未访问过 /tmp 下的文件和目录,则系统实用程序将从中清除文件和目录,此行为可能会导致在运行Elasticsearch时删除私有临时目录。如果 Elasticsearch 随后使用了该目录,则删除私有临时目录会导致问题。

如果您使用 .deb.rpm 软件包安装的Elasticsearch并运行在 systemd 下,则定期清理将排除Elasticsearch使用的私有临时目录。

如果使用 .tar.gz 包部署的集群,那应该考虑给每个集群中的节点创建专用的临时目录,该目录应设置权限,以便只有运行Elasticsearch的用户(此文中是:elastic)才能访问它。
然后,在启动Elasticsearch之前,将环境变量 $ES_TMPDIR 设置为该目录。

以下步骤在每个节点上执行

1 切换为 root 用户

su - root

2 创建目录并改变属主和属组

mkdir /opt/ela

chown elastic:elastic  /opt/ela/

3 切换为普通用户 elastic, 并声明环境变量

su - stastic

4 声明环境变量

export ES_TMPDIR=/opt/ela

5 重新启动 Elasticsearch

./bin/elasticsearch -d -p /opt/ela/elasticsearch.pid

猜你喜欢

转载自blog.csdn.net/Houaki/article/details/111875783
今日推荐