デプロイメントシステム
PM2は、本番環境でアプリケーションのプロビジョニングと更新を可能にする、シンプルながらも強力なデプロイメントシステムを備えています。これは、ベアメタルサーバーに、1台または複数のサーバーに一度にアプリケーションをデプロイする場合に最適です。
> 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
デプロイメント設定
デプロイメントシステムを設定するには、アプリケーション設定ファイルに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"
}
}
};
注記: ローカルフォルダ内のアプリケーション設定ファイルの名前は、ecosystem.config.jsまたはpm2.config.jsのいずれかにしてください。そうすることで、各コマンドで設定ファイル名を入力する必要がなくなります。
リモートサーバーのプロビジョニング
リモートサーバーのプロビジョニングを開始する前に、以下のことを確認してください。
- リモートサーバーにPM2がインストールされていること
- リモートサーバーで、ターゲットリポジトリのGITクローンに必要な権限が付与されていること
リモートサーバーの設定が完了したら、プロビジョニングを開始できます。
$ pm2 deploy production setup
注記: アプリケーション設定ファイルがローカルフォルダでecosystem.config.jsまたはpm2.config.jsという名前であるため、毎回ファイル名を指定する必要はありません。
アプリケーションのデプロイ
リモートサーバーのプロビジョニングが完了したら、アプリケーションをデプロイできます。
$ pm2 deploy production
注記: gitがローカルに変更があるというエラーを報告した場合でも、リモートGITにあるものをプッシュしたい場合は、--force
オプションを使用してデプロイを強制実行できます。
以前のデプロイメントへのロールバック
以前のデプロイメントにロールバックする必要がある場合は、revert
オプションを使用できます。
# Revert to -1 deployment
$ pm2 deploy production revert 1
各サーバーでのコマンドの実行
1回限りのコマンドを実行するには、exec
オプションを使用できます。
$ pm2 deploy production exec "pm2 reload all"
詳細
デプロイメントライフサイクル
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'"
マルチホストデプロイメント
複数のホストに同時にデプロイするには、host
属性配列内に各ホストを宣言するだけです。
"host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],
SSHキーの指定
公開キーへのパスを使用して"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"
},
トラブルシューティング
SSHクローンエラー
ほとんどの場合、これらのエラーは、pm2
がリポジトリをクローンするための正しいキーを持っていないことが原因です。キーが使用可能であることを各ステップで確認する必要があります。
ステップ1 キーが正しく機能していることを確認したら、最初にターゲットサーバーでgit clone your_repo.git
を実行してみてください。成功した場合は、次のステップに進みます。失敗した場合は、キーがサーバーとGitアカウントの両方に保存されていることを確認してください。
ステップ2 既定では、ssh-copy-id
は、通常id_rsa
という名前の既定のIDをコピーします。それが適切なキーでない場合
ssh-copy-id -i path/to/my/key your_username@server.com
これにより、公開キーが~/.ssh/authorized_keys
ファイルに追加されます。
ステップ3 次のエラーが発生した場合
--> 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
…SSH設定ファイルを作成することを検討してください。これは、クローンしようとしている任意のリポジトリに対して正しいSSHキーが確実に使用されるための確実な方法です。この例を参照してください。
# ~/.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