ELK部署

ELK概述

ELK是一整套解决方案,是三个软件产品的首字母缩写:Elasticsearch、Logstash、Kibana。

Elasticsearch:负责日志的检索和存储

Logstash:负责日志的收集和分析、处理

Kibana:负责日志的可视化

Elasticsearch

Elasticsearch概述

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful API的Web接口。

Elasticsearch的一大优势是拥有集群协调子系统。集群协调子系统可以从只有几个节点的小集群无缝扩展到拥有数百个节点的大集群。

Elasticsearch特点

实时分析,文档导向,分布式实时文件存储

高可用性,易扩展,支持集群、分片和复制

接口友好,支持JSON

没有典型意义的事务

是一种面向文档的数据库

Node:装有一个ES服务器的节点

Cluster:由多个Node组成的集群

Shards:索引的分片,每一分片就是一个Shard

Replicas:索引的拷贝

Index:拥有相似特征的文档的集合

Type:一个索引中可以定义一种或多种类型

Document:一个可被搜索的基础信息单元

Filed:是ES的最小单位,相当于数据的某一列

Elasticsearch与关系型数据库对比

关系型数据库Elasticsearch
DatabaseIndex
Table

type

RowDocument
ColumnFiled
InsertPUT http://
DeleteDELETE http://
UpdatePOST http://
SelectGET http://

Web集群部署

主机列表

主机名称IP地址配置
nfs192.168.1.101核CPU 1G内存
web-0001192.168.1.112核CPU 4G内存
web-0002192.168.1.122核CPU 4G内存
web-0003192.168.1.132核CPU 4G内存


部署NFS服务

# 创建共享目录,并部署网站页面
[root@nfs ~]# mkdir -p /var/webroot
[root@nfs ~]# tar -zxf website.tar.gz -C /var/webroot/

# 部署 NFS 服务
[root@nfs ~]# dnf install -y nfs-utils
[root@nfs ~]# vim /etc/exports
/var/webroot    192.168.1.0/24(rw,no_root_squash)

[root@nfs ~]# systemctl enable --now nfs-server.service

部署web服务

[root@ecs-proxy ~]# mkdir website
[root@ecs-proxy ~]# cd website
[root@ecs-proxy website]# vim ansible.cfg
[defaults]
inventory         = hostlist
host_key_checking = False
[root@ecs-proxy website]# vim hostlist
[web]
192.168.1.[11:13]
[root@ecs-proxy website]# vim web_install.yaml
---
- name: web 集群安装
  hosts: web
  tasks:
  - name: 安装 apache 服务 
    dnf:
      name: httpd,php,nfs-utils
      state: latest
      update_cache: yes
  - name: 配置 httpd 服务 
    service:
      name: httpd
      state: started
      enabled: yes
  - name: 编辑 fstab 文件,添加 NFS 配置
    lineinfile:
      path: /etc/fstab
      regexp: '^192.168.1.10:/.*'
      line: '192.168.1.10:/var/webroot /var/www/html nfs defaults,_netdev,nolock 1 1'
      create: yes
  - name: 挂载 NFS
    shell:
      cmd: mount /var/www/html

# 执行 playbook 完成安装
[root@ecs-proxy website]# ansible-playbook web_install.yaml

ELB负载均衡


配置华为云负责均衡对外发布服务

[root@ecs-proxy ~]# curl http://192.168.1.250/info.php

部署Elasticsearch集群

主机列表

主机名称IP地址配置
es-0001192.168.1.212核CPU 4G内存
es-0002192.168.1.222核CPU 4G内存
es-0003192.168.1.232核CPU 4G内存
es-0004192.168.1.242核CPU 4G内存
es-0005192.168.1.252核CPU 4G内存

部署es-0001

