Docker安装部署ElasticSearch

参考文档:

https://blog.csdn.net/qq_37726813/article/details/129917352

https://blog.csdn.net/yangkei/article/details/126837326

https://blog.csdn.net/yangkei/article/details/126837326

1.部署单点ElasticSearch

因为我们还需要部署kibana容器,因此需要让ElasticSearchkibana容器互联。这里先创建一个网络:

# 建创建bridge网络
docker network create es-net
 
# 查看docker 网络
docker network ls
 
# 查看mysqlnet网络详细信息
docker network inspect es-net

1.2.拉取镜像

考虑到ElasticSearch的兼容性,这里ElasticSearchkibana统一下载8.8.1版本

# 拉取镜像
docker pull elasticsearch:8.8.1

1.3.第一次执行docker脚本

docker run -it \
    -p 9200:9200 \
    -p 9300:9300 \
    --name es \
    --net es-net \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -e "discovery.type=single-node" \
    -e LANG=C.UTF-8 \
    -e LC_ALL=C.UTF-8 \
    elasticsearch:8.6.2

1.4.创建Elasticsearch挂载目录

mkdir -p ~/data/elasticsearch

1.5.给创建的文件夹授权

sudo chown -R 1000:1000 ~/data/elasticsearch

1.6.将容器内的文件复制到主机上

docker cp es:/usr/share/elasticsearch/config ~/data/elasticsearch
docker cp es:/usr/share/elasticsearch/data ~/data/elasticsearch
docker cp es:/usr/share/elasticsearch/plugins ~/data/elasticsearch
docker cp es:/usr/share/elasticsearch/logs ~/data/elasticsearch

1.7.删除容器

docker rm -f es

1.8.修改docker脚本,增加-v挂载目录和-d参数

docker run -d \
	--name es \
    --privileged \
    --network es-net \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -e "discovery.type=single-node" \
 	-e LANG=C.UTF-8 \
 	-e LC_ALL=C.UTF-8 \
    -p 9200:9200 \
    -p 9300:9300 \
    -v es-data:~/data/elasticsearch/data \
    -v es-plugins:~/data/elasticsearch/plugins \
    -v es-logs:~/data/elasticsearch/logs \
elasticsearch:8.8.1

1.9.修改配置~/data/elasticsearch/config/elasticsearch.yml

增加:xpack.monitoring.collection.enabled: true
说明:添加这个配置以后在kibana中才会显示联机状态,否则会显示脱机状态

1.10.重启容器

docker restart es

1.11.Elasticsearch.8X以上默认开启了X-Pack安全功能

说明:请求Elasticsearch必须使用https

测试:https://127.0.0.1:9200/

1.12.如果账户密码忘了,设置修改账号密码如下:

docker exec -it es /bin/bash

cd /usr/share/elasticsearch/bin
./elasticsearch-setup-passwords interactive

 ElasticSearch设置后账户:elastic,密码:123456

 

2.部署kibana

kibana可以给我们提供一个ElasticSearch的可视化界面,便于我们使用。

2.1.拉取镜像

docker pull kibana:8.8.1

2.2.第一次执行docker启动脚本

docker run -it \
 -d \
 --restart=always \
 --log-driver json-file \
 --log-opt max-size=100m \
 --log-opt max-file=2 \
 --name kibana \
 -p 5601:5601 \
 --net es-net \
kibana:8.8.1  

2.3.创建kibana挂载目录

mkdir -p ~/data/kibana

2.4.给创建的文件夹授权

sudo chown -R 1000:1000 ~/data/kibana

2.5.将容器内的文件复制到主机上

docker cp kibana:/usr/share/kibana/config ~/data/kibana
docker cp kibana:/usr/share/kibana/data ~/data/kibana
docker cp kibana:/usr/share/kibana/plugins ~/data/kibana
docker cp kibana:/usr/share/kibana/logs ~/data/kibana

2.6.删除容器

docker rm -f kibana

2.7.修改配置文件/home/elk8.6.2/kibana/config/kibana.yml:

  • 增加:i18n.locale: “zh-CN”
  • 修改:elasticsearch.hosts: [‘https://172.20.0.2:9200’],将IP改成elasticsearchdocker ip,注意一定要用https
  • 增加,不验证ssl:elasticsearch.ssl.verificationMode: none
  • 设置用户名密码:lasticsearch.username: "kibana_system"、elasticsearch.password: "123456"

 

2.8.修改docker启动脚本,增加挂载目录

docker run -it \
 -d \
 -e ELASTICSEARCH_HOSTS=https://es:9200 \
 -p 5601:5601 \
 --restart=always \
 --log-driver json-file \
 --log-opt max-size=100m \
 --log-opt max-file=2 \
 --name kibana \
 --net es-net \
 -v ~/data/kibana/config:/usr/share/kibana/config \
 -v ~/data/kibana/data:/usr/share/kibana/data \
 -v ~/data/kibana/plugins:/usr/share/kibana/plugins \
 -v ~/data/kibana/logs:/usr/share/kibana/logs \
kibana:8.8.1

2.9.查看kibana日志

docker logs -f kibana

此时,在浏览器输入地址访问:http://127.0.0.1:5601,即可看到结果。

账号密码是kibana.yml中配置的用户名密码

2.10.DevTools

kibana中提供了一个DevTools界面:

这个界面中可以编写DSL来操作elasticsearch。并且对DSL语句有自动补全功能。

 

4.部署es集群

es集群可以直接使用docker-compose来完成,不过要求你的Linux虚拟机至少有4G的内存空间

首先编写一个docker-compose文件,内容如下:

 

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
    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=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic
  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
    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=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data02:/usr/share/elasticsearch/data
    networks:
      - elastic
  es03:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
    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=-Xms512m -Xmx512m"
    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

运行docker-compose以启动集群:

docker-compose up

 

遇到的问题:

1、es 登陆时需要用https

2、es 账号密码需要重置,

设置账号密码
docker exec -it es /bin/bash
cd /usr/share/elasticsearch/bin
./elasticsearch-setup-passwords interactive

4、kibana 启动报错:

参考文档:https://blog.csdn.net/yangkei/article/details/126837326 

[2022-09-06T19:47:01.491-04:00][ERROR][elasticsearch-service] Unable to retrieve version information from Elasticsearch nodes. security_exception: [security_exception] 
Reason: unable to authenticate user [kibana_system] for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]
 
1. kibana启动报错,kibana_system用户认证

[2022-09-06T19:47:01.491-04:00][ERROR][elasticsearch-service] Unable to retrieve version information from Elasticsearch nodes. security_exception: [security_exception] Reason: unable to authenticate user [kibana_system] for REST request [/_nodes?filter_path=nodes.*.version%2Cnodes.*.http.publish_address%2Cnodes.*.ip]

解决办法:设置kibana_system用户的密码。
[es@goya1 cert]$ elasticsearch-reset-password -u kibana_system -i
warning: ignoring JAVA_HOME=/app/elasticsearch/jdk; using bundled JDK
This tool will reset the password of the [kibana_system] user.
You will be prompted to enter the password.
Please confirm that you would like to continue [y/N]y
Enter password for [kibana_system]: changeit123
Re-enter password for [kibana_system]: changeit123
Password for the [kibana_system] user successfully reset.

修改,确认kibana.yml文件的配置部分:
# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
elasticsearch.username: "kibana_system"
elasticsearch.password: "changeit123"

  

 



 
 



  


 
posted @ 2023-07-26 18:05  SweetBaby。  阅读(389)  评论(0编辑  收藏  举报