Docker部署Loki:一个容器化的日志聚合系统
紧密集成。
Docker部署Loki
要在Docker中部署Loki,我们需要以下几个步骤:
1. 安装Docker
首先,我们需要在主机上安装Docker。这里以Ubuntu系统为例,可以使用以下命令安装Docker:
$ sudo apt-get update
$ sudo apt-get install docker.io
2. 创建Loki配置文件
接下来,我们需要创建一个Loki的配置文件。在本例中,我们将使用以下配置:
auth_enabled: false
server:
http_listen_port: 3100
ingester:
lifecycler:
address: 127.0.0.1
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 5m
chunk_retain_period: 30s
schema_config:
configs:
- from: 2018-04-15
store: boltdb
object_store: filesystem
schema: v11
index:
prefix: index_
period: 168h
storage_config:
boltdb:
directory: /tmp/loki/index
filesystem:
directory: /tmp/loki/chunks
limits_config:
enforce_metric_name: false
reject_old_samples: true
reject_old_samples_max_age: 168h
将上述配置保存为loki-config.yaml
文件。
3. 创建Docker Compose文件
接下来,我们需要创建一个Docker Compose文件来定义Loki的Docker容器。
version: '3.7'
services:
loki:
image: grafana/loki:latest
volumes:
- ./loki-config.yaml:/etc/loki/local-config.yaml
command: -config.file=/etc/loki/local-config.yaml
ports:
- 3100:3100
将上述内容保存为docker-compose.yaml
文件。
4. 启动Loki容器
最后,我们可以使用Docker Compose来启动Loki容器。
$ docker-compose up -d
Loki容器将被启动,并且将在本地的3100端口上监听请求。
使用Loki
一旦Loki容器启动成功,我们可以通过发送日志到Loki服务器来开始记录日志。
1. 发送日志到Loki
Loki提供了一个HTTP API,我们可以使用它来发送日志。以下是一个示例Python脚本,演示如何通过HTTP API发送日志到Loki:
import requests
import json
import time
def send_log(message):
url = 'http://localhost:3100/loki/api/v1/push'
headers = {
'Content-Type': 'application/json'
}
data = {
'streams': [
{
'stream': {
'label': 'app_logs'
},
'values': [
[str(int(time.time() * 1000000000)), message]
]
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 204:
print('Log sent successfully!')
else:
print('Failed to send