<p>What is the best approach and what are the benefits of using sophisticated solutions (Kubernetes, Docker Swarm) for the deployment of a few microservices developed using Spring Boot, Angular and Django projects backed by PostgreSQL database?</p>

<p>Consider that I have the following microservices:</p>

<ul>
<li>microservice 1 and 2 (developed in Spring Boot), backed by PostgreSQL</li>
<li>microservice 3 (developed in Django), backed by PostgreSQL</li>
<li>microservice 4 (front-end developed in Angular) </li>
</ul>

<p>Each microservice contains one Dockerfile to build it up.</p>

<p>What is the best approach for production-based deployments of such platform?</p>

<ol>
<li>I could deploy it using docker-compose quite easy but what is the real advantage of using sophisticated solutions as Kubernetes or Docker Swarm in the scenario like this?</li>
<li>How many containers of PostgreSQL database should I create? One PostgreSQL container per microservice or shared one for all microservices using PostgreSQL?</li>
</ol>

<p>Like most of these kind of questions regarding deployment "it depends" but let me try to answer.
Lest first see the differences:</p>

<p><strong>Docker compose vs (Docker swarm or Kubernetes)</strong>  </p>

<p>The most important difference which you need to acknowledge is that docker compose can run your multi container application on a <strong>Single Host only</strong>. It can not run your application on cluster of computers. If you have to run your application on a cluster you can remove the option to use docker compose for that. For this you will need to use either Docker swarm or Kubernetes.
You can read more about it <a href="https://stackoverflow.com/questions/47536536/whats-the-difference-between-docker-compose-and-kubernetes?rq=1">here</a> and <a href="https://stackoverflow.com/questions/43875117/what-benefits-does-docker-compose-have-over-docker-swarm-and-docker-stack?rq=1">here</a>.</p>

<p><strong>Docker swarm vs Kubernetes</strong></p>

<p>In short they are both container orchestration solutions. This means that you would use them to orchestrate your containers in a cluster. Both of them tend to be solving the same problem but in a different way. 
Both of them are used for:</p>

<ul>
<li>container orchestration across the cluster</li>
<li>scaling containers</li>
<li>load balancing containers</li>
<li>communication between containers</li>
<li>and many more. In <a href="https://stackoverflow.com/questions/40844726/how-is-docker-swarm-different-than-kubernetes">This</a> answer you can see the summary of the comparison of them.</li>
</ul>

<p>One of the benefits if you decide to use docker swarm is that the CLI tooling used for docker swarm will very similar to the standard Docker cli with some additional options. So setting it up and using can be much easier if you are familiar with docker command line.
Also you can use your docker compose yaml file to run on swarm directly. You can read about it <a href="https://docs.docker.com/compose/production/" rel="nofollow noreferrer">here</a> and <a href="https://docs.docker.com/compose/swarm/" rel="nofollow noreferrer">here</a>.</p>

<p>Kubernetes on the other hand is very powerful and is supported by all major cloud providers like: AWS, Azure and Google Cloud. It is: </p>

<ul>
<li>open source</li>
<li>backed by the Cloud Native Computing Foundation (CNCF)</li>
<li>has a big community supporting it</li>
<li>can be used with other container solutions(not only docker)</li>
</ul>

<p>If you would use use Kubernetes keep in mind that you will need to learn a separate tools for managing it including kubectl CLI.</p>

<p>There are pros and cons for using both but my suggestion would be to use Kubernetes if you are deploying your solution on the cloud and aim to be cloud agnostic as much as possible.</p>

<hr>

<blockquote>
  <p>What is the best approach for production-based deployments of such
  platform?</p>
</blockquote>

<p>Knowing only the infos that you gave us here I would say it depends also where you plan to deploy it:</p>

<ul>
<li>On some cloud provider like AWS, Azure, Google Cloud or some other?</li>
<li>On non cloud provider but still multiple servers or on some cluster of servers?</li>
<li>On one dedicated server for all of your components?</li>
</ul>

<p><strong>On some cloud provider like AWS, Azure, Google Cloud or some other?</strong></p>

<p>If you are using some cloud provider and you plan to deploy it there then I would suggest using Kubernetes as the major cloud providers have a very good support for it and you can port it to another cloud provider if needed.
I would also suggest taking a look at some specific solutions for Container orchestration for the specific cloud provider you are using. I was using AWS ECS for deployment and I had very good experience with it. Keep in mind that this way you will be bound more to that specific cloud provider compared to if you are using Kubernetes or Docker swarm.</p>

<p><strong>On non cloud provider but still multiple servers or on some cluster of servers?</strong></p>

<p>Here again your need to use Kubernetes or Docker swarm in order to deploy your solution to multiple servers. </p>

<p><strong>On one dedicated server for all of your components?</strong></p>

<p>By deploying on one server technically you could also use docker-compose. The same one that you use for your development setup with some different configurations. This will of course limit you if you at some point decide to deploy on a cluster as you will need to migrate to docker swarm or Kubernetes later.</p>

<blockquote>
  <p>I could deploy it using docker-compose quite easy but what is the real
  advantage of using sophisticated solutions as Kubernetes or Docker
  Swarm in the scenario like this?</p>
</blockquote>

<p>I think that in the above section I explained what would be the benefits and drawbacks of using docker compose for production deployment. Usually how I personaly do it is that I have 3 environments:</p>

<ol>
<li>Development environment setup with docker-compose</li>
<li>Production environment setup with Kubernetes and deployed to a cluster on AWS</li>
<li>Staging environment(Copy of Production) setup with Kubernetes and deployed to a cluster on AWS but using less servers(resources)</li>
</ol>

<p>So for the Staging I use the same Kubernetes configuration but with less servers(It is expensive :)). This way you ensure that your setup is working as you use the same configuration but with less resources as your staging will not need so much resources as Production which is used by your customers.</p>

<blockquote>
  <p>How many containers of PostgreSQL database should I create? One
  PostgreSQL container per microservice or shared one for all
  microservices using PostgreSQL?</p>
</blockquote>

<p>I would not recommend to use containers for databases on production.
Why?
There are many reasons why not. Simply there are too many risks and a a few people have made a mistake by doing it. Sure it can work for very small application but still I would advise not to use docker for Postgres on production.
You can read more about it <a href="https://vsupalov.com/database-in-docker/" rel="nofollow noreferrer">here</a>. Here is one quote from the post:</p>

<blockquote>
  <p>Here’s a pretty bad anti-pattern, which can cause you a lot of
  trouble, even if you’re just working on a small project. You should
  not run stateful applications in orchestration tools which are built
  for stateless apps.</p>
</blockquote>

<p>If you are using a cloud provider I would suggest to use the build in Services for databases like Database as a Service or on AWS for example RDS. <a href="https://aws.amazon.com/rds/postgresql/" rel="nofollow noreferrer">Here</a> you can read about it. For Azure this would be Aurora and other cloud providers have similar solutions.
On the other hand for your development setup it is fine to use your databases inside your docker containers.</p>

<hr>

<p><strong>Conclusion</strong></p>

<p>Depending on your preference and needs use Kubernetes or Docker swarm for all your services except of the databases. Since you are developing a solution using micro-services using kubernetes will allow you to be cloud agnostic. This means that you will be able to have your infrastructure setup portable so you can move it easily to another cloud provider. Of course if that is important to you? You can even deploy it on standard hosting providers which is not a cloud provider on one or multiple servers.</p>
