ELK 入门学习文章
Dockerfile 以及配置文件
ENV
.env
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
VOLUMES_DRIVER=local
NETWORKS_DRIVER=bridge
ELK_VERSION=7.8.1
ELASTICSEARCH_HOST_HTTP_PORT=9200 ELASTICSEARCH_HOST_TRANSPORT_PORT=9300
KIBANA_HTTP_PORT=5601
|
elasticsearch
Dockerfile
1 2 3 4
| ARG ELK_VERSION=7.6.1 FROM docker.elastic.co/elasticsearch/elasticsearch:${ELK_VERSION}
EXPOSE 9200 9300
|
logstash
Dockerfile
1 2
| ARG ELK_VERSION=7.6.1 FROM logstash:${ELK_VERSION}
|
logstash.yml
1 2 3 4 5 6 7
| http.host: "0.0.0.0"
config.reload.automatic: true path.config: "/usr/share/logstash/pipeline/"
xpack.monitoring.enabled: true xpack.monitoring.elasticsearch.hosts: ["elasticsearch:9200"]
|
kibana
Dockerfile
1 2 3 4
| ARG ELK_VERSION=7.6.1 FROM docker.elastic.co/kibana/kibana:${ELK_VERSION}
EXPOSE 5601
|
kibana.yml
1 2 3 4 5
| server.name: kibana server.host: "0" elasticsearch.hosts: [ "http://elasticsearch:9200" ] xpack.monitoring.ui.container.elasticsearch.enabled: true i18n.locale: "zh-CN"
|
docker-compose
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| version: '3.4'
networks: service: driver: ${NETWORKS_DRIVER}
volumes: elasticsearch: driver: ${VOLUMES_DRIVER} logstash: driver: ${VOLUMES_DRIVER} kibana: driver: ${VOLUMES_DRIVER}
services: elasticsearch: build: context: ./elasticsearch args: - ELK_VERSION=${ELK_VERSION} volumes: - ./elasticsearch/data:/usr/share/elasticsearch/data environment: - cluster.name=cluster - node.name=node - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - cluster.initial_master_nodes=node ulimits: memlock: soft: -1 hard: -1 ports: - "${ELASTICSEARCH_HOST_HTTP_PORT}:9200" - "${ELASTICSEARCH_HOST_TRANSPORT_PORT}:9300" restart: always networks: - service
logstash: build: context: ./logstash args: - ELK_VERSION=${ELK_VERSION} volumes: - './logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml' - './logstash/pipeline:/usr/share/logstash/pipeline' - './logstash/GeoLite2-City:/usr/share/logstash/GeoLite2-City' ports: - '5001:5001' - '5044:5044' environment: LS_JAVA_OPTS: '-Xmx1g -Xms1g' env_file: - .env networks: - service restart: always depends_on: - elasticsearch
kibana: build: context: ./kibana args: - ELK_VERSION=${ELK_VERSION} volumes: - ./kibana/config:/usr/share/kibana/config ports: - "${KIBANA_HTTP_PORT}:5601" depends_on: - elasticsearch restart: always networks: - service
|
部署
1 2 3 4 5
| docker-compose up -d
docker-compose ps
|
测试服务是否可用
elasticsearch
kibana
Logstash
测试标准输入输出
1 2 3 4 5 6 7 8 9 10 11 12 13
| bash-4.2$ /usr/share/logstash/bin/logstash -e 'input { stdin {} } output { stdout { codec => rubydebug} }'
hello world
{ "@version" => "1", "@timestamp" => 2021-09-02T07:57:12.277Z, "host" => "e8ff6e2a9658", "message" => "hello world" }
|
测试输出到文件
1 2 3 4 5 6 7 8 9 10 11 12
| bash-4.2$ /usr/share/logstash/bin/logstash -e 'input { stdin{} } output { file { path => "/tmp/log-%{+YYYY.MM.dd}messages.log"}}'
hello world
Opening file {:path=>"/tmp/log-2021.09.02messages.log"}
bash-4.2$ tail /tmp/log-2021.09.02messages.log {"@timestamp":"2021-09-02T08:04:06.500Z","host":"e8ff6e2a9658","message":"hello world","@version":"1"}
|
测试输出到 elasticsearch
1 2 3 4 5 6 7 8
| bash-4.2$ /usr/share/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch {hosts => ["elasticsearch:9200"] index => "mytest-%{+YYYY.MM.dd}" }}'
bash-4.2$ curl http://elasticsearch:9200/mytest-2021.09.02 {"mytest-2021.09.02":{"aliases":{},"mappings":{"properties":{"@timestamp":{"type":"date"},"@version":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"host":{"type":"text","fields":{"keyword":{"type":"keywo rd","ignore_above":256}}},"message":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"settings":{"index":{"creation_date":"1630570225131","number_of_shards":"1","number_of_replicas":"1","uuid":"0yt4C-0RRt 2DdG_5aI16UQ","version":{"created":"7070199"},"provided_name":"mytest-2021.09.02"}}}}
|
常见错误解决
查看logstash.yml 中 path.data 路径,若无配置,默认在/usr/share/logstash/data
1 2 3 4 5 6 7
| cd /usr/share/logstash/data
ls -alh
rm .lock
|
1 2 3 4 5 6 7
| vim /etc/sysctl.conf
vm.max_map_count=262144
sysctl -p
|