docker-compose快速创建ElasticStack环境.md
** TIPS:受Log4j漏洞影响,请选择7.16.1+ 或者6.8.22+版本
零、注意事项
Elasticsearch使用了mmapfs目录,需要调大以下参数:
sysctl -w vm.max_map_count=262144
ES集群需要使用磁盘进行存储,所以需要挂载磁盘目录,并需要调整目录权限,官方docker镜像中的运行用户为elasticsearch,uid和gid都是1000。
我们这里使用docker-compose作为快速部署工具。
一、单机版ES搭建
version: "3"
services:
es:
image: elasticsearch:7.16.1
ports:
- "29200:9200"
- "29300:9300"
environment:
- "bootstrap.memory_lock=true"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.type=single-node"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- "/data/apps/jaeger/es:/usr/share/elasticsearch/data"
kibana:
image: kibana:7.16.1
ports:
- "25601:5601"
environment:
ELASTICSEARCH_HOSTS: http://es:9200
二、两台机器集群版ES
由于两台机器需要进行容器通信,这里我们直接使用了宿主机host模式的网络。
# 主机ES01配置
version: "3"
services:
es01:
image: elasticsearch:7.16.1
network_mode: host
restart: on-failure
ports:
- "9200:9200"
- "9300:9300"
environment:
- "bootstrap.memory_lock=true"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "http.cors.enabled=true"
- "http.cors.allow-origin=*"
- "node.name=es02"
- "network.host=0.0.0.0"
- "cluster.name=es-docker-cluster"
- "discovery.seed_hosts=es01,es02"
- "cluster.initial_master_nodes=es01,es02"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- "/data/apps/jaeger/es:/usr/share/elasticsearch/data"
extra_hosts:
- "es01:10.140.100.22"
- "es02:172.28.11.46"
kibana:
image: kibana:7.16.1
network_mode: host
ports:
- "5601:5601"
environment:
ELASTICSEARCH_HOSTS: http://localhost:9200
# 主机ES02配置
version: "3"
services:
es02:
image: elasticsearch:7.16.1
network_mode: host
restart: on-failure
ports:
- "9200:9200"
- "9300:9300"
environment:
- "bootstrap.memory_lock=true"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "http.cors.enabled=true"
- "http.cors.allow-origin=*"
- "node.name=es02"
- "network.host=0.0.0.0"
- "cluster.name=es-docker-cluster"
- "discovery.seed_hosts=es01,es02"
- "cluster.initial_master_nodes=es01,es02"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- "/data/apps/jaeger/es:/usr/share/elasticsearch/data"
extra_hosts:
- "es01:10.140.100.22"
- "es02:172.28.11.46"
三、Elasticsearch常用命令行操作
# 查看nodes列表
curl -XGET http://localhost:9200/_cat/nodes?v
# 查看索引列表
curl -X GET "localhost:9200/_cat/indices?v"
# 创建索引(名字是customer)
curl -X PUT "localhost:9200/customer?pretty"
# 删除索引(名字是customer)
curl -X DELETE "localhost:9200/customer?pretty"
# 新增数据(没有指定ID,自动生成)
curl -X POST "localhost:9200/customer2/_doc/?pretty" -H 'Content-Type: application/json' \
-d '{"name": "Wei Song", "age": 24, "location": "xian", "sex": "Female"}'
# 新增数据(指定ID为2)
curl -X PUT "localhost:9200/customer2/_doc/1?pretty" -H 'Content-Type: application/json' \
-d '{"name": "Brace Lee", "age": 38, "location": "Hangzhou", "sex": "Male"}'
# 查看所有数据(所有字段)
curl -XGET 'localhost:9200/customer2/_doc/_search?pretty'
# 删除数据
curl -X POST "localhost:9200/customer2/_delete_by_query?pretty" -H 'Content-Type: application/json' -d \
'{
"query": {
"match": {
"name": "John Doe"
}
}
}'