Chinaunix首页 | 论坛 | 博客
  • 博客访问: 606098
  • 博文数量: 74
  • 博客积分: 1845
  • 博客等级: 上尉
  • 技术积分: 731
  • 用 户 组: 普通用户
  • 注册时间: 2005-03-14 20:49
文章分类

全部博文(74)

文章存档

2017年(3)

2016年(7)

2015年(4)

2014年(6)

2013年(7)

2012年(10)

2011年(10)

2010年(4)

2009年(5)

2008年(4)

2007年(7)

2006年(6)

2005年(1)

分类: 大数据

2017-08-03 16:02:09

参考:


使用filebeat在服务器端收集日志(定义字段,做多行合并处理)-传给logstash,通过filebeat定义的字段输出给elasticsearch,elasticsearch通过logstash的定义自动建立索引。最后通过kibana展示。使用es的search-gruad插件做ssl用户认证。


大致的架构是 filebeat-logstash-elasticsearch-kibana


除了filebeat,其他都在一台虚拟机上,没做集群。大概的环境流程如下:




 


二、部署安装
(一)、部署logstash+elasticsearch+kibana(持久、检索、展示层)


1、部署jdk,配置环境变量。


2、系统调优


[webapp@localhost ~]$ vim /etc/sysctl.conf


fs.file-max=65536


vm.max_map_count = 262144




sysctl -p


[webapp@localhost ~]$ vim /etc/security/limits.conf


* soft nofile 65535


* hard nofile 131072


* soft nproc 2048


* hard nproc 4096




4.1、配置logstash的配置文件


[root@elk config]# cat logstash_product.conf 
input {
  beats {
    port => 5044                
  }


}
filter{
    grok {
        match => ["message", "%{TIMESTAMP_ISO8601:logdate}"]
    }
    date {
        match => ["logdate", "yyyy-MM-dd HH:mm:ss,SSS"]
        target => "@timestamp"
    }                                                         #这段是把默认的时间戳变更为收集的系统日志时间。
    mutate {
        remove_field => ["logdate"]
    }
}
output {
  elasticsearch {
    hosts => ["localhost:9200"]                               #不要用ip,否则会报错。 
    ssl => true
    ssl_certificate_verification => true
    truststore => "/home/elk/elasticsearch-5.4.0/config/truststore.jks"
    truststore_password => changeit                                       #上面4行是使用search-gruad插件必须配置的。
    manage_template => false
    index => "%{[fields][project]}-%{[fields][app_name]}-%{+YYYY.MM.dd}"   #es生成索引对应的名字。
    document_type => "%{[@metadata][type]}"
    user => "admin"
    password => "admin"
  }
#stdout{
#   codec =>rubydebug{
#   metadata => true
#}
#}
}
         






启动命令


nohup /home/elk/logstash-5.4.1/bin/logstash -f /home/elk/logstash-5.4.1/config/logstash_product.conf &


4.3、配置elasticsearch的配置文件


[elasticsearch@elk config]$ cat elasticsearch.yml
node.name: elk1
path.data: /home/elk/elk_data
path.logs: /home/elk/elk_data
network.host: 0.0.0.0
http.port: 9200
bootstrap.system_call_filter: false 
http.cors.enabled: true
http.cors.allow-origin: "*"




######## Start Search Guard Demo Configuration ########
searchguard.ssl.transport.keystore_filepath: keystore.jks
searchguard.ssl.transport.truststore_filepath: truststore.jks
searchguard.ssl.transport.enforce_hostname_verification: false
searchguard.ssl.http.enabled: true
searchguard.ssl.http.keystore_filepath: keystore.jks
searchguard.ssl.http.truststore_filepath: truststore.jks
searchguard.authcz.admin_dn:
  - CN=kirk,OU=client,O=client,L=test, C=de


cluster.name: searchguard_demo
#network.host: 172.168.1.202
######## End Search Guard Demo Configuration ########


#号中的这段是安装search-gruad插件时自动添加的,需要注释掉最后的network.host。


启动elasticsearch不能用root用户,各种限制打开


下面是es的一些优化。


找到这行 ES_JAVA_OPTS="$(parse_jvm_options "$ES_JVM_OPTIONS") $ES_JAVA_OPTS"
修改成   ES_JAVA_OPTS="-Xms8g -Xmx8g"


最好是机器内存大小的一半,但是不要超过32个G。


> vim /etc/security/limits.conf
...
elasticsearch hard nofile 65536  # 针对 max file descriptors
elasticsearch soft nproc 2048    # 针对 max number of threads
 
> vim /etc/sysctl.conf
...
vm.max_map_count=262144          # 针对 max virtual memory areas
 
> vim /etc/elasticsearch/elasticsearch.yml
 
...
bootstrap.system_call_filter: false   # 针对 system call filters failed to install




启动 进入bin下 sh elasticsearch -d
#######################################################################################


安装search-gruad插件做ssl用户认证。  


