r/node • • Feb 23 '24

Couldnt help feeling bad for pm2

Post image
340 Upvotes

107 comments sorted by

4

u/[deleted] Feb 23 '24

I use Docker and PM2, am I doing it wrong?

0

u/PrestigiousZombie531 Feb 23 '24

i dont know the answer to that but i ll ask you a question: why do you need a process manager running inside another process manager?

10

u/iwrestlecode Feb 23 '24

Docker is not a process manager. You will want to use dumb-init supervisord or similar around your node bin/index.js or run in to funny issues with signals.

3

u/VanDieDorp Feb 23 '24

A good example will be the gitlab omnibus docker image. Docker also have official documentation on the idea Run multiple processes in a container just swapping out pm2 for superviserd(python based).

2

u/[deleted] Feb 23 '24

That’s why I’m asking.

I’ve viewed as: Kubernetes manages my containers, Docker init’s the machine, PM2 turns on Node and restarts Node when needed.

Kubernetes rebuilds my containers when they crap out, but Docker is just the spec for my container, it’s not a process manager? But I may be super ignorant to this.

4

u/dawar_r Feb 23 '24

Wait and you have Kubernetes?! hahaha that's a lot of abstractive layers!

4

u/[deleted] Feb 23 '24

That’s why I’m trying to see if I’m doing it wrong.

Kubernetes manages the number of machines running and scales as my users scale up/down throughout the week.

Docker inits my machine, installs my Linux packages, C packages, PM2, etc.

PM2 starts node and keeps node up and running. Any issues and Kubernetes kills and restarts container all behind the scenes.

What’s a better way?

12

u/wilsonodk Feb 23 '24

I'm literally writing up a best practices guide on this very subject at $WORK today.

It's an anti-pattern to run a process manager inside of a container.

Using a process manager inside obfuscates the resource consumption of the application. It can allow for scaling inside, which will lead to poor visibility into resource consumption. Common examples of problems include overuse of database connections and improper release of unused connections, additional resources such as CPU and memory being consumed without observability systems knowing why the additional resources were needed.

If you're running in an environment like Kubernetes, this is doubly true. Kubernetes can manage the uptime of the container process, manage it's CPU/memory usage, autoscale on resource usage, and so much more. But, hiding the app behind a process manager makes all of this much more challenging, if not impossible.

Some real world examples of issues caused by using pm2 in a container with Node applications

  • MongoDB connections completely consumed because pm2 kept an app that was crashing up. The connections couldn't be released since the container wasn't crashing (pm2 was running fine).

  • AWS LBs returning 502 Bad Gateway errors caused by pm2 keeping the container alive while the app was not able to return responses

  • Datadog/New Relic dashboards showing pod consuming 4 vCPU each. pm2 was "autoscaling" the app inside the container, which meant that HPA scaled too slow for actual production traffic.

  • Kubernetes Logs not reporting issues with app container because pm2 kept them "alive", even though they should have been reporting issues (only discovered when additional logging was added)

I can go on and on and on.

3

u/ben_db Feb 23 '24

Can you post the guide when it's done? Very interested to read this, big gap in my knowledge.

4

u/wilsonodk Feb 23 '24

Unfortunately, I can’t because it’s very specific to our environment and workflows. 😞

1

u/Hehehelelele159 Feb 24 '24

So instead of running pm2, would you use nginx and multiple containers?

1

u/wilsonodk Feb 24 '24

Yes. But, really I'd use a container orchestration tier with a load balancing tier. I definitely want to be "bin packing" containers onto VMs.

Out-of-the-box, Node can only utilize one CPU, and most VMs are going to have multiple cores. So, if you're not running multiple instances of the app on the same machine, you'll be underutilizing the infrastructure.

3

u/[deleted] Feb 24 '24

Fantastic points! I’m working on removing pm2 now.

1

u/VCkc Feb 24 '24

Say if you are running a node based application that can only make use of 40% CPU at best before performance degradation. (Load test)

Tge requirement is to set up autoscaling at 70%. Otherwise, it can be construed as overprovisioned and sitting idle. Wouldn't using cluster mode in pm2 be the inly solution?

1

u/wilsonodk Feb 24 '24

I think you might be conflating two separate issues. One one hand you have the infrastructure, and you want higher that 70% CPU utilization before provisioning another instance. That make sense and is the correct thing to do.

