欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 每天40分玩转Django:Django DevOps实践指南

每天40分玩转Django:Django DevOps实践指南

2025/1/14 0:12:48 来源:https://blog.csdn.net/weixin_40780178/article/details/144998730  浏览:    关键词:每天40分玩转Django:Django DevOps实践指南

Django DevOps实践指南

1. 学习目标

  • 掌握Django项目的CI/CD流程
  • 学习使用GitHub Actions实现自动化部署
  • 理解测试自动化和代码质量监控
  • 掌握生产环境的部署和监控

2. 核心知识点

模块重要程度掌握要求
CI/CD基础概念⭐⭐⭐⭐⭐深入理解
GitHub Actions⭐⭐⭐⭐⭐熟练使用
自动化测试⭐⭐⭐⭐掌握配置
代码质量检查⭐⭐⭐⭐理解并应用
自动化部署⭐⭐⭐⭐⭐熟练掌握

3. CI/CD配置示例

3.1 GitHub Actions工作流配置

# .github/workflows/django.ymlname: Django CI/CDon:push:branches: [ main ]pull_request:branches: [ main ]jobs:test:runs-on: ubuntu-latestservices:postgres:image: postgres:13env:POSTGRES_USER: postgresPOSTGRES_PASSWORD: postgresPOSTGRES_DB: github_actionsports:- 5432:5432options: >---health-cmd pg_isready--health-interval 10s--health-timeout 5s--health-retries 5steps:- uses: actions/checkout@v2- name: Set up Pythonuses: actions/setup-python@v2with:python-version: '3.9'- name: Install Dependenciesrun: |python -m pip install --upgrade pippip install -r requirements.txt- name: Run Testsenv:DATABASE_URL: postgresql://postgres:postgres@localhost:5432/github_actionsrun: |python manage.py test- name: Run Lintingrun: |pip install flake8flake8 .- name: Run Coveragerun: |pip install coveragecoverage run manage.py testcoverage reportdeploy:needs: testruns-on: ubuntu-latestif: github.ref == 'refs/heads/main'steps:- uses: actions/checkout@v2- name: Deploy to Productionuses: appleboy/ssh-action@masterwith:host: ${{ secrets.SERVER_HOST }}username: ${{ secrets.SERVER_USER }}key: ${{ secrets.SSH_PRIVATE_KEY }}script: |cd /var/www/myprojectgit pull origin mainsource venv/bin/activatepip install -r requirements.txtpython manage.py migratepython manage.py collectstatic --noinputsudo systemctl restart gunicorn

3.2 自动化测试配置

# myproject/settings_test.pyfrom .settings import *DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql','NAME': 'github_actions','USER': 'postgres','PASSWORD': 'postgres','HOST': 'localhost','PORT': '5432',}
}EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

3.3 代码质量检查配置

