努力挣扎的生活 努力挣扎的生活
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 运维基础
  • 监控
  • 日志系统
  • gitlab安装
  • jenkins安装和管理
  • Jenkins工具集成
  • pipeline流水线
  • Docker
  • Kubernetes
  • Nexus
  • Rancher
  • Prometheus
  • ELK(EFK)
  • 虚拟化
  • Mysql
  • PostgreSQL
  • Redis
  • MongoDB
  • clickhouse
关于
  • 分类
  • 标签
  • 归档
  • 收藏
  • 更多
GitHub (opens new window)

yangfk

瑟瑟发抖的小运维
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 运维基础
  • 监控
  • 日志系统
  • gitlab安装
  • jenkins安装和管理
  • Jenkins工具集成
  • pipeline流水线
  • Docker
  • Kubernetes
  • Nexus
  • Rancher
  • Prometheus
  • ELK(EFK)
  • 虚拟化
  • Mysql
  • PostgreSQL
  • Redis
  • MongoDB
  • clickhouse
关于
  • 分类
  • 标签
  • 归档
  • 收藏
  • 更多
GitHub (opens new window)
  • 运维基础

  • 监控

  • 日志系统

    • Graylog日志服务部署
    • Graylog服务配置
    • Graylog日志告警
    • elasticsearch部署
    • Elasticsearch HQ与curl操作
      • HQ安装
      • ES5 常用curl 操作
      • 备份与还原
        • 备份
        • 还原
        • 数据迁移
        • 备份并压缩
  • 安全记录

//
  • 运维
  • 日志系统
yangfk
2021-10-30

Elasticsearch HQ与curl操作

//

ElasticSearch HQ(http://www.elastichq.org)是一个监视和管理应用程序,它同时管理实例和集群。 这是一个开源解决方案,对于私人和商业用途都是免费的。

这个应用是一个python的项目。按照它的Get Started官方文档,我们可以看到它必须运行在:

Python 3.4+ Elasticsearch. Supported versions: 2.x, 5.x, 6.x, 7.x

Centos升级Python3.6

# HQ安装

HQ文档: (opens new window)

HQ包下载地址 (opens new window)

wget https://github.com/ElasticHQ/elasticsearch-HQ/archive/refs/tags/v3.5.2.zip
unzip elasticsearch-HQ-3.5.2.zip

#安装模块
cd elasticsearch-HQ-3.5.2
pip3 install -r requirements.txt

#启动
python3 application.py
1
2
3
4
5
6
7
8
9

或者Docker 安装:docker run -it -d --name es-hq --restart=always -p 5000:5000 elastichq/elasticsearch-hq:latest

  • 浏览器的地址栏中输入如下的地址http://IP:5000/

es8.png

# ES5 常用curl 操作

  • cat api使用

curl http://localhost:9200/_cat?help

  • 查看状态

curl -XGET http://192.168.5.106:9200/_cat/health

  • 查看节点

curl -XGET http://192.168.5.106:9200/_cat/nodes

  • 查看库:

curl -X GET http://192.168.5.106:9200/_cat/indices?v

  • 查看分片分布情况

curl -X GET http://192.168.5.106:9200/_cat/shards

  • 创库:PUT -注意:分片数一旦创建,就不可修改

curl -X PUT http://192.168.5.106:9200/secisland?pretty #不推荐

  • 多副本,为了数据均衡,一般指定6分片,推荐

curl -X PUT '192.168.5.106:9200/secisland/' -d '{"settings":{"number_of_shards":6,"number_of_replicas":1}}'

  • ES数据库发现磁盘将要写满之后,会尝试将所有的index设置index.blocks.read_only_allow_delete为true,会导致数据无法写入

curl -XPUT -H "Content-Type: application/json" http://192.168.5.106:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": false}'

  • 添加ES数据POST

curl -X POST http://192.168.5.106:9200/secisland/secilog/1/ -d '{"computer": "secisland"}'

curl  -XPOST 'http://192.168.5.106:9200/secisland/secilog/2/' -H "Content-Type: application/json" -d '{
	"computer": "secisland",
	"message": "secisland is an security company1111"
}'
1
2
3
4
  • 查询数据 GET

curl -X GET http://192.168.5.106:9200/secisland/secilog/1/?pretty

  • 删除数据

