第18章:生产环境部署指南
18.1 部署前准备
18.1.1 系统要求
最低配置:
- CPU:2 核心
- 内存:2 GB
- 磁盘:10 GB(SSD 推荐)
- 操作系统:Linux/Windows/macOS
推荐配置:
- CPU:4+ 核心
- 内存:8+ GB
- 磁盘:100+ GB(SSD)
- 操作系统:Linux(Ubuntu 20.04+)
18.1.2 依赖检查
bash
# Go 版本
go version
# 检查端口占用
netstat -tulpn | grep -E '8081|1883'
# 检查磁盘空间
df -h
18.2 配置优化
18.2.1 生产配置示例
json
{
"db_path": "/var/lib/sfsedgestore/data",
"db_use_encryption": false,
"db_scenario": "edge",
"mqtt_broker": "tcp://mqtt-broker:1883",
"mqtt_client_id": "sfsedgestore-prod",
"mqtt_qos": 1,
"mqtt_batch_size": 100,
"mqtt_batch_interval": 5,
"http_port": "8081",
"http_use_tls": true,
"http_cert": "/etc/ssl/certs/sfsedgestore.crt",
"http_key": "/etc/ssl/private/sfsedgestore.key",
"log_level": "info",
"log_file": "/var/log/sfsedgestore/app.log"
}
18.2.2 系统参数调优
bash
# /etc/sysctl.conf
# 增加文件描述符限制
fs.file-max = 100000
# 网络调优
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
# 内存调优
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
bash
# /etc/security/limits.conf
sfsedgestore soft nofile 65536
sfsedgestore hard nofile 65536
sfsedgestore soft nproc 4096
sfsedgestore hard nproc 4096
18.3 容器化部署
18.3.1 Dockerfile
dockerfile
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o sfsedgestore .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/sfsedgestore .
COPY --from=builder /app/config.json .
EXPOSE 8081
CMD ["./sfsedgestore"]
18.3.2 Docker Compose
yaml
version: '3.8'
services:
sfsedgestore:
build: .
ports:
- "8081:8081"
volumes:
- data:/var/lib/sfsedgestore/data
- logs:/var/log/sfsedgestore
environment:
- MQTT_BROKER=tcp://mosquitto:1883
depends_on:
- mosquitto
restart: unless-stopped
mosquitto:
image: eclipse-mosquitto:2.0
ports:
- "1883:1883"
volumes:
- mosquitto-config:/mosquitto/config
- mosquitto-data:/mosquitto/data
restart: unless-stopped
volumes:
data:
logs:
mosquitto-config:
mosquitto-data:
18.4 Kubernetes 部署
18.4.1 Deployment
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sfsedgestore
spec:
replicas: 3
selector:
matchLabels:
app: sfsedgestore
template:
metadata:
labels:
app: sfsedgestore
spec:
containers:
- name: sfsedgestore
image: sfsedgestore:latest
ports:
- containerPort: 8081
env:
- name: MQTT_BROKER
value: "tcp://mqtt-broker:1883"
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "2000m"
memory: "2Gi"
livenessProbe:
httpGet:
path: /health
port: 8081
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8081
initialDelaySeconds: 5
periodSeconds: 5
volumeMounts:
- name: data
mountPath: /var/lib/sfsedgestore/data
volumes:
- name: data
persistentVolumeClaim:
claimName: sfsedgestore-data
18.4.2 Service
yaml
apiVersion: v1
kind: Service
metadata:
name: sfsedgestore
spec:
selector:
app: sfsedgestore
ports:
- protocol: TCP
port: 80
targetPort: 8081
type: LoadBalancer
18.5 监控与告警
18.5.1 Prometheus 指标
go
import "github.com/prometheus/client_golang/prometheus"
var (
messagesReceived = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "sfsedgestore_messages_received_total",
Help: "Total number of messages received",
},
)
httpRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "sfsedgestore_http_requests_total",
Help: "Total number of HTTP requests",
},
)
databaseLatency = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "sfsedgestore_database_latency_seconds",
Help: "Database operation latency",
Buckets: prometheus.DefBuckets,
},
)
)
func init() {
prometheus.MustRegister(messagesReceived)
prometheus.MustRegister(httpRequests)
prometheus.MustRegister(databaseLatency)
}
18.5.2 Grafana 仪表盘
json
{
"dashboard": {
"panels": [
{
"title": "Messages Received",
"type": "graph",
"targets": [
{
"expr": "rate(sfsedgestore_messages_received_total[5m])"
}
]
},
{
"title": "HTTP Requests",
"type": "graph",
"targets": [
{
"expr": "rate(sfsedgestore_http_requests_total[5m])"
}
]
},
{
"title": "Database Latency",
"type": "graph",
"targets": [
{
"expr": "histogram_quantile(0.95, rate(sfsedgestore_database_latency_seconds_bucket[5m]))"
}
]
}
]
}
}
18.6 备份与恢复
18.6.1 自动化备份脚本
bash
#!/bin/bash
BACKUP_DIR="/var/backups/sfsedgestore"
DATA_DIR="/var/lib/sfsedgestore/data"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/sfsedgestore_$DATE.tar.gz"
mkdir -p $BACKUP_DIR
tar -czf $BACKUP_FILE -C $DATA_DIR .
find $BACKUP_DIR -name "sfsedgestore_*.tar.gz" -mtime +7 -delete
echo "Backup completed: $BACKUP_FILE"
18.6.2 Cron 定时备份
bash
# /etc/cron.d/sfsedgestore-backup
0 2 * * * sfsedgestore /opt/sfsedgestore/scripts/backup.sh
18.7 实战练习
练习 18.1:生产部署
在生产环境中部署 sfsEdgeStore,包括监控和告警。
练习 18.2:灾难恢复
模拟数据丢失,执行灾难恢复流程。
练习 18.3:性能调优
根据生产环境监控数据,进行性能调优。
18.8 本章小结
本章讲解了生产环境部署:
- 部署前准备和系统要求
- 配置优化
- 容器化部署(Docker、Kubernetes)
- 监控与告警(Prometheus、Grafana)
- 备份与恢复策略
完善的部署和运维流程是生产环境稳定运行的保障。
本书版本 :1.0.0
最后更新 :2026-03-08
sfsEdgeStore - 让边缘数据存储更简单!🚀
技术栈 - Go语言、sfsDb与EdgeX Foundry。纯golang工业物联网边缘计算技术栈
项目地址 :GitHub
GitCode 镜像 :GitCode