This is the 25th day of my participation in Gwen Challenge

Writing in the front

Djangos REST Framework deployment Uwsgi + Nginx deployment is the basis of today’s article. It belongs to the traditional deployment mode, the steps are cumbersome, the process is complex, not suitable for real operation and maintenance scenarios, today we will talk about the deployment of real projects.

The text start

With the advent of the containerization era, the current operation and maintenance is no longer simply writing scripts to deploy services, but using K8s + Docker to deploy services. It makes o&M more server focused and developers more business focused. Those interested can check out the relevant documentation.

  • Kubernetes Chinese documentation
  • Docker Chinese document

1. Dcokerfile

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable image that can then be distributed to any popular Linux or Windows machine, as well as virtualization. Containers are completely sandboxed and have no interface with each other.

Here’s an overview of the syntax used in Dcokerfile

  • FROM Base image selection
  • WORKDIR Working directory
  • COPY Copies files to the working directory
  • RUN Command to RUN
  • EXPOSE Ports that the container exposes to the outside world
  • ENTRYPOINT is similar to CMD directives and is used to execute real deployment scripts
The FROM python: 3.6 WORKDIR/opt/app COPY.. the RUN PIP install - r requirements. TXT -i https://mirrors.aliyun.com/pypi/simple/  EXPOSE 8080 ENTRYPOINT ["sh"."entrypoint.sh"]

Copy the code

2. Deploy the script

Entrypoint. sh is a script that you can run and use to configure the deployment logic. The following files can be used to configure database changes based on environment variables.

Whether database changes need to be performed
if [ "$ENABLE_MIGRATE"= ="true" ];then
  python3 manage.py migrate
fi

echo "start demo web service"

Run according to the configuration environment
if [ "$ENV"= ="local" ];then
  execPython3 manage. Py runserver then executes 0.0.0.0:8080else
  python3 manage.py collectstatic
  exec gunicorn demo.wsgi:application \
    --name main_django \
    --max-requests 2000 \
    --max-requests-jitter 500 \
    --bind0.0.0.0:8080 \ --workers 4 \ -- Threads 4 \"$@"
fi
Copy the code

The Gunicorn deployment service is used here. For details, see the official gunicorn documentation.

Gunicorn is a high-performance Python WSGI Unix HTTP Server that is widely used on Unix. It is compatible with most Web frameworks and has the characteristics of simple implementation, lightweight and high performance.

There are no major functional differences from UWSGi, but the main differences are in the network model. Personal cognition of the two can be summarized as follows:

Gunicorn uses coroutines to provide concurrency support, which is beneficial for network IO intensive services. The advantages are more stable, high performance, and suitable for lightweight deployment.

UWSGI uWSGI is written in C and is forwarded through python’s interface that calls C. Supports protocols such as HTTP and WSGI. The advantage is that the function is very comprehensive, with cache, queue, RPC and other functions support.

3. The Docker

# Build docker image
docker build -t demo  . 

Run the Docker service
docker run -dit demo -p 8080:8080 /bin/bash
Copy the code

4. Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.

version: '2'
services:
  nginx:
    image: Nginx: 1.15 - alpine
    restart: always
    ports:
      - "8000:8080"
    volumes:
      - ./docker_nginx.conf:/etc/nginx/conf.d/default.conf:ro

  api:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - "8001:8080"
    volumes:
      - ./:/opt/app:rw
    depends_on:
      - nginx

Copy the code

This configuration file specifies two containers, one for API services and one for nginx, which meets the basic requirements of development. Of course, you can also specify the container of the database; This allows you to configure a perfect development environment using Docker.

Run the service in docker-comemage. yaml. If it is foreground startup, you can view the real-time log of the service. If it is started in the background, you can specify a container to view logs. The specific commands are as follows

# Foreground run service
docker-compose up 

The following are some common commands
# Background run service
docker-compose up -d

# Rebuild + start
docker-compose up -d --build

# view log specified container
docker-compose logs -f --tail=100 api
Copy the code

Some explanations:

  • In the YAML file, version indicates that the syntax version is 2.0. The syntax varies with different versions.
  • Dependencies between services are specified using depends_on. Dependencies are built first.
  • Services within a service can be accessed using names defined in YAML (the virtual network built inside docker-compose).
  • volumes: ./:/opt/app:rwIndicates that you have read and write permissions on the host directory. You can specify different permissions based on file usage.

The resources

  • Gunicorn
  • Docker Chinese document
  • In-depth understanding of UWSGI and Gunicorn network models