ELK stack部署

1. docker创建网络

docker network create elastic

docker run 命令说明:

--name xxx 命名
--net xxx 设置网络
-p 物理机端口:容器端口 设置端口
-e "xxx=abc" 设置环境变量
-d 后台服务启动

2. es部署:

命令:

 docker run --name es01-test --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.15.0

3. kibana部署:

docker run --name kib01-test --net elastic -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://es01-test:9200" docker.elastic.co/kibana/kibana:7.15.0

4. 构造filebeat基础镜像,dockerfile如下(java-agent忽略,是为了做skywalking的配置):

FROM jdk:8u301
VOLUME /tmp
ADD filebeat-7.15.0-linux-x86_64.tar.gz /filebeat/
ADD filebeat.yml /config/
#COPY README.md .revision* /
COPY java-agent /java-agent

4.1 filebeat.yml配置文件如下:


filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /app/Logs/*/romens-*.log
    #- c:\programdata\elasticsearch\logs\*
  fields:
# ** 此处读取环境变量,作为es的index名;配合logstash使用 **
      index_name: ${INDEX_NAME:romens_default_service}
#    level: debug
#    review: 1

  multiline.pattern: ^\[
  multiline.negate: true
  multiline.match: after
# ============================== Filebeat modules ==============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

# ======================= Elasticsearch template setting =======================

setup.template.settings:
  index.number_of_shards: 1
  #index.codec: best_compression
  #_source.enabled: false


# ------------------------------ Logstash Output -------------------------------
output.logstash:
  hosts: ["logstash-test:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

# ================================= Processors =================================
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

4.2 构造基础镜像:

在如下目录下执行命令,构造基础镜像


image.png

命令:

docker build -t jdk:8u301_skywalking_filebeat .

5. 基于基础镜像,以容器的方式运行服务:

5.1 设置启动脚本 run.sh:

#!/bin/bash
java -javaagent:/java-agent/skywalking-agent.jar -Dserver.port=80 -jar /app.jar &
/filebeat/filebeat-7.15.0-linux-x86_64/filebeat -e -c /config/filebeat.yml

5.2 打服务镜像:

FROM jdk:8u301_skywalking_filebeat
VOLUME /tmp
# 设置skywalking环境变量
ENV SW_AGENT_NAME romens-promotion-stand-webapp
ENV SW_AGENT_COLLECTOR_BACKEND_SERVICES 192.168.147.1:11800
# 设置日志索引环境变量
ENV INDEX_NAME romens-promotion-stand-webapp
COPY romens-promotion-stand-webapp-1.0.0-SNAPSHOT.jar app.jar
COPY run.sh run.sh
ENTRYPOINT ["sh","./run.sh"]

5.3 在如下目录执行打镜像命令:

image.png
docker build -t promotion:v2 .

5.4 启动服务

等第6步logstash部署完成并启动之后,再启动服务:

docker run --net elastic -p 41001:80 -d promotion:v2

6. 部署logstash

6.1 logstash读取filebeat的配置文件如下:

input{
    beats {
        port => 5044
    }
}
 
output{ 
#   stdout { 
#       codec => rubydebug
#   }
    elasticsearch{
        #es集群地址
        hosts => ["es01-test:9200"]
#       index_name为filebeat.yml文件中的配置
        index => "%{[fields][index_name]}-%{+YYYY-MM-dd}"
    }
}

6.2 dockerfile如下:

FROM docker.elastic.co/logstash/logstash:7.15.0
COPY config/logstash-filebeat.conf /config/logstash-filebeat.conf
COPY config/logstash.yml /config/logstash.yml
EXPOSE 5044
CMD logstash -f /config/logstash-filebeat.conf

6.3 在如下目录执行构造镜像命令:

image.png
docker build -t logstash:7.15.0 .

6.4 启动logstash

docker run --name logstash-test --net elastic -p 5044:5044 logstash:7.15.0

6.5 启动完毕之后,执行5.4

你可能感兴趣的:(ELK stack部署)