ELK介绍
ELK简介
为什么要使用ELK
- 收集:能够采集多种来源的日志数据
- 传输:能够稳定的把日志数据解析过滤并传输到存储系统
- 存储:存储日志数据
- 分析:支持UI分析
- 警告:能够提供错误报告,监控机制
ELK架构分析
Beats + Elasticsearch + Kibana模式
Beats + Logstash + Elasticsearch + Kibana模式
- 通Logstash具有基于磁盘的自适应缓冲系统,该系统将吸收传入的吞吐量,从而减轻背压
- 从其他数据源(例如数据库,S3或消息传递队列)中提取
- 将数据发送到多个目的地,例如S3,HDFS或写入文件
- 使用条件数据流逻辑组成更复杂的处理管道
- 水平可扩展性,高可用性和可变负载处理:Filebeat和Logstash可以实现节点之间的负载均衡,多个Logstash可以实现Logstash的高可用。
- 消息持久性与至少一次交付保证:使用Filebeat或Winlogbeat进行日志收集时,可以保证至少一次交付。从Filebeat或Winlogbeat到Logstash以及从Logstash到Elasticsearch的两种通信协议都是同步的,并且支持确认。Logstash持久队列提供跨节点故障的保护。对于Logstash中的磁盘级弹性,确保磁盘冗余非常重要。
- 具有身份验证和有线加密的端到端安全传输:从Beats到Logstash以及从Logstash到Elasticsearch的传输都可以使用加密方式传递 。与Elasticsearch进行通讯时,有很多安全选项,包括基本身份验证,TLS,PKI,LDAP,AD和其他自定义领域。
- 降低对日志所在机器的影响,这些机器上一般都部署着反向代理或应用服务,本身负载就很重了,所以尽可能的在这些机器上少做事;
- 如果有很多台机器需要做日志收集,那么让每台机器都向Elasticsearch持续写入数据,必然会对Elasticsearch造成压力,因此需要对数据进行缓冲,同时,这样的缓冲也可以一定程度的保护数据不丢失;
- 将日志数据的格式化与处理放到Indexer中统一做,可以在一处修改代码、部署,避免需要到多台机器上去修改配置。
ELK部署
Filebeat的安装介绍
curl-L-Ohttps://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.7.0-linux-x86_64.tar.gz tar -xzvf filebeat-7.7.0-linux-x86_64.tar.gz
./filebeat -e
Logstash的安装介绍
tar -zxvf logstash-7.7.0.tar.gz
./bin/logstash -e 'input { stdin { } } output { stdout {} }'
Elasticsearch的安装介绍
- 分布式:横向扩展非常灵活;
- 全文检索:基于Lucene的强大的全文检索能力;
- 近实时搜索和分析:数据进入ES,可达到近实时搜索,还可进行聚合分析;
- 高可用:容错机制,自动发现新的或失败的节点,重组和重新平衡数据;
- 模式自由:ES的动态mapping机制可以自动检测数据的结构和类型,创建索引并使数据可搜索;
- RESTful API:JSON + HTTP。
Linux系统参数设置
ulimit #暂时修改,切换到该用户es,ulimit -n 65535
/etc/security/limits.conf #永久修改 es – nofile 65535
ulimit -a #查看当前用户的资源限制
swapoff -a #临时禁用所有的swap文件
vim /etc/fstab #注释掉所有的swap相关的行,永久禁用
cat /proc/sys/vm/swappiness #查看该值
sysctl vm.swappiness=1 #临时修改该值为1
vim /etc/sysctl.conf #修改文件 永久生效
vm.swappiness = 1 #如果有该值,则修改该值,若没有,则追加该选项,sysctl -p生效命令
配置elasticsearch.yml文件,添加如下配置:
bootstrap.memory_lock: true
GET _nodes?filter_path=**.mlockall #检查如上配置是否成功
vim /etc/security/limits.conf #永久修改
es soft nproc 65535
es hard nproc 65535
sysctl -w vm.max_map_count=262144 #临时修改该值
vim /etc/sysctl.conf #永久修改
vm.max_map_count=262144
ulimit -u 4096 #临时修改
vim /etc/security/limits.conf #永久修改
Elasticsearch安装
useradd elk -d /data/hd05/elk -g elastic
echo ‘2edseoir@’ | passwd elk –stdin
bin: $ES_HOME/bin #ES启动命令和插件安装命令
conf:$ES_HOME/conf #elasticsearch.yml配置文件目录
data:$ES_HOME/data #对应的参数path.data,用于存放索引分片数据文件
logs:$ES_HOME/logs #对应的参数path.logs,用于存放日志
jdk:$ES_HOME/jdk #自带支持该ES版本的JDK
plugins: $ES_HOME/jplugins #插件存放目录
lib: $ES_HOME/lib #存放依赖包,比如Java类库
modules: $ES_HOME/modules #包含所有的ES模块
Vim ~/.bashrc
############往后面添加如下内容######################
export JAVA_HOME=/data/hd05/elk/es/jdk
export PATH=JAVAHOME/bin:PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar\:/lib/tools.jar
配置Java参数
一种是通过修改/data/hd05/elk/elasticsearch-7.7.0/config/jvm.options文件修改JVM参数,一个使用过一个变量ES_JAVA_OPTS来声明JVM参数
/data/hd05/elk/elasticsearch-7.7.0/config/jvm.options介绍:
8:-Xmx2g #表示只适合Java 8
8-:-Xmx2g #表示适合高于Java 8的版本
8-9:-Xmx2g #表示适合Java 8,和Java 9
其他配置,都是JVM的相关参数,如果要想明白,得去看Java虚拟机
通过变量ES_JAVA_OPTS来声明JVM参数:
例如:export ES_JAVA_OPTS=”$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir”
./bin/elasticsearch
[elk@lgh config]$ cat jvm.options | egrep -v ‘^$|#’
-Xms2g
-Xmx2g
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30
-Djava.io.tmpdir=${ES_TMPDIR}
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=data
-XX:ErrorFile=logs/hs_err_pid%p.log
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
./bin/elasticsearch-certutil ca -out config/elastic-certificates.p12 -pass “password”
./bin/elasticsearch-certutil cert –ca elastic-stack-ca.p12 #为节点颁发证书,与上面密码一样
执行./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password 并输入第一步输入的密码
执行./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password 并输入第一步输入的密码
将生成的elastic-certificates.p12、elastic-stack-ca.p12文件移动到config目录下
cluster.name: my_cluster
node.name: lgh01
node.data: true
node.master: true
path.data: /data/hd05/elk/elasticsearch-7.7.0/data
path.logs: /data/hd05/elk/elasticsearch-7.7.0/logs
network.host: 192.168.110.130
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: [“192.168.110.130″,”192.168.110.131″,”192.168.110.132″,”192.168.110.133”]
cluster.initial_master_nodes: [“lgh01″,”lgh02″,”lgh03”]
cluster.routing.allocation.cluster_concurrent_rebalance: 32
cluster.routing.allocation.node_concurrent_recoveries: 32
cluster.routing.allocation.node_initial_primaries_recoveries: 32
http.cors.enabled: true
http.cors.allow-origin: ‘*’
#下面的是配置x-pack和tsl/ssl加密通信的
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
bootstrap.memory_lock: false #CentOS 6需要配置
bootstrap.system_call_filter: false #CentOS 6需要配置
[elk@lgh elasticsearch-7.7.0]$ ./bin/elasticsearch-setup-passwords interactive
Enter password for the elasticsearch keystore :
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
1qaz@WSXChanged password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
#Running with built in server
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open http://localhost:9100/
Kibana的安装介绍
[elk@lgh config]$ cat kibana.yml | egrep -v “^$|#”
server.port: 5601
server.host: “0.0.0.0”
server.name: “my-kibana”
elasticsearch.hosts: [“http://192.168.110.130:9200″,”http://192.168.110.131:9200″,”http://192.168.110.132:9200”]
elasticsearch.preserveHost: true
kibana.index: “.kibana”
elasticsearch.username: “elastic”
elasticsearch.password: “password” #或者使用keystore的保存的密码”${ES_PWD}”
[elk@lgh ~]$ tail -3 logstash-tutorial.log
86.1.76.62 – – [04/Jan/2015:05:30:37 +0000] “GET /projects/xdotool/ HTTP/1.1” 200 12292 “http://www.haskell.org/haskellwiki/Xmonad/Frequently_asked_questions” “Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0”
86.1.76.62 – – [04/Jan/2015:05:30:37 +0000] “GET /reset.css HTTP/1.1” 200 1015 “http://www.semicomplete.com/projects/xdotool/” “Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0”
86.1.76.62 – – [04/Jan/2015:05:30:37 +0000] “GET /style2.css HTTP/1.1” 200 4877 “http://www.semicomplete.com/projects/xdotool/” “Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20140205 Firefox/24.0 Iceweasel/24.3.0”
#=========================== Filebeat inputs =============================
filebeat.inputs:
# Each – is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.
– type: log
# Change to true to enable this input configuration.
enabled: true
# Paths that should be crawled and fetched. Glob based paths.
paths:
– /data/elk/logstash-tutorial.log #这里使用的是apache的日志格式
#- c:\programdata\elasticsearch\logs\*
# Exclude lines. A list of regular expressions to match. It drops the lines that are
# matching any regular expression from the list.
#exclude_lines: [‘^DBG’]
# Include lines. A list of regular expressions to match. It exports the lines that are
# matching any regular expression from the list.
#include_lines: [‘^ERR’, ‘^WARN’]
# Exclude files. A list of regular expressions to match. Filebeat drops the files that
# are matching any regular expression from the list. By default, no files are dropped.
#exclude_files: [‘.gz$’]
# Optional additional fields. These fields can be freely picked
# to add additional information to the crawled log files for filtering
#fields:
# level: debug
# review: 1
### Multiline options
# Multiline can be used for log messages spanning multiple lines. This is common
# for Java Stack Traces or C-Line Continuation
# The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
#multiline.pattern: ^\[
# Defines if the pattern set under pattern should be negated or not. Default is false.
#multiline.negate: false
# Match can be set to “after” or “before”. It is used to define if lines should be append to a pattern
# that was (not) matched before or after or as long as a pattern is not matched based on negate.
# Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
#multiline.match: after
#================================ Outputs =====================================
output.kafka:
hosts: [“192.168.110.130:9092”] #配置kafka的broker
topic: ‘filebeat_test’ #配置topic 名字
partition.round_robin:
reachable_only: false
required_acks: 1
compression: gzip
max_message_bytes: 1000000
cd filebeat-7.7.0-linux-x86_64 && nohup ./filebeat -e &
cd conf.d
vim apache.conf
################apache.conf文件中填入如下内容##############################
input {
kafka{
bootstrap_servers => “192.168.110.130:9092”
topics => [“filebeat_test”]
group_id => “test123”
auto_offset_reset => “earliest”
}
}
filter {
json
{
source => “message”
}
grok {
match => { “message” => “%{COMBINEDAPACHELOG}”}
remove_field => “message”
}
}
output {
stdout { codec => rubydebug }
elasticsearch {
hosts => [“192.168.110.130:9200″,”192.168.110.131:9200″,”10.18.126.224:9200″,”192.168.110.132:9200”]
index => “test_kakfa”
user => “elastic”
password => “${ES_PWD}”
}
}
cd logstash-7.7.0/ && nohup ./bin/logstash -f conf.d/apache.conf &