进入es目录 执行/bin/elasticsearch-plugin install -b com.floragunn:search-guard-5:5.4.0-12 (要和es的版本对应)


cd into /plugins/search-guard-/tools


./install_demo_configuration.sh


启动elasticsearch之后再执行下面的脚本


./sgadmin_demo.sh          (重启es要启动es之后再执行这个脚本,做认证。)


es装了这个插件,kibana也要装,logstash要改配置


cd /home/elk/elasticsearch-5.4.0/plugins/search-guard-5/sgconfig   用户名密码认证在这路径下


1、sg_config.yml:主配置文件不需要做改动。


2、sg_internal_users.yml:本地用户文件,定义用户密码以及对应的权限。


3、sg_roles.yml:权限配置文件


4、sg_roles_mapping.yml:定义用户的映射关系


5、sg_action_groups.yml:定义权限




4.5、配置Kibana的配置文件


[webapp@localhost ~]$ cd /home/elk/kibana-5.4.0-linux-x86_64/config


[webapp@localhost config]$ vim kibana.yml


server.port: 5601


server.host: "172.168.1.202"








elasticsearch.url: ""








elasticsearch.username: "kibanaserver"
elasticsearch.password: "kibanaserver"








elasticsearch.ssl.verificationMode: none




启动kibana 
 
node/bin/node --no-warnings src/cli &
  
部署kibana的search-gruad插件  -docs/blob/master/kibana.md 
  
先确定 elasticsearch.yml的配置里


searchguard.ssl.http.enabled: true


安装 bin/kibana-plugin install -kibana-plugin/releases/download/v5.4.0/searchguard-kibana-5.4.0-2.zip






2、配置filebeat配置文件


解压缩之后,修改配置文件,启动。






#=========================== Filebeat prospectors =============================


filebeat.prospectors:






- input_type: log
  paths: ["/home/appuser/server/horizon-stapt/logs/Sapphire.log"]
  fields:
    tenant: xinjiang
    project: horizon
    app_name: stapt
    apptype: java
    env: product
  document_type: stapt
  multiline:
          pattern: '^[[:space:]]+|^Caused by:'
          negate: false
          match: after




#----------------------------- Logstash output --------------------------------
#output.logstash:
  # The Logstash hosts
 # hosts: ["localhost:5044"]
output.logstash:


  hosts: ["172.168.1.202:5044"]




#################################################################################


2个配置项,一个是收集日志定义字段多行合并,一个是输出到logstash


替换之前配置的多行合并项。


sed -i 's/\^\[\[:space:\]\]/^[[:space:]]+|^Caused by:/g' filebeat.yml


3、启动filebeat


[webapp@localhost filebeat-5.2.0-linux-x86_64]$ nohup ./filebeat -c filebeat.yml > /dev/null &








5、通过web展示,访问






6、在kibana里展示nginx访问ip的top10




配置nginx日志格式
nginx.conf里添加


 log_format  access  '$remote_addr $remote_port $remote_user $time_iso8601 $status $body_bytes_sent '
                                   '$bytes_sent $request_length $request_time '
                                   '"$request" "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
  
在logstash里添加(整个logstash配置)



input {
  beats {
    port => 5044                
  }


}
filter{
    grok {
        match => { "message" => "%{IPORHOST:remote_addr} %{NUMBER:remote_port}  %{NUMBER:body_bytes_sent} %{NUMBER:bytes_sent} %{NUMBER:request_length} %{NUMBER:request_time} \"%{WORD:request_method} %{DATA:request_url} %{DATA:http_version}\" \"%{DATA:http_referer}\"\"%{DATA:user_agent}\" \"%{DATA:http_x_forwarded_for}\"" }
    }


    geoip {
        source  => "remote_addr"
        target  => "geoip"
        add_tag => [ "geoip" ]
    } 
    date {
        match => ["logdate", "yyyy-MM-dd HH:mm:ss,SSS"]
        target => "@timestamp"
    }                                                         #这段是把默认的时间戳变更为收集的系统日志时间。
    mutate {
        remove_field => ["logdate"]
    }
}
output {
  elasticsearch {
    hosts => ["localhost:9200"]                               #不要用ip,否则会报错。 
    ssl => true
    ssl_certificate_verification => true
    truststore => "/home/elk/elasticsearch-5.4.0/config/truststore.jks"
    truststore_password => changeit                                       #上面4行是使用search-gruad插件必须配置的。
    manage_template => false
    index => "%{[fields][project]}-%{[fields][app_name]}-%{+YYYY.MM.dd}"   #es生成索引对应的名字。
    document_type => "%{[@metadata][type]}"
    user => "admin"
    password => "admin"
  }
#stdout{
#   codec =>rubydebug{
#   metadata => true
#}
#}
}
阅读(1513) | 评论(0) | 转发(0) |
0

上一篇:使用searchguard插件后定期清理es索引。

下一篇:没有了

给主人留下些什么吧!~~