Technology Sharing

Install ElasticSearch in Docker

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

This article refers to the following two articles

✅ElasticSearch&Kibana deployment · Cloud Effect Thoughts · Enterprise Knowledge Base (aliyun.com)

Install ElasticSearch & Kibana in Docker - Feishu

Install elasticsearch

Use docker to download es:

docker pull elasticsearch:8.13.0

Mount configuration

Create a directory to hang on the file

  1. mkdir -p /home/docker/es/config
  2. mkdir -p /home/docker/es/data
  3. mkdir -p /home/docker/es/plugins
  4. mkdir -p /home/docker/es/logs
  5. #权限
  6. chmod 777 /home/docker/es/config
  7. chmod 777 /home/docker/es/data
  8. chmod 777 /home/docker/es/plugins
  9. chmod 777 /home/docker/es/logs

Edit /home/docker/es/config/elasticsearch.yml file

  1. cluster.name: "nfturbo-cluster"
  2. network.host: 0.0.0.0
  3. http.cors.enabled: true
  4. http.cors.allow-origin: "*"
  5. xpack.security.enabled: true

Start the image

  1. docker run --name elasticsearch
  2. -p 9200:9200
  3. -p 9300:9300
  4. -e "discovery.type=single-node"
  5. -e ES_JAVA_OPTS="-Xms256m -Xmx512m"
  6. -v /home/docker/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  7. -v /home/docker/es/data:/usr/share/elasticsearch/data
  8. -v /home/docker/es/plugins:/usr/share/elasticsearch/plugins
  9. -v /home/docker/es/logs:/usr/share/elasticsearch/logs
  10. -d elasticsearch:8.13.0

Set the built-in user password

For setting the Elasticsearch built-in user password in a Docker environment, it is recommended to use elasticsearch-setup-passwords auto command, because it automatically generates a random password for the built-in user and displays it directly on the console. This method is more suitable for automated deployment or scripted operations.

If you wish to manually enter the password and set it interactively, you can use elasticsearch-setup-passwords interactive command. This method is suitable for interactive password setting, and you can manually enter the password of each user according to your needs.

  1. # 进入es容器内部
  2. docker exec -it a46f2f8bdfd7 /bin/bash
  3. #手动设置用户密码
  4. elasticsearch-setup-passwords interactive
  5. #重启es容器

However, this does not work. You will encounter a problem when you start kibana.BecauseLatest version Elasticsearch Introduced stricter security policies, especially regarding system indexes that Kibana needs to access. Specifically, the error message indicates that theelastic A superuser account, but this account is not allowed to write to the system indexes required by Kibana

Error: [config validation of [elasticsearch].username]: value of "elastic" is forbidden

Enter the es container, add a new user, set role permissions, create a password according to the prompts, and confirm the password again

  1. bin/elasticsearch-users useradd gxj
  2. #密码123456
  3. bin/elasticsearch-users roles -a superuser gxj
  4. bin/elasticsearch-users roles -a kibana_system gxj

Warning is fine, ignore it

WARNING: Owner of file [/usr/share/elasticsearch/config/users_roles] used to be [root], but now is [elasticsearch]

Browser access

ip:9200, Remember to open the mapping in the firewallport

Install Kibana

Download kibana using docker

docker pull kibana:8.13.0

View es ip

docker inspect elasticsearch|grep IPAddress

Mount configuration

  1. #创建挂载文件
  2. touch /home/docker/es/config/kibana.yml
  3. #权限
  4. chmod 777 /home/docker/es/config/kibana.yml

editkibana.yml, you need to set the es ip you just found toelasticsearch.hostsOther configurations can be adjusted appropriately

  1. server.name: kibana
  2. #server.port: 5601
  3. server.host: 0.0.0.0
  4. #改成 es 的内网 ip
  5. elasticsearch.hosts: [ "http://172.17.0.2:9200" ]
  6. elasticsearch.username: "gxj"
  7. elasticsearch.password: "123456"
  8. xpack.monitoring.ui.container.elasticsearch.enabled: true
  9. i18n.locale: "zh-CN"

Start Kibana

  1. docker run --name kibana
  2. -p 5601:5601
  3. -v /home/docker/es/config/kibana.yml:/usr/share/kibana/config/kibana.yml
  4. -d kibana:8.13.0

Browser access

http://ip:5601, Remember to open the mapping in the firewallport

Enter username (gxj) and password (123456) to access

 
  1. #kibana容器运行日志
  2. docker logs kibana