[root@es-0001 ~]# vim /etc/hosts
192.168.1.21    es-0001
192.168.1.22    es-0002
192.168.1.23    es-0003
192.168.1.24    es-0004
192.168.1.25    es-0005
[root@es-0001 ~]# dnf install -y elasticsearch
[root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
17:  cluster.name: my-es
23:  node.name: es-0001
56:  network.host: 0.0.0.0
70:  discovery.seed_hosts: ["es-0001", "es-0002", "es-0003"]
74:  cluster.initial_master_nodes: ["es-0001", "es-0002", "es-0003"]
[root@es-0001 ~]# systemctl enable --now elasticsearch
# 服务启动成功
[root@es-0001 ~]# curl http://127.0.0.1:9200
{
  "name" : "es-0001",
  "cluster_name" : "my-es",
  "cluster_uuid" : "_na_",
  "version" : {
    "number" : "7.17.8",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "120eabe1c8a0cb2ae87cffc109a5b65d213e9df1",
    "build_date" : "2022-12-02T17:33:09.727072865Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}


部署es-0002

# 验证集群状态失败
[root@es-0002 ~]# curl http://192.168.1.21:9200/_cat/nodes?pretty
{
  "error" : {
    "root_cause" : [
      {
        "type" : "master_not_discovered_exception",
        "reason" : null
      }
    ],
    "type" : "master_not_discovered_exception",
    "reason" : null
  },
  "status" : 503
}
# 部署服务
[root@es-0002 ~]# vim /etc/hosts
192.168.1.21    es-0001
192.168.1.22    es-0002
192.168.1.23    es-0003
192.168.1.24    es-0004
192.168.1.25    es-0005
[root@es-0002 ~]# dnf install -y elasticsearch
[root@es-0002 ~]# vim /etc/elasticsearch/elasticsearch.yml
17:  cluster.name: my-es
23:  node.name: es-0002
56:  network.host: 0.0.0.0
70:  discovery.seed_hosts: ["es-0001", "es-0002", "es-0003"]
74:  cluster.initial_master_nodes: ["es-0001", "es-0002", "es-0003"]
[root@es-0002 ~]# systemctl enable --now elasticsearch
# 验证集群状态 成功
[root@es-0002 ~]# curl http://es-0001:9200/_cat/nodes?pretty
192.168.1.21 16 89  2 0.15 0.06 0.04 cdfhilmrstw * es-0001
192.168.1.22  6 88 61 1.00 0.23 0.08 cdfhilmrstw - es-0002


集群扩容

在所有es主机安装Elasticsearch
 

[root@ecs-proxy ~]# mkdir es
[root@ecs-proxy ~]# cd es
[root@ecs-proxy es]# vim ansible.cfg 
[defaults]
inventory         = hostlist
host_key_checking = False
[root@ecs-proxy es]# vim hostlist
[es]
192.168.1.[21:25]
[root@ecs-proxy es]# rsync -av 192.168.1.21:/etc/elasticsearch/elasticsearch.yml elasticsearch.j2
[root@ecs-proxy es]# vim elasticsearch.j2
23:  node.name: {{ ansible_hostname }}
[root@ecs-proxy es]# vim es_install.yaml 
---
- name: Elasticsearch 集群安装
  hosts: es
  tasks:
  - name: 设置 /etc/hosts
    copy:
      dest: /etc/hosts
      owner: root
      group: root
      mode: '0644'
      content: |
        ::1     localhost localhost.localdomain localhost6 localhost6.localdomain6
        127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
        {% for i in groups.es %}
        {{ hostvars[i].ansible_eth0.ipv4.address }} {{ hostvars[i].ansible_hostname }}
        {% endfor %}
  - name: 安装 ES 服务
    dnf:
      name: elasticsearch
      state: latest
      update_cache: yes
  - name: 拷贝配置文件
    template:
      src: elasticsearch.j2
      dest: /etc/elasticsearch/elasticsearch.yml
      owner: root
      group: elasticsearch
      mode: '0660'
  - name: 配置 ES 服务 
    service:
      name: elasticsearch
      state: started
      enabled: yes

[root@ecs-proxy es]# ansible-playbook es_install.yaml
[root@ecs-proxy es]# curl http://192.168.1.21:9200/_cat/nodes?pretty
192.168.1.21 32 88 0 0.04 0.01 0.00 cdfhilmrstw * es-0001
192.168.1.22 16 87 0 0.13 0.04 0.01 cdfhilmrstw - es-0002
192.168.1.23  6 86 1 0.64 0.21 0.07 cdfhilmrstw - es-0003
192.168.1.24 18 86 0 0.44 0.13 0.05 cdfhilmrstw - es-0004
192.168.1.25  6 86 1 0.67 0.21 0.07 cdfhilmrstw - es-0005


插件管理


部署插件页面

# 在 es-0001 上安装 web 服务,并部署插件
[root@es-0001 ~]# dnf install -y nginx
[root@es-0001 ~]# systemctl enable --now nginx
[root@es-0001 ~]# tar zxf head.tar.gz -C /usr/share/nginx/html/


认证和代理

[root@es-0001 ~]# vim /etc/nginx/default.d/esproxy.conf 
location ~* ^/es/(.*)$ {
    proxy_pass http://127.0.0.1:9200/$1;
    auth_basic "Elasticsearch admin";
    auth_basic_user_file /etc/nginx/auth-user; 
}
[root@es-0001 ~]# dnf install -y httpd-tools
[root@es-0001 ~]# htpasswd -cm /etc/nginx/auth-user admin
New password: 
Re-type new password: 
Adding password for user admin
[root@es-0001 ~]# systemctl reload nginx

API原语管理


集群状态查询

# 查询支持的关键字
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/
# 查具体的信息
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master
# 显示详细信息 ?v
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?v
# 显示帮助信息 ?help
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?help
# 显示易读格式 ?pretty
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?pretty


创建索引

# 创建 tedu 索引
[root@es-0001 ~]# curl 'http://127.0.0.1:9200/tedu?pretty' \
    -X PUT -H 'Content-Type: application/json' -d '{
      "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
      }
    }'


增加数据

[root@es-0001 ~]# curl -XPUT -H "Content-Type: application/json" \
    http://127.0.0.1:9200/tedu/teacher/1?pretty -d '{
        "职业": "诗人","名字": "李白","称号": "诗仙","年代": "唐"
    }' 