1.删数据 curl -X DELETE http://192.168.5.106:9200/secisland/secilog/1/

2.删库 curl -X DELETE http://192.168.5.106:9200/secisland

  • 创建secisland 库,指定分片 6,设置初始字段
curl -XPUT 192.168.5.106:9200/secisland -d '{
"settings":{"number_of_shards":6,"number_of_replicas":1},
"mappings":{"tv":
	{"properties":{
		"act":{
			"type":"text",
			"fields":{
				"keyword":{"type":"keyword","ignore_above":256}}},
				"area":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
				"cat":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
				"dir":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
				"id":{"type":"long"},"is_end":{"type":"long"},
				"news_type":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
				"online_time":{"type":"long"},"pubdate":{"type":"long"},
				"read_count":{"type":"long"},"score":{"type":"float"},
				"title":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
			}
		}
	}
}'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

settings是修改分片和副本数的。

mappings是修改字段和类型的。

  • 查询索引库的settings信息

curl -X GET http://192.168.5.106:9200/secisland/_settings?pretty

  • 动态修改副本数,在创建新的索引库时,可以指定索引分片的副本数。默认是1

curl -XPUT '192.168.5.106:9200/secisland/_settings' -d '{"index":{"number_of_replicas":1}}'

Mapping,就是对索引库中索引的字段名称及其数据类型进行定义,类似于mysql中的表结构信息。不过es的mapping比数据库灵活很多,它可以动态识别字段。

一般不需要指定mapping都可以,因为es会自动根据数据格式识别它的类型,如果你需要对某些字段添加特殊属性(如:定义使用其它分词器、是否分词、是否存储等),就必须手动添加mapping。

我们在es中添加索引数据时不需要指定数据类型,es中有自动影射机制,字符串映射为string,数字映射为long。通过mappings可以指定数据类型是否存储等属性。

  • 查询索引库的mapping信息

curl -XGET http://192.168.5.106:9200/secisland/_mapping?pretty

  • 新增不存在的索引

curl -XPUT '192.168.5.106:9200/secisland/secilog/1/' -d '{"mappings":{"properties_name":{"properties":{"name111":{"type":"string","analyzer": "ik_max_word"}}}}}'

  • 操作已存在的索引,在properties_name下新增一个name111字段数据类型

curl -XPOST http://192.168.5.106:9200/secisland/secilog/1/ -d '{"mappings":{"properties_name":{"name111":{"type":"string","analyzer": "ik_max_word"}}}}'

  • 分片数据均衡,节点数据迁移,注意:number_of_replicas 数设置为0

  • 从node-id-208 节点,ip为192.168.5.106 里面的数据迁移出来,到 另一个节点

curl -X PUT -H 'content-type: application/json;charset=UTF-8' -d '{
    "settings":{
        "index.routing.allocation.exclude._name": "node-2",
        "index.routing.allocation.exclude._ip_": "192.168.5.106",
        "index.number_of_replicas": 2
    }
}' http://192.168.5.106:9200/$index/_settings

1
2
3
4
5
6
7
8
  • 关闭分片自动转移-待验证
curl -X PUT http://192.168.5.106:9200/_cluster/settings '{
"transient":{
   "cluster.routing.allocation.enable":"none"
 }
}'

1
2
3
4
5
6

# 备份与还原

# 备份

./elasticdump --input=http://192.168.1.2:9200/test --output=/opt/esdump/test.json

# 还原

./elasticdump --input=/opt/esdump/test.json --output=http:/192.168.1.3:9200/test --type=data

# 数据迁移

./elasticdump --input=http:/192.168.1.2:9200/test --output=http:/192.168.1.3:9200/test --type=data

# 备份并压缩

./elasticdump --input=http://192.168.1.2:9200/test --output=$ | gzip > /opt/esdump/test.json.gz

//
如果此文章对您有帮助,点击 -->> 请博主喝咖啡 (opens new window)
上次更新: 2022/09/09, 08:58:03
elasticsearch部署
Nginx DNS解析漏洞(CVE-2021-23017)

← elasticsearch部署 Nginx DNS解析漏洞(CVE-2021-23017)→

最近更新
01
Linux Polkit 权限提升漏洞(CVE-2021-4034)
03-28
02
postgreSQL维护
03-17
03
trivy系统漏洞扫描
02-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 yangfk | 湘ICP备2021014415号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×
//