初始化环境

关闭SELinux

  1. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

关闭防火墙

systemctl  stop firewalld  &&  systemctl  disable firewalld

配置源

# 清除源
cd /etc/yum.repos.d/
rm -f *
# 下载新的软件源
curl http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 生效
yum makecache

安装Java

yum install -y java

Elasticsearch

1. 安装

# 创建存放软件的目录
mkdir -p /opt/elk && cd /opt/elk
# 下载
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-x86_64.rpm
# 安装
rpm -ivh elasticsearch-7.5.1-x86_64.rpm

2. 配置

  • 修改主配置文件
vi /etc/elasticsearch/elasticsearch.yml
#---------- Node -----------------
node.name: ela01

#---------- Network ---------
network.host: 10.0.0.110,127.0.0.1
http.port: 9200

cluster.initial_master_nodes: ["ela01"]

3. 启动

# 启动
systemctl deamon-reload
systemctl start elasticsearch

# 查看日志
tailf /var/log/elasticsearch/elasticsearch.log

4. Elasticsearch-head安装

使用Elasticsearch-head来直接对Elasticsearch进行操作
https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm?utm_source=chrome-ntp-icon

Kibana

1. 安装

# 获取
cd /opt/elk
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.5.1-x86_64.rpm

# 安装
rpm -ivh kibana-7.5.1-x86_64.rpm

2. 配置

vi /etc/kibana/kibana.yml
server.port: 5601
server.host: "10.0.0.110"
# 配置elasticsearch的地址
elasticsearch.hosts: ["http://10.0.0.110:9200"]
kibana.index: ".kibana"

3. 启动

# 启动
systemctl start kibana

Filebeat

1. 安装

cd /opt/elk
# 获取
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.5.1-x86_64.rpm
# 安装
rpm -ivh filebeat-7.5.1-x86_64.rpm

2. 配置

vi /etc/filebeat/filebeat.yml
# 仅列出了基础功能部分
#====== Filebeat inputs =======
enabled: true
#====== Filebeat modules ======
reload.enabled: true
#============ Outputs =========
hosts: ["10.0.0.110:9200"]

2.1 使用模块

Filebeat中自带了一些常用服务的数据收集模块,使用模块收集数据可以轻松的让Filebeat帮助我们完成数据的格式化操作

  • 模块开启与关闭
# 列出可用的模块
filebeat modules list
# 开启模块(以nginx为例)
filebeat modules enable nginx
# 关闭模块
filebeat modules disable nginx
  • 配置模块
  # Access logs
  access:
    enabled: true
    var.paths: ["/var/log/nginx/access.log"]

  # Error logs
  error:
    enabled: true
    var.paths: ["/var/log/nginx/error.log"]

如此,filebeat收集到的数据就是json格式了,可以在kibana中看到

3. 启动

systemctl start filebeat

至此,打开http://10.0.0.110:5601进入Index patterns就可以看到filebeat的数据了
不过现在的数据是直接输入到Elasticsearch中,在我们的构架中,还需要进一步的部署,让数据输入到Redis/Kafka中进行缓存,再由Logstash进行处理,将已经过滤的数据存入Elasticsearch

Logstash

1. 安装

cd /opt/elk
# 获取
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.5.1.rpm
# 安装
rpm -ivh logstash-7.5.1.rpm

2. 配置

Logstash的配置主要是在 /etc/logstash/conf.d/ 下编写配置文件
通用日志收集

input {
  beats {
    port => 5044
  }
}
output {
  elasticsearch {
    hosts => ["localhost"]
    manage_template => false
    index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
  }
}

3. 启动

# 启动
systemctl start logstash
# 关闭
systemctl stop logstash

FAQ

此时Elasticsearch是无法直接启动的(这个问题仅存在于单机中),需要修改内存设置(两种方法不能同时使用)

  1. 方法一,在主配置文件中打开内存锁定
# 主配置文件
vi /etc/elasticsearch/elasticsearch.yml
#----------- Network ----------
bootstrap.memory_lock: true

# elasticsearch启动配置文件
systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
  1. 方法二,修改内存计数 ```bash

    编辑文件,在最后追加

    vi /etc/sysctl.conf vm.max_map_count=262144

生效

sysctl -p ```