概要

在工作过程中,经常需要测试环境搭建Es环境,本文基于Es V8.12.2来演示如何快速搭建单节点Es和kibana。

服务器默认已按装docker

一、Es

1:拉取镜像

docker pull elasticsearch:8.12.2

2:准备环境

mkdir -p /home/docker/es  #创建挂载目录
mkdir -p /home/docker/es/logs
chmod 777 /home/docker/es/logs #授权
mkdir -p /home/docker/es/data
chmod 777 /home/docker/es/data #授权
docker run -d --name es_temp -p 9200:9200 -p 9300:9300 elasticsearch:8.12.2 #起一个临时容器
docker cp es_temp:/usr/share/elasticsearch/config /home/docker/es/  #复制Es配置文件
docker rm es_temp #删除临时容器

3:修改配置文件
vim /home/docker/es/config/elasticsearch.yml
配置文件默认是集群配置,而我们测试环境只需单节点,所以要调整如下:

cluster.name: "docker-single"
network.host: 0.0.0.0

#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 20-03-2024 19:23:28
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.security.enabled: false  #关闭认证

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new ciluster with the current node only
discovery.type: single-node  #单节点模式
# Additional nodes can still join the cluster later
#cluster.initial_master_nodes: ["986f0573b289"]

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

4:启动容器

docker run --privileged -d --name es -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -v /home/docker/es/config:/usr/share/elasticsearch/config -v /home/docker/es/data:/usr/share/elasticsearch/data -v /home/docker/es/logs:/usr/share/elasticsearch/logs -p 9200:9200 -p 9300:9300 elasticsearch:8.12.2

5:检测

curl -X GET http://127.0.0.1:9200/_cluster/health?pretty
{
  "cluster_name" : "docker-single",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "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
}

二、kibana

与Es兼容的kibana版本,按需拉取镜像即可。
1:拉取镜像

docker pull kibana:8.12.2

2:启动容器
kibana就不挂载了,其配置文件/usr/share/kibana/config/kibana.yml,也可以挂载出来做一些配置。

docker run -d --name kibana -p 5601:5601 \
-e ELASTICSEARCH_HOSTS=http://200.200.107.192:9200 \ #Es Url
-e ELASTICSEARCH_USERNAME=root \  #Es 账号,Es不开启认证可不设置
-e ELASTICSEARCH_PASSWORD=123456  \ #Es 密码 ,Es不开启认证可不设置
-e I18N_LOCALE=zh-CN \ #汉化
kibana:8.12.2

3:检测
浏览器访问宿主机ip:5601,即可验证,成功如下:
kibana

三、dcoker compose管理

我们可以用docker compose管理es和kibana。

mkdir -p /etc/docker
vim /etc/docker/docker-compose.yml

内容如下:

# yaml 配置
version: '3'
services:
  es:
    image: "elasticsearch:8.12.2"
    privileged: true
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - "/home/docker/es/config:/usr/share/elasticsearch/config"
      - "/home/docker/es/data:/usr/share/elasticsearch/data"
      - "/home/docker/es/logs:/usr/share/elasticsearch/logs"
    environment:
      - ES_JAVA_OPTS=-Xms512m -Xmx512m
      - TZ=Asia/Shanghai
    container_name: "es"
  kibana_srv:
    image: "kibana:8.12.2"
    ports:
      - "5601:5601"
    environment:
      - ELASTICSEARCH_HOSTS=http://200.200.107.192:9200
      - ELASTICSEARCH_USERNAME=root
      - ELASTICSEARCH_PASSWORD=123456
      - I18N_LOCALE=zh-CN
    container_name: "kibana"
docker compose -f /etc/docker/docker-compose.yml up es
docker compose -f /etc/docker/docker-compose.yml stop es
docker compose -f /etc/docker/docker-compose.yml up kibana_srv
docker compose -f /etc/docker/docker-compose.yml stop kibana_srv

四、参考

1]:Docker 容器中运行 Kibana
2]:Kibana 的安装

Logo

一起探索未来云端世界的核心,云原生技术专区带您领略创新、高效和可扩展的云计算解决方案,引领您在数字化时代的成功之路。

更多推荐