Deployment System部署系统
PM2 features a simple but powerful deployment system that allows to provision and update applications in production environment. PM2具有一个简单但功能强大的部署系统,允许在生产环境中调配和更新应用程序。This is great when you want to deploy applications on baremetal server in one or many servers at once.当您希望同时在一台或多台服务器上的裸机服务器上部署应用程序时,这非常有用。
> pm2 deploy <configuration_file> <environment> <command>
Commands:
setup run remote setup commands
update update deploy to the latest release
revert [n] revert to [n]th last deployment or 1
curr[ent] output current release commit
prev[ious] output previous release commit
exec|run <cmd> execute the given <cmd>
list list previous deploy commits
[ref] deploy to [ref], the "ref" setting, or latest tag
Deployment Configuration部署配置
To configure the deployment system, add a 要配置部署系统,请向应用程序配置文件添加deploy
attribute to the Application Configuration File:deploy
属性:
module.exports = {
apps : [{
script: 'api.js',
}, {
script: 'worker.js'
}],
// Deployment Configuration
deploy : {
production : {
"user" : "ubuntu",
"host" : ["192.168.0.13", "192.168.0.14", "192.168.0.15"],
"ref" : "origin/master",
"repo" : "git@github.com:Username/repository.git",
"path" : "/var/www/my-repository",
"post-deploy" : "npm install"
}
}
};
Note: make sure the application configuration file in the local folder is named either ecosystem.config.js or pm2.config.js, so you don’t need to type the configuration filename for each command.注意:确保本地文件夹中的应用程序配置文件名为ecosystem.config.js
或pm2.config.js
,因此不需要为每个命令键入配置文件名。
Provision remote server配置远程服务器
Before provisioning remote server verify that:在设置远程服务器之前,请验证:
Remote servers have PM2 installed远程服务器已安装PM2Remote servers have granted permissions to GIT clone the target repository远程服务器已授予GIT克隆目标存储库的权限
Once remote servers have been configured you can start provisioning them:配置远程服务器后,您可以开始配置它们:
$ pm2 deploy production setup
Note: as the app configuration file is named ecosystem.config.js or pm2.config.js in the local folder, you do not need to specify the filename each time注意:由于应用程序配置文件在本地文件夹中名为ecosystem.config.js
或pm2.config.js
,因此不需要每次都指定文件名
Deploy application部署应用程序
Once the remote server have been provisioned you can now deploy the application:设置远程服务器后,现在可以部署应用程序:
$ pm2 deploy production
Note: if git report an error that there are local changes but still want to push what is on the remote GIT, you can use the 注意:如果git报告存在本地更改但仍希望推送远程git上的内容的错误,则可以使用--force
option to force deployment.--force
选项强制部署。
Rollback to previous deployment回滚到以前的部署
If you need to rollback to previous deployment you can use the 如果需要回滚到以前的部署,可以使用revert
option:revert
选项:
# Revert to -1 deployment
$ pm2 deploy production revert 1
Execute a command on each server在每台服务器上执行一个命令
To execute a one-time running command you can use the 要执行一次性运行的命令,可以使用exec
option:exec
选项:
$ pm2 deploy production exec "pm2 reload all"
Specifics详情
Deployment Lifecyle部署生命周期
When deploying with PM2, you can specify what do before/after setup and before/after update:使用PM2部署时,您可以指定安装之前/之后以及更新之前/之后要执行的操作:
"pre-setup" : "echo 'commands or local script path to be run on the host before the setup process starts'",
"post-setup": "echo 'commands or a script path to be run on the host after cloning the repo'",
"pre-deploy" : "pm2 startOrRestart ecosystem.json --env production",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production",
"pre-deploy-local" : "echo 'This is a local executed command'"
Multi host deployment多主机部署
To deploy to multiple hosts in the same time, you just have to declare each host in an array under the attribute 要同时部署到多个主机,只需在属性host
.host
下声明阵列中的每个主机。
"host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],
Specifying SSH keys指定SSH密钥
You just have to add the “key” attribute with path to the public key, see below example :您只需将带有路径的“key”属性添加到公钥,请参阅下面的示例:
"production" : {
"key" : "/path/to/some.pem", // path to the public key to authenticate
"user" : "node", // user used to authenticate
"host" : "212.83.163.1", // where to connect
"ref" : "origin/master",
"repo" : "git@github.com:repo.git",
"path" : "/var/www/production",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production"
},
Troubleshooting故障排除
SSH clone errorsSSH克隆错误
In most cases, these errors will be caused by 在大多数情况下,这些错误将由pm2
not having the correct keys to clone your repository. pm2
没有克隆存储库的正确密钥引起。You need to verify at every step that the keys are available.您需要在每一步都验证密钥是否可用。
Step 1 If you are certain your keys are correctly working, first try running 如果您确定密钥正常工作,请首先尝试在目标服务器上运行git clone your_repo.git
on the target server. git clone your_repo.git
。If it succeeds, move onto the next steps. If it failed, make sure your keys are stored both on the server and on your git account.如果成功,请继续下一步。如果失败,请确保您的密钥存储在服务器和git帐户上。
Step 2 By default 默认情况下,ssh-copy-id
copies the default identiy, usually named id_rsa
. ssh-copy-id
复制默认标识,通常命名为id_rsa
。If that is not the appropriate key:如果这不是合适的键:
ssh-copy-id -i path/to/my/key your_username@server.com
This adds your public key to the 这会将公钥添加到~/.ssh/authorized_keys
file.~/.ssh/authorized_keys
文件中。
Step 3 If you get the following error:如果出现以下错误:
--> Deploying to production environment
--> on host mysite.com
○ hook pre-setup
○ running setup
○ cloning git@github.com:user/repo.git
Cloning into '/var/www/app/source'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and that the repository exists.
**Failed to clone**
Deploy failed
…you may want to create a ssh config file. ……您可能需要创建一个ssh配置文件。This is a sure way to ensure that the correct ssh keys are used for any given repository you’re trying to clone. 这是确保您尝试克隆的任何给定存储库使用正确ssh密钥的可靠方法。See this example:请参见此示例:
# ~/.ssh/config
Host alias
HostName myserver.com
User username
IdentityFile ~/.ssh/mykey
# Usage: `ssh alias`
# Alternative: `ssh -i ~/.ssh/mykey username@myserver.com`
Host deployment
HostName github.com
User username
IdentityFile ~/.ssh/github_rsa
# Usage:
# git@deployment:username/anyrepo.git
# This is for cloning any repo that uses that IdentityFile. This is a good way to make sure that your remote cloning commands use the appropriate key