查询数据

[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/tedu/teacher/_search?pretty
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/tedu/teacher/1?pretty


修改数据

[root@es-0001 ~]# curl -XPOST -H "Content-Type: application/json" \
    http://127.0.0.1:9200/tedu/teacher/1/_update -d '{ 
      "doc": {"年代":"公元701"}
    }'


删除数据

# 删除一条
[root@es-0001 ~]# curl -XDELETE http://127.0.0.1:9200/tedu/teacher/1
# 删除索引
[root@es-0001 ~]# curl -XDELETE http://127.0.0.1:9200/tedu

Logstash

Logstash概述

Logstash是一个数据采集、加工处理以及传输的工具

Logstash特点

所有类型的数据集中处理

不同模式和格式数据的正常化

自定义日志格式的迅速扩展

为自定义数据源轻松添加插件

Logstash结构

input:负责收集数据

filter:负责处理数据

input:负责输出数据

Logstash支持的数据类型:

布尔、字节、字符串、数值、数组、键值对

Logstash支持的判断方法:

==、!=、>、>=、<、<=、=~、!~

Logstash支持的逻辑判断:

in、notin、and、or、nand、xor

Logstash配置管理


安装logstash

logstash使用Java开发

logstash没有默认的配置文件,需要手动配置

logstash安装在/usr/share/logstash目录下,配置文件安装在/etc/logstash目录下,由于程序找不到配置文件路径,需要在/usr/share/logstash目录下创建一个名为config的/etc/logstash目录的软链接

安装部署

主机名称IP地址配置
logstash192.168.1.274核CPU 8G内存

[root@logstash ~]# vim /etc/hosts
192.168.1.21    es-0001
192.168.1.22    es-0002
192.168.1.23    es-0003
192.168.1.24    es-0004
192.168.1.25    es-0005
192.168.1.27    logstash
[root@logstash ~]# dnf install -y logstash
[root@logstash ~]# ln -s /etc/logstash /usr/share/logstash/config


logstash最简单配置

[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  stdin {}
}

filter{ 

}

output{ 
  stdout{}
}
[root@logstash ~]# /usr/share/logstash/bin/logstash


插件与调试格式

[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  stdin { codec => "json" }
}

filter{ 

}

output{ 
  stdout{ codec => "rubydebug" }
}
[root@logstash ~]# /usr/share/logstash/bin/logstash

使用json格式字符串测试:{"a":"1", "b":"2", "c":"3"}


input模块


file插件

file插件基本配置

[root@logstash ~]# touch /tmp/{a,b}.log
[root@logstash ~]# echo 'string 01' >>/tmp/a.log
[root@logstash ~]# echo 'string 02' >>/tmp/a.log
[root@logstash ~]# echo 'string 03' >>/tmp/a.log
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input {
  file {
    path => ["/tmp/a.log", "/tmp/b.log"]
  }
}
# filter { 不做任何修改 }
# output { 不做任何修改 }

# 启动程序,等待数据输出
[root@logstash ~]# /usr/share/logstash/bin/logstash

#---------------------------------------------------
# 在另一个终端模拟写入日志
[root@logstash ~]# echo 'string 04' >>/tmp/b.log
[root@logstash ~]# echo 'string 05' >>/tmp/a.log

