42

一、单机单节点

[root@es ~]# docker pull elasticsearch:7.9.3
[root@es ~]# docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "ES_JAVA_OPTS=-Xms256m -Xmx256m" elasticsearch:7.9.3
[root@es ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                            NAMES
694543d4d758        elasticsearch:7.9.3   "/tini -- /usr/local…"   16 seconds ago      Up 15 seconds       0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   mystifying_dewdney

[root@es ~]# curl 10.0.0.5:9200
{
  "name" : "694543d4d758",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "if5lllhHSce9v0kkOY5-aw",
  "version" : {
    "number" : "7.9.3",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
    "build_date" : "2020-10-16T10:36:16.141335Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

二、单机集群

使用Docker部署一个三节点的Elasticsearch集群,我们使用Docker Compose来实现:

[root@es ~]# mkidr /es-docker-compose
[root@es ~]# cd /es-docker-compose/
[root@es es-docker-compose]# vim docker-compose.yml
version: '2.2'
services:
  es01:
    image: elasticsearch:7.9.3
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic
  es02:
    image: elasticsearch:7.9.3
    container_name: es02
    environment:
      - node.name=es02
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data02:/usr/share/elasticsearch/data
    networks:
      - elastic
  es03:
    image: elasticsearch:7.9.3
    container_name: es03
    environment:
      - node.name=es03
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es02
      - cluster.initial_master_nodes=es01,es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data03:/usr/share/elasticsearch/data
    networks:
      - elastic

volumes:
  data01:
    driver: local
  data02:
    driver: local
  data03:
    driver: local

networks:
  elastic:
    driver: bridge

[root@es es-docker-compose]# docker-compose up -d
Creating es01 ... done
Creating es03 ... 
Creating es02 ... 
[root@es es-docker-compose]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                              NAMES
90c690db6322        elasticsearch:7.9.3   "/tini -- /usr/local…"   8 seconds ago       Up 7 seconds        0.0.0.0:9200->9200/tcp, 9300/tcp   es01
7346fc36c1ca        elasticsearch:7.9.3   "/tini -- /usr/local…"   8 seconds ago       Up 7 seconds        9200/tcp, 9300/tcp                 es03
53e9e6f0409f        elasticsearch:7.9.3   "/tini -- /usr/local…"   8 seconds ago       Up 7 seconds        9200/tcp, 9300/tcp                 es02

  以上示例通过Docker Compose创建了一个三节点的Elasticsearch集群,节点es01监听在localhost:9200上,es02和es03通过Docker网络与es01进行通信形成集群,我们可以通过localhost:9200和ES集群进行交互。

[root@es ~]# curl -X GET "localhost:9200/_cat/nodes?v&pretty"
ip         heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.18.0.2           59          96   7    0.00    0.18     0.61 dilmrt    -      es02
172.18.0.3           64          96   7    0.00    0.18     0.61 dilmrt    -      es01
172.18.0.4           67          96   6    0.00    0.18     0.61 dilmrt    *      es03

部署kibana

[root@es ~]# docker run -d -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://10.0.0.5:9200" kibana:7.9.3

三、注意事项

1、端口
    请注意,以上示例我们是在所有网络接口上公开端口9200,如果不想公开端口9200,
    而是使用反向代理,请在docker-compose.yml文件中将9200:9200替换为127.0.0.1:9200:9200。

2、堆大小
    要配置堆大小,有两种方式:
    一是,将提前准备好的jvm.options配置文件挂载到容器内的/usr/share/elasticsearch/config/jvm.options.d下。
    二是,使用 ES_JAVA_OPTS 环境变量来配置堆大小。例如,要配置为16GB,在docker run 时指定 -e ES_JAVA_OPTS =“-Xms16g -Xmx16g” 。

3、数据卷
    你应该将数据卷挂载绑定到容器内的/usr/share/elasticsearch/data上,出于以下原因我们要使用数据卷:
      • 如果容器被杀死,Elasticsearch节点的数据将不会丢失。
      • Elasticsearch对 I/O 敏感,而Docker存储驱动程序对于快速 I/O 而言并不是很理想。
      • 可以使用高级Docker卷插件。

4、内存锁
    为了性能和节点稳定性,需要禁用交换建议开启内存锁。我们可以在在docker run 时可以指定 -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1 。

四、生产环境通过docker部署

  在生产环境中使用Docker部署Elasticsearch时,就必须考虑更多的事项,此处请阅读官方文档https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

42

发表回复

验证码: − 3 = 1