📊
Monitoring & Observability

ELK Stack

Elasticsearch + Logstash + Kibana — log aggregation and visualization

🐧 Linux🍎 Mac🪟 Windows
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

The ELK stack collects logs (Logstash/Beats), stores them (Elasticsearch), and visualizes them (Kibana).

📥

Step 01

Install ELK Stack

(01)Install ELK Stack

Linux
1curl -fsSL https://elastic.co/start-local | sh -s -- --version 8.15.0
2# Or Docker Compose:
3docker compose -f https://raw.githubusercontent.com/elastic/elasticsearch/main/docs/reference/setup/install/docker/docker-compose.yml up -d
⚙️

Step 02

Configure ELK Stack

(01)Configure ELK Stack

Linux
1# Logstash pipeline example
2cat > logstash.conf << 'EOF'
3input { beats { port => 5044 } }
4output { elasticsearch { hosts => ["http://localhost:9200"] } }
5EOF
6# Kibana: http://localhost:5601

Step 03

Verify

(01)Verify installation

Linux
1curl http://localhost:9200
2curl http://localhost:5601/api/status
🔧

Step 04

Common Problems

#1Elasticsearch won't start (vm.max_map_count)

Linux
1sudo sysctl -w vm.max_map_count=262144
2echo 'vm.max_map_count=262144' | sudo tee -a /etc/sysctl.conf
3docker logs elasticsearch

#2Kibana can't connect to Elasticsearch

Linux
1curl http://localhost:9200
2# Check kibana.yml elasticsearch.hosts
3docker logs kibana

📋Config templates

1 YAML template for ELK Stack. Copy and deploy after setup.

1 ready-to-copy template. Expand one, copy the YAML, then run the deploy commands.

frontend.yaml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: webserver
5 namespace: laravel-app
6 labels:
7 app: apache
8spec:
9 replicas: 3
10 selector:
11 matchLabels:
12 app: apache
13 template:
14 metadata:
15 labels:
16 app: apache
17 spec:
18 containers:
19 - name: apache
20 image: your-registry/laravel-app:latest
21 ports:
22 - containerPort: 80
23 securityContext:
24 allowPrivilegeEscalation: false
25---
26apiVersion: v1
27kind: Service
28metadata:
29 name: front-service
30 namespace: laravel-app
31spec:
32 type: LoadBalancer
33 selector:
34 app: apache
35 ports:
36 - port: 80
37 targetPort: 80

db.yaml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: dbserver
5 namespace: laravel-app
6 labels:
7 app: db
8spec:
9 replicas: 1
10 selector:
11 matchLabels:
12 app: db
13 template:
14 metadata:
15 labels:
16 app: db
17 spec:
18 containers:
19 - name: db
20 image: mysql:5.7
21 env:
22 - name: MYSQL_ALLOW_EMPTY_PASSWORD
23 value: "1"
24 - name: MYSQL_DATABASE
25 value: laravel-db
26 ports:
27 - containerPort: 3306
28 securityContext:
29 allowPrivilegeEscalation: false
30---
31apiVersion: v1
32kind: Service
33metadata:
34 name: db-service
35 namespace: laravel-app
36spec:
37 selector:
38 app: db
39 ports:
40 - port: 3306
41 targetPort: 3306

phpmyadmin.yaml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: phpmyadmin
5 namespace: laravel-app
6spec:
7 replicas: 1
8 selector:
9 matchLabels:
10 app: phpmyadmin
11 template:
12 metadata:
13 labels:
14 app: phpmyadmin
15 spec:
16 containers:
17 - name: phpmyadmin
18 image: phpmyadmin:latest
19 env:
20 - name: PMA_HOST
21 value: db-service
22 ports:
23 - containerPort: 80
24---
25apiVersion: v1
26kind: Service
27metadata:
28 name: phpmyadmin-service
29 namespace: laravel-app
30spec:
31 type: LoadBalancer
32 selector:
33 app: phpmyadmin
34 ports:
35 - port: 80
36 targetPort: 80
📄

Step 01

Deploy Laravel Stack

(01)Apply all manifests

Linux
1kubectl create namespace laravel-app
2kubectl apply -f db.yaml
3kubectl apply -f frontend.yaml
4kubectl apply -f phpmyadmin.yaml
5kubectl -n laravel-app get pods,svc