Docker Compose+Dockerfile搭建elasticsearch7.8.0与kibana

警告
本文最后更新于 2023-02-19,文中内容可能已过时,请谨慎使用。
  • Ubuntu-20.04 LTS
  • elasticsearch-7.8.0
  • kibana-7.8.0
  • Docker version 23.0.0
  • Docker Compose version v2.15.1

修改vm.max_map_count, 打开配置文件

vim /etc/sysctl.conf

最后添加vm.max_map_count=655360

/data/elasticsearch/: elasticsearch数据和配置文件文件夹

/data/kibana/: kibana数据和配置文件文件夹

/data/dockers/: docker-compose配置文件文件夹

创建文件夹

mkdir -p /data/elasticsearch/data
mkdir -p /data/elasticsearch/logs
mkdir -p /data/elasticsearch/config
mkdir -p /data/elasticsearch/plugins

编写elasticsearch配置文件

vim /data/elasticsearch/config/elasticsearch.yml

配置内容如下:

#cluster.name: my-application
node.name: node-1
#node.attr.rack: r1
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/logs
#bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
#discovery.seed_hosts: ["host1", "host2"]
cluster.initial_master_nodes: ["node-1"]
#gateway.recover_after_nodes: 3
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.audit.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.keystore.type: PKCS12
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.type: PKCS12

创建文件夹

mkdir -p /data/kibana/config

创建kibana配置文件

vim /data/kibana/config/kibana.yml

配置内容如下:

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://elasticsearch:9200"]
elasticsearch.username: "elastic"
elasticsearch.password: "w12345" # elastic用户密码,如果在网页中修改了,这里也要修改
i18n.locale: "zh-CN"

创建文件夹

mkdir -p /data/dockers/elasticsearch
cd /data/dockers/elasticsearch

编写docker-compose.yml

vim /data/dockers/elasticsearch/docker-compose.yml

配置内容如下:

version: '3'
services:
  elasticsearch:
    image: elasticsearch
    build:
        context: .
        dockerfile: ES-Dockerfile
    container_name: elasticsearch
    restart: always
    volumes:
      - /data/elasticsearch/data:/usr/share/elasticsearch/data:rw
      - /data/elasticsearch/logs:/usr/share/elasticsearch/logs:rw
      - /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:rw
      - /data/elasticsearch/plugis:/usr/share/elasticsearch/plugis:rw
    environment:
      - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
    hostname: elasticsearch
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - "9201:9200"
    networks:
      - esnet
  kibana:
    image: docker.io/kibana:7.8.0
    container_name: kibana
    ports:
      - 5602:5601
    volumes:
      - /data/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml:rw
    networks:
      - esnet
      
networks:
  esnet:

创建ES-Dockerfile

vim /data/dockers/elasticsearch/ES-Dockerfile

配置内容如下:

FROM elasticsearch:7.8.0
USER elasticsearch
 
#生成证书,密码可自己配置,与kibana.yml中一致
RUN bin/elasticsearch-certutil ca --out config/elastic-stack-ca.p12 --pass w12345
 
#生成证书,密码可自己配置,与kibana.yml中一致
RUN bin/elasticsearch-certutil cert --ca config/elastic-stack-ca.p12 --ca-pass w12345 --out config/elastic-certificates.p12 --pass w12345
 
#创建keystore
RUN bin/elasticsearch-keystore create
 
#将密码添加至keystore
RUN sh -c '/bin/echo -e "w12345" | sh bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password'
RUN sh -c '/bin/echo -e "w12345" | sh bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password'
 
#文件赋权限
RUN chmod 777 /usr/share/elasticsearch/config/elastic-certificates.p12
RUN chmod 777 /usr/share/elasticsearch/config/elastic-stack-ca.p12
  • 添加启动用户

    添加用户elasticsearch

    adduser elasticsearch
    

    创建用户密码

    passwd elasticsearch
    

    授权

    cd /data/
    chown -R elasticsearch elasticsearch
    cd dockers/
    chown -R elasticsearch elasticsearch
    chmod 777 -R elasticsearch /data/
    
  • elasticsearch用户添加至docker用户组

    加入docker用户组

    usermod -G docker elasticsearch
    

    重启docker服务

    systemctl restart docker
    
  1. 切换用户

    su elasticsearch
    
  2. 构建

    cd /data/dockers/elasticsearch/
    
    docker compose build
    
  3. 启动

    应先前台启动,查询是否有异常、报错

    docker compose up
    

    再开一个窗口进入elasticsearch docker设置密码(docker容器):

    docker exec -it elasticsearch /bin/bash
    

    执行

    ./bin/elasticsearch-setup-passwords interactive --verbose
    

    按提示信息输入密码w12345

    此处密码需与kibana.yml中相同。

    /images/all/image-20230219171347797.png

    因为上面配置时,宿主机使用5602对应容器的5601端口,所以将安全组放开、防火墙放 开端口后(或nginx转发后),可在自己电脑浏览器中访问:服务器ip:5602

/images/all/image-20230219171545754.png
  1. 后台启动

    前台启动无误后按ctrl+c结束,使用docker compose up -d改为后台启动

    /images/all/image-20230219171741084.png

上面在docker-compose.yml中配置的是1G内存。

vim /data/dockers/elasticsearch/docker-compose.yml

即第15行

 - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"

修改完成后依次执行以下命令: 先停止现有docker—>销毁—>构建—>启动

cd /data/dockers/elasticsearch
docker compose stop
docker compose down
docker compose build
docker compose up -d

相关文章