Docker 1.12:多个副本,单个数据库 [英] Docker 1.12: Multiple replicas, single database
问题描述
我们的应用程序需要运行一些初始数据库脚本才能启动它。
我们没有将数据库打包在我们的dockerized应用程序中,以便它可以遵循无状态的微服务架构,多个容器最终会与单个(当时)数据库实例进行通信。
在创建服务时,我们不能将 create service
命令中的 - replicas
命令用作多个实例将尝试在单个数据库上创建表并失败。虽然我们的脚本会检查数据库是否已被设置并跳过创建,但是由于所有容器同时启动,因此无法使用。
我们找不到任何等待类型的机制,我们可以利用码头工具为此问题。如果我们只能在第一个容器创建数据库(并暴露了端口)时才能启动第二个容器,那么我们可以如何配置容器间通信?
或者,像 flywaydb 这样的工具可以在某种程度上帮助?
应该如何使用生产?
从 Flyway FAQ :
多个节点可以并行迁移?
是的! Flyway使用数据库的锁定技术来协调多个节点。这样可以确保即使多个实例的应用程序尝试同时迁移数据库,仍然可以正常工作。集群配置得到完全支持。
With the introduction of the new 'swarm mode' with Docker 1.12, we've been trying to migrate our application on containers and make use of the swarm mode's orchestration & clusters.
Our application requires some initial database scripts to be run for it to start. We're not packaging the database inside our dockerized application so that it could follow a stateless microservice architecture and multiple containers would eventually talk to a single (at the moment) database instance.
While creating the service, we cannot use --replicas
with the create service
command as multiple instances would try and create tables on a single database and fail. Although our scripts would check if the database has been set-up and skip the creation but since all containers start simultaneously, it could not be used.
We couldn't find any wait-for kind of mechanism that we could leverage with dockers for this issue. It would have been good if we could only start the second container when the first one had created the database (and exposed the ports) but how can we configure inter-container communication for this?
Alternatively, can tools like flywaydb help in some way?
How should this be used in production?
From the Flyway FAQ:
Can multiple nodes migrate in parallel? Yes! Flyway uses the locking technology of your database to coordinate multiple nodes. This ensures that even if even multiple instances of your application attempt to migrate the database at the same time, it still works. Cluster configurations are fully supported.
这篇关于Docker 1.12:多个副本,单个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!