网站运营
位置:首页>> 网站运营>> docker搭建es集群实现过程详解

docker搭建es集群实现过程详解

作者:程序员皮卡秋  发布时间:2023-05-27 19:00:10 

标签:docker,es,集群搭建

前言

该系列默认开启Nacos 服务,还不会搭建的小伙伴可以参考往期文章~

本节重点是给大家介绍利用docker来搭建Es集群,废话不多说直接开整吧~

什么是es

同样的,在学习之前,先了解一下这玩意到底是个啥?

es这个名词或许大家都听过,它的全称是Elasticsearch,它是一个分布式文档储存中间件,它不会将信息储存为列数据行,而是储存已序列化为 JSON 文档的复杂数据结构。当你在一个集群中有多个节点时,储存的文档分布在整个集群里面,并且立刻可以从任意节点去访问。

当文档被储存时,它将建立索引并且近实时(1s)被搜索。 Elasticsearch 使用一种被称为倒排索引的数据结构,该结构支持快速全文搜索。在倒排索引里列出了所有文档中出现的每一个唯一单词并分别标识了每个单词在哪一个文档中。有时候面试官会问,es为什么这么快?这也是一个小的知识点。

通过上面简单的介绍,我们大体可以知道,它是用来做数据检索的,而且速度特别快。

不知道小伙伴们有没有遇到过这样一个问题,比方说我们在用sql查商品库表的时候,想要通过某个关键词来匹配相应的商品,当数据量很小的时候ok,但是随着商品数据的不断导入,后期的数据量越来越大,而且都是关联着好几张表,这时候我们用sql去查询我们想要的数据的时候,会显得特别吃力,这种是相当危险的操作,因为可能会把整张表锁死,导致我们的系统出现故障,如果其它系统也使用这个库,那么也会受到影响。所以这时候,我们就需要借助es这种中间件来帮我们处理这种需求,系统的性能也会有显著的提升,当然,维护上也会增加一些难度,当然也不是啥都上es的。其实我们也可以使用其它的比如mongo,如何选取,取决于系统架构和实际的业务场景。

使用docker搭建es集群

为了大家快速的体验到es,这里推荐大家使用docker来搭建,因为它比较方便。但是生产中,如果你对docker不是很熟悉,维护会稍微有点麻烦,那么建议你还是到官网去下载具体的安装包,本节默认大家都已经安装好了docker。如果你还不知道docker是啥也没关系,这个后边我会专门给大家讲讲,本节跟着我敲就可以了。

docker的安装非常简单,官网都有具体的平台的安装包,winmac都有,无脑安装就好了。win11安装可能会遇到wsl的问题,需要开启linux子系统,如果启动错误,直接百度错误就好了,已经有人踩过坑了。

下面,我们进入正题,首先启动好docker,本节带大家安装的是7.6.2的版本,这个版本相对好一些,控制台的功能也都很完善。

执行已下命令获取官方镜像, 打开cmd/mac终端

# es镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.6.2
# kibana镜像
docker pull docker.elastic.co/kibana/kibana:7.6.2

kibana它是一个可视化的平台,我们查看数据就是通过它,es只是用作数据引擎,市面上也有一些第三方的工具,但是官方的这个已经非常完善了,界面也很美观。

紧接着,进入指定安装目录,比方说当前目录叫es,终端进入这个目录后执行一下命令:

# kibana数据挂载的目录
mkdir data/kibana
# 三个节点数据挂载的目录
mkdir data/node1
mkdir data/node2
mkdir data/node3

这一步主要是创建相关的目录,因为后边docker的数据卷会映射到该目录,这样做的目的是防止容器意外销毁后的数据丢失。这里为什么是三个节点,因为es集群至少需要三个节点,这是跟它的内部机制有关,为了防止脑裂现象,这里就不给大家过多展开了

接下来进入data/kibana目录,新建kibana.yml,这个文件是它的配置文件,后边我们会把它映射到docker容器内部

#
## ** THIS IS AN AUTO-GENERATED FILE **
##
#  
#  # Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://es01:9200","http://es02:9200","http://es03:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: zh-CN

elasticsearch.hosts指的是三个es节点,会和这些节点进行通信

