十分钟搭建一个EFK集群

二进制部署

ELK 介绍

ELK 是三个开源软件的缩写,提供一套完整的企业级日志平台解决方案。
分别是:

  • Elasticsearch:搜索、分析和存储数据。
  • Logstash :采集日志、格式化、过滤,最后将数据推送到Elasticsearch存储
  • Kibana:数据可视化。
  • Beats :集合了多种单一用途数据采集器,用于实现从边缘机器向 Logstash 和
    Elasticsearch 发送数据。里面应用最多的是Filebeat,是一个轻量级日志采集器,将采集的数据推送到Logstash、ES存储。
-l: 让shell成为登录shell,用 runuser -l PAM 文件替代默认的
-g:指定主要的组
-G 追加组
-c:命令,要传到shell的单个命令
--session-command=COMMAND:使用-c传递单个命令道shell中并且不创建新的会话
-m: 不重置环境变量

一,准备环境

环境信息:

主机名操作系统版本IP地址
masterCentos7192.168.30.133
node1Centos7192.168.30.134
node2Centos7192.168.30.135

架构:
在这里插入图片描述

系统初始化:

#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
sed -i ‘s/enforcing/disabled/’ /etc/selinux/config # 永久
setenforce 0 # 临时
#根据规划设置主机名
hostnamectl set-hostname
#添加hosts
cat >> /etc/hosts << EOF
192.168.30.133 elk1
192.168.30.134 elk2
192.168.30.135 elk3
EOF
# 创建用户
groupadd elk
useradd elk -g elk
# 创建数据及⽇志⽂件并授权
mkdir -pv /data/elk/{data,logs}
chown -R elk:elk /data/elk/

启动先决条件:
调整进程最大打开文件数数量

#临时设置
ulimit -n 65535
#永久设置,重启生效
vi /etc/security/limits.conf
* hard nofile 65535
* soft nofile 65535
#验证
ulimit -n

调整进程最大虚拟内存区域数量

#临时设置
sysctl -w vm.max_map_count=262144
#永久设置
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p

二,Elasticsearch安装

2.1 下载部署elasticsearch

2.1.1 下载安装

https://www.elastic.co/cn/downloads/elasticsearch

在这里插入图片描述

cd /opt/
#解压到当前⽬录
tar -xf elasticsearch-7.9.3-linux-x86_64.tar.gz -C .
mv elasticsearch-7.9.3-linux-x86_64 elasticsearch

#创建用户、授权
chown -R elk.elk elasticsearch

2.1.2 更改配置文件
更改elk1的配置文件:

vim elasticsearch/config/elasticsearch.yml
#必改配置
cluster.name: elk-cluster # 集群名称
node.name: elk1 # 集群节点名称
network.host: 0.0.0.0 # 监听地址
discovery.seed_hosts: [“192.168.30.133”, “192.168.31.62”,“192.30.134”] # 集群节点列表
cluster.initial_master_nodes: [“elk1”] # 首次启动指定的Master节点
#可选配置
node.master: true 
node.data: true
path.data: /data/elk/data # 数据目录
path.logs: /date/elk/logs # 日志目录
bootstrap.memory_lock: false # 锁内存,尽量不使⽤交换内存
http.port: 9200 # 监听端口
#transport.tcp.port: 9300 #默认内部节点之间通信端口

更改elk2的配置文件:

vim elasticsearch/config/elasticsearch.yml

cluster.name: elk-cluster # 集群名称
node.name: elk2 # 集群节点名称
path.data: /data/elk/data # 数据目录
path.logs: /date/elk/logs # 日志目录
bootstrap.memory_lock: false # 锁内存,尽量不使⽤交换内存
network.host: 0.0.0.0 # 监听地址
http.port: 9200 # 监听端口
#transport.tcp.port: 9300 #内部节点之间通信端口
discovery.seed_hosts: [“192.168.30.133”, “192.168.31.62”,“192.30.134”] # 集群节点列表

