出品丨Docker公司(ID:docker-cn)
编译丨小东
每周一、三、五,与您不见不散!
使用 Docker 在笔记本电脑上安装 Elasticsearch 集群进行测试是非常方便的。在这篇文章中,我将向您展示如何快速、简便地在 docker 上运行一个3节点的 elasticsearch 集群进行测试。
我们需要设置“vm.max_map_count”内核参数:
$ sudo sysctl -w vm.max_map_count=262144
若要永久设置此值,请将其添加到“/etc/sysctl.conf”并使用“sudo sysctl -p”命令重新加载。
我们将引用的 docker compose 文件:
version: '2.2'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
container_name: elasticsearch
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- http.cors.enabled=true
- http.cors.allow-origin=*
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata1:/home/ruan/workspace/docker/elasticsearch/data
ports:
- 9200:9200
networks:
- esnet
elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
container_name: elasticsearch2
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- http.cors.enabled=true
- http.cors.allow-origin=*
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=elasticsearch"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata2:/home/ruan/workspace/docker/elasticsearch/data
networks:
- esnet
elasticsearch3:
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
container_name: elasticsearch3
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- http.cors.enabled=true
- http.cors.allow-origin=*
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=elasticsearch"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata3:/home/ruan/workspace/docker/elasticsearch/data
networks:
- esnet
kibana:
image: 'docker.elastic.co/kibana/kibana:6.3.2'
container_name: kibana
environment:
SERVER_NAME: kibana.local
ELASTICSEARCH_URL: http://elasticsearch:9200
ports:
- '5601:5601'
networks:
- esnet
headPlugin:
image: 'mobz/elasticsearch-head:5'
container_name: head
ports:
- '9100:9100'
networks:
- esnet
volumes:
esdata1:
driver: local
esdata2:
driver: local
esdata3:
driver: local
networks:
esnet:
现在,只需确保我们在 compose 文件中所引用的路径是真实存在的,在我展示的实例中,其路径是“/home/ruan/workspace/docker/elasticsearch/data”。
使用 docker compose 来部署 elasticsearch 集群:
$ docker-compose up
它将在前台运行,您可以在控制台看到其输出结果。
接下来,让我们运行几个查询。首先,检查群集运行状况 api:
$ curl http://127.0.0.1:9200/_cluster/health?pretty
{
"cluster_name" : "docker-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 1,
"active_shards" : 2,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
创建一个复制计数为2的索引:
$ curl -H "Content-Type: application/json" -XPUT http://127.0.0.1:9200/test -d '{"number_of_replicas": 2}'
将文档提取到 elasticsearch:
$ curl -H "Content-Type: application/json" -XPUT http://127.0.0.1:9200/test/docs/1 -d '{"name": "ruan"}'
{"_index":"test","_type":"docs","_id":"1","_version":1,"result":"created","_shards":{"total":3,"successful":3,"failed":0},"_seq_no":0,"_primary_term":1}
查看索引:
$ curl http://127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open test w4p2Q3fTR4uMSYBfpNVPqw 5 2 1 0 3.3kb 1.1kb
green open .monitoring-es-6-2018.04.29 W69lql-rSbORVfHZrj4vug 1 1 1601 38 4mb 2mb
Kibana 也包含在应用栈中,可以通过“http://localhost:5601/”访问它,如下图所示:
我喜欢直接使用 RESTFul API,但是如果您想使用 UI 与 Elasticsearch 进行交互,你可以通过“http:// localhost:9100/”访问它,如下图所示:
因为它在前台运行,所以您只需按下“ctrl + c”就可以持续地将数据保存在我们的 compose 文件中。当您再次启动集群时,数据仍然会存在。
浏览 https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html 了解更多Elasticsearch 的相关信息!