第二章:安装与部署
掌握 Loki 的多种安装方式,包括 Docker、Helm 和二进制部署。
最后更新: 2024-01-16
页面目录
Loki 安装与部署
本章节介绍 Loki 的多种安装和部署方式。
安装方式概述
| 安装方式 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| Docker | 开发测试 | 快速部署 | 生产环境不推荐 |
| Helm | Kubernetes | 云原生支持 | 需要 Helm |
| 二进制 | 传统服务器 | 简单直接 | 需手动管理 |
| Tanka | Kubernetes | 代码化配置 | 学习曲线 |
Docker 部署
单节点部署
# 创建数据目录
mkdir -p /data/loki
# 运行 Loki
docker run -d \
--name loki \
-p 3100:3100 \
--volume /data/loki:/loki \
grafana/loki:2.9.0 \
-config.file=/loki/local-config.yaml
Docker Compose 部署
# docker-compose.yml
version: '3.8'
services:
loki:
image: grafana/loki:2.9.0
container_name: loki
ports:
- "3100:3100"
volumes:
- ./loki:/loki
- ./config/loki-config.yaml:/etc/loki/config.yaml
command: -config.file=/etc/loki/config.yaml
restart: unless-stopped
promtail:
image: grafana/promtail:2.9.0
container_name: promtail
volumes:
- ./config/promtail-config.yaml:/etc/promtail/config.yaml
- /var/log:/var/log
volumes_from:
- nginx
command: -config.file=/etc/promtail/config.yaml
restart: unless-stopped
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
volumes:
- ./grafana:/var/lib/grafana
depends_on:
- loki
restart: unless-stopped
运行
# 启动服务
docker-compose up -d
# 查看日志
docker-compose logs -f loki
# 停止服务
docker-compose down
Helm 部署 (Kubernetes)
添加 Helm 仓库
# 添加 Loki Helm 仓库
helm repo add grafana https://grafana.github.io/helm-charts
# 更新仓库
helm repo update
# 搜索 Loki
helm search repo loki
基本安装
# 创建 namespace
kubectl create namespace loki
# 安装 Loki
helm install loki grafana/loki \
--namespace loki \
--set persistence.enabled=true \
--set persistence.size=50Gi
生产环境配置
# values-production.yaml
---
replicas: 3
image:
repository: grafana/loki
tag: 2.9.0
pullPolicy: IfNotPresent
config:
schema_config:
configs:
- from: 2023-01-01
store: boltdb-shipper
object_store: s3
schema: v11
index:
prefix: loki_index_
period: 24h
storage_config:
boltdb_shipper:
shared_store: s3
aws:
bucketnames: loki-data
region: us-east-1
s3forcepathstyle: false
limits_config:
reject_old_samples: true
reject_old_samples_max_age: 168h
ingestion_rate_mb: 50
ingestion_burst_size_mb: 100
persistence:
enabled: true
storageClassName: gp3
size: 100Gi
resources:
requests:
cpu: 200m
memory: 512Mi
limits:
cpu: 2000m
memory: 4Gi
nodeSelector:
node-type: loki
tolerations:
- effect: NoSchedule
key: node-type
operator: Equal
value: loki
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app.kubernetes.io/name: loki
topologyKey: kubernetes.io/hostname
部署命令
# 使用自定义配置安装
helm install loki grafana/loki \
--namespace loki \
-f values-production.yaml
# 查看部署状态
kubectl get pods -n loki
# 查看日志
kubectl logs -n loki -l app.kubernetes.io/name=loki -f
安装 Promtail (DaemonSet)
# 安装 Promtail
helm install promtail grafana/promtail \
--namespace loki \
--set config.lokiAddress=http://loki:3100/loki/api/v1/push
二进制部署
下载二进制
# 下载 Loki
curl -O -L "https://github.com/grafana/loki/releases/download/v2.9.0/loki-linux-amd64.zip"
unzip loki-linux-amd64.zip
mv loki-linux-amd64 /usr/local/bin/loki
# 下载 Promtail
curl -O -L "https://github.com/grafana/loki/releases/download/v2.9.0/promtail-linux-amd64.zip"
unzip promtail-linux-amd64.zip
mv promtail-linux-amd64 /usr/local/bin/promtail
# 下载 LogCLI
curl -O -L "https://github.com/grafana/loki/releases/download/v2.9.0/logcli-linux-amd64.zip"
unzip logcli-linux-amd64.zip
mv logcli-linux-amd64 /usr/local/bin/logcli
# 设置权限
chmod +x /usr/local/bin/loki /usr/local/bin/promtail /usr/local/bin/logcli
创建系统用户
# 创建用户
useradd -r -s /bin/false loki
# 创建目录
mkdir -p /data/loki /etc/loki
chown -R loki:loki /data/loki /etc/loki
配置 Loki
# /etc/loki/config.yaml
---
schema_config:
configs:
- from: 2023-01-01
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: loki_index_
period: 24h
storage_config:
boltdb_shipper:
shared_store: filesystem
filesystem:
directory: /data/loki/chunks
limits_config:
reject_old_samples: true
reject_old_samples_max_age: 168h
ingestion_rate_mb: 50
ingestion_burst_size_mb: 100
memberlist:
join_members:
- loki:7946
ingester:
lifecycler:
address: 127.0.0.1
ring:
kvstore:
store: inmemory
replication_factor: 1
chunk_encoding: snappy
query_scheduler:
max_outstanding_per_tenant: 4096
frontend:
max_outstanding_per_tenant: 4096
# 日志配置
server:
http_listen_address: 0.0.0.0
http_listen_port: 3100
grpc_listen_address: 0.0.0.0
grpc_listen_port: 9095
创建 Systemd 服务
# /etc/systemd/system/loki.service
[Unit]
Description=Loki Log Aggregation System
After=network-online.target
[Service]
Type=simple
User=loki
Group=loki
ExecStart=/usr/local/bin/loki -config.file /etc/loki/config.yaml
Restart=on-failure
RestartSec=10
LimitNOFILE=65536
StandardOutput=journal
StandardError=journal
SyslogIdentifier=loki
[Install]
WantedBy=multi-user.target
启动服务
# 重新加载 systemd
systemctl daemon-reload
# 启动 Loki
systemctl start loki
# 设置开机自启
systemctl enable loki
# 查看状态
systemctl status loki
# 查看日志
journalctl -u loki -f
Tanka 部署
安装 Tanka
# 下载 Tanka
curl -o /usr/local/bin/tk https://github.com/grafana/tanka/releases/download/v0.27.0/tk-linux-amd64
chmod +x /usr/local/bin/tk
# 安装 jsonnet-bundler
go install github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest
# 配置环境变量
export PATH=$PATH:$HOME/go/bin
初始化项目
# 创建项目
mkdir loki-tanka && cd loki-tanka
tk init
# 安装 Loki jsonnet
jb install github.com/grafana/loki/production/ksonnet/loki
# 配置
tk env add environments/default --server=https://kubernetes.default.svc
部署配置
// environments/default/main.jsonnet
local loki = import 'loki/loki.libsonnet';
loki {
_config+:: {
namespace: 'loki',
},
loki+: {
config+: {
schema_config+: {
configs: [{
from: '2023-01-01',
store: 'boltdb-shipper',
object_store: 'filesystem',
schema: 'v11',
index: {
prefix: 'loki_index_',
period: '24h',
},
}],
},
storage_config+: {
boltdb_shipper+: {
shared_store: 'filesystem',
},
filesystem+: {
directory: '/data/loki/chunks',
},
},
},
},
}
部署
# 应用配置
tk apply environments/default
# 查看状态
kubectl get pods -n loki
验证安装
检查服务状态
# 检查 Loki API
curl http://localhost:3100/ready
# 检查配置
curl http://localhost:3100/config
# 查看版本
curl http://localhost:3100/loki/api/v1/version
检查日志
# Docker
docker logs loki
# Systemd
journalctl -u loki -n 100
配置 Grafana
# 访问 Grafana
# http://localhost:3000
# 添加数据源
# 1. 进入 Configuration → Data Sources
# 2. Add data source
# 3. 选择 Loki
# 4. URL: http://loki:3100
# 5. Save & Test
下一步
现在你已经掌握了 Loki 的安装部署。接下来让我们学习配置详解。
👉 配置详解