docker部署django框架并实现nginx负载均衡

2023-05-2708:17:01服务器及运维Comments1,159 views字数 3571阅读模式

docker部署django项目

对于生成环境来说,使用python manage.py runserver来说是不合适的。可以使用uWSGI服务器来运行django项目。但是由于自己的机子装不好uwsgi,所以使用docker安装。在这里用来测试的django项目名字叫做sjor或者是todj,都是简单的新建的django项目。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

首先使用uwsgi搭建可以使用http的环境的方法。这里可以直接用浏览器访问。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

拉取python:3.8镜像并且映射端口。使用docker cp /path id:/sjor命令将项目文件复制到编号为id的容器的/sjor目录下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

因为python镜像的CMD命令是python3所以应该使用docker exec -it id bashattach到容器的bash中。使用pip install -r requirements.txt安装依赖。参考依赖:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
asgiref==3.5.1
backports.zoneinfo==0.2.1
Django==4.0.4
mysqlclient==2.1.0
PyMySQL==1.0.2
sqlparse==0.4.2
typing_extensions==4.2.0
uWSGI==2.0.20

使用uwsgi --ini uwsgi.ini来运行项目,示例的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
[uwsgi]
http = :8002
chdir = /sjor
wsgi-file = sjor/wsgi.py
process = 4
threads = 2
pidfile = pro.pid
daemonize=sjor.log
master=true

可以使用uwsgi --stop pro.pid来停止服务。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

以上表示将http请求发送到了容器8002端口。如果在运行容器的时候进行了端口映射,那么直接浏览器访问就可以看到:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

可以在容器内使用ps -aux看到进程列表:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

还可以考虑使用uwsgi和nginx的方法来搭建发送socket的后端。这里不能直接用浏览器访问uswgi。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

这里可以使用uwsgi的socket服务而不是使用http,这样的话就更快。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
[uwsgi]
socket = :8002
chdir = /sjor
wsgi-file = sjor/wsgi.py
process = 4
threads = 2
pidfile = pro.pid
daemonize = sjor.log
master = true
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000

开启一个nginx容器,映射端口80到宿主机上,如下所示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
upstream todj{
    server 119.23.182.180:10003 weight=1;
}

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    access_log  /var/log/nginx/host.access.log  main;

    location / {
        uwsgi_pass todj;
        include /etc/nginx/uwsgi_params;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

这里的流的名字,要加入到django项目的ALLOW_HOST字段中。例如这里的todj文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

打开本地宿主机被绑定的端口,可以看到已经成功访问了:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

但是如果是本地docker的Python容器运行的uwsgi,用nginx进行上述连接会报错。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

txt文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

nginx转发到django负载均衡

背景:如果有多台服务器,可以部署多个后端对数据进行分布式处理。配合docker等工具,虽然有多个服务器,但是前端在请求的时候只用请求nginx服务器,之后的工作由nginx处理。并且使用nginx的反向代理可以顺带解决跨域请求的问题。如果每个服务器端配置不同,可以通过nginx实现端口转发,负载控制操作。碰巧我就有那么几台服务器,所以可以部署诺干个django后端。并且可以使用nginx的代理操作将某一个请求的端口号省略掉。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

在宝塔或者阿里云控制台等开启某防火墙端口,运行django,确保可以直接访问。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

django里面的app的views视图可以添加如下函数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

python文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
def showG(request):
    print('Request from {}:'.format(request.META['REMOTE_ADDR']))
    return HttpResponse('get')

使用runserver运行服务器。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

本地运行一个docker的nginx容器,使用如下命令docker run -it -d -p 12345:80 nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

浏览器打开http://127.0.0.1:12345就可以看到nginx默认页面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

可以修改配置文件etc/nginx/conf.d/defalut.conf进行一次反向代理文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
server {
    listen       80;
    listen  [::]:80;
    server_name  www.masaikk.xyz;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://www.masaikk.xyz:10003;
        proxy_redirect default;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

这里将本地的80转发到了http://www.masaikk.xyz:10003,可以正常访问。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

后端也可以正常收到请求并且记录。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

添加对于多个服务器的配置,在配置文件中添加如下流:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

css文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
upstream todj{
    server www.masaikk.xyz:10003;
    server 119.23.182.180:10003;
}

这个流的名字todj需要添加到django的ALLOW_HOSTS中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

修改配置文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
upstream todj{
    server www.masaikk.xyz:10003;
    server 119.23.182.180:10003;
}

server {
    listen       80;
    listen  [::]:80;
    server_name  www.masaikk.xyz;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://todj;
        proxy_redirect default;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

重启容器,即可达到预想效果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

docker部署django框架并实现nginx负载均衡文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

两台服务器都能收到请求。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

同时,可以通过设置权重的方式来设置对于每个服务器的访问流量控制。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

ini文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
复制代码
upstream todj{
    server www.masaikk.xyz:10003 weight=1;
    server 119.23.182.180:10003 weight=3;
}

也可以使用上述说明的使用socket的方式和nginx沟通,不过需要注意的是,使用了socket的ip不需要记上http://等前缀。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html

作者:马赛柯柯
链接:https://juejin.cn/post/7150093955105816612
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42776.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/42776.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定