Elastic (ELK) Stack产品部署

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章