我怎样才能从多台机器为我的 django 网站提供服务,也就是说我怎样才能让它分布式?

How can I serve my django website from multiple machines, that is how can I make it distributed?

我有我的 django 网站,我想将其分布式,我知道系统设计和分布式系统的所有概念,但仍然无法弄清楚如何使用多个服务器为其提供服务。我试图让我的系统分布式,这样我就可以从两台机器上为我的网站提供服务(这就是分布式系统的工作方式)。我用 Django 编写了我的网站。我想知道让我的同一个网站在两台机器上运行的步骤。这就是两个系统将如何相互了解,它们将如何连接以及每当有请求进入时,将选择其中一个服务器来处理该请求。我应该用什么软件或工具来加入我的服务器,在这种情况下,哪些软件会接受请求,以便它可以决定将请求发送到哪台机器,在这种情况下,数据库应该如何配置?

P.S :我唯一知道的是如何使用一台服务器为我的 django 网站提供服务(从 Linode、DigitalOcean 获取机器实例)。我想为我的网站实现系统设计分布式系统概念,这样我就可以通过亲自实现它来学习系统设计的所有概念

从单一服务器设置到分布式(高可用)的步骤并不是一个非常简单的步骤,与 Django 关系不大,但与通用服务器基础设施关系更大。但是,有很多资源可以帮助您入门。

考虑到您提到了 DigitalOcean,我相信他们网站上的以下教程将指导您朝着正确的方向前进:Building for Production: Web Applications — Overview

在完整阅读那篇文章之前,请务必阅读 5 Common Server Setups For Your Web Application。它很好地概述了常见的服务器设置,从单个服务器到您想要的状态。

您要做的是为您的应用程序实施分布式系统概念。为此,您需要了解分布式系统和系统设计的概念。 Django 与将您的网站扩展到多个服务器无关。现在一切都掌握在分布式系统技术和软件的手中。无论我在这个答案中解释了什么,都可以让您对分布式系统的工作原理有一个基本而全面的了解。

我可以向您简要说明如何实现这一目标。我假设你有两台服务器,你希望请求进入并由任何一台服务器提供服务。(就像任何分布式系统的工作方式一样)。

首先您应该熟悉负载均衡器、Web 服务器、应用程序服务器、数据库服务器的概念。

当我们只有一台服务器时,一切就容易多了,我们有一台服务器来设置我们的 web 服务器(例如 apache),我们需要一些接口,以便我们的 web 服务器和 web 框架可以相互通信所以我们安装了 mod_wsgi apache 模块,它可以通过 WSGI 接口规范与 Django 通信。从那时起,我们只需要编辑我们的 apache conf 文件,我们就可以开始了。

现在,我们了解到单个 Web 服务器不足以处理这些请求。我们又买了一台服务器来减少一台服务器的负载。您需要做的第一件事是将相同的 Django 网站代码复制到另一台服务器。这样两者都是 运行 相同的前端代码,我们的应用程序服务器(Django)可以处理请求。现在,当一个请求到来时,我们需要将它转发到任何一个服务器,为了实现这一点,我们配置一个负载均衡器来一个一个地向它们发送请求(或者您可以使用其他算法)。现在,无论何时收到请求,负载均衡器都会接收请求并将其转发到我们的任何一台服务器。你应该在你的两台机器上都有网络服务器(例如apache),因为这里负载平衡器的任务只是将请求定向到任何一台服务器。负载均衡器是一个服务器(通常)——位于一组应用程序服务器之前并管理它们之间的流量。传入的 Web 流量通过负载均衡器,该负载均衡器在 Web 和应用程序服务器之间分配流量。

为了更好地理解让我们理解这张图,

其中有两个负载平衡器,但它们在 active/passive 对中 - 也称为 HA(高可用性)对。让我们将它们视为一个(因为另一个是故障转移)。另一个只有在第一个失败时才会出现。在它们后面是两个 Web 服务器。这些可能是 Apache Web 服务器。负载均衡器具有应用程序服务器的内部 IP。负载均衡器接受传入的请求并将它们转发到准备好接受它们的服务器。负载均衡器仅执行接收初始请求并确保它得到 Web 服务器响应的功能。 Web 服务器是一个软件,它通过 information.The 应用程序服务器接收请求并处理它,然后 returns 通过作为中介的 Web 服务器进行回复。 应用程序服务器的示例可以是 Ruby/Rails、PHP、Django 等。这些服务器是应用程序执行“代码”的地方。缓存、正向和反向代理都是在 web 服务器层之前执行的,以减轻 web 服务器的压力。 HAProxy 是一种这样的技术,它是免费的开源软件,可为基于 TCP 和 HTTP 的应用程序提供高可用性负载平衡器和代理服务器,将请求分散到多个服务器。

现在进入 数据库 部分,您可以在任何一台机器上托管您的数据库(使用 MYSQL 服务器)并使两台服务器都连接到使用您的数据库地址的同一数据库。您可以拥有一个中央数据库,您的应用程序框架将从中读取数据。数据将由数据库服务器(例如 MYSQL)提供服务,并且所有并发性和可用性概念将由它处理,以防两个服务器同时请求访问相同数据。

另请注意,您的负载均衡器也会在任何一台机器上 运行 接收请求并将其转发到您的任何一台服务器。

当我们有多台服务器时,又出现了一个问题:

When your website is served by only one web server, for each client-server pair, a session object is created and remains in the memory of the web server. All the requests from the client go to this web server and update this session object. If some data needs to be stored in the session object over the period of interaction, it is stored in this session object and stays there as long as the session exists. If your website is served by multiple web servers which sit behind a load balancer, the load balancer decides which actual (physical) web-server should each request go to. For example, if there are 3 web servers A, B and C behind the load balancer, it is possible that www.mywebsite.com/index.jsp is served from server A, www.mywebsite.com/login.jsp is served from server B and www.mywebsite.com/accoutdetails.php are served from server C.

Now, if the requests are being served from (physically) 3 different servers, each server has created a session object for you and because these session objects sit on three independent boxes, there's no direct way of one knowing what is there in the session object of the other.

有多种方法可以为一个会话维护同一个服务器:

  • 通过存储会话 :

You have to store the sessions in a common storage where both your app servers can access, You can store the sessions in files ( mounted by NFS so accessible to both servers ) this is usually not recommended as it is slow. You can store the sessions in DB . Best method is to use a Redis/Memcached type of setup to store your sessions and retrieve them and they are fast and they can be shared between lot of nodes.

  • 使用粘性会话

If the load balancer is instructed to use sticky sessions, all of your interactions will happen with the same physical server, even though other servers are present. Thus, your session object will be the same throughout your entire interaction with this website. A router or load balancer with sticky-session support can assign a single server to a particular user, based on their HTTP session or IP address. The assigned server is remembered by the router for a certain amount of time, ensuring that all future requests for the same session are sent to the same server.

注意:如果有人发现答案中的任何信息可能与实际概念不符,请建议编辑或在评论中回复,这将有助于相互理解分布式系统概念。