Django application deployment using Docker containerized deployment practice (1)

At the weekend, some students in a technology group asked about the development and deployment of Django, so today I will share with you the deployment of the Python Django framework based on Docker.

First of all, we need to know two common deployment methods for Django:

  1. Django + Nginx + uWSGI

  2. Django+ Nginx + Gunicorn

Due to space limitations, today we will mainly talk about the first deployment method of uWSGI and Nginx.

First of all, we need to know the steps for our release and launch. Here we take Docker containerization as an example:

1. Synchronize the code to the online server through automated scripts (through fabric or ansible, etc.)
2. Restart supervisord (process management tool, control Django service startup
through uWSGI ) 3. Update docker image through docker-compose (if there is a mirror Change, such as installing the package into the container)
4. Make the data table migrate, such as the data table has been changed (if it is a relational database)
5. Restart the docker container through docker-compose

In the above five steps, as long as the corresponding configuration files (nginx, uWSGI, supervisor, etc.) are configured, we can release the code with one click through a release script.

Let's briefly explain the configuration files and usage of Nginx, Supervisor, uWSGI, D ocker-compose and so on.

Nginx

Nginx is a high-performance HTTP and reverse proxy server.

We need to deploy the service. First, we need to configure the corresponding configuration file for Nginx.

Put the configuration file under the /etc/nginx/sites-enabled/ directory, here you can also put it in the project and link it through ln.

The reason for this configuration is that our main configuration file /etc/nginx/nginx.conf is to include the /etc/nginx/sites-enabled/ directory.

Here our configuration file is named api.yourdomain.com.conf 配置文件如下

# api.yourdomain.com.conf

server {

    listen 80;

    charset utf-8;

    server_name api.yourdomain.com;  # DNS解析到当前nginx服务器

    location / {

        proxy_pass  http://127.0.0.1:9527; # 代理的端口
        proxy_pass_header Server;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Scheme $scheme;

    }

    access_log /test/log/nginx.access.log;

    error_log /test/log/nginx.error.log;

}

uWSGI

After configuring nginx, the next step is to configure uWSGI. uWSGI is a web server that implements the WSGI protocol, uwsgi, http and other protocols.

Later we will say that uWSGI and Gunicorn are both servers that implement the WSGI server protocol.

Through uWSGI we can get better service performance, detailed logs, multi-application management and other customized functions. Let's briefly look at the configuration file below.

# /yourdir/uwsgi.ini 

[uwsgi]

chdir     = /yourdir/  # 执行目录
module    = yourproject.wsgi # wsgi文件
master    = true
processes = 1  # 一般越多进程处理能力越强,由于这里是测试设为1
vacuum    = true
http      = 0.0.0.0:9527

# virtualenv = /home/test/project/python/yourproject/venv

这里我们用了docker就不用考虑virtualenv情况

env = DJANGO_SETTINGS_MODULE=yourproject.settings

harakiri  = 30 # 当进程被卡住的时间超过特定的秒数后就销毁这个进程

no-orphans  # 在没有主进程的情况下自动结束工作进程。

In order to facilitate the display and interpretation, I have deleted and simplified some configurations. The specific online configuration is more complicated. Please configure it for specific situations.

In addition, we need to know that uWSGI is a language-independent server. Here we can deploy Django, Flask, Web2py, etc. You can equate it with the command Python manage.py runserver here, and the difference is in a test environment and an online environment.

Supervisor

I have shared the three commonly used commands in my first Linux series of articles for supervisor . Here is a configuration file.

[supervisord]

nodaemon=true

logfile=/data/log/supervisord.log
pidfile=/var/run/supervisord.pid

[program:your_app_name]


process_name=app%(process_num)s

command=uwsgi -i /yourdir/uwsgi.ini --touch-reload=/yourdir/uwsgi.ini

directory=/yourdir ;执行命令时切换工作目录
user=sitin                 ; 使用sitin来启动进程
stopsignal=HUP
autostart = true
autorestart=true  ;自动重启
redirect_stderr = true  ;重定向日志
stdout_logfile = /data/log/stdout-%(program_name)s.log

logfile_maxbytes=300MB

logfile_backups=5

numprocs=3  ;启动3个 952795289529

numprocs_start=9527 ; 从9527开始

Here, the supervisor controls the service uWSGI to start the service, and we can perform operations such as start, restart, and stop.

Docker-compose

Docker-compose is for container orchestration to facilitate our container management. We have configured the above files, and then take a look at the docker-compose configuration file, here also I shared in the previous docker article, refer to the  Docker containerized deployment practice Docker Compose

version: "3"  # 注意版本号

services:  # 一个应用的容器,可以包括多个相同镜像容器实例

  dev:
    image: hub.yourdomain.com/test:1.0 # 镜像地址
    command: supervisord -c /yourdir/supervisord.conf # 容器执行命令
    container_name: test  # 容器名
    restart: always  
    volumes:
      - ./docker:/data  # 挂载地址

    ports:
      - "9527:9527" #端口

    environment:
      - PYTHONPATH=/data
      - XXX_API_SETTINGS=XXX.config.dev

      - DJANGO_SETTINGS_MODULE=yourproject.settings
    network_mode: bridge
    extra_hosts: # 配置额外的host名称

      - "test.yourdomain.com:127.0.0.1"
    external_links: # 链接到外部容器
      - redis:redis 
      - mysql:mysql

I think some of my classmates are already a little dizzy when I see this, let's sort out the logic here.

  1. We control the startup of supervisor through the docker-compose configuration file

  2. Supervisor controls uWSGI port service startup

  3. uWSGI pulls up the entire Django application (here similar to python manage.py runserver)

  4. 而我们浏览器请求到达Nginx之后被反向代理到uWSGI端口服务上面,从而访问到我们Django应用。

  5. 至此就是整个应用的部署配置(除了没有自动化发布脚本以外)

最后

通过上面的配置文件,我们介绍了整个Django应用发布过程的涉及到的主要步骤,实际情况会稍微复杂点儿。更多详细优化参数大家可以去进行了解。今天我们uWSGI的部署就先到这儿,下一期我们再进行分享Gunicorn部署方式。

容器化部署实践,我打算从基础操作到实战应用以一个系列进行分享,今天是第四篇docker部署Django应用,后期我将持续分享更多相关内容,从容器化入门使用到部署实践编排技术,欢迎大家持续进行关注。


Guess you like

Origin blog.51cto.com/15009257/2552384