PM2 is an extremely powerful tool. No matter what stage of development we are in, PM2 always has something to offer.
In this phase of software development, the deployment is where PM2 really shines. Through a JSON configuration file, PM2 will manage a cluster of applications so that we can easily deploy, redeploy, and manage applications on remote servers.
PM2 calls a group of applications ecosystem. Every ecosystem is described by a JSON file, and the easiest way to generate it is executing the following command:
pm2 ecosystem
This should output something similar to the following code:
[PM2] Spawning PM2 daemon [PM2] PM2 Successfully daemonized File /path/to/your/app/ecosystem.json generated
The content of the ecosystem.json
file varies, depending on the version of PM2, but what this file contains is the skeleton of a PM2 cluster:
{ apps : [ { name : "My Application", script : "app.js" }, { name : "Test Web Server", script : "proxy-server.js" } ], */ deploy : { production : { user : "admin", host : "10.0.0.1", ref : "remotes/origin/master", repo : "[email protected]:the-repository.git", path : "/apps/repository", "post-deploy" : "pm2 startOrRestart ecosystem.json --env production" }, dev : { user : "devadmin", host : "10.0.0.1", ref : "remotes/origin/master", repo : "[email protected]:the-repository.git", path : "/home/david/development/test-app/", "post-deploy" : "pm2 startOrRestart ecosystem.json --env dev", } } }
This file contains two applications configured for two environments. We are going to modify this skeleton to adapt it to our needs, modeling our entire ecosystem written in Chapter 4, Writing Your First Microservice in Node.js.
However, for now, let's explain a bit for the configuration:
apps
) defining two apps: API and WEBname
: This is the name of the applicationscript
: This is the startup script of the appenv
: These are the environment variables to be injected into the system by PM2env_<environment>
: This is same as env
, but it is tailored per environmentdeploy
key, as follows:production
dev
As you can see, between these two environments, there are no significant changes except for the fact that we are configuring one environment variable in development and the folder where we deploy our application.
In Chapter 4, Writing Your First Microservice in Node.js, we wrote a simple e-commerce in order to show the different concepts and common catches in microservices.
Now, we are going to learn how to deploy them using PM2.
First thing we need to do in order to deploy software with PM2 is to configure the remote machine and local machine to be able to talk using SSH, with a public/private key schema.
The way of doing it is easy, as follows:
Let's do it:
ssh-keygen -t rsa
That should produce something similar to the following output:
Generating public/private rsa key pair. Enter file in which to save the key (/Users/youruser/.ssh/id_rsa): /Users/youruser/.ssh/pm2_rsa Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in pm2_rsa. Your public key has been saved in pm2_rsa.pub. The key fingerprint is: eb:bc:24:fe:23:b2:6e:2d:58:e4:5f:ab:7b:b7:ee:38 [email protected] The key's randomart image is: +--[ RSA 2048]----+ | | | | | | | . | | o S | | o .. | | o o..o. | | . +.+=E.. | | oo++**B+. | +-----------------+
Now, if we go to the folder indicated in the preceding output, we can find the following two files:
pm2_rsa
: The first one, pm2_rsa
, is your private key. As you can read from the name, no one should have access to this key as they may steal your identity in the servers that trust this key.pm2_rsa.pub
: The pm2_rsa.pub
is your public key. This key can be handed over to anyone so that using asymmetric cryptography techniques, they can verify your identity (or who you say you are).What we are going to do now is copy the public key to the remote server so that when our local machine PM2 tries to talk to the server, it knows who we are and let's get into the shell without password:
cat pm2_rsa.pub | ssh youruser@yourremoteserver 'cat >> .ssh/authorized_keys'
The last step is to register your private key as a known identity in your local machine:
ssh-add pm2_rsa
That's about it.
From now on, whenever you SSH into the remote server using as a user youruser
, you won't need to enter the password in order to get into the shell.
Once this configuration is done, there is very little to do in order to deploy any application into this server:
pm2 deploy ecosystem.json production setup pm2 deploy ecosystem.json production
The first command will configure everything needed to accommodate the app. The second command will actually deploy the application itself as we configured earlier.