环境介绍
elasticsearch:6.0.1 下载地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.tar.gz
logstash:6.0.1 下载地址:https://artifacts.elastic.co/downloads/logstash/logstash-6.0.1.tar.gz
kibana:6.0.1 下载地址:https://artifacts.elastic.co/downloads/kibana/kibana-6.0.1-linux-x86_64.tar.gz
Filebeat:6.0.1 下载地址:https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.0.1-linux-x86_64.tar.gz
我们按照顺序安装
192.168.1.1:
1. 安装JDK1.8以上,去官网复制链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" 官网复制到的安装包链接
(例如我的:wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u171-linux-x64.tar.gz)
tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local //解压到指定目录
sed -i '$a\ \n###JAVA###\nexport JAVA_HOME=/usr/local/jdk1.8.0_171\nexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar\nexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile //添加环境变量
source /etc/profile //使环境变量生效
java -version //查看是否成功
2. 安装ElasticSearch
tar -zxvf elasticsearch-6.0.1.tar.gz -C /usr/local
修改elasticsearch/config/elasticsearch.yml:
cluster.name: server-logs //自定义集群名称 node.name: node-001 //自定义节点名称 path.data: /usr/local/elasticsearch-6.0.1/data //自定义数据目录 没有则创建 path.logs: /usr/local/elasticsearch-6.0.1/logs //自定义服务日志目录 没有则创建 network.host: 192.168.1.1 //主机地址
elasticsearch不允许以root用户启动,创建新的用户
useradd elkuser
chown elkuser:elkuser /usr/local/elasticsearch-6.0.1 -R //修改es目录属主
echo "* soft nofile 65536" >> /etc/security/limits.conf //修改最大文件数 重启生效
echo "* hard nofile 131072" >> /etc/security/limits.conf //同上
ulimit -n 65536 //当前终端临时生效
echo "elkuser soft nproc 4096" >> /etc/security/limits.conf //修改最大进程数 使用elkuser用户启动即指定修改elkuser用户 重启生效
echo "elkuser hard nproc 4096" >> /etc/security/limits.conf //同上
ulimit -u 4096 //当前终端临时生效
ulimit -a //查看
echo "vm.max_map_count=262144" >> /etc/sysctl.conf //修改最大虚拟内存
sysctl -p //生效
sysctl -w vm.max_map_count=262144 //同上 当前终端临时生效
su - elkuser
/usr/local/elasticsearch-6.0.1/bin/elasticsearch -d && tailf /usr/local/elasticsearch6.0.1/logs/server-logs.log //切换至elkuser启动 -d 后台启动 追踪日志信息中有无ERROR 日志文件名为自定义集群名称
ps -ef |grep elastic //查看进程是否存在
curl -XGET '192.168.1.1:9200/?pretty' //请求查看返回数据是否与修改的一致
3. 为Elasticsearch安装一个前端页面帮助我们更好的管理服务:elasticsearch-head
此服务需要一些必要的环境运行,先安装nvm (项目地址:https://github.com/creationix/nvm)
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash //脚本安装
source ~/.nvm/nvm.sh //激活nvm
nvm install node //激活后安装node
nvm use node //安装完成后切换到该版本
npm install -g grunt-cli //安装grunt grunt是基于Node.js的项目构建工具,可以进行打包压缩、测试、执行等等的工作,head插件就是通过grunt启动 执行后会胜场node_modules目录 grunt -version检查是否安装成功
以上必要的环境安装完成后则开始安装elasticsearch-head,项目地址:https://github.com/mobz/elasticsearch-head
cd /usr/local/elasticsearch-6.0.1 && git clone https://github.com/mobz/elasticsearch-head.git //拉取项目
cd elasticsearch-head/ && npm install 安装服务
sed -i "93a\ \t\t\t\t\thostname: '*'," elasticsearch-head/Gruntfile.js //增加服务器监听地址属性
sed -i 's/localhost/192.168.1.1/g' elasticsearch-head/_site/app.js //修改连接地址 将localhost替换为自己的es服务器ip
sed -i 's/en,fr,pt,zh,zh-TW,tr,ja/zh/g' elasticsearch-head/index.html //汉化
nohup npm run start & //后台启动服务 需要在服务所在目录下执行
vim /usr/local/elasticsearch-6.0.1/config/elasticsearch.yml底部添加几条属性
http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-credentials: true
重启es后访问http://192.168.1.1:9100 出现以下画面即代表成功
4. 安装Logstash
tar zxvf logstash-6.0.1.tar.gz -C /usr/local
vim /usr/local/logstash-6.0.1/config/logstash-test.conf //创建一个配置文件
input { //数据来源
file { //文件方式 此为了收集本机的日志
path => /usr/local/nginx/logs/*.log //路径
type => "nginx-logs" //事务类型
start_position => "beginning" //从文件开头开始读取 end则为从尾部开始读取
} beats { //filebeat port => 5044 //端口 } }
filter {
过滤规则 \\此处较为复杂 略过 根据自己需求百度学习
} output { //输出到es elasticsearch { hosts => ["192.168.1.1:9200"] //es地址 index => "%{[fields][log_source]}-%{+YYYY.MM.dd}" //使用filebeat中配置的field字段来区分不同的索引 } }
nohup /usr/local/logstash-6.0.1/bin/logstash -f config/logstash-test.conf & //指定配置文件后台启动
5. 安装Kibana
tar zxvf kibana-6.0.1-linux-x86_64.tar.gz -C /usr/local
cp -dr /usr/local/kibana-6.0.1-linux-x86_64 /usr/local/kibana-6.0.1 //为后期汉化做备份,汉化不可逆
vim /usr/local/kibana-6.0.1/config/kibana.yml
server.host: "192.168.1.1" //主机地址 elasticsearch.url: "http://192.168.1.1:9200" //es主机地址
nohup /usr/local/kibana-6.0.1/bin/kibana & //后台启动
打开浏览器访问 http://192.168.1.1:5601 验证
6. 安装X-Pack
现在我们已经将所需的服务都安装完毕了,但是我们的页面只要链接就可以访问并操作未免也太不安全了,这时候我们就需要再安装一个插件 X-Pack.
X-Pack是ES的一个扩展,它将安全性,警报,监控,报告和图形功能捆绑在一起,并可以轻松管理要使用的功能
在安装之前我们需要确定我们的ELK版本,X-Pack的版本必须要和其相匹配.
包下载地址:https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-6.0.1.zip (这个在我安装时我是下载到本地上传到服务器上的,因为不知道为什么wget下载这个包速度极慢!)
①. es安装
su elkuser //es是非root启动的 安装他的插件也要切到对应用户,如果后续忘记这一步可以elasticsearch-glugin remove x-pack移除插件并删除config下的elasticsearch.keystore文件和x-pack目录重新安装
/usr/local/elasticsearch-6.0.1/bin/elasticsearch-plugin install install file:///usr/local/x-pack-6.0.1.zip //安装 指定压缩包 出现内容后连按两次y即可安装成功,之后也会在plugins目录下生成插件对应的目录.
/usr/local/elasticsearch-6.0.1/bin/x-pack/setup-passwords interactive //执行后输入y会设置elastic,kibana,logstash_system三个用户密码;该命令只可执行一次,之后你可以在Kibana的管理>用户界面更新密码
echo "http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type" >> config/elasticsearch.yml //追加一行到配置文件用于head插件的安全认证
重启es后访问 http://192.168.1.1:9100/?auth_user=elastic&auth_password=yourpasswd 登陆head插件查看
②. 修改logstash配置文件添加认证属性
vim /usr/local/logstash-6.0.1/config/logstash-test.conf //在output区域中添加es认证
output { //输出到es elasticsearch { hosts => ["192.168.1.1:9200"] index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}" user => "elastic" password => "yourpasswd" } }
重启后查看下nohup文件中的输出是否有异常信息
③. kibana安装
/usr/local/kibana-6.0.1/bin/kibana-plugin install file:///usr/local/x-pack-6.0.1.zip //切回用户同样方式在kibana安装
sed -i '$a\elasticsearch.username: "elastic"\nelasticsearch.password: "yourpasswd"' config/kibana.yml //在配置文件中添加认证属性
重启后汉化 汉化过程在项目中有详细讲解 汉化项目链接:https://github.com/anbai-inc/Kibana_Hanization
汉化后访问 http://192.168.1.1:5601 出现以下界面代表成功 (登陆时使用elastic账户,我使用了kibana账户登陆后无法连接到es获取索引和数据)
192.168.1.2:
1. 安装filebeat
tar zxvf filebeat-6.0.1-linux-x86_64.tar.gz -C /usr/local
mv filebeat-6.0.1-linux-x86_64 filebeat //重命名
vim filebeat.yml
filebeat.prospectors: - type: log enabled: true //设置为true时该配置生效 paths: - /usr/local/nginx/logs/www_access.log //日志路径,如果有多个,继续往下写 fields: log_source: test_nginx_www //自定义属性,用来区分日志,接上述logstash中index属性配置 - type: log enabled: true paths: - /usr/local/nginx/logs/api_access.log fields: log_source: test_nginx_api //如果要收集类似java的多行为一条信息的日志类型可百度一下multiline属性,此属性在filebeat和logstash中都可以配置 output.logstash: //输出到logstash 如果不需要logstash做更加详细的数据格式化也可以选择直接输出到es hosts: ["192.168.1.1:5044"] index: "filebeat" //这个index的值对应@metadata[beat]的值,可以自定义,我在此项目中没有用到该属性
nohup ./filebeat -e -c filebeat.yml > filebeat.log & //后台启动filebeat并将服务信息输出到filebeat.log中, 如果想看详细的输出到logstash的数据流可以不输出到文件 -d "publish"然后查看nohup文件
至于为什么logstash就可以收集我们还要选择filebeat呢?
这是因为logstash的功能多但是也非常的吃资源,首先就是必须要保留出1G的内存给它用,但是我们被收集日志的服务器本来就是在给我们的用户提供服务,负载已经很大了,我们再增加一个logstash只会使该服务器更不稳定
而filebeat是一个轻量级的日志传输工具,它的存在弥补了logstash的缺点,理论上如果只是简单的收集日志并展示的话仅file+es+kibana就足够实现了,当我们需要使用更加复杂的功能时,则可以使用该项目的方式将日志推送到logstash再处理然后统一传输给es。