一.部署计划
准备三台服务器,分别为:
192.168.65.143: 部署nginx、Logstash,收集nginx日志
192.168.65.144: 部署Elasticsearch
192.168.65.145: 部署Kibana
logstash下载地址:https://www.elastic.co/cn/downloads/logstash
Elasticsearch下载地址:https://www.elastic.co/cn/downloads/elasticsearch
Kibana下载地址:https://www.elastic.co/cn/downloads/kibana
二.Linxu服务器环境准备
三台服务器操作:
1.关闭防火墙: systemctl stop firewalld && systemctl distable firewalld
2.关闭selinux: 将/etc/selinux/config中的SELINUX=enforcing修改为SELINUX=disabled
3.修改文件描述符大小: 执行 echo -e "* soft nproc 65535\n* hard nproc 65535\n* soft nofile 65535\n* hard nofile 65535\n" >> /etc/security/limits.conf
192.168.65.143操作:
1.安装jdk: yum -y install java-11-openjdk.x86_64
三.部署Logstash
192.168.65.143服务器上执行
1.安装nginx(这里就不详细阐述如何安装nginx了,也可以使用其他日志进行代替)
2.mkdir /data/ #创建文件夹并将Logstash的压缩包放到里面
3.cd /data/ #进入文件夹
4.tar -zxvf logstash-7.8.0.tar.gz //解压logstash压缩包
5.cd /data/logstash-7.8.0/config //进入到logstash配置文件夹中
6.vim mynginx.conf //创建配置文件
内容为:
input {
#从文件中读取日志信息 输送到控制台
file {
#指定日志文件
path => "/usr/local/nginx/logs/access.log"
#以json格式
codec => "json"
#类型为elasticsearch
type => "elasticsearch"
start_position => "beginning"
}
}
filter {
}
#输出到指定位置
output {
#输出进行格式化,采用Ruby库来解析日志
stdout { codec => rubydebug }
elasticsearch {
#设置elasticsearch的IP地址和端口号
hosts => ["192.168.65.144:9200"]
#设置索引
index => "es-%{+YYYY.MM.dd}"
}
#}
7./data/logstash-7.8.0/bin/logstash -f ../config/mynginx.conf #启动logstash, -f 指定配置文件
测试访问nginx服务
这时启动logstash服务的终端就会出现(因为这时还没启动Elasticsearch服务,所以这里会出现以下错误
Attempted to resurrect connection to dead ES instance, but got an error. {:url=>“http://192.168.65.144:9200/”, :error_type=>LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError, :error=>“Elasticsearch Unreachable: [http://192.168.65.144:9200/][Manticore::SocketException] 拒绝连接”})
四.启动Elasticsearch服务
192.168.65.144服务器上执行
1.useradd elasticsearch #创建elasticsearch用户
2.passwd elasticsearch #设置密码
3.su - elasticsearch #切换身份 * elasticsearch服务必须以非root身份启动,如果不是非root启动会导致以下错误:“Caused by: java.lang.RuntimeException: can not run elasticsearch as root“
4.tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz #解压压缩包(此时我是在/home/elasticsearch目录下)
5.vim /home/elasticsearch/elasticsearch-7.8.0/config/elasticsearch.yml #修改配置文件
修改以下内容:
network.host: 192.168.65.144 #取消注释并将地址修改为本机IP,如果不修改默认为["127.0.0.1", "[::1]"]
http.port: 9200 #取消注释
cluster.initial_master_nodes: ["node-1"] #取消注释 *如果这里不取消集群节点的注释启动时会出现 会出现:“ERROR: [1] bootstrap checks failed [1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured“的错误
6./home/elasticsearch/elasticsearch-7.8.0/bin/elasticsearch #启动服务
* 如果启动后出现"max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]"的错误(elasticsearch用户拥有的内存权限太小,至少需要262144)
使用root身份在/etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144
保存后再次启动
五.启动Kibana服务
192.168.65.145服务器上执行
1.mkdir /data/
2.tar -zxvf kibana-7.8.0-linux-x86_64.tar.gz //解压文件夹
3.vim /data/kibana-7.8.0-linux-x86_64/config/kibana.yml
修改以下内容:
server.port: 5601 //本机服务端口
server.host: "192.168.65.145" //本机IP
elasticsearch.hosts: ["http://192.168.65.144:9200"] //elasticsearch服务的地址
保存并退出
4./data/kibana-7.8.0-linux-x86_64/bin/kibana //启动kibana服务
六.测试访问Kibana服务
1.打开Kibana网页平台
2.使用开发工具
3.查找日志数据
查找代码为
GET /es-2020.09.23/_search
GET /es-2020.09.23/_search
{
“query”: {
“match”: {
“message”: “192.168.65.145”
}
}
}