目录
4.部署elasticsearch-head-master插件
简介
ELK 是一个流行的开源日志管理和数据分析平台,它由三个主要组件组成:
-
Elasticsearch:用于存储、搜索和分析数据的分布式搜索引擎。Elasticsearch 能够高效地处理大量结构化和非结构化数据,使用户能够快速搜索和检索数据。
-
Logstash:用于数据收集、转换和传输的服务器端数据处理管道。Logstash 可以从各种来源(如日志文件、数据库、消息队列等)采集数据,然后对数据进行处理和格式化,最终将数据发送到 Elasticsearch 或其他目标存储。
-
Kibana:用于数据可视化和分析的用户界面。Kibana 提供了强大的数据仪表板、图表和搜索功能,使用户能够直观地理解和分析存储在 Elasticsearch 中的数据。
-
Filebeat:是一款轻量级的开源日志文件数据搜集器,通常在需要采集数据的客户端安装Filebeat,并指定目录与日志格式,Filebeat就能快速收集数据后发送给logstash进行解析,或发送给Elasticsearch存储
-
Redis:Redis 可以作为 Logstash 和其他数据源之间的缓冲区,允许数据在 Logstash 收集和传输到 Elasticsearch 之间进行缓存。这有助于平滑处理突发的大量数据,并减轻 Elasticsearch 的负担。
前期准备
1.每台服务器的节点和对应的ip
hostname | ip |
node-1 | 192.168.42.130 |
node-2 | 192.168.42.131 |
node-3 | 192.168.42.131 |
2.每台服务器搭建的架构
节点 | 所搭建的服务 |
node-1 | redis+nginx+filebeat |
node-2 | es群集 |
node-3 | es群集+logstash+kibana |
一.node-1节点对应服务搭建
部署redis
1.使用yum安装redis
yum install epel-release -y
yum install redis -y
2.修改配置文件
vim /etc/redis.conf
bind 0.0.0.0 //61行
daemonize yes //128行
3.启动服务
systemctl start redis
部署nginx
1.安装依赖包
yum -y install gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel wget
2.下载nginx安装包
wget http://nginx.org/download/nginx-1.8.1.tar.gz -P /usr/local/src/
3.进行解包并编译安装
cd /usr/local/src/
tar xzf nginx-1.8.1.tar.gz
cd /usr/local/src/nginx-1.8.1
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre
make && make install
4.修改配置文件
cd /usr/local/src/nginx-1.8.1/conf
vim nginx.conf
log_format main '{ "time_local": "$time_local", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"request_time": "$request_time", '
'"status": "$status", '
'"host": "$host", '
'"request": "$request", '
'"request_method": "$request_method", '
'"uri": "$uri", '
'"http_referrer": "$http_referer", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_user_agent": "$http_user_agent" '
'}';
access_log /var/log/nginx/access.log main;
然后创建配置文件中的日志目录,然后启动服务
mkdir -p /var/log/nginx
5.启动服务
/usr/local/nginx/sbin/nginx
部署filebeat
1.使用wget下载filebeat安装包
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.9.2-linux-x86_64.tar.gz -P /opt
2.安装完成后进行解包
tar -xzf /opt/filebeat-7.9.2-linux-x86_64.tar.gz -C /usr/local/
//更改原本的文件名称
mv /usr/local/filebeat-7.9.2-linux-x86_64 /usr/local/filebeat
3.修改配置文件
//备份配置文件
mv /usr/local/filebeat/filebeat.yml /usr/local/filebeat/filebeat.yml.bak
//重写配置文件
cat > /usr/local/filebeat/filebeat.yml << EOF
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/*.log
output.redis:
hosts: [192.168.42.130:6379]
key: "web_log"
EOF
4.启动服务
cd /usr/local/filebeat
./filebeat &
5.查看redis日志是否产生
[root@localhost /]# redis-cli
127.0.0.1:6379> keys *
1) "web_log"
二.node-2节点对应服务搭建
搭建es群集
1.使用wget下载安装包并进行安装
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-x86_64.rpm -P /opt
rpm -i /opt/elasticsearch-7.12.0-x86_64.rpm
2.设置自带用户登入的shell以及查看安装路径
chsh -s /bin/bash elasticsearch
rpm -ql elasticsearch
3.修改es配置文件
//先进行备份
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
要在最后再添加2条配置文件
cat >> /etc/elasticsearch/elasticsearch.yml << EOF
http.cors.enabled: true
http.cors.allow-origin: "*"
EOF
4.启动服务
systemctl start elasticsearch
curl http://localhost:9200
三.node-3节点对应服务搭建
搭建es群集
1.安装es
安装步骤和刚刚的一样不同的在配置文件
2.修改es配置文件
//先进行备份配置文件
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
要在最后再添加2条配置文件
cat >> /etc/elasticsearch/elasticsearch.yml << EOF
http.cors.enabled: true
http.cors.allow-origin: "*"
EOF
3.启动服务
systemctl start elasticsearch
curl http://localhost:9200
4.部署elasticsearch-head-master插件
安装依赖关系
# 安装依赖 #
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
yum install -y nodejs
# 查看安装版本
npm -v
使用wget下载所需安装包并且进行解包
wget https://codeload.github.com/mobz/elasticsearch-head/zip/master -O /opt/elasticsearch-head-master.zip
unzip /opt/elasticsearch-head-master.zip
//优化路径
mkdir -p /usr/local/head-master
mv /elasticsearch-head-master/* /usr/local/head-master
//进入文件目录进行安装
cd /usr/local/head-master/_site
npm install
//启动服务
cd /usr/local/head-master/
nohup npm run start &
通过 http://192.168.42.132:9100/ 进行查看集群状态
5.部署logstash
使用wget下载所需安装包并进行安装
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.6.0.tar.gz -P /opt
tar zvxf /opt/logstash-7.6.0.tar.gz -C /usr/local
优化路径并修改配置文件
mv /usr/local/logstash-7.6.0 /usr/local/logstash
mkdir /usr/local/logstash/conf.d/
cat >> /usr/local/logstash/conf.d/redis_to_elk.conf << EOF
input {
redis {
host => "192.168.42.130"
port => "6379"
data_type => "list"
type => "log"
key => "web_log"
}
}
output {
elasticsearch {
hosts => ["192.168.42.132"]
index => "beats_log-%{+YYYY.MM.dd}"
}
}
EOF
启动服务
cd /usr/local/logstash/bin/
./logstash -f /usr/local/logstash/conf.d/redis_to_elk.conf &
6.部署kibana
使用wget下载安装包并且进行解压
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.12.0-x86_64.rpm -P /opt
rpm -i /opt/kibana-7.12.0-x86_64.rpm
修改配置文件
cp /etc/kibana/kibana.yml /etc/kibana/kibana.yml.bak
//备份配置文件
sed -i '2s/^#//' /etc/kibana/kibana.yml
sed -i '7s/.*/server.host: "0.0.0.0"/' /etc/kibana/kibana.yml
sed -i '111s/.*/i18n.locale: "zh-CN"/' /etc/kibana/kibana.yml
启动服务
systemctl start kibana
curl http://localhost:5601
游览器中输入 http://192.168.42.132:5601/ 进行索引创建