配置文件说明:
在节点2或节点3不启用cluster.initial_master_nodes参数,注释掉
cluster.name: 集群名称,唯一确定一个集群。
node.name:节点名称,一个集群中的节点名称是唯一固定的,不同节点不能同名。
node.master: 主节点属性值
node.data: 数据节点属性值
network.host: 本节点的ip
http.port: 本节点的http端口
transport.port:9300 集群之间通信的端口,若不指定默认:9300
discovery.seed_hosts:节点发现需要配置一些种子节点,与7.X之前老版本:disvoery.zen.ping.unicast.hosts类似,一般配置集群中的全部节点
cluster.initial_master_nodes:指定集群初次选举中用到的具有主节点资格的节点,称为集群引导,只在第一次形成集群时需要。
path.data: 数据存放路径
path.logs: 日志存放路径
node.master:true 表示该节点是否可以被选举为主节点(master node)
node.data:true 表示该节点是否可以存储数据
jvm.option的设置(使用elastic用户)

#实际生产环境中计算公式:min(机器内存的一半,32GB内存)。也就是说:取机器环境内存的一半和32GB内存之间的小值。
vim /opt/elasticsearch/config/jvm.options
-Xms1g
-Xmx1g

2.1.3 启动elasticsearch

#控制台启动
./bin/elasticsearch
#后台启动
./bin/elasticsearch -d

#设置systemctl启动(Prod)

[Unit]
Description=Elasticsearch Server
After=network.target
[Service]
Type=forking
#Environment=JAVA_HOME=/data/soft/jdk1.8.0_191ExecStart=/opt/elasticsearch/bin/elasticsearch -p /tmp/elasticsearch.pid -d
ExecStop=kill -SIGTERM cat /tmp/elasticsearch.pid
Restart=always
User=es
Group=es
StandardOutput=journal
StandardError=inherit
LimitNOFILE=65536
LimitNPROC=4096
LimitAS=infinity
LimitFSIZE=infinity
TimeoutStopSec=0
KillSignal=SIGTERM
KillMode=process
SendSIGKILL=no
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target

2.1.4 访问验证

curl http://172.18.2.35:920
查看es健康状态: curl http://172.18.2.35:9200/_cat/health
查看集群节点:curl -XGET 'http://127.0.0.1:9200/_cat/nodes?pretty'
查询集群状态: curl -i -XGET http://127.0.0.1:9200/_cluster/health?pretty
  • green:集群所有数据都处于正常状态
  • yellow:集群所有数据都可以访问,但一些数据的副本还没有分配
  • red:集群部分数据不可访问

在这里插入图片描述

2.2 安装图形页面管理ES

图形管理界面推荐:ElasticHD 、cerebro。

下载安装ElasticHD:

wget https://github.com/qax-os/ElasticHD/releases/download/1.4/elasticHD_linux_amd64.zip
unzip elasticHD_linux_amd64.zip
 nohup ./ElasticHD &
#访问:
http://192.168.67.154:9800

在这里插入图片描述

三,Kibana 安装

安装在es1上,二进制部署kibana。

cd /opt/elk 
tar zxvf kibana-7.9.3-linux-x86_64.tar.gz 
mv kibana-7.9.3-linux-x86_64 kibana 
#修改配置文件
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://192.168.30.136:9200","http://192.168.30.137:9200","http://192.168.30.138:9200"]
i18n.locale: "zh-CN"

配置系统服务管理

vim kibana.service

[Unit] 
Description=kibana 
[Service] 
#User=es
#Group=es
ExecStart=/opt/elk/kibana/bin/kibana --allow-root 
ExecReload=/bin/kill -HUP $MAINPID 
KillMode=process 
Restart=on-failure 
[Install] 
WantedBy=multi-user.target 

访问:
http://192.168.30.136:5601

四,filebeat安装使用

