生产部署
部署 Potato 用于多用户生产环境标注。
生产部署
为多用户、数据持久化和适当安全性的真实标注任务部署 Potato。
服务器配置
基本生产配置
yaml
server:
host: 0.0.0.0
port: 8000
workers: 4
timeout: 120使用 HTTPS
用于安全连接(生产环境推荐):
yaml
server:
host: 0.0.0.0
port: 443
ssl:
cert: /path/to/cert.pem
key: /path/to/key.pem使用 Gunicorn 运行
为了更好的性能和可靠性:
bash
gunicorn "potato.server:create_app('config.yaml')" \
--workers 4 \
--bind 0.0.0.0:8000 \
--timeout 120带 SSL
bash
gunicorn "potato.server:create_app('config.yaml')" \
--workers 4 \
--bind 0.0.0.0:443 \
--certfile /path/to/cert.pem \
--keyfile /path/to/key.pem反向代理设置
Nginx 配置
nginx
upstream potato {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name annotation.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name annotation.example.com;
ssl_certificate /etc/letsencrypt/live/annotation.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/annotation.example.com/privkey.pem;
location / {
proxy_pass http://potato;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Increase timeouts for long annotation sessions
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
}Apache 配置
apache
<VirtualHost *:443>
ServerName annotation.example.com
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
</VirtualHost>Docker 部署
Dockerfile
dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "potato.server:create_app('config.yaml')", \
"--workers", "4", "--bind", "0.0.0.0:8000"]Docker Compose
yaml
version: '3.8'
services:
potato:
build: .
ports:
- "8000:8000"
volumes:
- ./data:/app/data
- ./annotations:/app/annotations
environment:
- POTATO_DEBUG=false
restart: unless-stopped
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./certs:/etc/ssl/certs
depends_on:
- potato
restart: unless-stopped用户认证
受限访问
yaml
allow_all_users: false
authorized_users:
- user1@example.com
- user2@example.com
- user3@example.com管理员访问
yaml
admin_users:
- admin@example.com
admin_dashboard:
enabled: true
path: /admin会话管理
yaml
session:
secret_key: "your-secure-secret-key-here"
timeout: 3600 # 1 hour
secure_cookie: true # Requires HTTPS数据持久化
基于文件的存储
yaml
output:
path: /var/potato/annotations/
format: json
backup_interval: 3600 # Hourly backups数据库后端
对于更大规模的部署,使用 MySQL:
yaml
database:
type: mysql
host: localhost
port: 3306
name: potato_db
user: potato
password: ${DB_PASSWORD} # Use environment variable详见 MySQL 后端。
监控
健康检查端点
yaml
monitoring:
health_check: /health
metrics: /metrics日志配置
yaml
logging:
level: INFO
file: /var/log/potato/app.log
max_size: 100MB
backup_count: 5
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"Prometheus 指标
yaml
monitoring:
prometheus:
enabled: true
port: 9090扩展
水平扩展
用于高流量部署:
yaml
# docker-compose.yml
services:
potato:
deploy:
replicas: 3
# ... rest of config
nginx:
# Load balancer
volumes:
- ./nginx-lb.conf:/etc/nginx/nginx.conf负载均衡配置
nginx
upstream potato_cluster {
least_conn;
server potato_1:8000;
server potato_2:8000;
server potato_3:8000;
}备份策略
自动备份
bash
#!/bin/bash
# backup.sh
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR=/var/backups/potato
# Backup annotations
tar -czf $BACKUP_DIR/annotations_$DATE.tar.gz /var/potato/annotations/
# Backup config
cp /var/potato/config.yaml $BACKUP_DIR/config_$DATE.yaml
# Keep only last 7 days
find $BACKUP_DIR -mtime +7 -delete添加到 crontab:
bash
0 2 * * * /var/potato/backup.sh安全检查清单
- 使用有效 SSL 证书的 HTTPS
- 设置强会话密钥
- 使用
authorized_users限制用户访问 - 保持 Potato 和依赖更新
- 对敏感数据使用环境变量
- 配置防火墙规则
- 启用日志和监控
- 设置定期备份
- 生产环境使用数据库后端
- 定期检查管理员访问权限
环境变量
bash
# .env file
POTATO_SECRET_KEY=your-super-secret-key
POTATO_DB_PASSWORD=database-password
POTATO_ADMIN_EMAIL=admin@example.com在 Docker Compose 中加载:
yaml
services:
potato:
env_file:
- .env故障排除
检查日志
bash
# Gunicorn logs
journalctl -u potato -f
# Docker logs
docker-compose logs -f potato常见问题
502 Bad Gateway:检查 Potato 是否正在运行并绑定到正确的端口
会话问题:确保密钥在重启之间保持一致
性能缓慢:增加 worker 数量或添加数据库后端