ELK基础配置

前言

近期在研究日志系统的设计,感觉现在公司的子系统和接口太多了,日志看不过来,就想着有没有一种方法可以把各个程序的日志组合到一起。于是乎就搜到了ELK。开始对ELK的概念完全搞不懂,就照着各个平台文档一顿安装和研究。终于搞明白了ELK这套系统的大致流程。

ELK即:Elasticsearch、Logstash、Kibana的简称。

简单介绍来说:Elasticsearch用来存储日志,Logstash用来搜集和过滤日志,Kibana用来展示日志。

为什么用Elasticsearch存储日志呢,它是个搜索引擎,可以存储海量数据,可以各种查询并且速度很快。

Logstash可以搜集和分析日志,但是它占的内存和cpu过大,所以我最终选择了研究FileBeat替代Logstash。日志搜集工具的工作流程就是在各个产生日志的服务器上安装该工具,然后它负责从数据库文件系统或者mq等地方搜集日志并通过http发送到ElasticSearch

ELK里面涉及到的每个工具的功能都相当丰富和强大,远不止日志记录这一功能。后面还要继续学习

关于ElK的基本安装和使用本文就不做介绍了,因为网上很多。记录一下基础的配置和常见的问题防止以后忘记,也留给需要的人希望给你们一些帮助。后面遇到新的坑和问题会继续完善该博客

ElasticSearch

配置修改

配置文件路径:/config/elasticsearch.yml

#开启外网访问
network.host: 0.0.0.0

node.name: node-1

cluster.initial_master_nodes: ["node-1"]

http.port:9200

#启用密码验证
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

设置密码:

bin/elasticsearch-setup-passwords interactive

接下来系统将会提示为几个用户挨个设置密码

执行该命令的前提是配置文件启用了密码验证

后台启动

启动:/elasticsearch -p /tmp/elasticsearch-pid -d

关闭:cat /tmp/elasticsearch-pid 
展示:pid
kill -9 pid

报错

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

原因:elasticsearch用户拥有的内存权限太小,至少需要262144:
解决方案:

执行命令:
sysctl -w vm.max_map_count=262144

查看结果:
sysctl -a|grep vm.max_map_count
显示:vm.max_map_count = 262144

/etc/sysctl.conf文件最后添加一行 vm.max_map_count=262144 。否则重启服务器配置将失效

Kibana

配置修改

配置文件路径:config/kibana.yml
server.host:"0.0.0.0" #用于外网访问

#配置elasticsearch的地址
elasticsearch.hosts: ["http://localhost:9200"]  

#es开启授权后配置es的用户名和密码
elasticsearch.username: "elastic"
elasticsearch.password: "123456"

#中文支持 
i18n.locale: "zh-CN" 

后台启动

启动(加上&):
kibana-4.5.2-linux-x64/bin/kibana &

退出:
 ps -ef|grep kibana
 kill -9 pid

展示

在IndexManage  中 Create IndexPattern 这一步是为了将日志的Index展示到Discover中去
在Discover中查看系统日志

FileBeat

配置文件

filebeat.inputs:
	enabled: true
	# 解决中文乱码
	encoding: GB2312
	paths:
	  D:\Logs\TopShelf\*\*\*.txt 
	 
# 正则表达式 The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
multiline.pattern: '^时间'
# true 或 false;默认是false,匹配pattern的行合并到上一行;true,不匹配pattern的行合并到上一行
multiline.negate: true
# Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
multiline.match: after
#匹配结尾
#multiline.flush_pattern: 'End event'
#合并最大行,默认500
multiline.max_lines: 50
#一次合并事件的超时时间,默认5s,防止合并消耗太多时间甚至卡死
multiline.timeout: 5s

setup.kibana:
	host: "****:5601"

#允许自定义索引名称
setup.ilm.enabled: auto
    #滚动更新别名,和pattern最终拼在一起,滚动更新是00001会自增
    setup.ilm.rollover_alias: "filebeat-testing"
    setup.ilm.pattern: "{now/d}-000001"

    #允许自动生成模板
    setup.template.enabled
    #可以覆盖模板
    setup.template.overwrite: true
#索引模板名称
setup.template.name: "filebeat-testing"
#索引模板匹配策略
setup.template.pattern: "filebeat-testing-%{[agent.version]}-*"

output.elasticsearch:
	hosts: ["*.*.*.*:9200"]  
	# Optional protocol and basic auth credentials.
	#protocol: "https"
	username: "elastic"
	password: "123456"

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

windows启动

powershell跳转至下载目录,执行命令

.\filebeat -e -c filebeat.yml

Docker配置###

	version: '3.0'

	services:
		elasticsearch:
			image: 'docker.elastic.co/elasticsearch/elasticsearch:7.5.1'
			container_name: elasticsearch01
			ports:
				- "9200:9200"
				- "9300:9300"
			volumes:
				- /etc/timezone:/etc/timezone
				- /etc/localtime:/etc/localtime
				- esdata01:/usr/share/elasticsearch/data
			environment:
				- node.name=es01
				- cluster.initial_master_nodes=es01
				- xpack.security.enabled=true
				- xpack.security.transport.ssl.enabled=true
				- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
		kibana:
			image: 'docker.elastic.co/kibana/kibana:7.5.1'
			ports:
				- "8005:5601"
			volumes:
				- /etc/timezone:/etc/timezone
				- /etc/localtime:/etc/localtime
			environment:
				SERVER_NAME: kibana
				ELASTICSEARCH_HOSTS: http://elasticsearch01:9200
				ELASTICSEARCH_USERNAME: "elastic"
				ELASTICSEARCH_PASSWORD: "backer2019"
				I18N_LOCALE: zh-CN
			depends_on:
				- elasticsearch
	volumes:
		esdata01:
			driver: local

Docker-filebeat###

	version: "3"

	services:
		filebeat:
			image: myfilebeat
	#    image: docker.elastic.co/beats/filebeat:7.5.1
			restart: always
			container_name: filebeat
			volumes:
				- /etc/timezone:/etc/timezone
				- /etc/localtime:/etc/localtime
				- filebeatdata:/usr/share/filebeat/data
				- /mnt/data/filebeat/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml:ro
				- /var/run/docker.sock:/var/run/docker.sock:ro
				- /var/lib/docker/containers:/var/lib/docker/containers:ro
				- /mnt/data:/usr/share/filebeat/logs
			environment:
				 - output.elasticsearch.hosts=["elasticsearch内网url:9200"]
				 - strict.perms=false
	volumes:
		filebeatdata:

myfileeat-Dockerfile###

	FROM docker.elastic.co/beats/filebeat:7.5.1
	ENV LANG en_US.UTF-8
	ENV LANGUAGE en_US:en
	ENV LC_ALL en_US.UTF-8
posted @ 2019-11-08 17:35  张三~~  阅读(754)  评论(0编辑  收藏  举报