Elasticsearch
Elasticsearch 是一个基于 Lucene 库的搜索引擎。它提供了一个分布式、支持多租户的全文搜索引擎,具有 HTTP Web 接口和无模式 JSON 文档。Elasticsearch 是用 Java 开发的,并在 Apache 许可证下作为开源软件发布。官方客户端在 Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby 和许多其他语言中都是可用的。Elastic 8.0 版通过改进 Elasticsearch 的矢量搜索功能、对现代自然语言处理模型的原生支持、不断简化的数据上线过程,以及精简的安全防护体验,在速度、扩展幅度、相关性和简便性方面,迎来了一个全新的时代。
Elastic 8.0重要更新
(1)Rest API相比较7.x而言做了比较大的改动(比如彻底删除_type),为了降低用户的升级成本,8.x会暂时的兼容7.x的请求。
(2)默认开启安全配置(三层安全),并极大简化了开启安全需要的工作量,可以这么说:7.x开启安全需要10步复杂的步骤比如CA、证书签发、yml添加多个配置等等,8.x只需要一步即可)。
(3)存储空间优化:更新了倒排索引,对倒排文件使用新的编码集,对于keyword、match_only_text、text类型字段有效,有3.5%的空间优化提升,对于新建索引和segment自动生效。
(4)优化geo_point,geo_shape类型的索引(写入)效率:15%的提升。
(5)新特性:支持上传pyTorch模型,在ingest的时候使用。比如在写入电影评论的时候,如果我们想要知道这个评论的感情正负得分,可以使用对应的AI感情模型对评论进行运算,将结果一并保存在ES中。
(6)技术预览版KNN API发布,(K邻近算法),跟推荐系统、自然语言排名相关。之前的KNN是精确搜索,在大数据集合的情况会比较慢,新的KNN提供近似KNN搜索,以提高速度。
(7)对ES内置索引的保护加强了:elastic用户默认只能读,如果需要写权限的时候,需有allow_restrict_access权限。
系统优化
系统版本
cat /etc/os-release
NAME="openEuler"
VERSION="22.03 (LTS-SP1)"
ID="openEuler"
VERSION_ID="22.03"
PRETTY_NAME="openEuler 22.03 (LTS-SP1)"
ANSI_COLOR="0;31"
关闭selinux
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
setenforce 0
getenforce
优化文件数限制及最大虚拟内存限制
#添加如下信息
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 65536
* hard nproc 65536
* hard memlock unlimited
* soft memlock unlimited
#查看当前用户文件数限制
ulimit -n
vim /etc/sysctl.conf
vm.swappiness = 0
vm.max_map_count = 262144
net.core.somaxconn = 65535
fs.file-max = 655360
使用sysctl -p使系统配置生效
elasticsearch 部署
1,下载elasticsearch8.6.1软件包
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.6.1-linux-x86_64.tar.gz
2,解压elasticsearch安装包
tar -zvxf elasticsearch-8.6.1-linux-x86_64.tar.gz
mv elasticsearch-8.6.1 /usr/local/
3,创建elastic用户并修改文件夹属组及权限
groupadd elastic
useradd -g elastic elastic
chown -R elastic:elastic /usr/local/elasticsearch-8.6.1
4,elasticsearch配置文件
vim /usr/local/elasticsearch-8.6.1/config/elasticsearch.yml
cluster.name: my-application
node.name: node-1
path.data: /path/to/data
path.logs: /path/to/logs
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9200
xpack.security.enabled: true
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12
xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
cluster.initial_master_nodes: ["192.168.8.50"]
5,配置文件参数说明
cluster.name: my-application #集群名称
node.name: node-1 #节点名称
node.roles: [master,data] # 注意集群至少有两个具有选举master资格的节点
path.data: /path/to/data # 数据存储位置
path.logs: /path/to/logs #日志存储位置
network.host: 0.0.0.0 #允许连接IP
http.port: 9200 # 网页访问端口
http.cors.enabled: true
http.cors.allow-origin: “*”
6,重置elastic用户密码
/usr/local/elasticsearch-8.6.1/bin/elasticsearch-reset-password -u elastic
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y
Password for the [elastic] user successfully reset.
New value: SKwj2Mj3IiRZGv2NlbR7
7,重置kibana用户密码
#随机生成密码
/usr/local/elasticsearch-8.6.1/bin/elasticsearch-reset-password -u kibana
#自定义密码
/usr/local/elasticsearch-8.6.1/bin/elasticsearch-reset-password -u kibana -i
8,systemctl 服务管理
vim /usr/lib/systemd/system/elasticsearch.service
[Unit]
Description=elasticsearch
After=network.target
[Service]
Type=simple
User=elastic
Group=elastic
LimitNOFILE=65535
LimitNPROC=65535
Restart=no
ExecStart=/usr/local/elasticsearch-8.6.1/bin/elasticsearch
PrivateTmp=true
[Install]
WantedBy=multi-user.target
9,启动开机启动
systemctl enable elasticsearch && systemctl start elasticsearch
Kibana部署
1,下载kibana软件包
https://artifacts.elastic.co/downloads/kibana/kibana-8.6.1-linux-x86_64.tar.gz
2,解压kibana安装包
tar -zvxf kibana-8.6.1-linux-x86_64.tar.gz
mv kibana-8.6.1 /usr/local/kibana
3,创建kibana用户并修改文件夹属组及权限
groupadd kibana
useradd -g kibana kiban
chown -R kibana:kibana /usr/local/kibana
4,kibana配置文件
egrep -v "*#|^$" /usr/local/kibana/config/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
server.name: "127.0.0.1"
elasticsearch.hosts: ["http://192.168.8.50:9200"]
elasticsearch.username: "kibana"
elasticsearch.password: "vKW60ZG+LmHrH97rlOTf"
i18n.locale: "zh-CN"
5,systemctl 服务管理
vim /usr/lib/systemd/system/kibana.service
[Unit]
Description=Kibana
[Service]
Type=simple
User=kibana
Group=kibana
# 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=-/usr/local/kibana/config
EnvironmentFile=-/etc/sysconfig/kibana
ExecStart=/usr/local/kibana/bin/kibana
Restart=always
WorkingDirectory=/
[Install]
WantedBy=multi-user.target
6,启动开机启动
systemctl enable kibana && systemctl start kibana
logsatsh部署
1,下载logsatsh软件包
https://artifacts.elastic.co/downloads/logstash/logstash-8.6.1-linux-x86_64.tar.gz
2,解压logstash安装包
tar -zvxf logstash-8.6.1-linux-x86_64.tar.gz
mv logstash-8.6.1 /usr/local/
3,创建logstash服务
以root身份执行logstash命令创建服务
/usr/local/logstash-8.6.1/bin/system-install
4,修改/etc/systemd/system/logstash.service 文件
[Unit]
Description=logstash
[Service]
Type=simple
User=root
Group=root
# 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/local/logstash-8.6.1/bin/logstash "--path.settings" "/etc/logstash"
ExecStart=/usr/local/logstash-8.6.1/bin/logstash " --path.settings" "/usr/local/logstash-8.6.1/config" "--path.config" "/usr/local/logstash-8.6.1/logstash.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
6,启动开机启动
systemctl start logstash && systemctl enable logstash
7,logstash配置文件
vim /usr/local/logstash-8.6.1/logstash.d/syslog.conf
input {
beats {
port => "5044"
}
}
filter {
if "secure" in [tags]{
mutate {
add_field => {"testname" => "songhongpeng"}
}
}
}
output{
#stdout{codec => rubydebug}
if "secure" in [tags]{
elasticsearch{
index => "secure-%{+YYYY.MM.dd}"
hosts => ["127.0.0.1:9200"]
user => "elastic"
password => "SKwj2Mj3IiRZGv2NlbR7"
}
}
}
filebeat部署
1,rpm 安装filebeat
rpm -ivh filebeat-8.6.1-x86_64.rpm
2,filebeat配置文件
egrep -v "*#|^$" /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/secure
tags: ["secure"]
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 1
setup.kibana:
output.logstash:
hosts: ["localhost:5044"]
3,启动开机启动
systemctl restart filebeat && systemctl enable filebeat
4,kibana 访问
http://ip:5601/
elastic API查看集群状态
#查看集群节点
curl -u elastic:SKwj2Mj3IiRZGv2NlbR7 http://192.168.8.50:9200/_cat/nodes?v
#查看集群健康状态
curl -u elastic:SKwj2Mj3IiRZGv2NlbR7 http://192.168.8.50:9200/_cluster/health
#查看集群索引
curl -u elastic:SKwj2Mj3IiRZGv2NlbR7 http://192.168.8.50:9200/_cat/indices
#删除索引
curl -u elastic:SKwj2Mj3IiRZGv2NlbR7 -XDELETE http://192.168.8.50:9200/heartbeat-2023-01