Elastic (ELK) Stack產品相關簡介請看這篇博客
Elasticsearch
參考地址:
https://github.com/elastic/elasticsearch/blob/main/distribution/src/config/elasticsearch.yml
https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html
鏡像
elasticsearch
有兩個鏡像,都是一樣的pull
必須制定tag
docker.elastic.co
需要使用國內源,建議使用阿里源
鏡像 | 網站 |
---|---|
elasticsearch | https://hub.docker.com/_/elasticsearch |
docker.elastic.co/elasticsearch/elasticsearch | https://www.docker.elastic.co/r/elasticsearch |
docker pull elasticsearch:8.5.0
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.5.0
部署
安裝之前找一個目錄docker
掛載目錄新建一個elasticsearch.yml
。建議目錄:/docker/data/elasticsearch/config/elasticsearch.yml
。這是elasticsearch的配置文件。初始化只有一個http.host
。
建議配置如下:
# 節點名稱
node.name: elasticsearch
# 綁定host,代表當前節點的ip
network.host: 0.0.0.0
# 如果要使用head,那麼需要解決跨域問題,使head插件可以訪問es
# 是否支持跨域,默認爲false
http.cors.enabled: true
# 當設置允許跨域,默認爲*
http.cors.allow-origin: "*"
# 跨域請求頭
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length
http.cors.allow-credentials: true
# java優化內存使用
bootstrap.memory_lock: true
#單節點部署
#discovery.seed_hosts: "single-node"
# 多節點集羣
#discovery.seed_hosts: ["host1", "host2"]
# 開啓X-Pack認證,配置用戶名密碼
xpack.security.enabled: false
# 開啓Htpps
xpack.security.transport.ssl.enabled: false
# 設置對外服務的http端口,默認爲9200
#http.port: 9200
# 設置節點間交互的tcp端口,默認是9300
#transport.tcp.port: 9300
#集羣名稱
#cluster.name: my-application
給本地目錄添加寫入權限,不然會報錯failed to obtain node locks
,一定要加
chown -R 1000:1000 /docker/data
chmod -R 777 /docker/data
起容器
mkdir -p /docker/data/elasticsearch/config
vi /docker/data/elasticsearch/config/elasticsearch.yml
docker run --name elasticsearch -d -p 9200:9200 -p 9300:9300 \
-e ELASTIC_PASSWORD=123456 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /docker/data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /docker/data/elasticsearch/data:/usr/share/elasticsearch/data \
-v /docker/data/elasticsearch/plugins:/usr/share/elasticsearch/plugins elasticsearch:8.5.0
參數 | 含義 |
---|---|
9200 | http端口 |
9300 | TCP端口 |
discovery.type=single-node | 單節點部署 |
ELASTIC_PASSWORD | elastic密碼,當認證沒開時此變量無用 |
ES_JAVA_OPTS | 限制ES內存。Xms64m表示初始內存64m,Xmx512m表示足底啊512m,最大1g內存寫法:Xmx1g |
開啓認證
修改elasticsearch.yml
文件並重啓容器
ES8.0版本後默認開啓
xpack.security.enabled: true
root用戶進入容器修改登錄用戶密碼
docker exec -it -u root elasticsearch bash
# 系統自動生成密碼
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords auto
# 自定義密碼
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
# 隨機重新設置用戶密碼
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
# 獲取用戶kibana的token
需要在配置文件配置 xpack.security.enrollment.enabled: true 並開啓Https正式認證纔可以使用
/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token --scope kibana
總共有以下幾個用戶,其中elastic
是超級管理員。
- elastic
- logstash_system
- kibana
- kibana_system
- apm_system
- beats_system
- remote_monitoring_user
Api修改用戶密碼
# 修改elastic密碼爲123456
curl -u elastic:123456 -X POST 'http://192.168.1.5:9200/_security/user/kibana/_password' -H 'Content-Type: application/json' -d '{ "password" : "1234567"}'
訪問
部署成功後瀏覽器訪問http://192.168.1.5:9200/
顯示以下內容表示部署成功
Kibana
鏡像
參考地址:
https://github.com/elastic/kibana/blob/main/config/kibana.yml
https://www.elastic.co/guide/en/kibana/current/settings.html
https://www.elastic.co/guide/cn/kibana/current/docker.html
kibana
有兩個鏡像,都是一樣的。pull
必須制定tag
docker.elastic.co
需要使用國內源,建議使用阿里源
鏡像 | 網站 |
---|---|
kibana | https://hub.docker.com/_/kibana |
docker.elastic.co/kibana/kibana | https://www.docker.elastic.co/r/kibana |
docker pull kibana:8.5.0
docker pull docker.elastic.co/kibana/kibana:8.5.0
部署
部署之前新建一個kibana.yml
配置文件。內容如下:
#server.port: 5601
#server.name: kibana
# Host地址,默認"0",這裏要設置下,不然宿主機無法映射端口
server.host: "0.0.0.0"
# elasticsearch地址,這裏不能填0.0.0.0和127.0.0.1,只能填IP或者容器網絡
elasticsearch.hosts: ["http://192.168.1.5:9200"]
# elasticsearch認證賬號,配置文件不能使用elastic,建議使用kibana或kibana_system賬號(讀取elasticsearch的賬號)
elasticsearch.username: "kibana_system"
elasticsearch.password: "123456"
# 設置kibana中文顯示
i18n.locale: "zh-CN"
xpack.monitoring.ui.container.elasticsearch.enabled: true
起容器
mkdir -p /docker/data/kibana/config
vi /docker/data/kibana/config/kibana.yml
docker run -d --name kibana -p 5601:5601 -v /docker/data/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:8.5.0
指定es url
docker run -d --name kibana -p 5601:5601 -e ELASTICSEARCH_HOSTS=http://elasticsearch:9200 -v /docker/data/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:8.5.0
訪問
部署成功後瀏覽器訪問http://192.168.1.5:5601/
顯示以下內容表示部署成功
kibana認證讀取自elasticsearch
Apm Server
參考地址
https://www.elastic.co/guide/en/apm/guide/current/apm-quick-start.html
https://github.com/elastic/apm-server/blob/main/apm-server.yml
鏡像
apm-server
有兩個鏡像。pull
必須制定tag
docker.elastic.co
需要使用國內源,建議使用阿里源
鏡像 | 網站 |
---|---|
elastic/apm-server | https://hub.docker.com/r/elastic/apm-server |
docker.elastic.co/apm/apm-server:8.5.0 | https://www.docker.elastic.co/r/apm |
docker pull elastic/apm-server
docker pull docker.elastic.co/apm/apm-server:8.5.0
部署
部署之前新建一個apm-server.yml
配置文件。內容如下:
apm-server:
#Host
host: "0.0.0.0:8200"
#apm-server自定義認證
auth:
secret_token: "123456"
rum:
enabled: true
#kibana地址
kibana:
enabled: true
host: "http://192.168.1.5:5601"
username: "elastic"
password: "123456"
#ES地址
output.elasticsearch:
enabled: true
hosts: ["http://192.168.1.5:9200"]
username: "elastic"
password: "123456"
apm-server.data_streams.wait_for_integration: false
Run
mkdir -p /docker/data/kibana/config
vi /docker/data/kibana/config/apm-server.yml
docker run -d -p 8200:8200 --name=apm-server -u=root --volume="/docker/data/apm-server.yml:/usr/share/apm-server/apm-server.yml" docker.elastic.co/apm/apm-server:8.5.0 --strict.perms=false -e
訪問
部署成功後瀏覽器訪問http://192.168.1.5:8200/
顯示以下內容表示部署成功。開啓認證訪問是空白界面。因爲kibana
關聯應用比較多,最好使用docekr logs kibana
查看容器是否有錯誤日誌。
Elasticsearch-head
Elasticsearch-head 是一個搜索ES的Web程序。
有兩種方式可以使用:
- 谷歌商店下載瀏覽器插件Elasticsearch-head使用,谷歌商店地址
- Docker部署站點服務
參考文檔:
https://github.com/mobz/elasticsearch-head
https://hub.docker.com/r/mobz/elasticsearch-head
拉取鏡像
docker pull mobz/elasticsearch-head:5
Run
docker run --name=elasticsearch-head -p 9100:9100 -d mobz/elasticsearch-head:5
訪問站點http://192.168.1.5:9100/
如果ES開啓了認證,則需要使用在Url中加上認證:http://192.168.1.5:9100/?auth_user=elastic&auth_password=123456
在訪問並且ES的elasticsearch.yml
配置文件需要添加跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length
Docker-Compose 編排部署
ELK
ELK三個版本號建議保持一致
Github地址
https://github.com/deviantony/docker-elk/blob/main/docker-compose.yml
簡化版本
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.13.2
restart: always
container_name: elasticsearch
hostname: elasticsearch
environment:
- discovery.type=single-node
ports:
- 9200:9200
- 9300:9300
kibana:
image: docker.elastic.co/kibana/kibana:7.13.2
restart: always
container_name: kibana
hostname: kibana
environment:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
ports:
- 5601:5601
depends_on:
- elasticsearch
apm_server:
image: docker.elastic.co/apm/apm-server:7.13.2
restart: always
container_name: apm_server
hostname: apm_server
command: --strict.perms=false -e
environment:
- output.elasticsearch.hosts=["elasticsearch:9200"]
ports:
- 8200:8200
depends_on:
- kibana
- elasticsearch
Elasticsearch + Kibana + Fleet-server + Metricbeat
Github地址:
https://github.com/elastic/apm-server/blob/main/docker-compose.yml