On the other hand, you have an app that at 40% CPU needs to scale (it's awesome that you have that number, so many people I work with don't). These are separate concerns. These types of concerns is why container orchestration software exists. Things like Kubernetes, Docker Swarm, Openshift, etc.

You can go either cloud-based or self-hosted. But, regardless, what you want to do is "bin pack" the underlying infrastructure to maximize the CPU. In your use case, you could easily run three instances of the app on one VM (allotting 30% to each instance). That would keep you under the 40% peak and fully utilize the infrastructure that you've purchased. The 10% "wasted" CPU can be used to run the orchestration software and/or give you space to burst into before you autoscale the infrastructure (and then the app instances).

2

u/[deleted] Feb 24 '24

Yeah you’re doing it wrong, no need for PM2 inside container. Set each instance to 1 core and let kubernetes do the rest. It’s easier to imagine a running container as a single process and kubernetes as the process manager.

1

u/[deleted] Feb 24 '24

That’s why I was using pm2, to utilize all my cores. Can k8s manage multiple processes on 1 machine?

2

u/wilsonodk Feb 27 '24

Yes. Kubernetes is a container orchestration platform. It can manage all sorts for processes and infrastructure.

For example, you could have a micro-cluster with three VMs (called nodes in Kube-speak). You can then have a deployment (kube-speak for configuration for how to run an application) with 9 replicas (how many copies you need of the container that configured in the deployment). Kube will then run 3 replicas on each of the 3 nodes. And then route traffic to all of them. If one instance fails, it will immediately spin up another instance.

1

u/[deleted] Feb 27 '24

Awesome, I just deactivated pm2, and my systems are running smoothly still. With much less memory usage, etc. and better monitoring from kube.

3

u/dawar_r Feb 23 '24

In most cases I would imagine it would be counterproductive to have so many layers tbh. Plus they have contradicting philosophies of design. With Kubernetes for example the general idea is pods should be ephemeral and you should setup your environment to tolerate failing pods. In this paradigm you would want your containers to clunk out asap and be replaced by fresh pods, completely virtualized environment. PM2 is designed for running and restarting processes, typically uncontainerized processes. Not to mention they all have features that overlap which would make it very to complicated about which strategies you're using where (i.e. questions like should we be listening to Kubernetes events or Docker events or Pm2 events? What about errors at each of these levels? What about restarts?)

1

u/[deleted] Feb 23 '24

The question then is, how does Kube know my pod failed if node is the only process not running.

1

u/dawar_r Feb 23 '24

Kubernetes is concerned with containers and all containers have an init command. So if your containers init command is a node process (like a server) when it fails the container exits with an error code. Then Kubernetes will generally attempt to restart the pod, depending on the rest of your setup. 

1

u/ItalyPaleAle Feb 23 '24

Only way this could make sense IMHO is if you use PM2 to scale your app within the container. This way your app can run on multiple threads in parallel. But even there, there may be simpler ways to do that, such as the built-in cluster module.

3

u/wilsonodk Feb 23 '24

If you are running your container in Kube, you can use horizontal pod autoscaling (HPA). It's a much better experience than putting it in cluster or using pm2 because your telemetry/application data will be more accurate and easier to reason about how many resources you are consuming. It also make is easier to understand database connection use.

84

u/sombriks Feb 23 '24

not gonna lie, perform a docker compose up -d and go home is satisfying. even better, docker stack deploy -c docker-compose.yml and see the swarm do his thing.

14

u/Remarkable_Win7320 Feb 23 '24

What does this one do? Deploy onto some sort of a cluster?

31

u/argylekey Feb 23 '24

yes. You can use a docker-compose.yml file to spin up replications in a swarm.

Various VPSs can be setup to be added to a swarm, and deployments can be put behind a load balancer. In theory docker swarm manages several replicas of your applications, and puts them on whichever machine has the resources for it. i.e. you have 4 machines setup, and you want 3 replicas of some API, swarm will figure out which of the 4 machines to put stuff on.

It's like Kubernetes lite(or at least that is how I think about it).

11

u/Plasmatica Feb 23 '24

Even if you're not scaling over multiple machines Swarm can be useful for zero downtime deployments on a single machine.

6

u/Remarkable_Win7320 Feb 23 '24

So, swarm is also used in production?

5

u/argylekey Feb 23 '24

It can be.

2

u/Remarkable_Win7320 Feb 23 '24

Amazing! Thank you!

7

u/Far-Mathematician122 Feb 23 '24

How long does it take for a beginner that has no experience in docker to learn docker compose and swarm to make it production ready ?

