利用docker compose 搭建 elasticsearch 和kibana

4,236 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

本文介绍了从docker compose 搭建 elasticsearch 并安装IK 分词插件,然后再用kibana测试的详细步骤。

利用docker compose 搭建 elasticsearch 和kibana

1. 下载软件

1.1 下载镜像

docker pull  elasticsearch:7.17.1
docker pull  kibana:7.17.1

1.2 下载IK分词插件

从官方网站

Releases · medcl/elasticsearch-analysis-ik · GitHub

下载对应版本的分词插件,因为我们的 elasticsearch 为 7.17.1 所以下载

elasticsearch-analysis-ik-7.17.1.zip

1.3 下载docker compose

查看下载地址

# 下载2.5.0 版本到 /usr/local/bin/docker-compose

wget  -O /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 
# 添加执行权限
chmod u+x /usr/local/bin/docker-compose

# 查看版本
docker-compose -v
# Docker Compose version v2.5.0

2. 安装配置

2.1 准备目录和配置文件

cd /home/bwie/soft
# 创建Elasticsearch配置文件夹
mkdir -p elasticsearch/config
# 创建Elasticsearch数据文件夹
mkdir -p elasticsearch/data
# 创建Elasticsearch插件文件夹(如:ik)
mkdir -p elasticsearch/plugins

mkdir -p elasticsearch/kibana

# 给数据目录添加权限否则在es启动后报错
chmod 777 elasticsearch/data

echo "http.host: 0.0.0.0">>elasticsearch/config/elasticsearch.yml

实际的elasticsearch.yml 文件内容

network.host: 0.0.0.0

kibana.yml 文件内容如下:

elasticsearch.hosts: http://elasticsearch:9200
server.host: "0.0.0.0"
server.name: kibana
xpack.monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: zh-CN

2.2 安装分词插件

把 elasticsearch-analysis-ik-7.17.1.zip 解压后放到一个ik 文件夹中

,并拷贝到 /home/bwie/soft/elasticsearch/plugins 目录中

见下图

image.png

2.3 编写docker-compose 配置文件

在 elasticsearch 文件夹中创建 docker-compose.yml

# 启动 docker-compose up -d && docker-compose logs -f
# 关闭 docker-compose down &&  docker-compose rm -vf
version: '3.2'
services:
  elasticsearch:
    image: elasticsearch:7.17.1
    ports:
      - "9200:9200"
      - "9300:9300"
    container_name: "elasticsearch"
    environment:
      # 单节点     
      - discovery.type=single-node
      - "- ES_JAVA_OPTS=-Xms2G -Xmx4G"
    volumes:
      - /home/bwie/soft/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /home/bwie/soft/elasticsearch/data:/usr/share/elasticsearch/data
      - /home/bwie/soft/elasticsearch/plugins:/usr/share/elasticsearch/plugins
   kibana:
    image: kibana:7.17.1
    ports:
      - "5601:5601"
    container_name: "kibana"
    #restart: always
    depends_on:
      - elasticsearch
    volumes:
      - /home/bwie/soft/elasticsearch/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml

2.4 启动并查看状态

docker-compose up -d && docker-compose logs -f

查看状态

docker-compose ps

NAME                COMMAND                  SERVICE             STATUS              PORTS
elasticsearch       "/bin/tini -- /usr/l??   elasticsearch       running             0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9200->9200/tcp, :::9300->9300/tcp
kibana              "/bin/tini -- /usr/l??   kibana              running             0.0.0.0:5601->5601/tcp, :::5601->5601/tcp

3. 测试分词器

访问地址 http://你的ip:9200/ 这是es的基本信息

看到

{
  "name" : "es01",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "e3bQRkN7SFm68k4zB1ftNA",
  "version" : {
    "number" : "7.17.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "e5acb99f822233d62d6444ce45a4543dc1c8059a",
    "build_date" : "2022-02-23T22:20:54.153567231Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

访问地址 http://你的ip:5601/ 这是kibana 控制台

image.png

image.png

测试IK分词器

POST _analyze
{
  "analyzer":"ik_smart",
  "text":"以下判断正确的是2"
}

返回

#! Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html to enable security.
{
  "tokens" : [
    {
      "token" : "以下",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "判断",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "正确",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "的",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_CHAR",
      "position" : 3
    },
    {
      "token" : "是",
      "start_offset" : 7,
      "end_offset" : 8,
      "type" : "CN_CHAR",
      "position" : 4
    },
    {
      "token" : "2",
      "start_offset" : 8,
      "end_offset" : 9,
      "type" : "ARABIC",
      "position" : 5
    }
  ]
}

image.png