目 录CONTENT

文章目录

ELK相关笔记02-软件安装

cplinux98
2022-11-10 / 0 评论 / 0 点赞 / 585 阅读 / 3,958 字 / 正在检测是否收录...

00:文章简介

本文介绍了ELK组件的安装方法。

01:环境安排

节点 系统版本 主机名称 IP地址
es-master Ubuntu20.04 Elasticsearch-01 10.0.0.110
es-slave Ubuntu20.04 Elasticsearch-02 10.0.0.111
Logstash Ubuntu20.04 Logstash 10.0.0.112
Kibana Ubuntu20.04 Kibana 10.0.0.113
Filebeat Ubuntu20.04 Filebeat 10.0.0.114
nginx服务 Ubuntu20.04 Filebeat 10.0.0.114
#初始化
systemctl stop ufw
systemctl disable ufw
echo 'export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[00m\]@\[\033[1;01;32m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\\$ "' >> /root/.bashrc

02:elasticsearch

2.1:安装

2.1.1:apt安装

官方安装文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.14/deb.html#deb-repo

2.1.2:dpkg安装

# 下载地址
https://www.elastic.co/cn/downloads/

# 下载和安装
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-amd64.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-amd64.deb.sha512
shasum -a 512 -c elasticsearch-7.14.0-amd64.deb.sha512
dpkg -i elasticsearch-7.14.0-amd64.deb

# 配置环境变量
echo 'export PATH=/usr/share/elasticsearch/bin:$PATH' > /etc/profile.d/elasticsearch.sh
source /etc/profile.d/elasticsearch.sh

2.2:elasticsearch相关文件

/etc/elasticsearch/elasticsearch.yml                核心配置文件
/etc/elasticsearch/log4j2.properties                日志相关的配置
/etc/elasticsearch/jvm.options                      jvm相关的配置
/etc/default/elasticsearch                          环境变量配置文件
/usr/lib/systemd/system/elasticsearch.service       服务启动文件

2.3:配置文件

]$ vim /etc/elasticsearch/elasticsearch.yml
# 设定elasticsearch集群的名称
cluster.name: elastic.linux98.com

# 设定集群master界面的名称,节点名称
node.name: 10.0.0.110
# node.name: node-1

# 设定elasticsearch的存储目录,包括数据目录和日志目录
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch

# 允许所有主机都能访问我们的elasticsearch
network.host: 0.0.0.0

# 设置elasticsearch对外的访问端口
http.port:9200

# 设定主机发现
discovery.seed_hosts: ["10.0.0.110","10.0.0.111"] # 这里填主机名或者ip地址,前提是可以通过主机名通信
cluster.initial_master_nodes: ["10.0.0.110"] # 这里和node.name时一致的node-1

# 开启跨域访问支持,默认为false
http.cors.enabled: true

# 跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: "*"

注意:
对于 node 主机只需要更改一处 node.name 即可
如果是重新还原es集群,启动前将 path.data 和 path.logs 目录下的数据清空,避免数据不一致

2.4:启动服务

systemctl start elasticsearch.service
systemctl status elasticsearch.service
ss -ntlp | grep java # 9200和9300端口

2.5:查看效果

image

2.6:集群的接口

查看集群状态
curl -XGET 10.0.0.110:9200/_cat/health
curl -XGET 10.0.0.110:9200/_cat/health?v

查看集群节点
curl -XGET 10.0.0.110:9200/_cat/nodes
查看索引
curl -XGET 10.0.0.110:9200/_cat/indices
创建索引
curl -XPUT 10.0.0.110:9200/index_test
curl -XGET 10.0.0.110:9200/_cat/indices
格式化展示
curl 10.0.0.110:9200/index_test?pretty

删除索引
curl -XDELETE 10.0.0.110:9200/index_test
curl -XGET 10.0.0.110:9200/_cat/indices
批量删除
curl -s http://10.0.0.110:9200/_cat/indices | awk '{print $3}'
for index in $(curl -s http://10.0.0.110:9200/_cat/indices | awk '{print
$3}')
do
curl -XDELETE 10.0.0.110:9200/$index
done
修改切片属性
curl -X PUT 10.0.0.110:9200/index_test -H 'Content-Type:application/json' -d '
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}'
curl 10.0.0.110:9200/index_test?pretty
注意:
在设置切片属性的时候,一定要注意在历史数据中,最好不要存在同名的索引,否则报错

2.7:功能插件管理

elasticsearch最擅长的场景就是索引的操作,而索引的使用场景在不同的公司非常不同,
所以elasticsearch的索引场景可以基于不同的插件来实现对应的功能,而插件也是ELK非常重要的属性。