14

u/argylekey Feb 23 '24

That is entirely personal.

For someone who is well versed in linux, and has setup cloud servers a lot? Maybe a weekend? Production ready(Certs/SSL/proper firewall rules/load balancer setups/etc) maybe 2-4 weeks depending on how fast you are?

For someone who is not used to deploying to cloud, longer.

In general my opinion, I'd say for my personal projects, swarm makes sense. I'm not sure if there are managed swarm options, generally its roll your own(AFAIK). For real world production/mission critical services pay for a managed Kubernetes cluster and learn Helm.

I'm OK at setting up VPSs, and I can tell you it isn't easy to get right. I mess it up a lot. I wish I was good at it. Working on it.

2

u/StaticCharacter Feb 23 '24

I've been exploring caprover recently and it looks like they have lots of options for deploying docker swarm mostly managed :) might be worth looking into

3

u/sombriks Feb 23 '24

you can go through the official guides, they are a very gentle introduction https://docs.docker.com/get-started/overview/

1

u/Redicus Feb 25 '24

I am learning backend to become full-stack. Can you help me point to a learning source for the things you mentioned here?

Docker-compose Replications in a swarm. VPS in a swarm. Deployments behind a load balancer.

2

u/argylekey Feb 27 '24

https://docs.docker.com/engine/swarm/stack-deploy/

Sorry, I've been thinking about your question a lot of what documentation I actually like for this stuff. I think these two pages are what I actually reference the most when I'm building something out.

1

u/Redicus Feb 28 '24

Hey, thanks a lot for showing care. I feel that this resource is better than the previous one.

1

u/argylekey Feb 26 '24

https://doc.traefik.io/traefik/v1.7/user-guide/swarm-mode/

That’s a good place to start. Learning Traefik is a whole thing in itself. There are lots of other options, but this is my preference.

4

u/BlazingThunder30 Feb 23 '24

If you're going that route, doing helm install and seeing Kubernetes go is even better! And I even have it automatically assigning DNS subdomains to services if I want

-5

u/serendipity7777 Feb 24 '24

Is docker more scalable than lambda functions?

9

u/pwmcintyre Feb 24 '24

I'm not sure this question makes sense

Docker is for orchestration

AWS Lambda is a FaaS

It's a bit like asking if apples have more zest than oranges

0

u/Hour-Ladder-8330 Feb 25 '24

Op's question makes perfect sense. They asked if docker swarm is better for horizontal (and vertical) scaling compared to aws lambda which does similar thing at infra level EXCEPT lambda instance gets shut down by AWS once finished and no further requests.

So, horizontal scaling part is same in lambda and docker swarm and its a legit question if you only care about horizontal scaling

70

u/maximusprime2328 Feb 23 '24 edited Feb 23 '24

These aren't really comparable.

Docker ensures that you have a consistent environment. Local environments on dev teams are very inconsistent. This person is running this version of x software on their computer. Another person is running this version. So you use Docker, more commonly just referred to as containers, upstream in testing, staging and production to make sure that the environments are the same. Locally you can use Docker to ensure that your environment is consistent with environments upstream. It gets rid of the "well it works on my computer" excuse.

PM2 is just a process manager.

-36

u/alex-weej Feb 23 '24

It doesn't though because there's no guarantee of reproducibility in the build. Nix does a much better job.

16

u/minicrit_ Feb 23 '24

what? the whole point of an image is you set up the same exact environment lol

3

u/VanDieDorp Feb 23 '24

well a lot of copy pasters do alpine:latest and even if you do a oraclelinux:8.9 if you build the image today, and build it in 6 months the user env would have been updated with patched binaries, etc.

So that is why you have image registries, to share builds that is exact.

1

u/The_Fresser Feb 24 '24

Yes if you build with different tags you get different images.

Your already built and tagged image does not change even if you use latest, only if you rebuild it and overwrite the tag.

Also don't use latest besides for testing/dev. Pin your versions dammit.

1

u/Hour-Ladder-8330 Feb 25 '24

Unless you have ECR or something similar internally where you host docker images by title with your configuration and pull the exact same image from ECR on all dev's machine. This will always work in future

-14

u/alex-weej Feb 23 '24

OK, but you described Docker as a whole, not OCI images. Much of Docker usage is grabbing a Dockerfile or Docker Compose config and "building it" on the fly with no reproducibility.

10

u/oze4 Feb 23 '24

