E:elasticsearch主要用来存储日志文件
L:logstash主要用来收集日志
K:kibana主要用来展示日志
1、elasticsearch集群部署
两台服务器
192.168.188.128
192.168.188.129
(1)上传elasticsearch安装包(两部服务器同样操作)
rz elasticsearch-6.5.4.tar.gz jdk-8u191-linux-x64.tar
tar xf elasticsearch-6.5.4.tar.gz -C /usr/local
tar xf jdk-8u191-linux-x64.tar -C /usr/local
cd /usr/local && mv elasticsearch-6.5.4 elasticsearch && mv jdk1.8.0_191 java
配置Java环境变量
vim /etc/profile
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
source /etc/profile #使环境变量生效
image.png
image.png
(2)修改配置文件 elasticsearch.yml
192.168.188.128操作
cluster.name: elk #配置的集群名称
node.name: elk1 #当前配置所在机器的节点名,不设置就默认随机指定一个name列表中名字,该name列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。当创建ES集群时,保证同一集群中的cluster.name名称是相同的,node.name节点名称是不同的
node.master: true #指定该节点是否有资格被选举成为node(注意这里只是设置成有资格, 不代表该node一定就是master),默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。
node.data: true #指定该节点是否存储索引数据,默认为true。
path.data: /data/elasticsearch/data #设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:path.data: /path/to/data1,/path/to/data2
path.logs: /data/elasticsearch/logs #设置日志文件的存储路径,默认是es根目录下的logs文件夹
bootstrap.memory_lock: false #内存锁定,是否禁用交换。
bootstrap.system_call_filter: false #系统调用过滤器。
network.host: 0.0.0.0 #这个参数是用来同时设置bind_host和publish_host上面两个参数。(低版本时配置0.0.0.0,不然启动会报错。1.7.1和1.3.1版本亲测)
http.port: 9200 #设置对外服务的http端口,默认为9200。
discovery.zen.ping.unicast.hosts: [“192.168.188.128”, “192.168.188.129”] #设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。
discovery.zen.ping_timeout: 150s #设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。
discovery.zen.fd.ping_retries: 10 #节点发现重试次数。
client.transport.ping_timeout: 60s #
http.cors.enabled: true #是否允许跨源 REST 请求,用于允许head插件访问ES
http.cors.allow-origin: ““ #允许的源地址。
192.168.188.129操作
cluster.name: elk #配置的集群名称
node.name: elk2 #当前配置所在机器的节点名,不设置就默认随机指定一个name列表中名字,该name列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。当创建ES集群时,保证同一集群中的cluster.name名称是相同的,node.name节点名称是不同的
node.master: true #指定该节点是否有资格被选举成为node(注意这里只是设置成有资格, 不代表该node一定就是master),默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。
node.data: true #指定该节点是否存储索引数据,默认为true。
path.data: /data/elasticsearch/data #设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:path.data: /path/to/data1,/path/to/data2
path.logs: /data/elasticsearch/logs #设置日志文件的存储路径,默认是es根目录下的logs文件夹
bootstrap.memory_lock: false #内存锁定,是否禁用交换。
bootstrap.system_call_filter: false #系统调用过滤器。
network.host: 0.0.0.0 #这个参数是用来同时设置bind_host和publish_host上面两个参数。(低版本时配置0.0.0.0,不然启动会报错。1.7.1和1.3.1版本亲测)
http.port: 9200 #设置对外服务的http端口,默认为9200。
discovery.zen.ping.unicast.hosts: [“192.168.188.128”, “192.168.188.129”] #设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。
discovery.zen.ping_timeout: 150s #设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。
discovery.zen.fd.ping_retries: 10 #节点发现重试次数。
client.transport.ping_timeout: 60s #
http.cors.enabled: true #是否允许跨源 REST 请求,用于允许head插件访问ES
http.cors.allow-origin: “
“ #允许的源地址。
(3)useradd es && mkdir -p /data/elasticsearch/{data,logs} && chown -R es.es /usr/local/elasticsearch && chown -R es.es /data/elasticsearch #创建用户来启动es服务,并且修改属主和属组
(4)修改JVM堆大小,调优使用
[root@elasticsearch ~]# sed -i ‘s/-Xms1g/-Xms4g/‘ /usr/local/elasticsearch-6.5.4/config/jvm.options #若内存受限,可不修改
[root@elasticsearch ~]# sed -i ‘s/-Xmx1g/-Xmx4g/‘ /usr/local/elasticsearch-6.5.4/config/jvm.options #若内存受限,可不修改
(5)系统优化
1)修改/etc/security/limits.conf配置文件,将以下内容添加到配置文件中。(表示所有用户)
soft nofile 65536
hard nofile 131072
soft nproc 2048
hard nproc 4096
2)增加最大内存映射数
[root@xingdian ~]# echo “vm.max_map_count=262144” >> /etc/sysctl.conf
[root@xingdian ~]# sysctl -p
(6)启动
su - es -c “cd /usr/local/elasticsearch && bin/elasticsearch &”
两部虚拟机做上述同样操作
2、kibana部署
(1)在部署过elasticsearch集群的机器上面部署kibana集群(两台服务器同样操作)
rz kibana-6.5.4-linux-x86_64.tar.gz
tar zxf kibana-6.5.4-linux-x86_64.tar.gz -C /usr/local/
mv /usr/local/kibana-6.5.4-linux-x86_64/ /usr/local/kibana
(2)修改配置文件
vim /usr/local/kibana/config/kibana.yml
192.168.188.128服务器
server.port: 5601 #默认http开放端口
server.host: “192.168.188.128” #主机IP地址
elasticsearch.url: “http://192.168.188.128:9200“ #用来做查询的ES节点的URL
kibana.index: “.kibana” #kibana在Elasticsearch中使用索引来存储保存的searches, visualizations和dashboards,默认.kibana
192.168.188.129服务器
server.port: 5601 #默认http开放端口
server.host: “192.168.188.129” #主机IP地址
elasticsearch.url: “http://192.168.188.129:9200“ #用来做查询的ES节点的URL
kibana.index: “.kibana” #kibana在Elasticsearch中使用索引来存储保存的searches, visualizations和dashboards,默认.kibana
(3)启动设备
cd /usr/local/kibana
nohup ./bin/kibana &
(4)两台虚拟机同样操作
*拓展:nginx反向代理

