Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds baremetal deploy strategy #4750

Merged
merged 28 commits into from
Mar 17, 2022
Merged

Adds baremetal deploy strategy #4750

merged 28 commits into from
Mar 17, 2022

Conversation

cannikin
Copy link
Member

@cannikin cannikin commented Mar 14, 2022

To quote Alain de Lille, the French theologian and poet: "All roads lead to AWS." I've tried several different hosting providers and in the end, I ended back where I always do: deploying to EC2 servers on AWS. But right now you'd need to roll your own deployment setup. This PR intends to change that.

Redwood has the ability to serve both the web and api sides for production via yarn rw serve. In my case I've been serving the web side with nginx for maximum performance, and serving the api side via our built-in yarn rw serve api command, monitored by pm2. pm2 handles clustering the node processes across all available CPUs for maximum performance.

It's been great to see consistent GraphQL calls roundtrip in <50ms, compared to serverless environments where the cold-boot time alone can be 1000ms, and even a warmed-up endpoint takes >200ms returning minimal data.

Not to mention the other benefits that come with being able to ssh into your hardware and make changes (some would consider having to do this at all to be a drawback!). I'm also able to configure pm2 as a periodic background job processor. I'll include instructions for doing this in the docs.

Closes #4217

Setup / Testing Instructions

Until I get a doc together, here's how to do this:

Prerequisites

Start a server somewhere and note the hostname and user. I worked on an Ubuntu 20.04 (amd64) t2.small instance when developing this deploy strategy. Be sure that any attached security group allows access to port 22 from your home IP, as well as ports 8910 and 8911 open to at least your home IP, or 0.0.0.0 if you want it accessible from anywhere.

You can just commit the SQLite database to the codebase (remove the dev.db* entry from .gitignore) and not worry about configuring a remote DB instance somewhere.

Local Machine

  1. Configure SSH agent forwarding (this isn't required, but if you don't then you'll need to create a deploy key for GitHub and keep it on the server.)
  2. yarn -W -D add pm2 (an optional global install of pm2 would be nice, but that'll have to happen in a future release)
  3. Add CORS config to graphql function (set origin: mydomain.com:8911)
  4. yarn rw setup deploy baremetal
  5. Edit deploy.toml: set server host, user, path. If you setup agent forwarding above, leave agentForward: true, otherwise remove
  6. Commit changes and push up to repo

Server

  1. (optional) Add local public ssh key to ~/.ssh/authorized_keys for easy login, otherwise add privateKey: /path/to/private.pem to deploy.toml or password: [password] if you don't want to use keys
  2. sudo apt-get update && sudo apt-get upgrade -y
  3. Edit ~/.bashrc and comment out code block at top starting with # If not running interactively, don't do anything
  4. Install NVM: curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
  5. Exit ssh, login again to enable NVM (or follow post-install instructions)
  6. nvm install 16
  7. npm install -g yarn
  8. Create deploy directory, set to be owned by deploying user (something like /var/www/myapp)
  9. git clone repo into deploy directory
  10. (optional) If you want to avoid waiting an extra few minutes during the deploy, run a yarn install now

Locally

  1. yarn rw deploy baremetal --first-run
  2. Open http://server.com:8910

On future deploys just yarn rw deploy baremetal and it'll restart processes instead of starting them.

This should make the site available at port 8910 just like our default config. I'll have details in the docs for serving on port 80 (but ideally you want this behind a real webserver like nginx). This isn't the ideal deploy setup (not secure, CORS, one single serve process running) so the docs will go into installing nginx and having your web side served from there, only run yarn rw serve api and setup the proxy through nginx, removing CORS, etc. This is how https://app.algostake.org is configured!

@mojombo
Copy link
Contributor

mojombo commented Mar 14, 2022

🤘

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@thedavidprice
Copy link
Contributor

This will need to be added to deploy CI over here as well:

@cannikin
Copy link
Member Author

Are we gonna run a persistent server somewhere forever to support that?

Or we just mark it as experimental and anything goes. ;)

@thedavidprice
Copy link
Contributor

@cannikin Does PM2 support any notifications about deploy status? E.g. if you trigger a deploy and something fails, how do you know?

@cannikin
Copy link
Member Author

cannikin commented Mar 15, 2022

You'll see the error message in the console! For some of them at least...I don't know every error that pm2 could throw, but these common ones on startup/restart show nicely:

image

I'm not sure what happens if the process itself errors out though...I don't think pm2 itself even shows that in the console, it gets ported to the log output for that process...

@thedavidprice
Copy link
Contributor

Ok, got it. When added to the Deploy CI repo, I'll need to create a GH Workflow to deploy and can handle notification via the workflow success/fail accordingly. ✅

@cannikin
Copy link
Member Author

Do you just look for text in the output? If it's the kind of error I can catch it'll always start with that "Deploy failed!" message...

@thedavidprice
Copy link
Contributor

Ah, I just saw the error exit 1, which will cause the GH Workflow to fail. That's all I need!

@cannikin
Copy link
Member Author

I think it's working! Lemmie write up the instructions for getting a server setup and then you should be able to add to the deploy suite!

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@cannikin cannikin marked this pull request as ready for review March 16, 2022 22:30
@cannikin cannikin requested a review from thedavidprice March 16, 2022 22:30
@netlify
Copy link

netlify bot commented Mar 17, 2022

✔️ Deploy Preview for redwoodjs-docs canceled.

🔨 Explore the source changes: cba2729

🔍 Inspect the deploy log: https://app.netlify.com/sites/redwoodjs-docs/deploys/6232d02e969f7400086d5a76

@thedavidprice thedavidprice merged commit d7c79d7 into main Mar 17, 2022
@thedavidprice thedavidprice deleted the rc-baremetal-deploy branch March 17, 2022 06:25
@jtoar jtoar added this to the next-release milestone Mar 17, 2022
@thedavidprice thedavidprice modified the milestones: next-release, v0.50.0 Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature topic/deployment
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

Add standalone server deploy strategy - BAREMETAL
4 participants