ELK log analysis platform (1)-elasticsearch actual combat

1 Introduction

- Elasticsearch 是一个开源的分布式搜索分析引擎,建立在一个全文搜索引擎库 Apache Lucene基础之上。
  Elasticsearch 不仅仅是 Lucene,并且也不仅仅只是一个全文搜索引擎:
		一个分布式的实时文档存储,每个字段 可以被索引与搜索
		一个分布式实时分析搜索引擎
		能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据 

- 基础模块
	cluster:管理集群状态,维护集群层面的配置信息。
	alloction:封装了分片分配相关的功能和策略。
	discovery:发现集群中的节点,以及选举主节点。
	gateway:对收到master广播下来的集群状态数据的持久化存储。
	indices:管理全局级的索引设置。
	http:允许通过JSON over HTTP的方式访问ES的API。
	transport:用于集群内节点之间的内部通信。
	engine:封装了对Lucene的操作及translog的调用。


- elasticsearch应用场景:
	信息检索	
	日志分析
	业务数据分析	
	数据库加速	
	运维指标监控

Official website

2. ES distributed installation and deployment (using three virtual machines, all with the same configuration, just change the host name)

- 软件下载:
	https://elasticsearch.cn/download/
  安装软件
	# rpm -ivh jdk-8u171-linux-x64.rpm
	# rpm -ivh elasticsearch-7.6.1.rpm	//7.6版本自带jdk
  设置服务自启:
	# systemctl daemon-reload
	# systemctl enable elasticsearch

- 修改配置文件:
	# vim /etc/elasticsearch/elasticsearch.yml
		cluster.name: my-es			#集群名称
		node.name: server7			#主机名需要解析
		path.data: /var/lib/elasticsearch	#数据目录
		path.logs: /var/log/elasticsearch	#日志目录
		bootstrap.memory_lock: true	#锁定内存分配
		network.host: 172.25.0.7		#主机ip(0.0.0.0也可以)
		http.port: 9200			#http服务端口
		cluster.initial_master_nodes: ["server13"]
		discovery.seed_hosts: ["server12", "server13","server14"]    ##节点 

- 修改系统限制
	# vim /etc/security/limits.conf
		elasticsearch soft memlock unlimited
		elasticsearch hard memlock unlimited
		elasticsearch 	   - 	nofile 	65536
		elasticsearch	   -	nproc 	4096
	# vim jvm.options
		-Xms1g
		-Xmx1g
		Xmx设置不超过物理RAM的50%,以确保有足够的物理RAM留给内核文件系统缓存。但不要超过32G。

- 修改systemd启动文件
	# vim /usr/lib/systemd/system/elasticsearch.service
		[Service]		#在service语句块下添加
		LimitMEMLOCK=infinity
		# systemctl daemon-reload
		# systemctl start elasticsearch

2.1 Modify the configuration file and start the service

## 1.修改配置文件,并启动服务
[root@server12 ~]# rpm -ivh elasticsearch-7.6.1-x86_64.rpm 
[root@server12 elasticsearch]# vim elasticsearch.yml   ##修改配置文件
[root@server12 elasticsearch]# systemctl start elasticsearch.service   ##启动失败
Job for elasticsearch.service failed because the control process exited with error code. See "systemctl status elasticsearch.service" and "journalctl -xe" for details.

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

2.2 View log troubleshooting

##1. 查看日志错误
[root@server12 security]# cat /var/log/elasticsearch/my-es.log   ##查看日志错误
[root@server12 elasticsearch]# vim /etc/security/limits.conf  ##
	elasticsearch soft memlock unlimited
	elasticsearch hard memlock unlimited

## 2. 修改系统配置文件
[root@server12 elasticsearch]# vim /usr/lib/systemd/system/elasticsearch.service
[root@server12 elasticsearch]# cat /usr/lib/systemd/system/elasticsearch.service ##修改下面内容
	# Specifies the maximum size of virtual memory
	LimitAS=infinity
	LimitMEMLOCK=infinity     ##解除锁存问题
[root@server12 elasticsearch]# systemctl daemon-reload   ##修改系统配置需要reload

## 3.关闭交换空间
[root@server12 elasticsearch]# swapoff -a       ##关闭交换空间
[root@server12 elasticsearch]# vim /etc/fstab 
[root@server12 elasticsearch]# sysctl vm.max_map_count
vm.max_map_count = 262144