进入node1,同样新建配置文件elasticsearch.yml

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: es-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: es01
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["es01","es02","es03"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["es01","es02","es03"]
# bootstrap.memory_lock: true
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: '*'
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
node.master: true

我们把node1作为主节点,也就是老大,node.master: true可以配置。为了使它支持中文分词,我们给它安装一下插件, 到仓库下载指定版本的插件https://github.com/medcl/elasticsearch-analysis-ik/releases,然后我们解压到node1根目录,然后重新命名为ik目录,然后再新建一个Dockerfile用来重构```es````镜像,没错,后边我们就使用我们重构好的镜像,这样就自动安装好了插件

  • Dockerfile文件内容

FROM docker.elastic.co/elasticsearch/elasticsearch:7.6.2
COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
ADD ik /usr/share/elasticsearch/plugins/ik
ADD ik/config /data/erms/es/node1/ik/config

下面我们进入node2目录,这个目录只需要放配置文件就好了

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: es-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: es02
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["es01","es02","es03"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["es01","es02","es03"]
# bootstrap.memory_lock: true
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: '*'
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
node.data: true

这里我们指定为数据节点node.data: true用来做副本

同样的node3

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: es-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: es03
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["es01","es02","es03"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["es01","es02","es03"]
# bootstrap.memory_lock: true
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: '*'
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
node.data: true

然后我们回到根目录(es),新建一个docker-compose.yaml,我们使用docker-compose来编排我们的容器,默认安装好docker desktop就自动给我们安装好了docker-compose

version: '3'
services:
 es01:
   image: ${image}
   container_name: es01
   environment:
     - discovery.seed_hosts=es02,es03
     - cluster.initial_master_nodes=es01,es02,es03
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
   ulimits:
     memlock:
       soft: -1
       hard: -1
   volumes:
     - ./data/node1/data:/usr/share/elasticsearch/data
     - ./data/node1/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
     - ./data/node1/plugins:/usr/share/elasticsearch/plugins
   ports:
     - 9200:9200
   networks:
     - elastic
 es02:
   image: ${image}
   container_name: es02
   environment:
     - discovery.seed_hosts=es01,es03
     - cluster.initial_master_nodes=es01,es02,es03
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
   ulimits:
     memlock:
       soft: -1
       hard: -1
   volumes:
     - ./data/node2/data:/usr/share/elasticsearch/data
     - ./data/node2/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
     - ./data/node2/plugins:/usr/share/elasticsearch/plugins
   ports:
     - 9201:9201
   networks:
     - elastic
 es03:
   image: ${image}
   container_name: es03
   environment:
     - discovery.seed_hosts=es01,es02
     - cluster.initial_master_nodes=es01,es02,es03
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
   ulimits:
     memlock:
       soft: -1
       hard: -1
   volumes:
     - ./data/node3/data:/usr/share/elasticsearch/data
     - ./data/node3/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
     - ./data/node3/plugins:/usr/share/elasticsearch/plugins
   ports:
     - 9202:9202
   networks:
     - elastic
 kibana:
   image: ${image_kibana}
   container_name: kibana
   depends_on:
     - es01
   environment:
     ELASTICSEARCH_URL: http://es01:9200
     ELASTICSEARCH_HOSTS: http://es01:9200
   volumes:
     - ./data/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml
   networks:
     - elastic
   ports:
     - 5601:5601
networks:
 elastic:
   driver: bridge

这个文件有点长,不懂没关系,跟着配就完了。${image}是一个占位符,所以我们还需要指定环境变量,然后新建一个.env

image=m/es
image_kibana=docker.elastic.co/kibana/kibana:7.6.2

m/es这个是我们重构后的镜像名称,下面我们就来重构镜像

进入data/node1执行

docker build -t m/es .

执行完成后,到根目录执行启动命令:

docker-compose up -d

如果你想看实时日志,把-d去掉,这个是后台运行,初次启动,可能要花费一些时间。

启动成功后,我们可以访问一些es1的节点localhost:9200,可以查看节点的信息,如果显示正常,说明已经搭建成功了,下面我们直接进入kibana控制台

http://localhost:5601/,初次进入会让你设置控制台的密码

docker搭建es集群实现过程详解

我们进入控制台,执行一下,有如下输出,至此我们就搭建成功了

docker搭建es集群实现过程详解

如果你想卸载它们,执行docker-compose down就可以了,毕竟这几个家伙特别的吃资源。这里提醒一下大家,如果想尝试到服务器安装,建议新开一个机器,不要直接在生产环境里安装,因为挺吃硬件资源的,会容易出问题

来源:https://juejin.cn/post/7168343822986903582

0
投稿

猜你喜欢

  • 如今,互联网将进入一个崭新的阶段,信息化的发展带动其它产业的发展,各行业都将与它进行更深入的融合和渗透。越来越多的企业已开始从对互联网的认知
  • 有Godaddy主机用户问如果在共享主机上新增了FTP用户,现在想删除的话,该如何操作呢?要想从你的共享主机上删除新增FTP用户,你可以轻松
  • 一。域名管理面板1。在Godaddy账户中点击Domain manage进入域名管理2。域名管理界面如下面两个图片所示,左侧是管理选项,右侧
  • 技术小白,记录一下自己第一次安装Linux系统的过程。首先,请在Windows7下安装VMware虚拟机,这个比较简单,直接从官网下载安装即
  • Twitter联合创始人比兹·斯通北京时间11月20日早间消息,据国外媒体报道,Twitter联合创始人比兹·斯通(Biz Stone)周四
  • 就像家庭中90%的维修活都能用螺丝刀和扳手来完成,Web服务器也不例外。让我们来看看这些工具。1、服务器响应缓慢如果服务器响应缓慢,需要做的
  • 网站不仅仅只包含网页,有时你需要提供用户能下载的文件。将你的文件放到服务器上并在网页中附上链接只是第一步,你还需意识到HTTP响应的头文件影
  • 电子商务的兴起,使的很多中小企业都拥有了自己的服务器。对内用来建立局域网,提升办公效率;对外建立网站,更为广泛地宣传企业产品和形象,争取更多
  • 一 准备redis镜像、容器1.1 下载redis6.0.8docker pull redis:6.0.81.2 准备6台服务器配置文件#
  • 1.命令简介chkconfig 命令用于更新和查询系统服务的运行等级信息。它可查询操作系统在每一个运行等级中会自动执行哪些系统服务,包括各类
  • 目前,在广大站长的殷切期盼下,康盛创想(Comsenz)旗下的UCenter Home(简称UCHome)体验站陆续推出了涂鸦版、凑热闹、在
  • Google对于公司的内部运作一向口风很紧,但是也确有少数消息可能会被无意中透露出来。Google负责搜索品质监督的副总裁Udi Manbe
  • 本文实例讲述了Linux环境(CentOS6.7 64位)下安装subversion1.9.5的方法。分享给大家供大家参考,具体如下:安装环
  • 没有人喜欢垃圾信息,除了制造者本人。如果你正运行着一个 WordPress 博客系统,你可能已经至少安装了一个垃圾信息控制部件,但
  • 曾经想过做很多类型的垃圾站,做采集数据,做友情连接,努力在百度、谷歌等搜索引擎的框架之中。深有感触做好一个垃圾很容易,求一个搞质量的链接很难
  • 当初百度把自己的搜索外延拓展到日本市场的时候,很多人都不以为然,觉得百度的市场还是在国内,但是国内搜索市场的利润空间实在让李彦宏不能满意。扬
  • google和金山词霸合作了。看重的是金山词霸3kw的用户。google表示这三千万的用户,可以直接成为google的搜索用户。他们认为这个
  • 了解网赚的老手,都应该知道,网赚其实是一种思路。遍观现在的网赚项目,网赚教程,遍地飞,其实都是思路的延伸,生发出来的。说什么日赚100,日赚
  • 介绍Kafka是一个分布式的、可分区的、可复制的消息系统。它提供了普通消息系统的功能,但具有自己独特的设计。这个独特的设计是什么样的呢?首先
  • 终于决定把邮件系统重新弄一弄了。因为服务器经常出问题,DNS报错,SMTP也经常出问题。最重要的是,因为以前只有两台服务器,这台服务器上充当
手机版 网站运营 asp之家 www.aspxhome.com