file插件高级配置

# 删除默认书签文件
[root@logstash ~]# rm -rf /var/lib/logstash/plugins/inputs/file/.sincedb_*
[root@logstash ~]# cat /tmp/{a.log,b.log} >/tmp/c.log
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input {
  file {
    path => ["/tmp/c.log"]
    start_position => "beginning"
    sincedb_path => "/var/lib/logstash/sincedb"
  }
}
# filter { 不做任何修改 }
# output { 不做任何修改 }

[root@logstash ~]# /usr/share/logstash/bin/logstash


filter模块


grok插件

正则表达式分组匹配格式: (?<名字>正则表达式)
正则表达式宏调用格式: %{宏名称:名字}
宏文件路径: /usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-patterns-core-4.3.4/patterns

准备测试数据

# 从 web 服务器查找一条日志写入到日志文件
[root@logstash ~]# echo '60.26.217.109 - admin [13/Jan/2023:14:31:52 +0800] "GET /es/ HTTP/1.1" 200 148209 "http://127.70.79.1/es/" "curl/7.61.1"' >/tmp/c.log

# 调试技巧:设置路径为 /dev/null 可以多次反复测试
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input {
  file {
    path => ["/tmp/c.log"]
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}
# filter { 不做任何修改 }
# output { 不做任何修改 }

[root@logstash ~]# /usr/share/logstash/bin/logstash

测试匹配IP地址

[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
# input { 不做任何修改 }

filter {
  grok {
    match => { "message" => "(?<userIP>((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d))" }
  }
  grok {
    match => { "message" => "%{IP:clientIP}" }
  }
}

# output { 不做任何修改 }

[root@logstash ~]# /usr/share/logstash/bin/logstash

使用宏格式化日志

[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
# input { 不做任何修改 }

filter{ 
  grok {
    match => { "message" => "%{HTTPD_COMBINEDLOG}" }
    remove_field => ["message"]
  }
}

# output { 不做任何修改 }

[root@logstash ~]# /usr/share/logstash/bin/logstash


output模块


elasticsearch插件
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
# input { 不做任何修改 }
# filter { 不做任何修改 }

output{ 
  stdout{ codec => "rubydebug" }
  elasticsearch {
    hosts => ["es-0002:9200","es-0003:9200"]
    index => "weblog-%{+YYYY.MM.dd}"
  }
}

[root@logstash ~]# /usr/share/logstash/bin/logstash

访问Elasticsearch页面,验证数据写入成功

获取WEB日志

实现web日志实时分析

在每台web服务器上安装部署Logstash的弊端:

由于Logstash使用Java开发,占用资源巨大,Logstash所消耗的资源甚至比web服务器本身消耗的资源还大,因此在每一台web服务器上部署Logstash非常不合适

通过网络收集日志

在web服务器上部署filebeat日志采集程序

filebeat是使用Golang实现的轻量型日志采集程序,也是Elasticsearch stack里面的一员

filebeat占用资源非常小,可以忽略不计

filebeat本质上是一个agent,可以安装在应用服务器各个节点上,根据配置读取对应位置的日志文件,并通过网络上报到相应的服务中

beats插件

beats插件概述

beats是Logstash input模块的插件

专门用来接收filebeat发送过来的日志

默认监听5044端口

可以同时接收多台不同主机发送过来的日志信息

使用Logstash配置日志接收服务器

[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 
  beats {
    port => 5044
  }
} 
# filter { 不做任何修改 }
# output { 不做任何修改 }

[root@logstash ~]# /usr/share/logstash/bin/logstash

filebeat安装配置

# 安装 filebeat 服务
[root@web-0001 ~]# dnf install -y filebeat
[root@web-0001 ~]# systemctl enable --now filebeat

# 配置 filebeat
[root@web-0001 ~]# vim /etc/filebeat/filebeat.yml
25:  id: my-filestream-id # 如果同时配置多个收集器,id不能重复
28:  enabled: true # 打开收集模块
32:  - /var/log/httpd/access_log # 日志文件路径
135: # 注释掉 Elasticsearch 配置
137: # 注释掉 Elasticsearch 配置
148: output.logstash: # 设置输出模块
150:   hosts: ["192.168.1.27:5044"] # 输出给logstash
163: processors:
164:   - drop_fields:  # 删除冗余数据
165:      fields:
166:        - log
167:        - offset
168:        - agent
169:        - ecs
170: #   - add_host_metadata: 注释掉(收集主机信息)
171: #       when.not.contains.tags: forwarded 注释掉(判断是否为容器)
172: #   - add_cloud_metadata: ~  注释掉(收集 cloud 信息)
173: #   - add_docker_metadata: ~ 注释掉(收集 docker 信息)
174: #   - add_kubernetes_metadata: ~ 注释掉(收集 kubernetes 信息)

[root@web-0001 ~]# rm -f /var/log/httpd/*
[root@web-0001 ~]# systemctl restart httpd filebeat

# 测试验证: 访问页面,观察 logstash 是否能够获取数据
[root@web-0001 ~]# curl http://192.168.1.11/info.php

多日志标签

添加标签

[root@web ~]# vim /etc/filebeat/filebeat.yml
# 设置识别标签
49:  fields:
50:    logtype: apache_log

[root@web ~]# systemctl restart filebeat

# 测试验证: 访问页面,观察 logstash 输出的数据变化
[root@web-0001 ~]# curl http://192.168.1.11/info.php

匹配标签

[root@logstash ~]# cat /etc/logstash/conf.d/my.conf
input { 
  beats {
    port => 5044
  }
}

filter{
  if [fields][logtype] == "apache_log" {
  grok {
    match => { "message" => "%{HTTPD_COMBINEDLOG}" }
    remove_field => ["message"]
  }}
}

output{ 
  stdout{ codec => "rubydebug" }
  if [fields][logtype] == "apache_log" {
  elasticsearch {
    hosts => ["es-0004:9200", "es-0005:9200"]
    index => "weblog-%{+YYYY.MM.dd}"
  }}
}

[root@logstash ~]# /usr/share/logstash/bin/logstash

数据调试成功后,把logstash作为后台服务运行

# 注释 stdout 优化性能
[root@logstash ~]# vim /etc/logstash/conf.d/my.conf
input { 不做任何修改 }
filter{ 不做任何修改 }

output{ 
  # stdout{ codec => "rubydebug" }
  if [fields][logtype] == "apache_log" {
  elasticsearch {
    hosts => ["es-0004:9200", "es-0005:9200"]
    index => "weblog-%{+YYYY.MM.dd}"
  }}
}
[root@logstash ~]# chown -R logstash.logstash /var/log/logstash /var/lib/logstash
[root@logstash ~]# systemctl enable --now logstash

批量安装部署

[root@ecs-proxy ~]# cd website
[root@ecs-proxy website]# rsync -av 192.168.1.11:/etc/filebeat/filebeat.yml filebeat.j2
[root@ecs-proxy website]# vim filebeat.yaml
---
- name: 集群安装部署 filebeat
  hosts: web
  tasks:
  - name: 安装 filebeat
    dnf:
      name: filebeat
      state: latest
      update_cache: yes
  - name: 同步配置文件
    template:
      src: filebeat.j2
      dest: /etc/filebeat/filebeat.yml
      owner: root
      group: root
      mode: '0600'
  - name: 设置启动服务
    service:
      name: filebeat
      enabled: yes
  - name: 清理历史日志
    file:
      path: "{{ item }}"
      state: absent
    loop:
      - /var/log/httpd/access_log
      - /var/log/httpd/error_log
  - name: 重启服务
    service:
      name: "{{ item }}"
      state: restarted
    loop:
      - httpd
      - filebeat

[root@ecs-proxy website]# ansible-playbook filebeat.yaml

Kibana

Kibana概述

Kibana是一个数据可视化平台

Kibana特点

灵活分析和可视化平台

实时流量统计报表

种类繁多的数据图表

为不同的用户定制分析界面

Kibana安装配置

主机列表

主机名称IP地址配置
kibana192.168.1.262核CPU 4G内存

安装Kibana

[root@kibana ~]# vim /etc/hosts
192.168.1.21    es-0001
192.168.1.22    es-0002
192.168.1.23    es-0003
192.168.1.24    es-0004
192.168.1.25    es-0005
192.168.1.26    kibana
[root@kibana ~]# dnf install -y kibana
[root@kibana ~]# vim /etc/kibana/kibana.yml
02:  server.port: 5601
07:  server.host: "0.0.0.0"
23:  server.publicBaseUrl: "http://192.168.1.26:5601"
32:  elasticsearch.hosts: ["http://es-0004:9200", "http://es-0005:9200"]
115: i18n.locale: "zh-CN"
[root@kibana ~]# systemctl enable --now kibana

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值