## 4. 安全限制文件配置
[root@server12 elasticsearch]# vim /etc/security/limits.conf  ##下面是添加内容,参考官网文档
	elasticsearch soft memlock unlimited
	elasticsearch hard memlock unlimited 
	elasticsearch  - nofile  65535   
	elasticsearch  - nproc  4096

## 5. 重启并测试
[root@server12 elasticsearch]# systemctl restart elasticsearch.service 
[root@server12 elasticsearch]# cat /var/log/elasticsearch/my-es.log   ##查看是否有错误
[root@server12 elasticsearch]# curl localhost:9200  ##测试

##1. View log errors
Insert picture description here
Insert picture description here

##2. Modify the system configuration file

Insert picture description here

Insert picture description here
Insert picture description here

## 3. Close the swap space
Insert picture description here

4. Configure the security restriction file
Insert picture description here

Insert picture description here

## 5. Restart and test
Insert picture description here

3. Plug-in installation (one set can be done to achieve web interface control)

- 下载elasticsearch-head插件
	# wget https://github.com/mobz/elasticsearch-head/archive/master.zip   ##本实验使用本地下载好的
	# unzip elasticsearch-head-master.zip
- head插件本质上是一个nodejs的工程,因此需要安装node:
	# wget https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_9.x/el/7/x86_64/nodejs-9.11.2-1nodesource.x86_64.rpm
	# rpm -ivh nodejs-9.11.2-1nodesource.x86_64.rpm
	# node -v
	# npm -v

 - 更换npm源安装
	# cd elasticsearch-head-master/
	# npm install  --registry=https://registry.npm.taobao.org
- 修改ES主机ip和端口
	# vim _site/app.js
	"http://172.25.0.7:9200"
- 启动head插件
	# npm run start & 

- 修改ES跨域主持
	# vim /etc/elasticsearch/elasticsearch.yml
	http.cors.enabled: true	# 是否支持跨域
	http.cors.allow-origin: "*"	# *表示支持所有域名
- 重启ES服务
	# systemctl restart elasticsearch.service 

3.1 Do not make secret

[root@server12 ~]# ssh-keygen 
[root@server12 ~]# ssh-copy-id server13 
 [root@server12 ~]# ssh-copy-id server14 

Insert picture description here
Insert picture description here

3.2 Ensure that the three nodes belong to a cluster (uuid remains the same)

[root@server13 ~]# cd /var/lib/elasticsearch/  ##不一致需要删除数据目录(如果数据目录里面没有东西)
[root@server13 elasticsearch]# ls
nodes
[root@server13 elasticsearch]# rm -fr *   ##
[root@server13 elasticsearch]# systemctl restart elasticsearch.service 
[root@server13 elasticsearch]# curl localhost:9200

Insert picture description here

3.3 download elasticsearch-head plugin

## 1. 解压
# wget https://github.com/mobz/elasticsearch-head/archive/master.zip
# unzip elasticsearch-head-master.zip
[root@server12 ~]# ll elasticsearch-head-master.zip 
-rw-r--r-- 1 root root 1357536 Mar  6 02:10 elasticsearch-head-master.zip
[root@server12 ~]# unzip elasticsearch-head-master.zip   ##解压

3.4 Install nodejs (you can use Aliyun epel source to download directly)

## 2. nodejs安装
[root@server12 ~]# ll nodejs-9.11.2-1nodesource.x86_64.rpm    ##标题3 刚开始就有下载链接
[root@server12 ~]# rpm -ivh nodejs-9.11.2-1nodesource.x86_64.rpm   ##安装
[root@server12 ~]# node -v 
v9.11.2
[root@server12 ~]# npm -v 
5.6.0
- head插件本质上是一个nodejs的工程,因此需要安装node:

Insert picture description here

3.5 Replace npm source

## 3. 更换npm源安装
[root@server12 ~]# npm install -g cnpm  --registry=https://registry.npm.taobao.org  ##换成taobao源
[root@server12 ~]# cnpm config get   ##查看是否安装成功

Insert picture description here

Insert picture description here
Insert picture description here

3.6 Install and start the head plugin (you can download the required package in advance)


