How to Deploy Nextjs App on Kubernetes in 5 mins.
This is a tutorial on how to deploy Next.js application on Kubernetes.
Prerequisites
- Docker
- Kubectl
- Kubernetes Cluster
In this module, we’ll be using “Kind” Kubernetes Cluster to test our deployment.
You can learn more about Kind Cluster here.
Get your Next.js App ready
If you already have a Next.js app and want to learn how to Dockerize your app, please follow this link.
For simplicity, let’s just assume we have already Dockerized our app. You can pull this image to follow along.
Deployment
- Make a folder
<folder-name>
andcd
into the folder. - Create a file
deployment.yaml
and paste the following code.
3. Create a file service.yaml
and paste the code below.
4. Go a step back using cd ..
and run kubectl apply -f <folder-name>
.
4. Run kubectl get pods --watch
to check if the Pod is in running state.
5. Wait for Pod’s “Ready” state to be 1/1.
6. Run kubectl port-forward svc/web-srvice 8080:80
.
7. At this point, you should be able to see your app at http://localhost:8080
.
If you want to access your app without port-forwarding, you are going to have to do one additional step, adding an Ingress config.
8. Create a file called ingress.yaml
alongside service and deployment file and paste the code below.
Ingress config depends upon the Ingress Controller and would require few changes based on the controller used.
In this example, we are using nginx
Ingress controller on “Kind” Cluster.
9. Repeat the 4th step.
10. Open the browser, and you should be able to access your app on http://localhost:80
without using port-forwarding.
Happy Coding!
If you found this helpful, please subscribe to our newsletter.
Want to learn tech the right way? Check us out at rehearse.