Deploy a JavaScript, Node.js, and Express Website to Kubernetes in 5 Minutes with Jetpack.io
Rob Richardson
Posted on September 12, 2022
Deploying to Kubernetes is Hard
Maybe you can relate: We've finished coding a website, and we're ready to deploy. We know we want to deploy to Kubernetes, to take advantage of it's industrial-scale hosting, automatic load-balancing, fault tolerance, scaling, and more.
But deploying to Kubernetes is hard. Now we have to trek into the land of containers, pods, services, ingress, and yaml. Ugh. "I just finished the website. I don't want to read a book just to make it work." Kubernetes has too many options and concepts: Pods, Deployments, ReplicaSets, Services, ConfigMaps. Oh my! And the Kubernetes yaml file is really confusing and specific. I really wish it was easier to deploy a website to Kubernetes.
Why Jetpack.io
Jetpack.io is a zero-DevOps solution for deploying to Kubernetes. Jetpack.io takes your code, packages it up in a container, and publishes it to Kubernetes. Jetpack.io also creates a public URL so the site can be routable from the internet, and can stream application logs back to your terminal. All of the toil of Kubernetes melts away, and we can focus on our application.
In this tutorial, we'll build an Express website in JavaScript, configure it for Jetpack.io, and deploy it to Kubernetes – all in 5 minutes.
Building an Express.js app with Node.js
In this example we'll quickly scaffold an Express website. You could swap out these steps with use an existing Node web site, or scaffold a site using other frameworks or tools.
Open a terminal in an empty directory.
Generate an Express website:
npx express-generator --view hbs
Now we have a regular Node.js website. Feel free to customize this site to meet your needs.
Configure the App for Containers
Jetpack.io assumes your web server is running on port 8080
from inside a Docker container. In this section we'll build a Dockerfile to run the web site on port 8080
.
- Create a new file in the folder named Dockerfile and add this content:
FROM node:alpine
WORKDIR /app
COPY . .
RUN npm install
ENV PORT=8080
EXPOSE 8080
CMD ["npm", "start"]
Note: this is a bare-bones Dockerfile. Depending on your app's dependencies, you may need to add more.
Initialize the project with Jetpack.io
Jetpack.io needs some initial configuration to understand the Node.js web server. We need only do this once per website.
- To install the Jetpack CLI, open a terminal and run
curl https://get.jetpack.io -fsSL | bash
Jetpack CLI works on Linux, macOS, and on Windows via WSL2.
- Login to Jetpack CLI:
jetpack auth login
Logging into Jetpack allows you to deploy to the Jetpack.io Kubernetes cluster. You can also run on your own cluster in your Azure, AWS, GCP, or private cloud account.
- Initialize the project for Jetpack:
jetpack init
This wizard interviews you and configures the Jetpack deployment strategy to match your application.
- Answer that Yes, this is a web server. This tells Jetpack that we want to accept inbound traffic.
If we were building a scheduled cron job or a function that drained a queue, we could answer no.
- Finish the wizard, and Jetpack CLI automatically generates an appropriate
jetconfig.yaml
file. You should commit this to source control.
Deploy to Kubernetes using Jetpack.io
Now that the project is configured for Jetpack, deploying is really easy.
Open a terminal in the directory with the Dockerfile and jetconfig.yaml file.
Deploy to Kubernetes:
jetpack dev
Now Jetpack makes the deployment really simple. Automatically it will:
- Build the Docker image
- Push the image to a private registry
- Schedule the necessary Kubernetes resources
- Create a publicly routable URL to test the website
- Setup port-forwarding from your local machine
- Stream application logs back to the terminal
- Test the website:
In the console output will be the publicly routable URL. Click this URL to view the web page.
Jetpack also sets up port-forwarding, so you can also browse to http://localhost:8080/ to view the page.
Well that was easy
It's easy to deploy a Node.js and Express.js app built with JavaScript to Kubernetes with Jetpack.io. In this tutorial we scaffolded a website, configured it for Jetpack.io, and used Jetpack.io to deploy to Kubernetes. Jetpack.io is a great zero-DevOps solution for deploying to Kubernetes. Visit https://jetpack.io/docs/ to learn more and to start deploying your content to Kubernetes with ease.
Posted on September 12, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.