docker-compose 搭建 Elasticsearch + kibana
本文記錄使用docker-compose 搭建Elasticsearch + kibana 的過程,記錄必要步驟以及所經歷的坑
1.版本介紹
- jdk:1.8
- elasticsearch:7.8.0
- kibana:7.8.0
- docker-compose:1.28.6
- Linux: CentOS Linux release 7.9.2009
2.前提准備
-
調整用戶的mmap計數,否則啟動es時會出現內存不足的情況
# 查看當前限制 sysctl vm.max_map_count vm.max_map_count = 65530臨時修改,不需要重啟(重啟后 失效)
sysctl -w vm.max_map_count=262144永久修改,修改后需要重啟生效
vi /etc/sysctl.cof # 增加 如下內容 vm.max_map_count = 262144 -
創建目錄及對應文件夾,修改文件夾的權限
在home文件夾下新建es文件夾 ,在es文件夾中新建 data 、logs、plugins文件夾 及 docke-compose.yml 文件

修改data logs 文件夾的權限 (不添加權限,后續es啟動時會報錯 如:Error opening log file 'logs/gc.log': Permission denied)
chmod 777 -R ./logs chmod 777 -R ./data -
創建網絡環境
docker network create elk
3.docker-compose.yml
version: "3.2"
services:
elasticsearch:
image: elasticsearch:7.8.0
container_name: elasticsearch
restart: always
volumes:
- ./data:/usr/share/elasticsearch/data
- ./logs:/usr/share/elasticsearch/logs
- ./plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
environment:
# 開啟內存鎖定
- bootstrap.memory_lock=true
# 修改jvm內存
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
# 指定單節點啟動
- discovery.type=single-node
ulimits:
# 取消內存相關限制 用於開啟內存鎖定
memlock:
soft: -1
hard: -1
kibana:
image: kibana:7.8.0
container_name: kibana
restart: always
ports:
- 5601:5601
environment:
ELASTICSEARCH_HOSTS: http://elasticsearch:9200
I18N_LOCALE: zh-CN
es-head:
image: mobz/elasticsearch-head:5-alpine
container_name: es-head
restart: always
ports:
- 9100:9100
networks:
default:
external:
## 此處名稱與上面創建的網絡名稱一致
name: elk
# 以后台的方式運行容器。不會在終端上打印運行日志
docker-compoes up -d
#停止
docke-compose down -v
4.關閉防護牆
如果Kibana出現 Unable to revive connection: https://elasticsearch:9200/這種錯誤,需要關閉防火牆
#查看防火牆狀態
[root@centos es]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
#臨時關閉防護牆
systemctl stop firewalld
#永久關閉
systemctl disable firewalld
5.測試訪問
elasticsearch:localhost:9200

Kibana:http://localhost:5601/

6.安裝ik分詞器
在es/plugins文件夾下新建ik文件夾,下載,解壓,重啟即可
#下載es 對應版本ik
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
# 解壓
unzip elasticsearch-analysis-ik-7.8.0.zip
重啟后查看es日志已經加載了ik分詞器插件

備注:虛擬機基於virtual box管理 還需要創建端口轉發

