# ELK Lab — Commands & Steps (Windows)

Assuming all project files are already present.

## 1. Start Docker

Open CMD in the project folder:

```
cd C:\ELK-Lab
docker --version
docker compose version
docker compose config
docker compose up -d
docker compose ps
```

Open:

- Elasticsearch: [http://localhost:9200](http://localhost:9200?utm_source=chatgpt.com)
- Kibana: [http://localhost:5601](http://localhost:5601?utm_source=chatgpt.com)

## 2. Task 1 — Verify ingestion

Kibana → Management → Dev Tools

```
GET webapp-logs/_count
```

Expected: `100`

Check sample records:

```
GET webapp-logs/_search
{
  "size": 5,
  "sort": [{"@timestamp": "asc"}]
}
```

## 3. Task 2 — Filter logs

Kibana → Analytics → Discover

- Select data view: `webapp-logs`
- Time picker (top-right): Last 24 hours
- Enter these queries in the KQL search bar:

```
status >= 400
status >= 500
status >= 400 and status < 500
status < 400
severity: "ERROR"
severity: "WARN"
host: "app-server-01"
endpoint: "/api/users"
status >= 500 and host: "app-server-01"
```

If data view doesn't exist:

Kibana → Stack Management → Data Views → Create data view

- Index pattern: `webapp-logs`
- Timestamp field: `@timestamp`
- Save.

If fields are missing, open the data view and click Refresh field list.

## 4. Task 3 — Calculate HTTP error rate

Kibana → Management → Dev Tools

```
GET webapp-logs/_search
{
  "size": 0,
  "aggs": {
    "total_requests": {
      "value_count": {"field": "status"}
    },
    "http_errors": {
      "filter": {
        "range": {"status": {"gte": 400, "lt": 600}}
      }
    }
  }
}
```

Formula:

```
Error Rate = (HTTP Errors / Total Requests) * 100
```

Expected:

```
Total requests = 100
HTTP errors = 10
Error rate = 10%
```

Find maximum failures:

```
GET webapp-logs/_search
{
  "size": 0,
  "query": {
    "range": {"status": {"gte": 400, "lt": 600}}
  },
  "aggs": {
    "errors_over_time": {
      "date_histogram": {
        "field": "@timestamp",
        "fixed_interval": "1m"
      }
    }
  }
}
```

Expected:

```
10:02 = 1 error
10:03 = 5 errors (maximum)
10:07 = 3 errors
10:08 = 1 error
```

## 5. Task 4 — Visualization

Kibana → Analytics → Visualize Library → Create visualization → Lens.

Configure:

| Option           | Value            |
| ---------------- | ---------------- |
| Chart            | Bar              |
| X-axis           | @timestamp       |
| X-axis operation | Date histogram   |
| Interval         | 1 minute         |
| Y-axis           | Count of records |
| KQL filter       | `status >= 400`  |

Click Save.

Name: `HTTP Errors Over Time`

Optional dashboard:

Kibana → Analytics → Dashboard → Create dashboard → Add from library → Select visualization → Save.

## 6. Troubleshooting commands

Check containers:

```
docker compose ps
```

Check logs:

```
docker logs elasticsearch --tail 50
docker logs logstash --tail 50
docker logs kibana --tail 50
```

Follow Logstash logs:

```
docker logs -f logstash
```

Restart Logstash:

```
docker compose restart logstash
```

Check Elasticsearch index:

```
GET _cat/indices?v
```

Check document count:

```
GET webapp-logs/_count
```

### If duplicate records appear

Clean reset:

```
docker compose down -v
docker compose up -d
```

Expected clean count: 100.

Warning: `down -v` deletes stored Elasticsearch data.

### If Elasticsearch has vm.max_map_count error

```
wsl -d docker-desktop
sysctl -w vm.max_map_count=262144
```

### If Kibana is not opening

```
docker logs kibana --tail 100
docker compose ps
```

Wait for initialization and retry `http://localhost:5601`.

## 7. Stop Docker

Stop containers:

```
docker compose down
```

Stop and delete Elasticsearch data:

```
docker compose down -v
```

That's the complete execution sequence. No code creation required.