赞
踩
目录
6.3.安装 Elasticsearch-head 数据可视化工具
6.6.通过 Elasticsearch-head 查看 Elasticsearch 信息
4.将nginx服务器的日志(访问的、错误的)添加到 Elasticsearch 并通过 Kiabana 显示
ELK平台是一套完整的日志集中处理解决方案,将 ElasticSearch、Logstash 和 Kiabana 三个开源
工具配合使用, 完成更强大的用户对日志的查询、排序、统计需求
ElasticSearch
Kiabana
Logstash
Filebeat
日志主要包括系统日志、应用程序日志和安全日志。系统运维和开发人员可以通过日志了解服务器
软硬件信息、检查配置过程中的错误及错误发生的原因。经常分析日志可以了解服务器的负荷,性
能安全性,从而及时采取措施纠正错误。
往往单台机器的日志我们使用grep、awk等工具就能基本实现简单分析,但是当日志被分散存储在
不同的设备上。如果你管理数十上百台服务器,你还在使用依次登录每台机器的传统方法查阅日
志。这样是不是感觉很繁琐和效率低下。当务之急我们使用集中化的日志管理,例如:开源的
rsyslog,将所有服务器上的日志收集汇总。集中化管理日志后,日志的统计和检索又成为一件比
较麻烦的事情,一般我们使用grep、awk和wc等Linux命令能实现检索和统计,但是对于要求更高
的查询、排序和统计等要求和庞大的机器数量依然使用这样的方法难免有点力不从心。
一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大
部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系
统,可以提高定位问题的效率
总结
Logstash 作为日志搜集器,从数据源采集数据,并对数据进行过滤,格式化处理,然后交由
Elasticsearch 存储,Kibana 对日志进行可视化处理
es01 | 192.168.80.101 | Elasticsearch |
es02 | 192.168.80.102 | Elasticsearch |
es03 | 192.168.80.103 | ElasticSearch Kibana |
nginx节点 | 192.168.80.104 | Logstash Nginx |
- java -version
- #如果没有安装,yum -y install java-1.8.0-openjdk*
- openjdk version "1.8.0_131"
- OpenJDK Runtime Environment (build 1.8.0_131-b12)
- OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
部署 Elasticsearch 软件(三台)
- #上传elasticsearch-6.7.2.rpm到/opt目录下
- cd /opt
- yum localinstall -y elasticsearch-6.7.2_\(1\).rpm
- cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
- vim /etc/elasticsearch/elasticsearch.yml
- --17--取消注释,指定集群名字
- cluster.name: my-elk-cluster
- --23--取消注释,指定节点名字:Node1节点为node-1,Node2节点为node-2,Node3节点为node-3
- node.name: node-1
- node.master: true #是否master节点,false为否
- node.data: true #是否数据节点,false为否
- --33--取消注释,指定数据存放路径
- path.data: /var/lib/elasticsearch
- --37--取消注释,指定日志存放路径
- path.logs: /var/log/elasticsearch
- --43--取消注释,将系统内存锁定到es进程中,以保证es能够维护一定的内存空间,避免es使用swap交换分区
- bootstrap.memory_lock: true
- --55--取消注释,设置监听地址,0.0.0.0代表所有地址
- network.host: 0.0.0.0
- --59--取消注释,ES 服务的默认监听端口为9200
- http.port: 9200 #指定es集群提供外部访问的接口
- transport.tcp.port: 9300 #指定es集群内部通信接口
- --68--取消注释,集群发现通过单播实现,指定要发现的节点
- discovery.zen.ping.unicast.hosts: ["192.168.80.101:9300", "192.168.80.102:9300","192.168.80.103:9300"]
-
- grep -v "^#" /etc/elasticsearch/elasticsearch.yml

