
Elastic Stack (ELK) คืออะไร?
Elastic Stack หรือที่รู้จักกันในชื่อ ELK Stack คือชุดเครื่องมือ Open Source สำหรับ Log Management, Search และ Data Analytics ที่ใช้กันอย่างแพร่หลายในองค์กรทั่วโลก ชื่อ ELK มาจากตัวย่อของ 3 Components หลัก ได้แก่ Elasticsearch (Search & Analytics Engine), Logstash (Data Processing Pipeline) และ Kibana (Visualization & Dashboard) ต่อมา Elastic ได้เพิ่ม Beats (Lightweight Data Shippers) เข้ามาเป็น Component ที่ 4 จึงเปลี่ยนชื่อเป็น Elastic Stack
Elastic Stack ถูกใช้ในหลายสถานการณ์ เช่น Centralized Log Management (รวม Logs จากเซิร์ฟเวอร์หลายร้อยเครื่องมาที่เดียว), Application Performance Monitoring (APM), Security Information and Event Management (SIEM), Infrastructure Monitoring, Business Analytics และ Full-text Search Engine สำหรับเว็บไซต์ ในปี 2026 Elastic Stack เวอร์ชัน 8.x ได้ปรับปรุงหลายด้าน ทั้ง Security by default, Machine Learning, และ Elastic Agent ที่ช่วยให้การ Deploy ง่ายขึ้นมาก
Components ของ Elastic Stack
Elasticsearch — Search & Analytics Engine
Elasticsearch คือ Distributed Search Engine ที่สร้างบน Apache Lucene รองรับ Full-text Search, Structured Search, Analytics และ Geospatial queries ทำงานแบบ Cluster โดยข้อมูลจะถูกจัดเก็บในรูปแบบ JSON Documents
แนวคิดหลักของ Elasticsearch ประกอบด้วย:
| แนวคิด | คำอธิบาย | เทียบ RDBMS |
|---|---|---|
| Index | คอลเลกชันของ Documents ที่มีโครงสร้างคล้ายกัน | Database / Table |
| Document | หน่วยข้อมูลพื้นฐาน ในรูปแบบ JSON | Row |
| Field | คู่ key-value ภายใน Document | Column |
| Shard | ส่วนย่อยของ Index ที่กระจายไปยัง Nodes ต่างๆ | Partition |
| Replica | สำเนาของ Shard เพื่อ High Availability | Replica |
| Cluster | กลุ่มของ Nodes ที่ทำงานร่วมกัน | Cluster |
| Node | Instance เดียวของ Elasticsearch | Server |
Elasticsearch ใช้ Inverted Index สำหรับ Full-text Search ทำให้ค้นหาข้อมูลจากล้านๆ Documents ได้ในเวลา Milliseconds แต่ละ Shard คือ Lucene Index อิสระ โดย Default Elasticsearch จะสร้าง 1 Primary Shard + 1 Replica Shard ต่อ Index (ตั้งแต่ ES 7.x) สำหรับ Production ควรตั้ง Shard count ตามปริมาณข้อมูล โดย Shard ไม่ควรเกิน 50GB ต่อ Shard
Logstash — Data Processing Pipeline
Logstash คือ Server-side Data Processing Pipeline ที่รับข้อมูลจากแหล่งต่างๆ (Input), แปลงและ Enrich ข้อมูล (Filter), แล้วส่งไปยังปลายทาง (Output) Logstash ใช้ Plugin-based Architecture ที่มี Plugin มากกว่า 200 ตัว
Pipeline ของ Logstash ทำงาน 3 ขั้นตอน:
# Logstash Pipeline Structure
input {
# รับข้อมูลจากแหล่งต่างๆ
beats { port => 5044 } # รับจาก Filebeat/Metricbeat
syslog { port => 514 } # รับ Syslog
file { path => "/var/log/*.log" } # อ่านจากไฟล์
kafka { topic => "logs" } # รับจาก Kafka
jdbc { ... } # อ่านจาก Database
}
filter {
# แปลง / กรอง / Enrich ข้อมูล
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
date {
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
}
geoip {
source => "clientip"
}
mutate {
remove_field => [ "beat", "host", "tags" ]
}
}
output {
# ส่งไปยังปลายทาง
elasticsearch {
hosts => ["http://elasticsearch:9200"]
index => "weblog-%{+YYYY.MM.dd}"
user => "elastic"
password => "changeme"
}
# stdout { codec => rubydebug } # Debug output
}
Grok เป็น Filter ที่ใช้บ่อยที่สุดใน Logstash ทำหน้าที่ Parse Unstructured Log เป็น Structured Data ด้วย Pattern matching เช่น Pattern %{COMBINEDAPACHELOG} จะ Parse Apache Access Log ให้อัตโนมัติ คุณสามารถสร้าง Custom Pattern ได้เช่นกัน นอกจากนี้ยังมี Filter อื่นที่สำคัญ เช่น mutate (เปลี่ยนชื่อ/ลบ Field), geoip (แปลง IP เป็น Location), useragent (Parse User-Agent string) และ translate (Lookup table)
Kibana — Visualization & Dashboard
Kibana คือ Web UI สำหรับ Visualize และ Explore ข้อมูลใน Elasticsearch มีเครื่องมือหลักดังนี้:
Discover: ค้นหาและ Browse ข้อมูลด้วย KQL (Kibana Query Language) หรือ Lucene Query กรองตาม Time range ดู Raw documents พร้อม Field breakdown
Visualize: สร้าง Visualizations หลายรูปแบบ ได้แก่ Line/Bar/Area Chart, Pie Chart, Data Table, Metric, Gauge, Heat Map, Tag Cloud, Lens (drag-and-drop visualization builder) และ TSVB (Time Series Visual Builder)
Dashboard: รวม Visualizations หลายตัวเข้าด้วยกันในหน้าจอเดียว รองรับ Interactive filters, Time picker, Auto-refresh, Drill-down และ Export (PDF, PNG, CSV)
Canvas: สร้าง Infographic-style presentations จากข้อมูล Live เหมาะสำหรับแสดงบน TV Dashboard
Maps: แสดงข้อมูล Geospatial บนแผนที่โลก เช่น แสดง Request ตาม Location
Beats — Lightweight Data Shippers
Beats คือ Lightweight Agents ที่ติดตั้งบนเครื่องที่ต้องการเก็บข้อมูล แต่ละ Beat ออกแบบมาสำหรับข้อมูลเฉพาะประเภท:
| Beat | หน้าที่ | Use Case |
|---|---|---|
| Filebeat | เก็บ Log files | Application logs, System logs, Container logs |
| Metricbeat | เก็บ System/Service metrics | CPU, RAM, Disk, Network, Docker, Kubernetes |
| Packetbeat | Network packet analysis | HTTP, DNS, MySQL, Redis traffic monitoring |
| Winlogbeat | Windows Event Log | Security events, Application events, System events |
| Auditbeat | Audit data | File integrity monitoring, Process monitoring |
| Heartbeat | Uptime monitoring | HTTP/TCP/ICMP endpoint availability |
Beats ใช้ Resource น้อยมาก (CPU/RAM ต่ำ) จึงเหมาะกับ Production servers Filebeat เป็น Beat ที่ใช้มากที่สุด รองรับ Modules สำเร็จรูปสำหรับ Parse log จาก Apache, Nginx, MySQL, PostgreSQL, System logs และอื่นๆ อีกมากมาย
ติดตั้ง Elastic Stack ด้วย Docker Compose
วิธีที่ง่ายและเร็วที่สุดในการติดตั้ง Elastic Stack คือใช้ Docker Compose ด้านล่างนี้คือ docker-compose.yml ที่พร้อมใช้งาน:
# docker-compose.yml - Elastic Stack 8.x
version: '3.8'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.0
container_name: elasticsearch
environment:
- discovery.type=single-node
- xpack.security.enabled=true
- ELASTIC_PASSWORD=YourStrongPassword123
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"
- xpack.security.http.ssl.enabled=false
ports:
- "9200:9200"
volumes:
- es_data:/usr/share/elasticsearch/data
networks:
- elk
healthcheck:
test: ["CMD-SHELL", "curl -s http://localhost:9200 -u elastic:YourStrongPassword123 | grep -q 'cluster_name'"]
interval: 30s
timeout: 10s
retries: 5
logstash:
image: docker.elastic.co/logstash/logstash:8.12.0
container_name: logstash
volumes:
- ./logstash/pipeline:/usr/share/logstash/pipeline
- ./logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml
ports:
- "5044:5044" # Beats input
- "5514:5514" # Syslog input
- "9600:9600" # Logstash monitoring
environment:
- "LS_JAVA_OPTS=-Xms1g -Xmx1g"
networks:
- elk
depends_on:
elasticsearch:
condition: service_healthy
kibana:
image: docker.elastic.co/kibana/kibana:8.12.0
container_name: kibana
environment:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
- ELASTICSEARCH_USERNAME=kibana_system
- ELASTICSEARCH_PASSWORD=YourStrongPassword123
ports:
- "5601:5601"
networks:
- elk
depends_on:
elasticsearch:
condition: service_healthy
volumes:
es_data:
networks:
elk:
driver: bridge
# เริ่มต้น Elastic Stack
docker-compose up -d
# ตรวจสอบสถานะ
docker-compose ps
docker-compose logs -f elasticsearch
# ทดสอบ Elasticsearch
curl -u elastic:YourStrongPassword123 http://localhost:9200
curl -u elastic:YourStrongPassword123 http://localhost:9200/_cluster/health?pretty
# เข้า Kibana
# http://localhost:5601
# Login: elastic / YourStrongPassword123
Syslog Collection — รวม Log จากทุกเครื่อง
การรวม Syslog จาก Linux servers, Network devices (Router, Switch, Firewall) มาที่ ELK เป็น Use case พื้นฐานที่สุด ตั้งค่า Logstash ให้รับ Syslog:
# logstash/pipeline/syslog.conf
input {
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" ]
}
# เพิ่ม GeoIP สำหรับ IP ที่พบใน message
if [syslog_message] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ {
grok {
match => { "syslog_message" => "%{IP:extracted_ip}" }
tag_on_failure => []
}
if [extracted_ip] {
geoip { source => "extracted_ip" }
}
}
}
}
output {
if [type] == "syslog" {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
index => "syslog-%{+YYYY.MM.dd}"
user => "elastic"
password => "YourStrongPassword123"
}
}
}
# ส่ง Syslog จาก Linux servers มาที่ ELK
# แก้ /etc/rsyslog.conf เพิ่ม:
*.* @@elk-server-ip:5514
# Restart rsyslog
sudo systemctl restart rsyslog
# ส่ง Syslog จาก Cisco Router/Switch
# (config)# logging host elk-server-ip transport tcp port 5514
# (config)# logging trap informational
Windows Event Log Collection ด้วย Winlogbeat
Winlogbeat เป็น Beat ที่ออกแบบมาเฉพาะสำหรับเก็บ Windows Event Log ติดตั้งง่ายและใช้ Resource น้อย:
# winlogbeat.yml
winlogbeat.event_logs:
- name: Application
ignore_older: 72h
- name: System
ignore_older: 72h
- name: Security
ignore_older: 72h
event_id: 4624, 4625, 4634, 4648, 4672, 4688, 4720, 4722, 4732
# 4624=Logon success, 4625=Logon failure, 4634=Logoff
# 4648=Explicit credential logon, 4672=Special privileges
# 4688=Process creation, 4720=User created
# 4722=User enabled, 4732=Member added to group
- name: Microsoft-Windows-Sysmon/Operational
# Sysmon events สำหรับ Advanced threat detection
output.elasticsearch:
hosts: ["http://elk-server:9200"]
username: "elastic"
password: "YourStrongPassword123"
index: "winlogbeat-%{+yyyy.MM.dd}"
setup.kibana:
host: "http://elk-server:5601"
# Processors
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
# ติดตั้ง Winlogbeat บน Windows (PowerShell Admin)
# 1. Download Winlogbeat จาก elastic.co
# 2. Extract ไปที่ C:\Program Files\Winlogbeat
cd "C:\Program Files\Winlogbeat"
# ทดสอบ Config
.\winlogbeat.exe test config -c winlogbeat.yml
# Setup dashboards (ครั้งแรก)
.\winlogbeat.exe setup -c winlogbeat.yml
# ติดตั้งเป็น Windows Service
.\install-service-winlogbeat.ps1
# เริ่ม Service
Start-Service winlogbeat
Nginx / Apache Access Log Collection
ใช้ Filebeat modules สำเร็จรูปเพื่อเก็บ Web server logs:
# ติดตั้ง Filebeat
sudo apt-get install filebeat
# เปิดใช้ Nginx module
sudo filebeat modules enable nginx
# แก้ config module
# /etc/filebeat/modules.d/nginx.yml
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log*"]
# filebeat.yml
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
output.elasticsearch:
hosts: ["http://elk-server:9200"]
username: "elastic"
password: "YourStrongPassword123"
setup.kibana:
host: "http://elk-server:5601"
# Setup dashboards + Start
sudo filebeat setup
sudo systemctl enable filebeat
sudo systemctl start filebeat
# สำหรับ Apache
sudo filebeat modules enable apache
# /etc/filebeat/modules.d/apache.yml
- module: apache
access:
enabled: true
var.paths: ["/var/log/apache2/access.log*"]
error:
enabled: true
var.paths: ["/var/log/apache2/error.log*"]
สร้าง Dashboard ใน Kibana
หลังจากข้อมูลเข้า Elasticsearch แล้ว ขั้นตอนต่อไปคือสร้าง Dashboard ใน Kibana เพื่อ Visualize ข้อมูล:
ขั้นตอนที่ 1: สร้าง Data View — เข้า Kibana > Stack Management > Data Views > Create data view ตั้งชื่อ เช่น “syslog-*” หรือ “filebeat-*” กำหนด Time field เป็น @timestamp
ขั้นตอนที่ 2: Explore ข้อมูลใน Discover — เข้า Discover เลือก Data View ที่สร้าง ใช้ KQL ค้นหา เช่น syslog_program: "sshd" and syslog_message: "Failed" ดู Failed SSH login attempts กรอง Time range ตามต้องการ
ขั้นตอนที่ 3: สร้าง Visualizations — เข้า Visualize Library > Create visualization เลือก Lens (แนะนำ เพราะ drag-and-drop ง่าย) สร้าง Visualizations ที่จำเป็น เช่น Line chart แสดง Log volume ตามเวลา, Pie chart แสดงสัดส่วน Log severity, Bar chart แสดง Top 10 source IPs, Table แสดง Recent errors, Metric แสดง Total events today
ขั้นตอนที่ 4: รวมเป็น Dashboard — เข้า Dashboard > Create dashboard เพิ่ม Visualizations ที่สร้าง จัด Layout ให้เหมาะสม ตั้ง Auto-refresh interval (เช่น ทุก 30 วินาที) บันทึก Dashboard
Alerting — Watcher และ ElastAlert
Watcher (Built-in Alerting)
Watcher เป็นฟีเจอร์ Alerting ในตัวของ Elasticsearch (ต้อง Platinum License ขึ้นไป หรือ Kibana Alerting ใน Basic License):
# สร้าง Watcher ผ่าน API
PUT _watcher/watch/high_error_rate
{
"trigger": {
"schedule": { "interval": "5m" }
},
"input": {
"search": {
"request": {
"indices": ["syslog-*"],
"body": {
"query": {
"bool": {
"must": [
{ "match": { "severity": "error" } },
{ "range": { "@timestamp": { "gte": "now-5m" } } }
]
}
}
}
}
}
},
"condition": {
"compare": { "ctx.payload.hits.total": { "gt": 100 } }
},
"actions": {
"notify_slack": {
"webhook": {
"scheme": "https",
"host": "hooks.slack.com",
"port": 443,
"method": "post",
"path": "/services/YOUR/SLACK/WEBHOOK",
"body": "{"text": "ALERT: {{ctx.payload.hits.total}} errors in last 5 minutes!"}"
}
}
}
}
ElastAlert 2 (Open Source Alternative)
ElastAlert 2 เป็น Open Source Alerting framework สำหรับ Elasticsearch รองรับ Alert types หลายแบบ:
# elastalert_rules/high_error_rate.yaml
name: High Error Rate Alert
type: frequency
index: syslog-*
num_events: 50
timeframe:
minutes: 5
filter:
- query:
query_string:
query: "severity:error OR severity:critical"
alert:
- slack
- email
slack_webhook_url: "https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK"
slack_channel_override: "#alerts"
email:
- "[email protected]"
smtp_host: "smtp.gmail.com"
smtp_port: 587
Index Lifecycle Management (ILM)
ILM จัดการ Lifecycle ของ Indices อัตโนมัติ ช่วยลดต้นทุน Storage และรักษา Performance:
# สร้าง ILM Policy
PUT _ilm/policy/logs_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_age": "1d",
"max_primary_shard_size": "50gb"
},
"set_priority": { "priority": 100 }
}
},
"warm": {
"min_age": "7d",
"actions": {
"shrink": { "number_of_shards": 1 },
"forcemerge": { "max_num_segments": 1 },
"set_priority": { "priority": 50 },
"allocate": {
"require": { "data": "warm" }
}
}
},
"cold": {
"min_age": "30d",
"actions": {
"set_priority": { "priority": 0 },
"freeze": {},
"allocate": {
"require": { "data": "cold" }
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
# Hot (0-7 วัน): ข้อมูลล่าสุด SSD เร็ว
# Warm (7-30 วัน): ลด Shards, Force merge, ย้ายไป HDD
# Cold (30-90 วัน): Freeze index, อ่านได้แต่ช้า
# Delete (90+ วัน): ลบอัตโนมัติ
Elastic Security (SIEM)
Elastic Security เป็นโซลูชัน SIEM ที่รวมอยู่ใน Elastic Stack ตั้งแต่เวอร์ชัน 7.x เป็นต้นไป ให้ความสามารถในการ Detect, Investigate และ Respond ต่อภัยคุกคามด้าน Cybersecurity:
Detection Rules: มี Pre-built detection rules มากกว่า 700 rules สำหรับ Detect threats ทั่วไป เช่น Brute-force attacks, Malware execution, Lateral movement, Privilege escalation สามารถสร้าง Custom rules ด้วย KQL หรือ EQL (Event Query Language)
Timeline: เครื่องมือ Investigation สำหรับ Analyst เพื่อรวบรวม Events ที่เกี่ยวข้อง สร้าง Timeline ของ Incident ตั้งแต่เริ่มต้นจนจบ
Cases: ระบบ Case management สำหรับ Track incidents แบ่ง Tasks ให้ทีม Integrate กับ Jira, ServiceNow, IBM Resilient
Elastic Agent + Fleet: จัดการ Agents ทั้งหมดจาก Kibana UI ทำ Policy management, Agent upgrades, Integrations จากที่เดียว
ELK vs Splunk vs Grafana Loki — เปรียบเทียบ
| คุณสมบัติ | Elastic Stack | Splunk | Grafana Loki |
|---|---|---|---|
| License | Open Source (Basic) + Paid tiers | Commercial (แพงมาก) | Open Source (AGPLv3) |
| Cost (100GB/day) | $0 (self-hosted) – $3K/mo (Cloud) | $10K-50K+/month | $0 (self-hosted) |
| Full-text Search | ดีมาก (Lucene-based) | ดีมาก | จำกัด (label-based) |
| Indexing | Full inverted index ทุก Field | Full index | ไม่ Index content (label only) |
| Storage | ใช้มาก (full index) | ใช้มาก | ใช้น้อยมาก (compressed chunks) |
| Query Language | KQL, Lucene, EQL, ES|QL | SPL (Splunk Processing Language) | LogQL |
| Dashboard | Kibana (ดีมาก) | Splunk Dashboard | Grafana (ดีมาก) |
| Alerting | Watcher / Kibana Alerts | Built-in Alerts | Grafana Alerting |
| SIEM | Elastic Security (มี) | Splunk ES (มี) | ไม่มี |
| Learning Curve | ปานกลาง | สูง | ต่ำ (ถ้าใช้ Grafana อยู่แล้ว) |
| Best For | Search-heavy, SIEM, APM | Enterprise, Compliance | Kubernetes, Cost-sensitive |
Sizing และ Performance
Hardware Recommendations
| Scale | Data/Day | Elasticsearch | Logstash | Kibana |
|---|---|---|---|---|
| Small | < 10GB | 1 Node: 4 CPU, 16GB RAM, 500GB SSD | 1 Node: 2 CPU, 4GB RAM | 1 Node: 2 CPU, 4GB RAM |
| Medium | 10-100GB | 3 Nodes: 8 CPU, 32GB RAM, 2TB SSD each | 2 Nodes: 4 CPU, 8GB RAM | 1 Node: 4 CPU, 8GB RAM |
| Large | 100GB-1TB | 6+ Nodes (3 Master + 3 Data): 16 CPU, 64GB RAM, 4TB SSD each | 3+ Nodes: 8 CPU, 16GB RAM | 2 Nodes (LB) |
Performance Tips:
JVM Heap ตั้งไม่เกิน 50% ของ RAM และไม่เกิน 31GB (JVM Compressed OOPs limit) ใช้ SSD สำหรับ Hot tier Data nodes เสมอ ตั้ง Refresh interval เป็น 30s แทน 1s สำหรับ Log index (ลด I/O) ใช้ Bulk API สำหรับ Indexing จำนวนมาก ตั้ง Shard size 20-50GB ต่อ Shard (ไม่เล็กเกินไป ไม่ใหญ่เกินไป) ปิด Replica สำหรับ Index ที่เป็น Log (ถ้ามี ILM จัดการอยู่)
Elastic Cloud
Elastic Cloud คือ Managed Service ที่ Elastic เป็นผู้ดูแลเอง รองรับ Deployment บน AWS, GCP, Azure ข้อดีคือไม่ต้อง Manage infrastructure เอง, Auto-scaling, Automatic upgrades, Built-in monitoring, Snapshots, Cross-cluster replication เหมาะกับองค์กรที่ไม่มี DevOps/Infrastructure team หรือต้องการ SLA ที่ชัดเจน ราคาเริ่มต้นประมาณ $95/month สำหรับ Standard tier
สรุป
Elastic Stack (ELK) เป็นแพลตฟอร์ม Log Management และ Analytics ที่ทรงพลังที่สุดตัวหนึ่งในปัจจุบัน ด้วย Elasticsearch ที่ค้นหาเร็วระดับ Milliseconds, Logstash ที่ Parse log ได้ทุกรูปแบบ, Kibana ที่สร้าง Dashboard สวยงาม และ Beats ที่ติดตั้งง่ายใช้ Resource น้อย ทำให้เป็นทางเลือกอันดับ 1 สำหรับ IT Monitoring ในปี 2026
สำหรับผู้เริ่มต้น แนะนำให้เริ่มจาก Docker Compose setup ตามตัวอย่างข้างต้น ติดตั้ง Filebeat บน Server ที่ต้องการเก็บ Log สร้าง Dashboard ใน Kibana ตั้ง Alerts สำหรับ Critical events แล้วค่อยๆ ขยาย Cluster ตามปริมาณข้อมูลที่เพิ่มขึ้น Elastic Stack สามารถ Scale ได้ตั้งแต่ Lab เล็กๆ ไปจนถึง Enterprise ที่เก็บข้อมูลวันละหลาย Terabytes