elasticsearch提供了两种插件的管理方式:
- 专用的插件管理可以使用命令 elasticsearch-plugin,它可以对默认的插件进行管理;
- 定制的插件管理可以基于离线方式安装插件。

常见插件:
分词插件:analysis-icu、analysis-ik、等
管理插件:head、kopf、bigdest、等

注意:
随着elasticsearch的版本更新,很多之前可以直接安装的插件,目前无法直接安装了,
需要采取自定义的方式来安装

旧的插件地址
https://www.elastic.co/guide/en/elasticsearch/plugins/2.4/management.html
官方地址
https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html

2.7.1:安装插件方式

在线方式:
elasticsearch-plugin install [plugin_name]/[version]

离线方式:
方法1:elasticsearch-plugin install file:///path/to/plugin.zip
方法2:将下载的插件解压到elasticsearch的plugins目录即可

查看已安装插件
elasticsearch-plugin list

删除插件
elasticsearch-plugin remove [plugin_name]

注意:
删除插件的时候,推荐先关闭结点,然后再关闭。

2.7.2:安装默认的插件

安装中文语法分析后重启服务
elasticsearch-plugin install analysis-smartcn
elasticsearch-plugin install analysis-icu
systemctl restart elasticsearch.service

检查效果
# elasticsearch-plugin list
analysis-icu
analysis-smartcn

# ls /usr/share/elasticsearch/
bin jdk lib modules NOTICE.txt plugins README.asciidoc

# ls /usr/share/elasticsearch/plugins/
analysis-icu analysis-smartcn

测试安装的插件

curl -X POST 'http://10.0.0.110:9200/_analyze?pretty=true' -H 'content-type:application/json' -d '{
"analyzer": "icu_analyzer",
"text": "中华人民共和国国歌"
}'


curl -X POST 'http://10.0.0.110:9200/_analyze?pretty=true' -H 'content-type:application/json' -d '{
"analyzer": "smartcn",
"text": "中华人民共和国国歌"
}'

image

image

2.7.3:head插件安装

简介
    head插件,在elasticsearch中,应用的还算可以,但是自从2.x之后的版本,就不再支持了,需要我们自己来进行独立的部署。
代码资料:https://github.com/mobz/elasticsearch-head

准备工作
apt update
apt install npm git -y
安装插件
mkdir /data/server/elasticsearch/plugins -p
cd /data/server/elasticsearch/plugins
git clone git://github.com/mobz/elasticsearch-head.git
# git克隆的可能不完整导致安装失败
cd elasticsearch-head
npm config set registry https://registry.npm.taobao.org
npm install --force