# setup.cfg[flake8]
max-line-length = 120
exclude = .git,*/migrations/*,venv
ignore = E501,W503[coverage:run]
source = .
omit = */tests/**/migrations/*venv/*manage.py[coverage:report]
fail_under = 80
show_missing = True

4. 部署脚本

4.1 生产环境部署脚本

#!/bin/bash
# deploy.shset -e# 1. 更新代码
git pull origin main# 2. 激活虚拟环境
source venv/bin/activate# 3. 安装依赖
pip install -r requirements.txt# 4. 收集静态文件
python manage.py collectstatic --noinput# 5. 执行数据库迁移
python manage.py migrate --noinput# 6. 重启Gunicorn
sudo systemctl restart gunicorn# 7. 重启Nginx
sudo systemctl restart nginx# 8. 清理缓存
rm -rf /tmp/django_cache/*echo "部署完成!"

4.2 Gunicorn配置

# gunicorn.conf.pyimport multiprocessingbind = "unix:/run/gunicorn.sock"
workers = multiprocessing.cpu_count() * 2 + 1
threads = 2
worker_class = "gthread"
worker_connections = 1000
timeout = 30
keepalive = 2errorlog = "/var/log/gunicorn/error.log"
accesslog = "/var/log/gunicorn/access.log"
loglevel = "info"daemon = False
pidfile = "/run/gunicorn/pid"
user = "www-data"
group = "www-data"reload = False
max_requests = 2000
max_requests_jitter = 400capture_output = True
enable_stdio_inheritance = True

5. 监控配置

5.1 Prometheus配置

# prometheus.ymlglobal:scrape_interval: 15sevaluation_interval: 15sscrape_configs:- job_name: 'django'static_configs:- targets: ['localhost:8000']metrics_path: '/metrics'

5.2 Django应用监控

# monitoring.pyfrom prometheus_client import Counter, Histogram
from django.conf import settings# 请求计数器
REQUEST_COUNT = Counter('django_http_requests_total','Total HTTP requests count',['method', 'endpoint', 'status']
)# 请求延迟直方图
REQUEST_LATENCY = Histogram('django_http_request_duration_seconds','HTTP request latency',['method', 'endpoint']
)class PrometheusMiddleware:def __init__(self, get_response):self.get_response = get_responsedef __call__(self, request):if request.path == '/metrics':return self.get_response(request)method = request.methodpath = request.pathwith REQUEST_LATENCY.labels(method=method, endpoint=path).time():response = self.get_response(request)REQUEST_COUNT.labels(method=method,endpoint=path,status=response.status_code).inc()return response

6. 流程图

在这里插入图片描述

7. 最佳实践建议

7.1 安全措施

# security.pyfrom django.conf import settings
from django.http import HttpResponseForbiddenclass SecurityMiddleware:def __init__(self, get_response):self.get_response = get_responsedef __call__(self, request):# 检查IP白名单if settings.IP_WHITELIST and request.META.get('REMOTE_ADDR') not in settings.IP_WHITELIST:return HttpResponseForbidden()# 添加安全头response = self.get_response(request)response['X-Content-Type-Options'] = 'nosniff'response['X-Frame-Options'] = 'DENY'response['X-XSS-Protection'] = '1; mode=block'return response

7.2 备份策略

# backup.pyimport os
import datetime
import subprocessdef backup_database():date = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')filename = f'backup_{date}.sql'# 数据库备份subprocess.run(['pg_dump','-U', os.getenv('DB_USER'),'-h', os.getenv('DB_HOST'),os.getenv('DB_NAME'),'-f', f'/backups/{filename}'])# 压缩备份文件subprocess.run(['gzip', f'/backups/{filename}'])# 上传到S3subprocess.run(['aws', 's3', 'cp',f'/backups/{filename}.gz',f's3://{os.getenv("BACKUP_BUCKET")}/databases/'])

8. 性能优化

8.1 缓存配置

# settings.pyCACHES = {'default': {'BACKEND': 'django.core.cache.backends.redis.RedisCache','LOCATION': 'redis://127.0.0.1:6379/1','OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient','PARSER_CLASS': 'redis.connection.HiredisParser','CONNECTION_POOL_CLASS': 'redis.connection.BlockingConnectionPool','CONNECTION_POOL_CLASS_KWARGS': {'max_connections': 50,'timeout': 20,}}}
}# 缓存会话
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

8.2 数据库优化

# settings.pyDATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql','NAME': os.getenv('DB_NAME'),'USER': os.getenv('DB_USER'),'PASSWORD': os.getenv('DB_PASSWORD'),'HOST': os.getenv('DB_HOST'),'PORT': os.getenv('DB_PORT', '5432'),'CONN_MAX_AGE': 60,'OPTIONS': {'connect_timeout': 10,'statement_timeout': 30000,}}
}

配置完成后,你的Django项目就具备了完整的DevOps支持,包括自动化测试、部署和监控。记住要根据实际项目需求调整这些配置。定期检查和更新这些配置也是保持系统健康的重要部分。


怎么样今天的内容还满意吗?再次感谢朋友们的观看,关注GZH:凡人的AI工具箱,回复666,送您价值199的AI大礼包。最后,祝您早日实现财务自由,还请给个赞,谢谢!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com