Elasticsearch ile Merkezi Log Yönetimi Rehberi
zafer ak
Yazar
ELK Stack (Elasticsearch, Logstash, Kibana), log verilerini toplamak, saklamak, aramak ve görselleştirmek için en popüler açık kaynak çözümdür.
ELK Stack Bileşenleri
- Elasticsearch: Dağıtık arama ve analitik motoru
- Logstash: Log toplama ve işleme pipeline
- Kibana: Görselleştirme ve dashboard
- Beats: Hafif log shipper'lar (Filebeat, Metricbeat)
Elasticsearch Kurulumu
# GPG key ve repository
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
# Kurulum
sudo apt update
sudo apt install elasticsearch
# Başlat
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
# Test
curl -X GET "localhost:9200"
Elasticsearch Yapılandırması
# /etc/elasticsearch/elasticsearch.yml
cluster.name: production-logs
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node
# Güvenlik (production için)
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
Logstash Kurulumu
sudo apt install logstash
# Pipeline yapılandırması
sudo nano /etc/logstash/conf.d/main.conf
Logstash Pipeline
# /etc/logstash/conf.d/main.conf
input {
beats {
port => 5044
}
syslog {
port => 5514
type => "syslog"
}
}
filter {
if [type] == "syslog" {
grok {
match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
}
date {
match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
}
if [type] == "nginx-access" {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
geoip {
source => "clientip"
}
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "%{[type]}-%{+YYYY.MM.dd}"
}
}
Filebeat Kurulumu
sudo apt install filebeat
# Yapılandırma
sudo nano /etc/filebeat/filebeat.yml
# /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
fields:
type: nginx-access
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
fields:
type: nginx-error
output.logstash:
hosts: ["localhost:5044"]
# Veya direkt Elasticsearch
output.elasticsearch:
hosts: ["localhost:9200"]
Kibana Kurulumu
sudo apt install kibana
# Yapılandırma
sudo nano /etc/kibana/kibana.yml
# /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
# Başlat
sudo systemctl enable kibana
sudo systemctl start kibana
# Erişim: http://server:5601
Index Lifecycle Management
# ILM Policy oluşturma (Kibana Dev Tools)
PUT _ilm/policy/logs-policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50GB",
"max_age": "1d"
}
}
},
"warm": {
"min_age": "7d",
"actions": {
"shrink": {
"number_of_shards": 1
}
}
},
"cold": {
"min_age": "30d",
"actions": {
"freeze": {}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
Kibana Alerting
# Elasticsearch Watcher ile alert
PUT _watcher/watch/error_spike
{
"trigger": {
"schedule": { "interval": "5m" }
},
"input": {
"search": {
"request": {
"indices": ["nginx-error-*"],
"body": {
"query": {
"range": {
"@timestamp": { "gte": "now-5m" }
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total.value": { "gt": 100 }
}
},
"actions": {
"email_admin": {
"email": {
"to": "admin@example.com",
"subject": "High Error Rate Alert",
"body": "Error count: {{ctx.payload.hits.total.value}}"
}
}
}
}
Sonuç
ELK Stack, log yönetimi ve analizi için güçlü bir platformdur. Doğru yapılandırma ile tüm sistemlerinizin loglarını merkezi olarak yönetebilir ve sorunları hızlıca tespit edebilirsiniz.