Files
nodebestpractices/sections/docker/restart-and-replicate-processes.md
Daniele Fedeli 898f701e6f Update restart-and-replicate-processes.md
Fixed a typo in line 40, from indes.js -> index.js
2021-11-11 13:07:53 +01:00

44 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Let the Docker orchestrator restart and replicate processes
<br/><br/>
### One Paragraph Explainer
Docker runtime orchestrators like Kubernetes are really good at making containers health and placement decisions: They will take care to maximize the number of containers, balance them across zones, and take into account many cluster factors while making these decisions. Goes without words, they identify failing processes (i.e., containers) and restart them in the right place. Despite that some may be tempted to use custom code or tools to replicate the Node process for CPU utilization or restart the process upon failure (e.g., Cluster module, PM2). These local tools don't have the perspective and the data that is available on the cluster level. For example, when the instances resources can host 3 containers and given 2 regions or zones, Kubernetes will take care to spread the containers across zones. This way, in case of a zonal or regional failure, the app will stay alive. On the contrary side when using local tools for restarting the process the Docker orchestrator is not aware of the errors and can not make thoughtful decisions like relocating the container to a new instance or zone.
<br/><br/>
### Code Example Invoking Node.js directly without intermediate tools
<details>
<summary><strong>Dockerfile</strong></summary>
```dockerfile
FROM node:12-slim
# The build logic comes here
CMD ["node", "index.js"]
```
</details>
<br/><br/>
### Code Example Anti Pattern Using a process manager
<details>
<summary><strong>Dockerfile</strong></summary>
```dockerfile
FROM node:12-slim
# The build logic comes here
CMD ["pm2-runtime", "index.js"]
```
</details>