Have you ever experienced the "it doesn't work on my machine" problem with Docker? I'm not being sarcastic, I'm being genuine, because from what I have seen Docker has eliminated this.

The point is I'm not building/testing on my local machine, but to a standardized environment/container so anything local to my machine doesn't interfere.

2

u/Ruben_NL Feb 23 '24

I can give you one example, but it's very specific, and not many people will have that issue:

DIfferent processor architectures. A image build for x86/64 doesn't run on ARM64 (and if it runs, it's very slow because emulation).

0

u/Internal_Pride1853 Feb 23 '24

I did. It was related to running .sh files and on Macs and Ubuntu it was running well but it didn't work on Windows. Turned out to be an issue with line endings and changing git auto clrf config and redownloading the repo fixed the issue. The fix was not needed on Ubuntu / Macs :)

5

u/oze4 Feb 23 '24

Did those bash scripts run inside the container? Or were they used in the process to create the Docker container?

This is a good one, though. Thanks for sharing.

1

u/Internal_Pride1853 Mar 02 '24

Oh, I don’t remember now as it was quite a long time ago. But I think they were run inside the container but copied from Windows. I just Googled „docker sh not found” and the first stackoverflow result has a similar issue described and explanation why that does that happen.

I think I'll remember this for so long because it was my first issue to solve in a project and no one knew what's happening because the whole other team worked on Macs and I was a fresh hire 😂😂

And each new hire with Windows had the same issue which I was helping to resolve until we updated the dockerfile

-5

u/alex-weej Feb 23 '24

"With Docker?" Yes - people do apt-get install in their Dockerfiles a lot.

6

u/oze4 Feb 23 '24

Care to elaborate? I'm not sure I follow - are you saying because some people have Windows, apt wont work? (or any other OS that doesn't have apt).

Won't the apt-get command be running inside of the container?

Again, I'm being genuine here, not trying to argue or what not.

1

u/[deleted] Feb 24 '24

apt-get will install the latest binary in the repository, like what npm install does , therefore you cannot guarantee a reproducible build.

1

u/oze4 Feb 24 '24

But you can pin dependencies to specific versions, at least with NPM. Can you not do that with apt-get?

2

u/dcabines Feb 23 '24

I’d compare Nix to Ansible long before I’d compare it to Docker.

1

u/alex-weej Feb 23 '24

As far as I know, solutions like Ansible are inherently non-reproducible and are more about scaffolding infrastructure than applications. Maybe something changed in the last few years though?

This is a good, recent video that might convince you of my point (if you like that sort of thing): https://youtu.be/0ulldVwZiKA

41

u/ben_db Feb 23 '24

I use pm2 in docker, am I going to hell?

8

u/fabiotrezzi Feb 23 '24

Me too. I use docker to create and environment identical in development and production and I use pm2 to handle multiple instances. Pm2 is also very useful because it can check the memory usage and restart the process after a threshold to prevent memory leaks to kill the server

6

u/backdoorsmasher Feb 23 '24

I do as well. For me it's the clustering

1

u/wilsonodk Feb 23 '24

Yes. Yes you are.

1

u/shake10861 Feb 25 '24

Nah, but I do have questions…

29

u/[deleted] Feb 23 '24

[removed] — view removed comment

14

u/bytepursuits Feb 23 '24 edited Feb 23 '24

Just, why

because you don't typically have single core CPU. If you want better performance you would want multiprocessing and you need to create as many node.js processes as there are vCPUs (well math depends on the application really) - but this is the idea.

yes it depends on where you host your application. With ecs or kubernetes you probably want to scale using ECS/kubernetes features, but if you are on large EC2 - then pm2 is still useful

2

u/[deleted] Feb 23 '24

[removed] — view removed comment

4

u/iwrestlecode Feb 23 '24

You would not use docker but containers/images which previously typically were built by docker.

5

u/bytepursuits Feb 23 '24 edited Feb 23 '24

Ok but in that last case, why would I use docker and not only pm2 alone?

You may need to work on multiple versions of node.js (we juggle 14-21) or multiple versions of other languages like PHP or python on Go.
Typically these days you ALWAYS containerize for production, this may or may not need PM2, but containers are pretty much the golden standard.

4

u/ben_db Feb 23 '24

The host I run on for some reason allocates two processors to docker, so I run it with -i max and it runs two instances

17

u/yossarianyells Feb 23 '24