1.安装
yum -y install nginx httpd-tools
2.配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;

events {
worker_connections 65535;
use epoll;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘;

access_log /var/log/nginx/access.log main;
server_names_hash_bucket_size 128;
autoindex on;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 120;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#开启限制IP连接数的时候需要使用
#limit_zone crawler $binary_remote_addr 10m;
#tips:
#upstream bakend{#定义负载均衡设备的Ip及设备状态}{
# ip_hash;
# server 127.0.0.1:9090 down;
# server 127.0.0.1:8080 weight=2;
# server 127.0.0.1:6060;
# server 127.0.0.1:7070 backup;
#}
#在需要使用负载均衡的server中增加 proxy_pass http://bakend/;
server {
listen 80;
server_name 192.168.188.128;

charset koi8-r;

access_log /var/log/nginx/host.access.log main;
access_log off;

location / {
auth_basic “Kibana”; #可以是string或off,任意string表示开启认证,off表示关闭认证。
auth_basic_user_file /etc/nginx/passwd.db; #指定存储用户名和密码的认证文件。
proxy_pass http://192.168.188.128:5601;
proxy_set_header Host $192.168.188.128:5601;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via “nginx”;
}
location /status {
stub_status on; #开启网站监控状态
access_log /var/log/nginx/kibana_status.log; #监控日志
auth_basic “NginxStatus”; }

location /head/{
auth_basic “head”;
auth_basic_user_file /etc/nginx/passwd.db;
proxy_pass http://172.16.244.25:9100;
proxy_set_header Host $host:9100;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via “nginx”;
}

redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
(4).配置授权用户和密码
htpasswd -cm /etc/nginx/passwd.db username
3、logstash部署
logstash功能:
搜集日志
定义日志格式
输出日志
服务器:192.168.188.130
(1)安装jdk,修改环境变量,详情见部署elasticsearch(Java操作)
rz logstash-7.3.2.tar.gz
tar xf logstash-7.3.2.tar.gz -C /usr/local/
mv /usr/local/logstash-7.3.2/ /usr/local/logstash
(2)logstash使用
ln -s /usr/local/logstash/bin/logstash /usr/bin/logstash
启动
logstash -e “input{ stdin{}} output{ elasticsearch{ hosts => [“192.168.188.129:9200”]} stdout{ codec=>rubydebug} }”
codec :定义编码器,用什么格式输出
(3)编辑conf文件启动
vim es.conf
input {
file{
path => “/usr/log/yum.log”
type => “system”
codec => “json”
start_position => “beginning”
}
file{
path => “/usr/local/nginx/access.log”
type => “nginx”
codec => “json”
start_position => “beginning”
}
}
output{
if [type] == “system” {
elasticsearch {
hosts => [“192.168.188.128:9200”]
index => yum.log-%{+YYYY.MM.DD}
}
}
if [type] == “nginx” {
elasticsearch {
hosts => [“192.168.188.129:9200”]
index => access.log-%{+YYYY.MM.DD}
}
}
}
参数说明:
input 上传数据 path 指定日志文件的路径
start_position 从头开始
hosts 输出到那台elasticsearch server
index 定义格式,显示到web界面上

logstash -f es.conf
访问192.168.188.130,使用elasticsearch插件观察访问结果产生的日志文件
搜集nginx日志文件
修改nginx配置文件
日志格式修改main>>>>json
log_format json ‘{“@timestamp”:”$time_iso8601”,’
‘“@version”:”1”,’
‘“client”:”$remote_addr”,’
‘“url”:”$uri”,’
‘“status”:”$status”,’
‘“domain”:”$host”,’
‘“host”:”$server_addr”,’
‘“size”:$body_bytes_sent,’
‘“responsetime”:$request_time,’
‘“referer”: “$http_referer”,’
‘“ua”: “$http_user_agent”‘
‘}’;
access_log /usr/log/nginx/nginx_access.log json;
重启nginx
systemctl restart nginx
创建logstash 需要搜集的conf文件内容
vim nginx.conf(不是nginx的主配置文件,是logstash搜集文件,名字不唯一)
input{
file{
path => “/usr/log/nginx/nginx_access.log” #nginx配置文件中指定日志位置
type => “system”
codec => “json”
start_position => “beginning”
}
}
output{
elasticsearch{
hosts => “192.168.188.129:9200”
index => “nginx_access.log-%{+YYYY.MM.DD}”
}
}
logstash -f nginx.conf
使用浏览器elasticsearch 扩展观察日志文件
参数说明:
input 上传数据 path 指定日志文件的路径
codec定义编码器 用什么格式输入和输出
start_position 从头开始
hosts 输出到那台elasticsearch server
index 定义格式,显示到web界面上
执行上传
/opt/logstash/bin/logstash -f /etc/logstash/conf.d/json.conf
搜集nginx日志文件的配置到此结束,通过kibana页面进行查看即可