[root@server12 ~]# ll phantomjs-2.1.1-linux-x86_64.tar.bz2   (安装过程中要用到的)
-rw-r--r-- 1 root root 23415665 Apr 12  2019 phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@server12 ~]# tar jxf phantomjs-2.1.1-linux-x86_64.tar.bz2 
[root@server12 ~]# cd phantomjs-2.1.1-linux-x86_64/bin/
[root@server12 bin]# ls
phantomjs
[root@server12 bin]# mv phantomjs /usr/local/bin/
[root@server12 bin]# phantomjs 
phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
[root@server12 bin]# yum provides */libfontconfig.so.1
[root@server12 bin]# yum install fontconfig-2.13.0-4.3.el7.x86_64 -y  ##安装所需依赖
[root@server12 ~]#  phantomjs    ##这样表示安装成功
phantomjs> 

[root@server12 ~]# cd elasticsearch-head-master/
[root@server12 elasticsearch-head-master]# cnpm install   ##安装并启动(直接安装就可以,需要提前安装bz2,直接安装就不需要考虑上面的安装pha软件的问题)
[root@server12 elasticsearch-head-master]# cnpm run start &  ##启动head插件,关机重新启动后,需要在重新run一下,不然9100端口打不开

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here

3.6 Start and modify the configuration file

## 6. 修改配置文件
[root@server12 elasticsearch-head-master]# pwd
/root/elasticsearch-head-master
[root@server12 elasticsearch-head-master]# vim _site/app.js   ##查看端口,修改ES主机ip和端口
[root@server12 elasticsearch]# pwd
/etc/elasticsearch
[root@server12 elasticsearch]# vim elasticsearch.yml   ##修改配置文件,修改ES跨域主持。每台主机都需要修改
	# vim /etc/elasticsearch/elasticsearch.yml
		http.cors.enabled: true	# 是否支持跨域
		http.cors.allow-origin: "*"	# *表示支持所有域名

Insert picture description here

Insert picture description here

3.7 Restart and test

## 7. 重启ES服务,启动并测试(访问head插件服务,修改elasticsearch-head下Gruntfile.js文件,默认监听在9100端口)
[root@server12 elasticsearch]# systemctl restart elasticsearch.service  ##重新启动
[root@server12 elasticsearch]# cat /var/log/elasticsearch/my-es.log   ##查看日志是否启动

Insert picture description here

Insert picture description here
Insert picture description here

4. Elasticsearch node role

- Master:
	主要负责集群中索引的创建、删除以及数据的Rebalance等操作。Master不负责数据的索引和检索,所以负载较轻。当Master节点失联或者挂掉的时候,ES集群会自动从其他Master节点选举出一个Leader。
  Data Node:
	主要负责集群中数据的索引和检索,一般压力比较大。	
  Coordinating Node:
	原来的Client node的,主要功能是来分发请求和合并结果的。所有节点默认就是Coordinating node,且不能关闭该属性。
  Ingest Node:
	专门对索引的文档做预处理
  Mechine Learning node:
  	机器学习节点提供了机器学习功能,该节点运行作业并处理机器学习API请求。

5. Node optimization

- 在生产环境下,如果不修改elasticsearch节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题。
  默认情况下,elasticsearch集群中每个节点都有成为主节点的资格,也都存储数据,还可以提供查询服务。
	节点角色是由以下属性控制:
		node.master:  false|true		
		node.data:  true|false
		node.ingest:  true|false 
		search.remote.connect: true|false
		 默认情况下这些属性的值都是true。

-   node.master:这个属性表示节点是否具有成为主节点的资格
    注意:此属性的值为true,并不意味着这个节点就是主节点。
    因为真正的主节点,是由多个具有主节点资格的节点进行选
    举产生的。
	node.data:这个属性表示节点是否存储数据。
	node.ingest: 是否对文档进行预处理。
	search.remote.connect:是否禁用跨集群查询

5.1 The first combination: (default)

node.master: true
node.data: true
node.ingest:  true
search.remote.connect: true
这种组合表示这个节点即有成为主节点的资格,又存储数据。
如果某个节点被选举成为了真正的主节点,那么他还要存储数据,这样对于这个节点的压力就比较大了。
测试环境下这样做没问题,但实际工作中不建议这样设置。

5.2 The second combination: (Data node)