scp elasticsearch.yml 192.168.80.102:`pwd`
优化最大内存大小和最大文件描述符的数量
- vim /etc/security/limits.conf
- ......
- * soft nofile 65536
- * hard nofile 65536
- * soft nproc 65536
- * hard nproc 65536
- * soft memlock unlimited
- * hard memlock unlimited
- vim /etc/systemd/system.conf
- DefaultLimitNOFILE=65536
- DefaultLimitNPROC=65536
- DefaultLimitMEMLOCK=infinity
scp /etc/security/limits.conf 192.168.80.102:/etc/security
- vim /etc/sysctl.conf
-
- vm.max_map_count=262144
- net.ipv4.tcp_syncookies=1
- net.ipv4.tcp_tw_reuse=1
- net.ipv4.tcp_tw_recycle=1
- net.ipv4.tcp_fin_timeout=30
- net.ipv4.tcp_keepalive_time=1200
- net.ipv4.ip_local_port_range=1024 65535
- net.ipv4.tcp_max_syn_backlog=8192
- net.ipv4.tcp_max_tw_buckets=5000
- net.core.somaxconn=65535
-
- sysctl -a | grep vm.max_map_count
需重启生效
reboot
优化elasticsearch用户拥有的虚拟内存
由于ES构建基于lucene, 而lucene设计强大之处在于lucene能够很好的利用操作系统内存来缓存索
引数据,以提供快速的查询性能。lucene的索引文件segements是存储在单文件中的,并且不可
变,对于OS来说,能够很友好地将索引文件保持在cache中,以便快速访问;因此,我们很有必要
将一半的物理内存留给lucene ; 另一半的物理内存留给ES(JVM heap )。所以, 在ES内存设置方
面,可以遵循以下原则:
- 三台服务器共同操作
- systemctl start elasticsearch.service
- systemctl enable elasticsearch.service
- netstat -lntp | grep 9200
- JVM优化(三台服务器都需操作)
- cd /etc/elasticsearch/
- vim jvm.options
- 22行 -Xms2g
- 23行 -Xmx2g @@设置为物理内存的一般
-
- systemctl restart elasticsearch.service #重启服务
浏览器访问 http://192.168.80.101:9200 、 http://192.168.80.102:9200、 http://192.168.80.103:9200查看节点 es01、es02、es03 的信息。
浏览器访问 http://192.168.80.101:9200/_cluster/health?pretty 、 http://192.168.80.102:9200/_cluster/health?pretty、http://192.168.80.103:9200/_cluster/health?pretty查看群集的健康情况,可以看到 status 值为 green(绿色), 表示节点健康运行。
浏览器访问 http://192.168.80.101:9200/_cluster/state?pretty 检查群集状态信息。
使用上述方式查看群集的状态对用户并不友好,可以通过安装 Elasticsearch-head 插件,可以更
方便地管理群集
任意一个节点服务器安装就行 192.168.80.101
Elasticsearch 在 5.0 版本后,Elasticsearch-head 插件需要作为独立服务进行安装,需要使用npm
工具(NodeJS的包管理工具)安装
安装 Elasticsearch-head 需要提前安装好依赖软件 node 和 phantomjs。
- 上传软件包 node-v8.2.1.tar.gz 到/opt
- yum install gcc gcc-c++ make -y
-
- cd /opt
- tar zxvf node-v8.2.1.tar.gz
-
- cd node-v8.2.1/
- ./configure
- make && make install
- 上传软件包 phantomjs-2.1.1-linux-x86_64.tar.bz2 到
- cd /opt
- tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
- cd /opt/phantomjs-2.1.1-linux-x86_64/bin
- cp phantomjs /usr/local/bin
- 上传软件包 elasticsearch-head-master.zip 到/opt
- cd /opt
- unzip elasticsearch-head-master.zip
- cd /opt/elasticsearch-head-m/
- npm install //安装依赖包
- vim /etc/elasticsearch/elasticsearch.yml
- ......
- --末尾添加以下内容--
- http.cors.enabled: true #开启跨域访问支持,默认为 false
- http.cors.allow-origin: "*" #指定跨域访问允许的域名地址为所有
-
- systemctl restart elasticsearch
- #必须在解压后的 elasticsearch-head 目录下启动服务,进程会读取该目录下的 gruntfile.js 文件,否则可能启动失败。
- cd /etc/elasticsearch-head-master/
- npm run start &
-
- > elasticsearch-head@0.0.0 start /usr/local/src/elasticsearch-head
- > grunt server
-
- Running "connect:server" (connect) task
- Waiting forever...
- Started connect web server on http://localhost:9100
-
- #elasticsearch-head 监听的端口是 9100
- netstat -natp |grep 9100
通过浏览器访问 http://192.168.80.101:9100/ 地址并连接群集。如果看到群集健康值为 green 绿色,代表群集很健康。
- API基本格式:http://ip:port/<索引>/<类型>/<文档id>
- #通过命令创建一个测试索引,索引为 index-demo,类型为 test。
- curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' \
- -H 'content-Type: application/json' \
- -d '{"user":"zhangsan","mesg":"hello world"}'
- //输出结果如下:
- {
- "_index" : "index-demo",
- "_type" : "test",
- "_id" : "1",
- "_version" : 1,
- "result" : "created",
- "_shards" : {
- "total" : 2,
- "successful" : 2,
- "failed" : 0
- },
- "created" : true
- }
-
- 浏览器访问 http://192.168.80.101:9100/ 查看索引信息,可以看见索引默认被分片5个,并且有一个副本。
- 点击“数据浏览”,会发现在node1上创建的索引为 index-demo,类型为 test 的相关信息。

