托管 Django Docker Postgres 在 DigitalOcean Droplets

huangapple go评论72阅读模式
英文:

Hosting Django Docker Postgres on DigitalOcean Droplets

问题

我是新手在部署到DigitalOcean方面。我最近创建了一个Droplet,在其中我将放置我的Django API来提供我的项目。我看到我租用的Droplet有25GB的内存,对于我的项目的实际大小来说,它可能适合作为数据库。我使用Docker Compose运行我的整个代码,它处理Django和PostgreSQL。我在想是否将所有数据存储在由Docker Image创建的PostgreSQL中,并存储在Droplet的本地内存中是否是个好主意,还是我需要在Digital Ocean中设置一个外部数据库?

从技术上讲,我认为这可能可行,但我不知道这样做是否安全,当尺寸扩大时是否能够扩展Droplet,或者是否能够在Digital Ocean上无太多问题地迁移所有数据到外部数据库,例如托管的PostgreSQL。

如果我将所有数据保存在本地,那么冗余性如何?或者我是否可以以某种方式每天克隆我的数据库?

如果有人了解这方面的信息并能帮助我,请告诉我,我不知道从哪里开始。

英文:

I am new to deploying to DigitalOcean. I recently created a Droplet, into which I will put my Django API to serve my project. I saw that the droplet I rent has 25 GB of memory, and for the actual size of my project, it could be a good fit for the database. I run my entire code with Docker Compose, which handles Django and PostgreSQL. I was wondering if it is a good idea to store all the data in the PostgreSQL created by Docker Image, and stored in the local memory of the droplet, or do I need to set up an external Database in Digital Ocean?

Technically I think that could work, but I don't know if it is safe to do, if I will be able to scale the droplets once the size scales, or if I will be able to migrate all the data into an external database, on digital ocean, a managed PostgreSQL all my data without too many problems.

And if I save it all locally, what about redundancy? Or can I clone my database on a daily basis in some way?

If someone knows about that and can help me, please I don't know where to start

答案1

得分: 2

非常好的问题!

所以你的方法是完全正确的,尽管我不会在生产环境中使用docker compose。原因是你肯定需要多次重建和重新部署应用容器,最好将其与数据库容器解耦。

为了做到这一点,只需在服务器上运行类似于以下内容的命令:

docker run -d \
    --name my_db \
    -p 5432:5432 \
    -e POSTGRES_PASSWORD='mypassword' \
    -e POSTGRES_USER="postgres" \
    -v /var/run/postgresql:/var/run/postgresql \
    --network=mynetwork \ 
    --restart=always \
    postgres

> 确保定义一个自定义网络,并在其中注册您的数据库容器以及您的Web容器和任何其他独立的服务(例如Redis或Celery)

Digital Ocean数据库的优势在于它们是完全托管的。这意味着您可以使用它们的基础设施来处理备份,并且它将在一个单独的服务器上运行,您可以根据需要进行扩展和缩减。

如果我是你,我会从在服务器上创建一个PostgreSQL容器开始,确保定义了一个volume,这样即使服务器实例崩溃,数据也将存储在文件中,当您重新启动服务器和数据库时,数据将等待您。

然后,随着您的需求发展,您可以将数据迁移到更可靠和灵活的系统上。这可以是Digital Ocean数据库,AWS RDS或其他许多选项。数据库迁移可能有点吓人,但现在有很多工具可以帮助您完成这项工作。

英文:

Very good question!

So your approach is perfectly fine, even though I wouldn't use docker compose for your production environment. The reason being that you will definitely need to rebuild and redeploy your application container multiple times and it's better to decouple it from your database container.

In order to do that, just run something similar to this in your server:

docker run -d \
    --name my_db \
    -p 5432:5432 \
    -e POSTGRES_PASSWORD='mypassword' \
    -e POSTGRES_USER="postgres" \
    -v /var/run/postgresql:/var/run/postgresql \
    --network=mynetwork \ 
    --restart=always \
    postgres

> Make sure to define a custom network and to register in it your db
> container as well as your web container and any other standalone
> service that you need (for example Redis or Celery)

The advantage of the Digital Ocean databases is that they are fully managed. Meaning that you can use their infrastructure to handle backups, and that it would run on a separate server that you can scale up and down based on need.

If I were you I would start with a PostgreSQL container in your server, making sure to define a volume so that even if your server instance crashes the data will be stored on file and will be there waiting for you when you restart the server and db.

Then as your needs evolve you can migrate your data on a more reliable and flexible system. This can be a Digital Ocean database or AWS RDS or a number of others. DB migrations can be a bit scary but nowadays there are plenty of tools to help you with those too.

huangapple
  • 本文由 发表于 2023年1月9日 18:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055944.html
匿名

发表评论

匿名网友

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

确定