参考链接:https://www.cnblogs.com/wzxmt/p/10159951.html
//系统以及环境
| hostname | ip地址 | 系统 | 备注|
|----------|-------|--|---|-----|
| elk-server | 192.168.56.31/24 | CentOS Linux release 7.8.2003 (Core)| ELK服务端,接收日志,提供日志搜索服务 |
| elk-client | 192.168.56.32/24 | CentOS Linux release 7.8.2003 (Core)| Nginx服务端,Tomcat服务端产生的访问日志通过filebeat上报到Logstash |
//SELinux设置
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
//elk-server 以及elk-client 一样操作,更新系统并安装需要的包
# yum -y install wget
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo
# yum clean all
# yum update -y //可不选
# yum -y install vim lsof
# echo "192.168.56.31 elk-server" >> /etc/hosts
# echo "192.168.56.32 elk-client" >> /etc/hosts
# reboot
//分别安装jdk-11.0.7_linux-x64_bin(如果elk-client不使用tomcat可以不安装)
# mkdir /usr/java
# tar xf jdk-11.0.7_linux-x64_bin.tar.gz -C /usr/java/
# echo "JAVA_HOME=/usr/java/jdk-11.0.7" >> /etc/profile
# echo "PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin" >> /etc/profile
# echo "CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib/tools.jar:$JAVA_HOME%/lib/dt.jar:$JAVA_HOME/lib:$JAVA_HOME/jre/lib" >> /etc/profile
# echo "export JAVA_HOME PATH CLASSPATH" >> /etc/profile
# source /etc/profile && java -version
//在elk-server 操作
# firewall-cmd --zone=public --permanent --add-port=5601/tcp
# firewall-cmd --zone=public --permanent --add-port=5044/tcp
# firewall-cmd --reload
//下载并解压包
# wget "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz"
# wget "https://artifacts.elastic.co/downloads/logstash/logstash-7.6.2.tar.gz"
# wget "https://artifacts.elastic.co/downloads/kibana/kibana-7.6.2-linux-x86_64.tar.gz"
# mkdir /usr/local/elk
# tar zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz -C /usr/local/elk/
# tar zxvf logstash-7.6.2.tar.gz -C /usr/local/elk/
# tar zxvf kibana-7.6.2-linux-x86_64.tar.gz -C /usr/local/elk/
# ln -s /usr/local/elk/elasticsearch-7.6.2 /usr/local/elk/elasticsearch
# ln -s /usr/local/elk/logstash-7.6.2 /usr/local/elk/logstash
# ln -s /usr/local/elk/kibana-7.6.2-linux-x86_64 /usr/local/elk/kibana
//创建用户
# useradd elk
# passwd elk //为了安全,给elk增加密码
# chown -R elk:elk /usr/local/elk
//部署配置elasticsearch
# touch /usr/local/elk/elasticsearch/logs/elasticsearch.log
# cd /usr/local/elk/elasticsearch
# vim config/elasticsearch.yml //编辑配置文件制定集群/节点名称
====分割线====
17 cluster.name: my-application
23 node.name: node-1
====分割线====
# su elk
$ cd /usr/local/elk/elasticsearch/bin
$ ./elasticsearch -d
$ tail -f /usr/local/elk/elasticsearch/logs/elasticsearch.log //查看运行log
$ curl 127.0.0.1:9200 //检查服务响应
$ exit //回到root用户继续操作
//部署配置logstash
# cd /usr/local/elk/logstash
//创建文件 default.conf,内容如下:
====分割线====
# 监听5044端口作为输入
input {
beats {
port => "5044"
}
}
# 数据过滤
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
geoip {
source => "clientip"
}
}
# 输出配置为本机的9200端口,这是ElasticSerach服务的监听端口
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
}
}
====分割线====
# nohup bin/logstash -f default.conf --config.reload.automatic & //启动logstash
# ps aux|grep logstash //进程检查
# tail -f logs/logstash-plain.log //log查看
//部署配置Kibana
# chown -R elk:elk kibana-7.6.2-linux-x86_64
# cd /usr/local/elk/kibana/config/
# vim kibana.yml //在第七行,改成如下内容
====分割线====
server.host: "192.168.56.31"
====分割线====
# cd /usr/local/elk/kibana/
# su elk
$ nohup bin/kibana & //启动服务
$ tail -f nohup.out
//在浏览器访问 http://192.168.56.31:5601 出现如下界面,点击 “explore on my own”
//在elk-server 操作
//汉化 kibana (可选)
# yum install -y git //安装git
# git clone https://github.com/anbai-inc/Kibana_Hanization.git
# cp Kibana_Hanization/translations/zh-CN.json /usr/local/elk/kibana/src/legacy/core_plugins/kibana/
# cd /usr/local/elk/
# vim kibana/config/kibana.yml //修改为如下内容
====分割线====
115 i18n.locale: "zh-CN"
====分割线====
//重启kibana
# cd /usr/local/elk/kibana/
# su elk
$ lsof -i:5601 //查出进程号,kill掉
$ nohup bin/kibana & //启动服务
$ tail -f nohup.out
//在elk-server 操作
//kibana 增强安全性(配置基于角色的访问控制)
//官方参考 https://www.elastic.co/cn/blog/getting-started-with-elasticsearch-security
# cd /usr/local/elk/elasticsearch
# su elk
//生成tls证书
$ bin/elasticsearch-certutil cert -out config/elastic-certificates.p12 -pass ""
//编辑文件 config/elasticsearch.yml //将下列代码行粘贴到文件末尾
$ vim config/elasticsearch.yml
====分割线====
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
====分割线====
//重新启动(如果已经运行)elasticsearch
$ /usr/local/elk/elasticsearch/bin/elasticsearch -d
$ bin/elasticsearch-setup-passwords auto //为不同的内部堆栈用户生成随机密码,并且记住这些密码
//接下来为 Kibana 用户添加密码. setup-passwords 命令的输出内容中找到密码
$ cd /usr/local/elk/kibana/
$ vim config/kibana.yml //编辑文件kibana.yml 修改以下内容
====分割线====
46 elasticsearch.username: "kibana"
47 elasticsearch.password: "RQaSMCvi8CaQntJIpdfV"
====分割线====
//重新启动(如果已经运行)kibana
$ nohup bin/kibana &
//浏览器访问 kibana http://192.168.56.31:5601,使用 elastic 用户与密码登录。(如下图)
//登录后创建第一个角色 read_logs 选择 索引权限 字段并选择 日志索引。然后我们将其指定为 read 权限 并创建角色
//再创建另一个名为 read_flight 的角色,航班索引分配 read 权限
//然后创建用户并为他们分配这两个角色,将这名用户命名为 flight_user,并设置密码。无需设置全名和电子邮件地址。需要为这名用户分配 read_flight 角色,而且还需要为其分配 kibana_user 角色(因为该用户将会查看 Kibana 中的数据),点击创建用户。
//到此,步骤完成。登录对应的用户即可使用/查看对应角色所有的log和功能
//kibana 增加安全认证(可选,注意该方式与上面增加的安全方式冲突)(使用Nginx反向代理)
# firewall-cmd --zone=public --permanent --add-port=80/tcp
# firewall-cmd --reload
# yum -y install epel-release //添加额外存储库
# yum -y install nginx //安装Nginx
//编辑 nginx.conf 文件修改配置如下:
# vim nginx.conf //主要是在48到52 添加了一些内容,意义是端口转发,其他可以按需配置.
====分割线====
47 location / {
48 index index.html index.htm;
49 proxy_set_header Host $host;
50 proxy_pass http://127.0.0.1:5601;
51 auth_basic "Kibana login auth";
52 auth_basic_user_file /etc/nginx/kibana_httppasswd;
53 }
====分割线====
# yum install httpd-tools //安装生成密码工具
# htpasswd -bc /etc/nginx/kibana_httppasswd admin admin //生成密码文件
# chmod 400 /etc/nginx/kibana_httppasswd //设置权限
# vim /usr/local/elk/kibana/config/kibana.yml //修改kibana.yml,把host修改为127.0.0.1
====分割线====
server.host: "127.0.0.1"
====分割线====
# nginx -t //配置文件语法测试
# chown nginx:nginx kibana_httppasswd
//重启kibana与nginx
# systemctl restart nginx
# cd /usr/local/elk/kibana/
# ps aux|grep node //要重启kibana,先使用ps 找到其pid,kill掉再启动
# su elk
$ nohup bin/kibana & //启动服务
//在浏览器输入 http://192.168.56.31 然后输入设置好的帐密,即可正常使用
//在elk-server 操作
//所有内容部署完毕之后,重启机器需要启动的服务(即开机启动项)
# /bin/su - elk -c "/usr/local/elk/elasticsearch/bin/elasticsearch -d"
# /bin/su - elk -c "nohup /usr/local/elk/logstash/bin/logstash -f /usr/local/elk/logstash/default.conf --config.reload.automatic &"
# /bin/su - elk -c "nohup /usr/local/elk/kibana/bin/kibana &"
# systemctl start nginx
//接下来在 elk-client操作
# firewall-cmd --zone=public --permanent --add-port=80/tcp
# firewall-cmd --reload
# yum -y install epel-release //添加额外存储库
# yum -y install nginx //安装Nginx
# systemctl start nginx
# lsof -i:80 //服务端口检查
//部署 filebeat
# wget "https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.6.2-linux-x86_64.tar.gz"
# mkdir /usr/local/elk
# tar zxvf filebeat-7.6.2-linux-x86_64.tar.gz -C /usr/local/elk/
# ln -s /usr/local/elk/filebeat-7.6.2-linux-x86_64 /usr/local/elk/filebeat
# cd /usr/local/elk/filebeat
//编辑文件filebeat.yml,修改为以下内容:
# vim filebeat.yml
====分割线====
24 enabled: true
28 - /var/log/nginx/*.log
148 #output.elasticsearch:
150 # hosts: ["localhost:9200"]
161 output.logstash:
163 hosts: ["192.168.56.31:5044"]
====分割线====
# nohup ./filebeat -e -c filebeat.yml &> /dev/null & //启动filebeat
# curl 127.0.0.1:80 //多次访问Nginx制造一些log
//在浏览器上打开 kibana 页面 http://192.168.56.31:5601/ 选择 Discover ,输入logstash-*,点击”Next step”,下一步 选择 @timestamp,再点击“Create index pattern”,页面提示创建Index Patterns成功
//继续在 elk-client操作(可选)
# cd /usr/local/elk/filebeat
# ./filebeat modules list //查看modules列表
# ./filebeat modules enable nginx //通过启动modules简化操作
# ./filebeat modules disable nginx //禁用
//在elk-client 操作
//所有内容部署完毕之后,重启机器需要启动的服务(即开机启动项)
# nohup /usr/local/elk/filebeat/filebeat -e -c /usr/local/elk/filebeat/filebeat.yml &>/dev/null &
# systemctl start tomcat //如有
# systemctl start nginx
到此,ELK完成部署。
另:最好是所有的elk部件均使用elk用户去运行。
有新增的log服务器监控需求时,安装elk-client部分内容操作一遍即可。
有新增的服务log收集需求时,可以在 filebeat.yml 中添加 paths: 并重启服务即可。