
Next.js is a popular React framework for developing static websites and modern web applications. Next.js application usually deploys to a serverless platform. However, there are some scenarios that you want to deploy to your controlled server and scale it in your infrastructure. This tutorial explains how to deploy a full-stack Next.js application to Vultr Kubernetes Engine. The example application is built with Next.js and uses Prisma to connect with a MySQL database.
Here are a few advantages of this approach:
There are three separated parts in this tutorial:
Before you begin, you should:
kubectl and git in your machine.The Next.js application used in this tutorial is a full-stack application called next-short-urls. The source code for this application can be found at this repository. Vultr has also archived the repo here.
This is a simple URL Shortener built with Next.js, TypeScript, and Prisma. Prisma is an open-source ORM that helps you build and manage the MySQL database.
Clone the source code of the example application:
Prepare a .env file environment with the below content. This .env file is used in the building stage of the Docker image to generate static pages. The final production Docker image does not contain in this file.
Here is a brief explanation of the content of .env file:
DATABASE_URL is a database connection string started with mysql://SECRET_COOKIE_PASSWORD is an application specified password with a minimum length of 32AWAIT_HISTORY_UPDATE is an environment variable that the application needs.Make sure that the DockerFile file contains the below content. This is a multi-stage DockerFile with deps, builder and runner stages.
Here are some configurations that are specified to the next-short-urls application. In the builder stage, RUN npx prisma generate to generate the types from the Prisma schema. In the runner stage, make a folder /app/.next/cache/images and prepare the folder permission. This is used for the Automatic Image Optimization feature in Next.js.
Build the Docker Image
Login to Docker Hub
Tag the Docker Image with your Docker Hub ID and Push to Docker Hub. Replace <YOUR_DOCKER_HUB_ID> with your Docker Hub account id.
Run Docker local to verify the docker image. Navigate to http://localhost:3000 to access the application
(Optional) Go to your Docker Hub dashboard and make sure that your image is private.
In this part, you will create a secret that contains your Docker Hub credentials and create a Deployment to deploy some pods that run your Next.js application.
Create Docker Hub secret named regcred. Replace parameters in the command with your Docker Hub credentials
Create a Secret manifest file secrets.yaml with the data as the previously created .env.
Run the command to create the secret.
Create a Deployment file deployment.yaml with the following content. Replace <YOUR_DOCKER_HUB_ID> with your Docker Hub account ID.
Run the command to create the Deployment and Service.
Run the command kubectl get pods to see the newly created pods. The result should look similar to:
Run the command kubectl get services to see the newly created service. The result should look similar to:
Scale your application to 10 replicas using the following command.
Run the command kubectl get pods to see the newly created pods. The result should look similar to:
Perform port-forwarding to access your application through the service. Navigate to http://localhost:8080 to access your application.
You have successfully deployed the Next.js application with MySQL Database to Vultr Kubernetes Engine with 10 replicas in your cluster.
In this part, you will install NGINX Ingress Controller and expose your application through a domain name with secure SSL certificates.
Install ingress-nginx.
Go to your Load Balancers dashboard at https://console.vultr.com/loadbalancers/ and get the IP Address of the newly created Load Balancer. This is the Load Balancer created for the NGINX ingress.
Create an A record in your domain DNS that points to the above IP address.
Install cert-manager to manage SSL certificates
Create a manifest file letsencrypt.yaml to handle Let's Encrypt certificates. Replace
Run the command to install the above Let’s Encrypt issuers.
Create an Ingress manifest file ingress.yaml with the following content. Replace
Run the command to create the ingress.
Run the command kubectl get ingress to see the newly created ingress. The result should look similar to:
Navigate to https://<YOUR_DOMAIN to access your application.
0 Comments
Be the first to comment and share your perspective with the community.