I am using pm2 inside Docker container to use all the cores on the machine for my NextJS app. But now I realised I can use Nginx and multiple docker container (one per core) and say bye to pm2 :(

Why did I not read this thread before spending hours working around pm2-runtime messing up path for the source code

3

u/Tiis__ Feb 23 '24

Isn't Docker containers using all available cores if not specified?

6

u/yossarianyells Feb 23 '24

Yeah but running processes parallelly will have to be configured. In case of Nodejs based applications it can either be done via using a single container and use pm2-runtime within to manage parallel processes. Or run multiple docker containers by passing 'scale' flag to run your docker service and use Nginx as a load balancer

1

u/Hehehelelele159 Feb 24 '24

What would be the benefit of pm2 in one container vs nginx and multiple

1

u/Redicus Feb 25 '24

So currently your NextJS webapp uses all cores with PM2?

Now if you use Nginx with 1 container per core, wont your NextJS webapp/container only run on one core?

1

u/ZachVorhies Feb 26 '24

Right because it make more sense to spin up an virtual machine than launch a process.

😆

7

u/stevefuzz Feb 23 '24

I run pm2 in docker. Is this like a threesome?

2

u/az1d0 Feb 23 '24

put that inside a Vagrant and you will make a combo 🥹

9

u/zante2033 Feb 23 '24

Also using docker and pm2 : p

It's all just one giant orgy.

2

u/VanDieDorp Feb 23 '24

this guy omnibus package,

5

u/backdoorsmasher Feb 23 '24

The last thread seemed to be very lite on people talking about clustering - that is one of the major advantages of PM2. Keen to hear how people are clustering with just docker

2

u/wilsonodk Feb 27 '24

My container orchestration software handles the "clustering". My personal preference is to use Kubernetes, but YMMV. Would much rather run multiple instance of the container as it gives better

  • insight into how an individual container runs (it's much easier to reason about a single container being a single entity versus having to divide by the number of instances inside the container),

  • it's easier to run across network zones (ie, AWS AZs),

  • gives better uptime/availability (when a container fails instead of losing multiple instances, only lose one)

  • letting software that dedicated to orchestration worry about orchestration versus having developers try to figure it out

  • much easier to "bin pack" the infrastructure (can autoscale the underlying VMs when needed and autoscale the container instances to get the most out of the VMs)

Those are just a few of the reasons. There are also error scenarios that a much easier to recover from that I list in another post.

1

u/backdoorsmasher Feb 27 '24

Great inputs, thanks for responding, I'm looking into it now

4

u/Nice_Aioli_9991 Feb 23 '24 edited Feb 23 '24

Why? Does the docker can automatically start application when it crash?

21

u/Chaphasilor Feb 23 '24

Sure. Pass --restart-unless-stopped, I believe

3

u/Senior-Minute-9364 Feb 25 '24

If I’m not mistaken it restarts with exit code 0 too - I would use --restart on-failure to ensure it only restarts with exit code 1 and successfully stops with 0

4

u/[deleted] Feb 24 '24

I use pm2 when I need to persist a simple node process. I use docker when I want to containerize an application. Are you mouth breathers reaching for docker when you need simple persistence? Tf?

2

u/eMperror_ Feb 23 '24

I never used pm2, how do you use it? I just do everything in docker

2

u/polish_jerry Feb 23 '24

If I have to run just a node js script with no scaling then I'm not going to use docker.

1

u/dawar_r Feb 23 '24

I felt this for PM2 -> Docker and then again from Docker -> Kubernetes XD

1

u/Hour-Ladder-8330 Feb 25 '24 edited Feb 25 '24

Wait till there is kubernetes-> something coming soon to manage working with kubernetes easy

1

u/dawar_r Feb 25 '24

A Kubernetes AI? Imagine managed kubernetes but the best possible admin for free

1

u/Shehzman Feb 27 '24

Started off using PM2 for a simple node app I built. Switched it over to a docker container and I’m so glad I did. It is significantly easier to deploy and manage.

1

u/Leo-rick Feb 27 '24

I am confused because these are two separate things

1

u/AjexOr Feb 23 '24

Can i get an explanation please?

1

u/mnaa1 Feb 23 '24

Never liked pm2, added overhead complexity to a simple node application. I just went for a 1vCPU and 2GB memory per instance and never looked back.

-1

u/Illustrious_Pie_8197 Feb 23 '24

I don't know why pm2 is forced upon developers in private insurance sector and black list docker.

-2

u/OZLperez11 Feb 23 '24

laughs in Go embedded binary