3.1下载安装。

 curl ‐L ‐O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat‐7.13.2‐linux‐ x86_64.tar.gz 
 tar xzvf filebeat‐7.13.2‐linux‐x86_64.tar.gz ‐C /usr/local/ 
 mv /usr/local/filebeat‐7.13.2‐linux‐x86_64 /usr/local/filebeat

3.1 修改 Filebeat配置文件。
(1),配置文件
该配置⽂件的所有者必须是root或正在执⾏Beat进程的⽤户。 该⽂件的权限
必须不允许除所有者以外的任何⼈写⼊,需要是0644的权限

vim /usr/local/filebeat
filebeat.prospectors:
- input_type: log
 paths:
 - /var/log/nginx/*.log
 tags: ["nginx"]
 
 output.elasticsearch:
  hosts: ["192.168.30.136:9200","192.168.30.137:9200"]
  • 配置等级,输出到kibana
    当filebeat和kibana在一台机器上时候可不用配置。
fields:
    level: info
    review: 1

setup.kibana:
host: "192.168.30.136:5601"

路径的最后不能⽤ * 代替所有,必须有⼀个相对的匹配或者绝对的匹配。
/var/log/* 这样的匹配是错误的

(2) 检测配置文件:

./filebeat/filebeat test config -c filebeat/filebeat.yml

3.3 Filebeat启动管理
(1)前台运行 采用前台运行的方式查看Filebeat获取的日志结果

nohup /usr/local/filebeat/filebeat -e -c /usr/local/filebeat/kafka.yml >/dev/null 2>&1 &

(2)后台运行
使用nohup方式启动Filebeat到后台,日志结果可查看nohup.out文件
使用systemd管理的后台方式启动Filebeat进程不能查看输出日志,测试阶段勿用 配置systemd方式的Filebeat启动管理文件

vim /usr/lib/systemd/system/filebeat.service
[Unit] 
Description=Filebeat sends log files to Logstash or directly to Elasticsearch.
 Wants=network‐online.target
After=network‐online.target
[Service] 
ExecStart=/usr/local/filebeat/filebeat ‐c /usr/local/filebeat/filebeat.yml Restart=always 
[Install]
WantedBy=multi‐user.target
1. 给脚本赋权
   chmod +x /lib/systemd/system/elasticsearch.service
2. 设置开机启动
   #重新加载systemd的守护线程:
   systemctl daemon-reload
   systemctl enable elasticsearch

五,nginx安装

需要抓取nginx的日志,nginx和filebeat安装在同一台es1机器上。
yum -y install nginx

六,访问elasticsearch出现yellow

curl http://172.18.2.35:9200/_cat/health

1679992708 08:38:28 elasticsearch yellow 1 1 806 806 0 0 1 0 - 99.9%

curl http://172.18.2.35:9200/_cluster/health?pretty

{
  "cluster_name" : "elasticsearch",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 806,
  "active_shards" : 806,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 1,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 99.87608426270135
}

“unassigned_shards” : 1, 分片数为1,设置为0

curl -X PUT "172.18.2.35:9200/_settings" -H 'Content-Type: application/json' -d'{"number_of_replicas":0}'

查看elasticsearch的状态已经变成green了。

curl http://172.18.2.35:9200/_cluster/health?pretty
curl http://172.18.2.35:9200/_cat/health
filebeat后台启动
nohup /usr/local/filebeat/filebeat -e -c /usr/local/filebeat/kafka.yml >/dev/null 2>&1 &

logstash 后台启动
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/bin/k_es.conf -w 8 -b 1000 > /dev/null 2>&1 &

kafka 后台启动
nohup /usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties > /dev/null 2>&1 &

KafkaStreamsLog 后台启动
nohup /usr/local/jdk/bin/java -Xms128m -Xmx512m -jar /usr/local/KafkaStreams/KafkaStreamsLog.jar > /dev/null 2>&1 &
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值