https://hub.docker.com/_/elasticsearch/
https://www.docker.elastic.co/r/elasticsearch
https://hub.docker.com/r/sebp/elk/
elasticsearch kibana
# docker run elasticsearch
docker run -d -p 9200:9200 -p 9300:9300 --name elasticsearch --restart always --network mgr --ip 172.18.0.92 -e TZ='Asia/Shanghai' --ulimit nofile=262144:262144 -v /etc/localtime:/etc/localtime:ro -e discovery.type='single-node' -e ES_JAVA_OPTS='-Xms3G -Xmx3G' -e xpack.security.enrollment.enabled='true' -e LC_ALL='C.UTF-8' -v /data/site/docker/env/monitor/elasticsearch/elasticsearch-8.ini:/usr/share/elasticsearch/config/elasticsearch.yml:ro elasticsearch:8.14.3
# docker run kibana
docker run -d -p 5601:5601 --name kibana --restart always --network mgr --ip 172.18.0.156 -e TZ='Asia/Shanghai' --ulimit nofile=262144:262144 -v /etc/localtime:/etc/localtime:ro -v /data/site/docker/env/monitor/kibana/kibana-8.ini:/usr/share/kibana/config/kibana.yml:ro -e server.host="0.0.0.0" -e server.shutdownTimeout='5s' -e elasticsearch.hosts='http://elasticsearch:9200' -e monitoring.ui.container.elasticsearch.enabled='true' --link elasticsearch kibana:8.14.3
docker run -d -p 5601:5601 --name kibana --restart always --network mgr --ip 172.18.0.56 -e TZ='Asia/Shanghai' --ulimit nofile=262144:262144 -v /etc/localtime:/etc/localtime:ro --link elasticsearch -e server.publicBaseUrl='http://g.htmltoo.com:5601' -e elasticsearch.hosts=["http://172.18.0.92:9200"] -e server.host="0.0.0.0" kibana:8.10.2
docker run -d -p 5601:5601 --name kibana --restart always --network mgr --ip 172.18.0.56 -e TZ='Asia/Shanghai' --ulimit nofile=262144:262144 -v /etc/localtime:/etc/localtime:ro -v /data/site/docker/env/monitor/kibana/kibana-8.ini:/usr/share/kibana/config/kibana.yml:ro -e I18N_LOCALE='zh-CN' -e ELASTICSEARCH_URL="http://elasticsearch:9200" -e SERVER_HOST="0.0.0.0" --link elasticsearch kibana:8.10.2
---ok
https://hub.docker.com/r/780825/kibana
docker run -d -p 5601:5601 --name kibana --restart always --network mgr --ip 172.18.0.56 -e TZ='Asia/Shanghai' --ulimit nofile=262144:262144 -v /etc/localtime:/etc/localtime:ro -e I18N_LOCALE='zh-CN' --link elasticsearch -e ELASTICSEARCH_HOSTS='http://elasticsearch:9200' 780825/kibana:8.9.2
-命令生成:
docker exec -it kibana /usr/share/kibana/kibana-verification-code
docker exec -it elasticsearch /bin/bash
---执行设置用户名和密码的命令,这里需要为4个用户分别设置密码,elastic, kibana, logstash_system,beats_system
elasticsearch-setup-passwords interactive
-:y
-:elastic:elastic, kibana:kibana, logstash_system:logstash_system,beats_system : beats_system
---修改密码命令如下
curl -H "Content-Type:application/json" -XPOST -u elastic 'http://127.0.0.1:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "Wdq54321" }'
---elastic重置密码
elasticsearch-reset-password -u 需要重置密码的用户名
---在程序中可以使用如下格式指定用户名密码7
http://username:password@localhost:9200
---生成elastic密码及enrollment-token
-生成超级管理员elastic的密码
elasticsearch-reset-password -u elastic
elasticsearch-reset-password -u kibana_system
-生成enrollment-token(目前只发现kibana登录需要使用)
elasticsearch-create-enrollment-token -s kibana
cat /data/docker/monitor/elasticsearch/elasticsearch-8.ini
cluster.name: "cluster" network.host: 0.0.0.0 discovery.seed_hosts: 0.0.0.0 network.bind_host: 0.0.0.0 http.port: 9200 # Enable security features xpack.security.enabled: true xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: false # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: false http.cors.enabled: "true" http.cors.allow-origin: “*”
cat /data/docker/monitor/kibana/kibana-8.ini
server.host: "0.0.0.0" server.shutdownTimeout: "5s" elasticsearch.hosts: [ "http://elasticsearch:9200" ] monitoring.ui.container.elasticsearch.enabled: true # 注意这里的用户名不能用elastic,使用之后会启动报错,看下面的错误2 elasticsearch.username: "kibana_system" # 这里的密码是在启动时候配置的,如果忘记也没关系 elasticsearch.password: "wdq54321" i18n.locale: "zh-CN"
# elasticsearch-head
https://abc.htmltoo.com/thread-46069.htm
mkdir -p /data/file/logs/elasticsearch && chmod -R 777 /data/file/logs/ /data/db/
rm -rf /data/db/elasticsearch/* /data/file/logs/elasticsearch/*
docker exec -it elasticsearch /bin/bash
==========时间===========
1.高级设置
- > Date format -> YYYY-MM-DD HH:mm:ss
-> dateFormat:tz -> Asia/Shanghai
-> dateFormat:dow -> Monday
# 安装中文分词插件: https://github.com/medcl/elasticsearch-analysis-ik/releases
bin/elasticsearch-plugin install
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.7.1/elasticsearch-analysis-ik-8.7.1.zip
----测试ik分词器:
curl -XGET -H 'Content-Type: application/json' 'http://localhost:9200/_analyze?pretty' -d '{
"analyzer" : "ik_max_word",
"text": "中华人民共和国国歌"
}'
curl localhost:9200 # 测试
---elasticsearch查看所有index:
curl 'localhost:9200/_cat/indices?v'
# 多节点集群
-es01
docker run -d -p 9200:9200 --name es01 --restart=always --network=es -v /etc/localtime:/etc/localtime:ro -v /data/docker/monitor/elasticsearch/elasticsearch-7-1.ini:/usr/share/elasticsearch/config/elasticsearch.yml:ro -v /data/db/elasticsearch-7-1:/usr/share/elasticsearch/data -v /data/file/logs/elasticsearch-7-1:/usr/share/elasticsearch/logs elasticsearch:7.14.4
-es02
docker run -d --name es02 --restart=always --network=es -v /etc/localtime:/etc/localtime:ro -v /data/docker/monitor/elasticsearch/elasticsearch-7-2.ini:/usr/share/elasticsearch/config/elasticsearch.yml:ro -v /data/db/elasticsearch-7-2:/usr/share/elasticsearch/data -v /data/file/logs/elasticsearch-7-2:/usr/share/elasticsearch/logs elasticsearch:7.14.1
docker stop es01 es02
chmod -R 777 /data/file/logs/ /data/db/
rm -rf /data/db/elasticsearch-7-1/* /data/db/elasticsearch-7-2/*
rm -rf /data/file/logs/elasticsearch-7-1/* /data/file/logs/elasticsearch-7-2/*
docker rm es01 es02
docker restart es01 es02
logstash
# logstash: https://www.elastic.co/cn/downloads/logstash
docker run -d --name logstash -p 5043:5043 -p 5044:5044 --privileged=true --restart=always --link elasticsearch -v /data/docker/monitor/logstash/logstash.yml:/usr/share/logstash/pipeline/logstash.yml:ro -v /etc/localtime:/etc/localtime:ro logstash:8.6.1
vim /data/docker/monitor/logstash/logstash.yml
# 输入端
input {
stdin { }
#为logstash增加tcp输入口,后面springboot接入会用到
tcp {
mode => "server"
host => "0.0.0.0"
port => 5043
codec => json_lines
}
}
#输出端
output {
stdout {
codec => rubydebug
}
elasticsearch {
hosts => ["http://elasticsearch:9200"]
# 输出至elasticsearch中的自定义index名称
index => "nginx-%{+YYYY.MM.dd}"
}
}docker exec -it logstash /bin/bash
docker restart logstash
-安装json_lines所需的插件
/usr/share/logstash/bin/logstash-plugin install logstash-codec-json_lines
filebeat
# filebeat: https://www.elastic.co/cn/downloads/beats/filebeat
cd /opt
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.7.1-x86_64.rpm
rpm -vi filebeat-8.7.1-x86_64.rpm
---修改
chmod -R 777 /etc/filebeat/
---配置.其他人不能写
chmod -R 755 /etc/filebeat/
---调试模式下采用:终端启动(退出终端或ctrl+c会退出运行)
./filebeat -e -c filebeat.yml
---线上环境配合error级别使用:以后台守护进程启动启动filebeats
nohup /usr/share/filebeat/bin/filebeat -e -c /etc/filebeat/filebeat.yml &
---停止运行FileBeat进程
ps -ef | grep filebeat
Kill -9 线程号
---卸载filebeat
yum -y remove filebeat
---Elasticsearch 日志
-启用和配置 elasticsearch 模块
filebeat modules enable elasticsearch
filebeat setup
---Start Filebeat
service filebeat start
service filebeat stop
service filebeat restart
service filebeat status
vim /etc/filebeat/filebeat.yml
#=========================== Filebeat inputs ============================= filebeat.inputs: - type: log enabled: true paths: - /www/wwwlogs/*.log #keys_under_root可以让字段位于根节点,默认为false json.key_under_root: true #对于同名的key,覆盖原有key值 json.overwrite_keys: true #选择额外的字段进行输出 fields: ip: b.htmltoo.com #如果值为ture,那么fields存储在输出文档的顶级位置 fields_under_root: true #添加标签,用过过滤 #tags: ["nginx"] #指定Filebeat忽略指定时间段以外修改的日志内容,比如2h(两个小时)或者5m(5分钟) ignore_older: 96h #如果在制定时间没有被读取,将关闭文件句柄 close_inactive: 48h #支持正则 include_lines执行完毕之后会执行exclude_lines。 #导出那些所有包含ERR,WARN,err,warn,failed的行: #include_lines: ['^ERR', '^WARN'] #include_lines: [".*ERR.*",".*WARN.*",".*err.*",".*warn.*",".*failed.*"] #支持正则 排除匹配的行,如果有多行,合并成一个单一行来进行过滤, #删除INFO开头,带有INFO的行 #exclude_lines: ['^INFO'] #exclude_lines: ["^INFO", ".*INFO.*","^DEBUG", ".*DEBUG.*"] #设置删除不必要的字段 processors: - drop_fields: fields: ["@timestamp", "prospector.type", "input.type", "beat.hostname", "beat.version", "beat.name", "host.name", "offset", "agent.ephemeral_id", "agent.id", "agent.name", "ecs.version", "log.offset", "agent.type", "agent.version"] #提取多行日志 multiline.pattern: ^\[ multiline.negate: true multiline.match: after #==================== output.elasticsearch: hosts: ["b.htmltoo.com:9200"] setup.kibana: host: "b.htmltoo.com:5601"
cat filebeat-0.yml
###################### Filebeat Configuration Example #########################
# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/filebeat/index.html
# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.
# ============================== Filebeat inputs ===============================
filebeat.inputs:
# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.
- type: log
# Change to true to enable this input configuration.
enabled: false
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /var/log/*.log
#- c:\programdata\elasticsearch\logs\*
# Exclude lines. A list of regular expressions to match. It drops the lines that are
# matching any regular expression from the list.
#exclude_lines: ['^DBG']
# Include lines. A list of regular expressions to match. It exports the lines that are
# matching any regular expression from the list.
#include_lines: ['^ERR', '^WARN']
# Exclude files. A list of regular expressions to match. Filebeat drops the files that
# are matching any regular expression from the list. By default, no files are dropped.
#exclude_files: ['.gz$']
# Optional additional fields. These fields can be freely picked
# to add additional information to the crawled log files for filtering
#fields:
# level: debug
# review: 1
### Multiline options
# Multiline can be used for log messages spanning multiple lines. This is common
# for Java Stack Traces or C-Line Continuation
# The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
#multiline.pattern: ^\[
# Defines if the pattern set under pattern should be negated or not. Default is false.
#multiline.negate: false
# Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
# that was (not) matched before or after or as long as a pattern is not matched based on negate.
# Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
#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
# Period on which files under path should be checked for changes
#reload.period: 10s
# ======================= Elasticsearch template setting =======================
setup.template.settings:
index.number_of_shards: 1
#index.codec: best_compression
#_source.enabled: false
# ================================== General ===================================
# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
#name:
# The tags of the shipper are included in their own field with each
# transaction published.
#tags: ["service-X", "web-tier"]
# Optional fields that you can specify to add additional information to the
# output.
#fields:
# env: staging
# ================================= Dashboards =================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here or by using the `setup` command.
#setup.dashboards.enabled: false
# The URL from where to download the dashboards archive. By default this URL
# has a value which is computed based on the Beat name and version. For released
# versions, this URL points to the dashboard archive on the artifacts.elastic.co
# website.
#setup.dashboards.url:
# =================================== Kibana ===================================
# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:
# Kibana Host
# Scheme and port can be left out and will be set to the default (http and 5601)
# In case you specify and additional path, the scheme is required: http://localhost:5601/path
# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
#host: "localhost:5601"
# Kibana Space ID
# ID of the Kibana Space into which the dashboards should be loaded. By default,
# the Default Space will be used.
#space.id:
# =============================== Elastic Cloud ================================
# These settings simplify using Filebeat with the Elastic Cloud (https://cloud.elastic.co/).
# The cloud.id setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `cloud.id` in the Elastic Cloud web UI.
#cloud.id:
# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:
# ================================== Outputs ===================================
# Configure what output to use when sending the data collected by the beat.
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["localhost:9200"]
# Protocol - either `http` (default) or `https`.
#protocol: "https"
# Authentication credentials - either API key or username/password.
#api_key: "id:api_key"
#username: "elastic"
#password: "changeme"
# ------------------------------ Logstash Output -------------------------------
#output.logstash:
# The Logstash hosts
#hosts: ["localhost: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: ~
# ================================== Logging ===================================
# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
#logging.level: debug
# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publish", "service".
#logging.selectors: ["*"]
# ============================= X-Pack Monitoring ==============================
# Filebeat can export internal metrics to a central Elasticsearch monitoring
# cluster. This requires xpack monitoring to be enabled in Elasticsearch. The
# reporting is disabled by default.
# Set to true to enable the monitoring reporter.
#monitoring.enabled: false
# Sets the UUID of the Elasticsearch cluster under which monitoring data for this
# Filebeat instance will appear in the Stack Monitoring UI. If output.elasticsearch
# is enabled, the UUID is derived from the Elasticsearch cluster referenced by output.elasticsearch.
#monitoring.cluster_uuid:
# Uncomment to send the metrics to Elasticsearch. Most settings from the
# Elasticsearch output are accepted here as well.
# Note that the settings should point to your Elasticsearch *monitoring* cluster.
# Any setting that is not set is automatically inherited from the Elasticsearch
# output configuration, so if you have the Elasticsearch output configured such
# that it is pointing to your Elasticsearch monitoring cluster, you can simply
# uncomment the following line.
#monitoring.elasticsearch:
# ============================== Instrumentation ===============================
# Instrumentation support for the filebeat.
#instrumentation:
# Set to true to enable instrumentation of filebeat.
#enabled: false
# Environment in which filebeat is running on (eg: staging, production, etc.)
#environment: ""
# APM Server hosts to report instrumentation results to.
#hosts:
# - http://localhost:8200
# API Key for the APM Server(s).
# If api_key is set then secret_token will be ignored.
#api_key:
# Secret token for the APM Server(s).
#secret_token:
# ================================= Migration ==================================
# This allows to enable 6.7 migration aliases
#migration.6_to_7.enabled: truecat /etc/filebeat/filebeat.yml
# ref: https://my.oschina.net/openplus/blog/1589846
###################### Filebeat Configuration Example #########################
# ============================== Filebeat inputs ===============================
filebeat.inputs:
- type: filestream
id: my-filestream-id
enabled: true
paths:
- /data/web/gaea.qianbitou.cn/frontend/runtime/logs/app.log
- /data/web/gaea.qianbitou.cn/backend/runtime/logs/app.log
tail_files: true
exclude_lines: ['\$_COOKIE', '\$_SESSION', '\$_SERVER', '\$_FILES']
multiline:
charset: "utf8"
pattern: \[error\]
negate: true
match: after
flush_pattern: ^\]
# ============================== 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
# Period on which files under path should be checked for changes
#reload.period: 10s
# ======================= Elasticsearch template setting =======================
setup.template.enabled: true
setup.template.name: "prod-yun-%{+yyyy.MM.dd}"
setup.template.pattern: "prod-yun-*"
setup.ilm.overwrite: true
setup.template.settings:
index.number_of_shards: 1
#index.codec: best_compression
#_source.enabled: false
# =================================== Kibana ===================================
setup.kibana:
host: "172.16.8.176:5601"
#username: "kibana"
#password: "Qianbitou504"
# ================================== Outputs ===================================
# Configure what output to use when sending the data collected by the beat.
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
hosts: ["172.16.8.33:9200"]
#username: "elastic"
#password: "Qianbitou504"
index: "prod-yun-%{+YYYY.MM.dd}"---创建/删除索引、索引/查询文档这四个命令
curl -X PUT "localhost:9200/customer"
curl -X PUT "localhost:9200/customer/_doc/1" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
curl -X GET "localhost:9200/customer/_doc/1"
curl -X DELETE "localhost:9200/customer"
#Elasticsearch-6: https://abc.htmltoo.com/thread-46050.htm
#Elasticsearch-7: https://abc.htmltoo.com/thread-46446.htm