创建索引
- curl -X PUT 'http://ES-IP:9200/<索引名>/<类型>/<ID>?pretty&pretty' \
-
- -H 'content-Type: application/json' -d '{"键1":"值1","键2":"值2"}'
-
删除索引
curl -X DELETE 'http://ES-IP:9200/<索引名>'
查看索引配置
curl -X GET 'http://ES-IP:9200/<索引名>/_settings'
修改索引配置
- curl -X PUT 'http://ES-IP:9200/<索引名>/_settings' \
- -H 'content-Type: application/json' -d '{"键":"值"}'
创建索引别名
- curl -X POST 'http://ES-IP:9200/_aliases' \
- -H 'Content-Type: application/json' -d '{"actions":[{"add":{"index":"索引名","alias":"索引别名"}}]}'
删除索引别名
- curl -X POST 'http://ES-IP:9200/_aliases' \
- -H 'Content-Type: application/json' -d '{"actions":[{"remove":{"index":"索引名","alias":"索引别名"}}]}'
Logstash 一般部署在需要监控其日志的服务器。在本案例中,Logstash 部署在 nginx 服务器
上,用于收集 nginx服务器的日志信息并发送到 Elasticsearch
- systemctl disable --now firewalld
- setenforce 0
- vim /etc/selinux/config
- SELINUX=disabled
hostnamectl set-hostname nginx01
- cd /etc/yum.repos.d
- 上传nginx.repo文件
- yum install -y nginx
- systemctl enable --now nginx
-
- cd /usr/share/nginx/html
- vim test.html
-
- <h1>this is web page</h1>
-
- 浏览器进行测试:http://192.168.80.104/test.html
- 上传软件包 logstash-6.7.2.rpm 到/opt目录下
- cd /opt
- 上传软件包
- yum localinstall -y logstash-6.7.2.rpm #安装
- ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
Logstash 命令常用选项
-f | 通过这个选项可以指定 Logstash 的配置文件,根据配置文件配置 Logstash 的输入和输出流 |
-e | 从命令行中获取,输入、输出后面跟着字符串,该字符串可以被当作 Logstash 的配置(如果是空,则默认使用 stdin 作为输入,stdout 作为输出) |
-t | 测试配置文件是否正确 |
-w | 指定filter线程数量,默认线程数是 5 |
-l | 指定日志文件名称 |
输入采用标准输入,输出采用标准输出(类似管道),新版本默认使用 rubydebug 格式输出
logstash -e 'input { stdin{} } output { stdout{} }'
logstash -e 'input{stdin{}} output {elasticsearch{hosts=>["192.168.9.114:9200"]}}'
将标准输入的内容采用标准输出的方法输出到192.168.80.101
Logstash 配置文件基本由三部分组成:input、output 以及 filter(可选,根据需要选择使用)
Logstash 配置文件基本由三部分组成:input、output 以及 filter(可选,根据需要选择使用)。
●input:表示从数据源采集数据,常见的数据源如Kafka、日志文件等
file beats kafka redis stdin
●filter:表示数据处理层,包括对数据进行格式化处理、数据类型转换、数据过滤等,支持正则表
达式
grok 对若干个大文本字段进行再分割成一些小字段 (?<字段名>正则表达式) 字段名: 正则表
达式匹配到的内容
date 对数据中的时间格式进行统一和格式化
mutate 可以重命名,删除,替换和修改事件中的字段。比如对一些无用的字段进行剔除,或增
加自定义的字段
multiline 对多行数据进行统一编排,将多行数据汇总为一个单一的行
●output:表示将Logstash收集的数据经由过滤器处理之后输出到Elasticsearch。
elasticsearch stdou
格式如下
input {...}
filter {...}
output {...}
#在每个部分中,也可以指定多个访问方式。例如,若要指定两个日志来源文件,则格式如下:
input {
file { path =>"/var/log/messages" type =>"syslog"}
file { path =>"/var/log/httpd/access.log" type =>"apache"}
}
#修改 Logstash 配置文件,让其收集系统日志/var/log/messages,并将其输出到 elasticsearch 中。
chmod +r /var/log/messages #让 Logstash 可以读取日志
cd /etc/logstash/conf.d/
vim system.conf
input {
file{
path =>"/var/log/messages"
type =>"system"
start_position =>"beginning"
# ignore_older => 86400
sincedb_path => "/etc/logstash/sincedb_path/log_progress"
add_field => {"log_hostname"=>"${HOSTNAME}"}
}
}
---------------------------------------------------------------------------------------
常用参数
- chmod a+r /var/log/messages #保证要有可读权限
- mkdir -p /etc/logstash/sincedb_path
- touch /etc/logstash/sincedb_path/log_progress
- chown logstash:logstash !$
- cd /etc/nginx/conf.d
- vim syslog.conf #编写文件获取系统日志
- input {
- file {
- path => "/var/log/messages"
- type => "syslog"
- start_position => "beginning"
- sincedb_path => "/etc/logstash/sincedb_path/log_progress"
- add_field => {"logfrom" => "${HOSTNAME}"}
- }
-
-
-
- }
-
- #filter {}
-
-
- output {
- elasticsearch {
- hosts => ["192.168.80.101:9200","192.168.80.102:9200","192.168.80.103:9200"]
- index => "syslog-%{+yyyy.MM.dd}"
- }
- }
-
-
- logstash -t -f syslog.conf #配置完成后使用此命令查看配置文件是否正确
- logstash -f syslog.conf #启动文件