node.master: false
node.data: true
node.ingest: false
search.remote.connect: false
这种组合表示这个节点没有成为主节点的资格,也就不参与选举,只会存储数据。
这个节点称为data(数据)节点。在集群中需要单独设置几个这样的节点负责存储数据。后期提供存储和查询服务。

5.3 The third combination: (master node)

node.master: true
node.data: false
node.ingest: false
search.remote.connect: false
这种组合表示这个节点不会存储数据,有成为主节点的资格,可以参与选举,有可能成为真正的主节点。
这个节点我们称为master节点。

5.4 The fourth combination: (Coordinating Node)

node.master: false
node.data: false
node.ingest: false
search.remote.connect: false
这种组合表示这个节点即不会成为主节点,也不会存储数据,
这个节点的意义是作为一个协调节点,主要是针对海量请求的时候可以进行负载均衡。

5.5 The fifth combination: (Ingest Node)

node.master: false
node.data: false
node.ingest: true
search.remote.connect: false
这种组合表示这个节点即不会成为主节点,也不会存储数据,
这个节点的意义是ingest节点,对索引的文档做预处理。

5.6 Division of Responsibilities

- 	生产集群中可以对这些节点的职责进行划分
	建议集群中设置3台以上的节点作为master节点,这些节点只负责成为主节点,维护整个集群的状态。
	再根据数据量设置一批data节点,这些节点只负责存储数据,后期提供建立索引和查询索引的服务,这样的话如果用户请求比较频繁,这些节点的压力也会比较大。
	所以在集群中建议再设置一批协调节点,这些节点只负责处理用户请求,实现请求转发,负载均衡等功能。

- 节点需求
	master节点:普通服务器即可(CPU、内存 消耗一般)
	data节点:主要消耗磁盘、内存。
		path.data: data1,data2,data3	
		这样的配置可能会导致数据写入不均匀,建议只指定一个数据路径,磁盘可以使用raid0阵列,而不需要成本高的ssd。
	Coordinating节点:对cpu、memory要求较高。

5.7 Node expansion

- 和之前安装一样,然后将集群名字改为之前所做的集群名字即可加入集群。实现扩容。

5.7 Node scaling

##1.暴力一点就是之间停止节点,就会转移到别的节点
## 2. 正常转移方法,推荐
#删除节点前迁移分片:
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type:application/json' -d '{"transient":{"cluster.routing.allocation.exclude._ip":"172.25.13.12"}}'
##删除节点
[root@server12 ~]# systemctl stop elasticsearch.service 

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

5.8 Test (Limited Role)

[root@server12 ~]# vim /etc/elasticsearch/elasticsearch.yml  ##修改角色信息(设置12\13\14为master,15为调试节点)
node.data: false
node.ingest: false
node.ml: false

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

6. Alibaba Cloud install nodejs

## 安装nodejs部分,之前的操作是一样的
[root@server7 yum.repos.d]# vim nodejs.repo
[root@server7 yum.repos.d]# cat nodejs.repo 
[epel]
name=epel-nodejs
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0
[root@server7 yum.repos.d]# yum repolist 
epel                            epel-nodejs                          13,550


[root@server7 yum.repos.d]# yum list nodejs
Available Packages
nodejs.x86_64                             1:6.17.1-1.el7                              epel
[root@server7 yum.repos.d]# yum install -y nodejs    ##依赖问题自动解决
[root@server7 yum.repos.d]# npm config set registry https://registry.npm.taobao.org  ##切换淘宝源
[root@server7 yum.repos.d]# npm install -g n   ##安装工具
[root@server7 ~]# n stable    #更新npm版本

[root@server7 ~]# vim .bash_profile
[root@server7 ~]# cat .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/bin/node:/usr/local/bin/npm
[root@server7 ~]# npm -v     ##原来版本
3.10.10  
[root@server7 ~]# source  .bash_profile   ##加载环境变量
[root@server7 ~]# npm -v    ##新版
6.14.11
[root@server7 ~]# node -v 
v14.16.0
[root@server7 ~]# npm install cnpm -g   ##安装cnpm。相当于国内源

[root@server7 ~]# yum install -y unzip.x86_64 bzip2  ##解压工具必须提前做好,因为之后启动head插件需要用到
[root@server7 ~]# unzip elasticsearch-head-master.zip   ##下载head插件
[root@server7 ~]# cnpm install 

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qwerty1372431588/article/details/114574888