ElasticSearch简介
Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,可以实现单机和集群部署,轻松缩放规模。
ELK:Elasticsearch + Kibana + Logstash
https://www.elastic.co/cn/
https://www.elastic.co/guide/en/elastic-stack/7.6/overview.html
https://hub.docker.com/_/elasticsearch
日志分析:对IT设备进行运维分析与故障定位、对业务指标分析运营效果。
站内搜索:对网站内容进行关键字检索、对电商网站商品进行检索与推荐。
向量检索:对图像、视频、语料等非结构化数据提取的特征向量数据进行最近邻或近似近邻检索。
本次实验目标
目标1:Docker部署Elasticsearch + Kibana
目标2:安装中文分词器插件,配置认证
目标3:基本操作演示
目标4:在华为云CCE中部署
目标5:使用Logstash进行数据迁移
目标6:使用华为云CSS服务
视频教程地址:https://www.bilibili.com/video/BV1MP4y1e7u8/?vd_source=8e4a436fd4e37be82e3fee379554aa59
实验环境
ECS:通用计算增强型 | c7.xlarge.2 | 4vCPUs | 8GiB
SFS、CCE
1)准备好对应的云资源
2)已安装好Docker,配置加速镜像
Docker部署Elasticsearch + Kibana
$ mkdir -p /cxy/es76/data
$ chmod 777 /cxy/es76/data
$ docker run -d --name es7.6 -p 9201:9200 -p 9301:9300 -v "/cxy/es76/data":/usr/share/elasticsearch/data -e "discovery.type=single-node" elasticsearch:7.6.0
$ docker logs es7.6 --since 30m
http://ip:9201
$ docker run --name kibana7.6 -p 5602:5601 -d --link es7.6:elasticsearch kibana:7.6.0
$ docker logs -f kibana7.6
$ docker exec -it kibana7.6 bash
$ cd config
$ vi kibana.yml
1)如果地址不对,改一下地址
2)添加:i18n.locale: "zh-CN"
$ docker restart kibana7.6
http://ip:5602
安装中文分词器插件,配置认证
$ docker cp elasticsearch-analysis-ik-7.6.0.zip es7.6:/usr/share/elasticsearch/plugins/
$ docker exec -it es7.6 bash
$ cd plugins/
$ mkdir analysis-ik
$ mv elasticsearch-analysis-ik-7.6.0.zip analysis-ik/
$ cd analysis-ik
$ unzip elasticsearch-analysis-ik-7.6.0.zip
$ rm -rf elasticsearch-analysis-ik-7.6.0.zip
$ docker restart es7.6
POST _analyze
{
"analyzer": "ik_max_word",
"text": "2017春装新款休闲裤女装"
}
$ docker exec -it es7.6 bash
$ vi /usr/share/elasticsearch/config/elasticsearch.yml
xpack.security.enabled: true
$ docker restart es7.6
$ docker exec -it es7.6 bash
$ cd /usr/share/elasticsearch/bin
$ ./elasticsearch-setup-passwords interactive
$ docker exec -it kibana7.6 bash
$ vi config/kibana.yml
elasticsearch.username: "kibana"
elasticsearch.password: "123456"
$ docker restart kibana7.6
登录kibana时需要用 elastic 作为用户名。
基本操作
PUT /my_store
{
"mappings" : {
"properties" : {
"price" : {
"type" : "float"
},
"productName" : {
"type" : "text",
"analyzer" : "ik_smart",
"search_analyzer" : "ik_max_word"
},
"size" : {
"type" : "text"
},
"tag" : {
"type" : "keyword"
}
}
}
}
PUT /customer/_doc/1
{
"name": "cxy@toc"
}
GET _cat/indices
PUT /my_store/_doc/1
{
"productName":"李宁休闲裤",
"size":"L",
"price":100.5
}
POST /my_store/_update/1
{
"doc":{
"productName":"李宁休闲裤G",
"size":"L",
"price":100.5
}
}
GET /my_store/_doc/1
POST /my_store/_doc/_bulk
{"index":{}}
{"productName":"2017秋装新款文艺衬衫女装","size":"L","price":100.5}
{"index":{}}
{"productName":"2017秋装新款文艺衬衫女装","size":"M","price":110.5}
{"index":{}}
{"productName":"2017秋装新款文艺衬衫女装","size":"S","price":120.5}
{"index":{}}
{"productName":"2018春装新款牛仔裤女装","size":"M","price":130.5}
{"index":{}}
{"productName":"2018春装新款牛仔裤女装","size":"S","price":140.5}
{"index":{}}
{"productName":"2017春装新款休闲裤女装","size":"L","price":150.5}
{"index":{}}
{"productName":"2017春装新款休闲裤女装","size":"S","price":160.5}
POST /my_store/_doc/_bulk
{"index":{}}
{"productName":"2017春装女裤","size":"L","price":100.5}
{"index":{}}
{"productName":"2017夏装女裤","size":"M","price":110.5}
{"index":{}}
{"productName":"2018秋装女鞋","size":"S","price":120.5}
{"index":{}}
{"productName":"2018冬装女鞋装","size":"M","price":130.5}
{"index":{}}
{"productName":"2019春装男裤","size":"S","price":140.5}
{"index":{}}
{"productName":"2019夏装男裤","size":"L","price":150.5}
{"index":{}}
{"productName":"2019秋装男鞋","size":"S","price":160.5}
{"index":{}}
{"productName":"2019冬装男鞋","size":"S","price":160.5}
GET /my_store/_count
GET /my_store/_search
GET /my_store/_search
{
"query": { "match_all": {} },
"sort": [
{ "price": "asc" }
],
"from": 0,
"size": 5
}
GET /my_store/_search
{
"query": { "match": { "size": "S" } }
}
GET /my_store/_search
{
"query": {
"match": {
"productName": "春装"
}
},
"size": 0,
"aggs": {
"sizes": {
"terms": {
"field": "price"
}
}
}
}
POST /my_store/_delete_by_query
{
"query": {
"match_all": {
}
}
}
在华为云CCE中部署
镜像:elasticsearch:7.6.0
生命周期 -- 启动后:
/bin/bash
-c
sleep 1s;sed -i '$axpack.security.enabled: true' /usr/share/elasticsearch/config/elasticsearch.yml
环境变量:
discovery.type single-node
数据存储:
sfs
/usr/share/elasticsearch/data es/data
/usr/share/elasticsearch/plugins es/plugins
访问方式:
服务名称:elasticsearch:9200 (一定要是这个名称,因为kibana的kibana.yml里默认配置的是elasticsearch:9200)
镜像:kibana:7.6.0
生命周期 -- 启动后:
/bin/bash
-c
sleep 1s;sed -i '$ai18n.locale: "zh-CN"\nelasticsearch.username: "kibana"\nelasticsearch.password: "123456"' /usr/share/kibana/config/kibana.yml
使用Logstash进行数据迁移
https://support.huaweicloud.com/bestpractice-css/css_07_0009.html
https://help.aliyun.com/document_detail/418418.html
$ yum install java
$ yum install python
$ wget https://artifacts.elastic.co/downloads/logstash/logstash-oss-7.6.0-x86_64.rpm
$ yum install logstash-oss-7.6.0-x86_64.rpm
$ vim /etc/logstash/jvm.options
-Xms4g
-Xmx4g
$ cd /etc/logstash/conf.d/
$ vim logstash-es-es-all.conf
$ /usr/share/logstash/bin/logstash --path.settings /etc/logstash