- 上传软件包 kibana-6.7.2-x86_64.rpm 到/opt目录
- cd /opt
- yum localinstall -y kibana-6.7.2-x86_64.rpm
- cd /etc/kibana/
- cp kibana.yml kibana.yml.bak
- touch /var/log/kibana.log
- chown kibana:kibana !$
- vim /etc/kibana/kibana.yml
- --2--取消注释,Kiabana 服务的默认监听端口为5601
- server.port: 5601
- --7--取消注释,设置 Kiabana 的监听地址,0.0.0.0代表所有地址
- server.host: "0.0.0.0"
- --28--取消注释,配置es服务器的ip,如果是集群则配置该集群中master节点的ip
- elasticsearch.hosts: ["http://192.168.80.101:9200","http://192.168.80.102:9200","http://192.168.80.103:9200"]
- --96--取消注释,配置kibana的日志文件路径(需手动创建),不然默认是messages里记录日志
- logging.dest: /var/log/kibana.log
- --113--取消注释,指定页面字符格式为中文
- i18n.locale: "zh-CN"
-
- systemctl enable --now kibana.service

浏览器访问 http://192.168.80.103:5601
左侧[管理],点击【管理菜单】,点击【索引管理】即可在其中进行索引管理
创建索引模式:左侧[管理],点击【管理菜单】,点击【索引模式】,索引模式下对索引进行搜索
将192.168.80.104服务器中nginx的日志进行添加
- cd /etc/nginx/conf.d
- vim nginx_log.conf
- input {
- file {
- path => "/var/log/nginx/access.log"
- type => "nginx_access"
- start_position => "beginning"
- sincedb_path => "/etc/logstash/sincedb_path/log_progress"
- }
- file {
- path => "/var/log/nginx/error.log"
- type => "nginx_error"
- start_position => "beginning"
- sincedb_path => "/etc/logstash/sincedb_path/log_progress"
- }
- }
-
- #filter {}
-
- output {
- if [type] == "nginx_access" {
- elasticsearch {
- hosts => ["192.168.80.101:9200","192.168.80.102:9200","192.168.80.103:9200"]
- index => "nginx_access-%{+yyyy.MM.dd}"
- }
- }
- if [type] =="nginx_error" {
- elasticsearch {
- hosts => ["192.168.80.101:9200","192.168.80.102:9200","192.168.80.103:9200"]
- index => "access_error-%{+yyyy.MM.dd}"
- }
- }
- }
-
-
- logstash -t -f nginx_log.conf #测试
- logstash -f nginx_log.conf #启动

kibana查看添加索引
同样操作添加nginx_error索引
此时访问192.168.80.104 nginx 服务器的日志即可在kibana中显示出来
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。