***前言;安装jdk1.8并设置JAVA_HOME环境变量***
一、安装ES(本次安装使用三台虚拟机,提前下载或wget命令下载elasticsearch、logstash、kibana、filebeat到新建文件夹elk目录)
1、切换到root用户 并解压es:tar -zxvf elasticsearch-5.6.3.tar.gz
2、修改es文件夹的用户和用户组为普通用户:chown -R world:world elasticsearch-5.6.3
3、切换到普通用户(elasticsearch不能用root用户启动,所有修改es文件夹的用户和用户组为普通用户,并用普通用户启动)
4、修改内存参数:config目录下的jvm.options文件中的Xms和Xmx都为128m(根据机器配置修改)
5、修改es配置文件config目录下elasticsearch.yml(data和logs目录是新建的,更多配置请参加文档:es配置参数详解):
# 集群名称
cluster.name: insurance-elasticSearch
# 节点名称
node.name: node-1
# 设置索引的分片数,不用配置,默认为5
#index.number_of_shards: 5
# 设置索引的副本数,不用配置,默认为1:
#index.number_of_replicas: 1
# 存储数据目录
path.data: /usr/local/elk/elasticsearch-5.6.3/data
# log日志目录
path.logs: /usr/local/elk/elasticsearch-5.6.3/logs
# 是否允许内存交换:为true表示不允许内存交换
bootstrap.memory_lock: true
# 监听地址,以下地址表示所有机器都可以访问,感觉没啥用,最好配置成本机ip地址
network.host: 0.0.0.0
# 端口号
http.port: 9200
# 设置集群中master节点的初始列表,可以通过这些节点发现新加入集群的节点
discovery.zen.ping.unicast.hosts: ["world01", "world02", "world03"]
# 配置主节点数量的最小值,主节点数量小于该值集群将不可用,一般设置为n/2+1
discovery.zen.minimum_master_nodes: 2
# 增加新的参数,这样head插件可以访问es(跨域访问),如果安装了x-pack则需要配置响应消息头authorization和content-type
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "authorization,content-type"
6、对应修改第二台机器和第三台机器的配置文件(对应修改node.name、network.host即可,还有新建data和logs目录)
7、启动es:./bin/elasticsearch -d (-d表示后台启动,启动报错请按照第8点修改)
8、es启动常见错误
1)Java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMPandCONFIG_SECCOMP_FILTER
linux系统版本低,es5.0版本要求内核3.5以上版本,这只是一个警告
2)max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
无法创建本地文件,es要求用户可创建的最大文件数是65535,root用户下修改/etc/security/limits.conf,添加以下两行,修改完需要exit退出终端重新连接后才生效,然后执行ulimit -n 看看是不是65535
# es启动时报world用户可创建的最大文件数至少要65535,故在此配置以下两行
elk soft nofile 65535
elk hard nofile 131072
3)memory locking requested for elasticsearch process but memory is not locked
锁定内存失败,world用户下执行ulimit -a显示max locked memory只有64k,root用户下修改/etc/security/limits.conf,添加以下两行,修改完需要exit退出终端重新连接后才生效,然后执行ulimit -l 看看是不是unlimited(也可临时修改:world用户下执行 ulimit -l unlimited)
# 修改world用户的最大可锁定内存为无限制
elk soft memlock unlimited
elk hard memlock unlimited
4)max number of threads [1024] for user [world] is too low, increase to at least [2048]
无法创建本地线程,world用户可创建的最大线程数太小,root用户下修改/etc/security/limits.d/90-nproc.conf,* soft nproc 1024 修改为 * soft nproc 2048,,修改完需要exit退出终端重新连接后才生效,然后执行ulimit -u 看看是不是2048
5)max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
一个进程可以拥有的VMA(虚拟内存区域)的数量太小,root用户下修改/etc/sysctl.conf,添加vm.max_map_count=262144,修改完执行sysctl -p 生效
6)system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
因为Centos6不支持SecComp,而ES5.2.1默认bootstrap.system_call_filter为true进行检测,所以导致检测失败,失败后直接导致ES不能启动,修改elasticsearch.yml在bootstrap.memory_lock下边增加一行:
bootstrap.system_call_filter: false
7)maybe these locations are not writable or multiple nodes were started without increasing
#信息:当前服务已启动,解决办法
netstat -anp|grep 9200
kill -9 进程id
二、安装x-pack插件(x-pack集成了安全,警告,监视,图形和报告等一系列插件;注意需要关闭ES服务器,如果已经启动的话,否则安装失败,安装成功之后再启动ES服务器即可)
1、在线安装x-pack:./bin/elasticsearch-plugin install x-pack(安装过程中需要授予X-Pack附加权限)
2、删除x-pack插件:./bin/elasticsearch-plugin remove x-pack
3、在线安装x-pack默认只有一个月的试用期,通过以下方法可无限修改过期时间:
1)上传破解版x-pack包到服务器然后安装:./bin/elasticsearch-plugin install file:/home/world/x-pack-5.6.3.zip(下载包目录下有破解好的x-pack)
2)如何破解请参见elasticsearch的x-pack破解文档
3)官网订阅目录下注册后会发送邮件,在邮件里指定的下载地址里下载license(json文件),
下载后修改json文件中type的值为platinum(表示白金版),还有expiry_date_in_millis过期毫秒值(这个随机指定)
下载包目录下有修改好的license:mengjun-zuo-5572adc1-b456-4ed6-ace7-93d0efb6b126-v5.json(过期时间是2050年截至)
修改完上传到服务器(es的非破解版下载地址:https://artifacts.elastic.co/downloads/elasticsearch-plugins/x-pack/x-pack-5.6.3.zip)
4、x-pack安装成功后es访问就会受到控制,默认的用户名密码是 elastic changeme
5、更新license:curl -X PUT -u elastic:changeme 'http://10.143.116.15:9200/_xpack/license?acknowledge=true' -H 'Content-Type:application/json' -d '@/home/10.143.116.15/mengjun-zuo-5572adc1-b456-4ed6-ace7-93d0efb6b126-v5.json'
6、查看x-pack的license:curl -XGET -u elastic:changeme 'http://10.143.116.15:9200/_xpack/license'
7、修改elasticsearch的elastic用户的密码:curl -X PUT -u elastic:changeme 'world01:9200/_xpack/security/user/elastic/_password' -d '{"password":"elastic"}'
8、声明:请勿用于商业用途,否则后果自负。如需使用黄金或白金版X-Pack请购买正版
三、安装es head插件(只需要在一个es节点安装head插件即可,也可安装到多个节点或单独安装到一台机器上)
1、自己下载或wget下载head插件到/usr/local/elk/目录下(下载地址:https://github.com/mobz/elasticsearch-head)
2、root用户下或用sudo修改文件夹的用户和用户组:sudo chown -R world:world elasticsearch-head-master
3、安装node(由于head插件本质上还是一个nodejs的工程,因此需要安装node)
4、下载nodejs:wget https://nodejs.org/dist/v6.11.4/node-v6.11.4-linux-x64.tar.xz
5、下载下来的包是xz格式的,linux无法识别,还需安装xz:yum -y install xz
6、解压nodejs安装包:xz -d node-v6.11.4-linux-x64.tar.xz,解包:tar -xvf node-v6.11.4-linux-x64.tar
7、由于bin目录下npm指定的npm-cli.js文件的开头写的/usr/bin目录下的node文件,所有在/usr/bin目录下创建一个node软链接:ln -s /usr/local/elk/node-v6.11.4-linux-x64/bin/node /usr/bin/node
8、测试node是否生效:在node的bin目录下执行 ./node -v 和 ./npm -v
9、进入head目录:cd elasticsearch-head-master
10、安装所有npm模块包括grunt:../node-v6.11.4-linux-x64/bin/npm install (可能部分模块安装失败,但是貌似不影响;grunt是一个很方便的构建工具,可以进行打包压缩、测试、执行等等的工作,5.0里的head插件就是通过grunt启动的。因此需要安装一下grunt)
10、检查grunt是否安装成功:./node_modules/grunt/bin/grunt -version (打印 grunt-cli v1.2.0 和 grunt v1.0.1)
11、在head目录下用grunt命令启动head服务:nohup ./node_modules/grunt/bin/grunt server &(必须是在head目录下,因为head目录才有Gruntfile.js文件)
12、访问head:http://192.168.1.110:9100,然后连接es(如果es安装了x-pack则访问地址是:http://192.168.1.110:9100/?auth_user=elastic&auth_password=elastic)
四、elasticsearch的rest API(-u 参数是用户名,每次回车后都要输入密码,或者这么写: -u elastic:elastic )
1、查看集群健康状况:curl -X get -u elastic 'localhost:9200/_cat/health?v'
2、查看集群节点信息:curl -X get -u elastic 'localhost:9200/_cat/nodes?v'
3、查看索引: curl -X get -u elastic 'localhost:9200/_cat/indices?v'
4、新建索引:curl -X PUT -u elastic 'localhost:9200/customer?pretty'
5、在customer索引下创建或者替换一个文档:curl -X PUT -u elastic 'localhost:9200/customer/external/1?pretty' -d '{"name": "john doe"}'(如果customer索引不存在,Elasticsearch将会自动地创建这个索引;如果不指定id,则换成POST方式,es会随机一个id)
6、查询customer索引下打的文档1:curl -X GET -u elastic 'localhost:9200/customer/external/1?pretty'
7、删除customer索引:curl -X DELETE -u elastic 'localhost:9200/customer?pretty'
8、更新customer索引下的文档1:curl -X POST -u elastic 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": {"name": "john doe", "age": 20}}'
9、删除文档:curl -X DELETE -u elastic 'localhost:9200/customer/external/2?pretty'
10、删除符合条件的文档;curl -X DELETE -u elastic 'localhost:9200/customer/external/_query?pretty' -d '{"name": "john doe"}'
11、批处理bulk API 和 查询 等更多API请参见 elasticsearch rest API文档
五、安装kibana(nodejs编写,其实就是node服务)
1、root用户下解压tar包到elk目录:tar -zxvf kibana-5.6.3-linux-x86_64.tar.gz
2、在主目录下安装x-pack:./bin/kibana-plugin install x-pack
3、由于在线安装x-pack插件老是瞎提示证书过期,一气之下下载kibana的x-pack插件本地安装:./bin/kibana-plugin install file:/usr/local/elk/x-pack-5.6.3.zip
下载地址:https://artifacts.elastic.co/downloads/kibana-plugins/x-pack/x-pack-5.6.3.zip(下载包目录下有下载好的)
5、修改配置文件:vim vim config/kibana.yml
# 端口号
server.port: 5601
# 主机名
server.host: "world04"
# 服务器名称
server.name: "world04"
# es服务地址
elasticsearch.url: "http://world01:9200"
# kibana索引名称
kibana.index: ".kibana"
# 连接es的用户名密码
elasticsearch.username: "elastic"
elasticsearch.password: "elastic"
# 日志路径(注意要新建logs目录,这里记不记日志视情况而定,在页面上每次点击都会记录日志,日志量很大)
#logging.dest: /usr/local/elk/kibana-5.6.3-linux-x86_64/logs/kibana.log
6、启动kibana:nohup ./bin/kibana > /dev/null & (日志记录在logs下,这里就没必要记录nohup日志了)
7、查看kibana进程:ps -ef | grep node
8、访问kibana:http://10.134.203.167:5601 由于kibana安装了x-pack,需要认证,用户名密码就是es的认证用户名密码:elastic elastic
六、安装logstash(本次使用三台虚拟机安装logstash)
1、root用户下解压tar包到elk目录:tar -zxvf logstash-5.6.3.tar.gz
2、修改config目录下的jvm.options文件中的Xms和Xmx都为128m(根据机器配置修改)
3、以控制台作为input和output测试logtash:./bin/logstash -e 'input { stdin {} } output { stdout {} }'
4、修改config目录下logstash.yml,其它配置默认即可:(5.0之前的启动参数在5.0之后可以通过此配置文件的方式配置,更过配置请参见文档:logstash命令行参数和配置参数详解)
# 节点名称
node.name: logstash-node-1
# 配置文件路径
path.config: /usr/local/elk/logstash-5.6.3/conf
5、主目录下新建conf目录,此目录用户存放各输入输出配置文件,在此目录下新建test-project.conf用于测试项目的配置
# 输入源
input {
beats {
port => 5044
}
}
# 过滤
filter {
}
# 输出目的地
output {
if [type] == "test-project" {
elasticsearch {
hosts => ["world01:9200","world02:9200","world03:9200"]
index => "test-project.%{+YYYY-MM-dd}"
document_type => "debug"
user => elastic
password => elastic
}
}
if [type] == "test-project_error" {
elasticsearch {
hosts => ["world01:9200","world02:9200","world03:9200"]
index => "test-project_error.%{+YYYY-MM-dd}"
document_type => "error"
user => elastic
password => elastic
}
}
}
6、其它两台机器同理,注意修改logstash.yml中的node.name
7、启动logstash:nohup ./bin/logstash > /dev/null & (日志记录在logs目录下)
七、安装filebeat
1、root用户下解压tar包到elk目录:tar -zxvf filebeat-5.6.3-linux-x86_64
2、修改配置文件filebeat.yml(另外要注释掉其它的output,更过配置请参见文档:beats通用配置参数详解):
# input
- input_type: log
paths:
- /home/world/test/test-project.*.log
document_type: test-project
- input_type: log
paths:
- /home/world/test/test-project_error.*.log
document_type: test-project_error
# output
output.logstash:
hosts: ["world01:5044","world02:5044","world03:5044"]
# 输出插件将负载均衡的发布事件到所有logstash主机
loadbalance: true
3、启动filebeat:nohup ./filebeat > /dev/null & (日志记录在logs目录下)
4、在测试目录/home/world/test下新建文件test-project.2017-10-30.log和test-project_error.2017-10-30.log,并追加内容到文件,连接kibana新建索引test-project.*和test-project_error.*进行测试
八、参考资料
http://www.cnblogs.com/InCsharp/p/6810089.html elk多种架构
https://kibana.logstash.es/content/index.html elk中文指南
http://www.ywnds.com/?p=9776 elk 5.x版本安装文档(filebeat,kafka或redis,logstash,elasticsearch,kibana,nginx日志源)
https://my.oschina.net/gibsonxue/blog/1517717 elasticsearch的x-pack插件破解文档
http://blog.csdn.net/gamer_gyt/article/details/59077189 本编文档只需要关注es配置文件各参数的详解
http://blog.csdn.net/gamer_gyt/article/details/67773190 logstash命令行参数和配置参数详解
http://www.ttlsa.com/elk/elk-beats-common-configure-section-describe/ beats通用配置参数详解
http://blog.csdn.net/cnweike/article/details/33736429 elasticsearch基础教程文档(rest api)
http://blog.csdn.net/houzhe_adore/article/details/51315036 Logstash性能优化文档