# 配置插件的访问地址
vim Gruntfile.js

 94                 connect: {
 95                         server: {
 96                                 options: {
 97                                         hostname: '*',  # 增加此行

# 配置插件访问es
vim _site/app.js

4385                 init: function(parent) {
4386                         this._super();
4387                         this.prefs = services.Preferences.instance();
4388                         this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://10.0.0.110:9200";
# 修改4388中的ip地址

# 启动
npm run start >>/dev/null 2>&1 &
检查效果
# netstat -tnulp | egrep 'Add|grunt'

属性解析:
    一定要先保证连接按钮左侧的es地址是正确的,然后再来点击"连接";
    * 代表索引es的主节点,黑色加粗的方框0表示,索引的主副本;
    黄色代表有从分片丢失,但是没有主分片数据丢失,即当前没有数据丢失;
    红色代表主分配丢失,即有数据丢失;
    绿色代表所有主分片和从分片的数据正常。

image

2.7.4:head插件使用

image

属性解析:
    _index:创建了一个索引index 
    _type:创建了一个类型text1
    total:分片2个 
    Successful:成功2个
    Failed:失败0个 
    Created:状态成功
注意:
    我们可以在这里进行各种各样的编辑,"POST"的下拉框可以选择多种http操作属性,

2.7.5:配置head的启动脚本

# 启动脚本
vim /data/scripts/elasticsearch_head.sh

#!/bin/bash
# 启动elasticsearch 脚本
cd /data/server/elasticsearch/plugins/elasticsearch-head
npm run start >>/dev/null 2>&1

===================

vim /lib/systemd/system/elasticsearch_head.service

[Unit]
Description= elasticsearch head server project
[Service]
User=root
ExecStart=/bin/bash /data/scripts/elasticsearch_head.sh
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target


# 启动服务
systemctl daemon-reload
systemctl start elasticsearch_head.service
systemctl status elasticsearch_head.service

03:Logstash部署

3.1:安装

3.1.1:apt安装

官方资料
https://www.elastic.co/guide/en/logstash/current/installing-logstash.html


wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt-get update && sudo apt-get install logstash

3.1.2:dpkg安装

# 官方下载地址
https://www.elastic.co/cn/downloads/

# 下载安装
cd /data/softs
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.14.0-amd64.deb
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.14.0-amd64.deb.sha512
shasum -a 512 -c logstash-7.14.0-amd64.deb.sha512
dpkg -i logstash-7.14.0-amd64.deb

# 环境变量
echo 'export PATH=/usr/share/logstash/bin:$PATH' > /etc/profile.d/logstash.sh
source /etc/profile.d/logstash.sh

3.2:Logstash相关文件

/usr/share/logstash/bin/                二进制文件存放目录
/etc/logstash/startup.options           服务启动环境变量文件
/etc/logstash/jvm.options               jvm相关配置
/etc/logstash/logstash.yml              服务配置文件
/etc/logstash/logstash-sample.conf      应用配置文件模板
/usr/share/logstash/bin/system-install  生成系统管理配置文件

3.3:测试

命令格式

命令格式:
    logstash -e '启动参数'

启动参数:
    input {
      stdin {}
    }
    output {
      stdout {}
    }
参数解析:
    input {} 用于接受信息的输入
    output {} 用于对内部的数据输出
    stdin {} 表示屏幕上的标准输入
    stdout {} 表示屏幕的标准输出

3.3.1:①简单的输入输出

logstash -e 'input { stdin { } } output { stdout {} }'
# 结束时按Ctrl+c

image

3.3.2:②信息传递给es

logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["10.0.0.110:9200"] index => "message" } }'

image

image

image

3.3.3:③读取日志文件到es

模块简介
    logstash的信息采集模块支持file模块,可以通过指定日志文件,直接从文件中读取相关信息。

参考资料:
    https://www.elastic.co/guide/en/logstash/7.14/plugins-inputs-file.html

基本属性:
    path 指定文件路径
    start_position 设定从文件的那个位置开始读取,可选值 -- beginning, end(默认)
    type 传递信息的时候,增加一个额外的属性字段

配置示例:
file {
    path => "/var/log/syslog"
    start_position => "beginning"
    type => "elasticsearch"
}

image

image

3.4:服务文件

以命令行的方式来进行启动太繁琐,我们最好还是以配置文件的方式来进行服务的启动管理,对于
logstash来说,它提供好了一个专门用于生成配置文件的命令 system-install,我们只需要按照既定的
配置文件规则,定制应用配置,最后执行该命令,即可实现服务脚本的配置

3.4.1:定制服务启动文件

# 配置启动参数
进入应用目录
cd /etc/logstash
编辑启动参数文件
# vim startup.options
...
# Arguments to pass to logstash
LS_OPTS="--path.settings ${LS_SETTINGS_DIR} -f /etc/logstash/conf.d"

注意: -f 指定的是 logstash的应用配置文件(比如 logstash.conf)存放到的目录
# 定制信息输入到es的配置文件
生成配置文件
cp logstash-sample.conf conf.d/logstash.conf
修改配置文件 conf.d/logstash.conf
input {
#  beats {
#    port => 5044
#  }

  file {
    path => ["/tmp/log/syslog"]
    start_position => "beginning"
    type => "elasticsearch"
  }
}

output {
  elasticsearch {
    hosts => ["http://10.0.0.110:9200"]
    #index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
    index => "logstash-test-%{+YYYY.MM.dd}"
    #user => "elastic"
    #password => "changeme"
  }
}
# 生成配置文件
以root用户执行下面的命令
system-install
查看生成的服务配置文件
# ls /etc/systemd/system/logstash.service
/etc/systemd/system/logstash.service
查看服务配置文件内容
root@Logstash:/etc/logstash# cat /etc/systemd/system/logstash.service 
[Unit]
Description=logstash

[Service]
Type=simple
User=logstash     # 这里服务启动时使用的用户和组都是logstash
Group=logstash    # 所以需要采集的日志文件需要logstash用户能够查看
# Load env vars from /etc/default/ and /etc/sysconfig/ if they exist.
# Prefixing the path with '-' makes it try to load, but if the file doesn't
# exist, it continues onward.
EnvironmentFile=-/etc/default/logstash
EnvironmentFile=-/etc/sysconfig/logstash
ExecStart=/usr/share/logstash/bin/logstash "--path.settings" "/etc/logstash" "-f" "/etc/logstash/conf.d"
Restart=always
WorkingDirectory=/
Nice=19
LimitNOFILE=16384

# When stopping, how long to wait before giving up and sending SIGKILL?
# Keep in mind that SIGKILL on a process can cause data loss.
TimeoutStopSec=infinity

[Install]
WantedBy=multi-user.target

# 赋予文件权限
mkdir /tmp/log
cp /var/log/syslog /tmp/log/syslog
cd /tmp/log/
chown logstash:logstash syslog
# 启动服务测试效果
systemctl daemon-reload
systemctl start logstash.service
systemctl status logstash.service

# 查看9600端口
netstat -nutlp | grep java

3.4.2:查看效果

image

04:Kibana部署

4.1:安装

4.1.1:apt安装

# 官网安装资料
https://www.elastic.co/cn/downloads/kibana

4.1.2:dpkg安装

cd /data/softs
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.14.0-arm64.deb
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.14.0-arm64.deb.sha512
shasum -a 512 -c kibana-7.14.0-amd64.deb.sha512
dpkg -i kibana-7.14.0-amd64.deb

# 环境变量
echo 'export PATH=/usr/share/kibana/bin:$PATH' > /etc/profile.d/kibana.sh
source /etc/profile.d/kibana.sh

4.2:相关文件

/etc/kibana kibana家目录
/etc/systemd/system/kibana.service 服务启动文件
/usr/share/kibana/bin 执行命令目录文件

4.3:配置文件

修改配置文件
# vim /etc/kibana/kibana.yml
# 设定kibana对外开放的通信端口
server.port: 5601
# 设定可以访问kibana的主机地址
server.host: "0.0.0.0"
# 设定elasticsearch的主机地址
elasticsearch.hosts: ["http://10.0.0.110:9200"]
# 设定kibana的数据索引
kibana.index: ".kibana"
# 设定中文显示格式
i18n.locale: "zh-CN"

4.4:服务启动

systemctl start kibana.service 
systemctl status kibana.service

netstat -nutlp | grep 5601
# kibana默认端口时5601

image

4.5:测试添加数据

点击【添加数据】>【Sample data】>【Add data】

image

添加完成后就可以【view data】了

然后在es-head中查看索引

image

05:FileBeat部署

5.1:安装

5.1.1:apt安装

# 官方资料
https://www.elastic.co/cn/downloads/beats/filebeat

5.1.2:dpkg安装

# 下载安装
cd /data/softs
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-amd64.deb
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.14.0-amd64.deb.sha512
shasum -a 512 -c filebeat-7.14.0-amd64.deb.sha512
dpkg -i filebeat-7.14.0-amd64.deb

5.2:相关文件

/etc/filebeat/filebeat.yml                  核心配置文件
...
/etc/filebeat/filebeat.reference.yml
/etc/filebeat/fields.yml
...
/usr/share/filebeat/bin                     可执行文件位置
/usr/bin/filebeat                           命令
/lib/systemd/system/filebeat.service        服务启动文件

5.3:配置文件

5.3.1:配置文件介绍

root@Filebeat:~# grep -Env '#|^$' /etc/filebeat/filebeat.yml 
15:filebeat.inputs:                                 数据采集
21:- type: log
24:  enabled: false                                 默认数据采集功能没有开启
27:  paths:
28:    - /var/log/*.log
66:- type: filestream
69:  enabled: false
72:  paths:
73:    - /var/log/*.log
96:filebeat.config.modules:
98:  path: ${path.config}/modules.d/*.yml
101:  reload.enabled: false
108:setup.template.settings:
109:  index.number_of_shards: 1                     默认的数据分片个数为 1
145:setup.kibana:
176:output.elasticsearch:                           数据的输出
178:  hosts: ["localhost:9200"]
204:processors:
205:  - add_host_metadata:
206:      when.not.contains.tags: forwarded
207:  - add_cloud_metadata: ~
208:  - add_docker_metadata: ~
209:  - add_kubernetes_metadata: ~

结果显示:
    filebeat.yml 这就是filebeat的配置文件,里面有12部分的配置,而我们重点关心的就是
    "Filebeat inputs" 和 "Outputs",

5.3.2:定制配置文件

root@Filebeat:/etc/filebeat# grep -Env '#|^$' /etc/filebeat/filebeat.yml 
15:filebeat.inputs:
21:- type: log
24:  enabled: true                      启用模块
27:  paths:
28:    - /var/log/syslog                收集日志文件路径
109:  index.number_of_shards: 5         切片数量
176:output.elasticsearch:
178:  hosts: ["10.0.0.110:9200"]        es地址
181:template.name: "filebeat"           es中索引的名称

5.4:启动服务

systemctl start filebeat.service
systemctl status filebeat.service

# filebeat没有监听地址,它是主动发送数据给es的

# 通过es-head可以看到以filebeat开头的索引
# 格式为:"自定义索引名-版本号-日期-6位编号"

image

0

评论区