ezeparziale / nextjs-k8s

NextJS app with 2 pods sharing cache on redis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

⚑ Next.js 14 + Redis cache + Kubernetes

Next.js web application with 2 pods sharing cache on redis

πŸ’¬ About this repo

🐳 Docker

I created the docker image following the example in:

https://github.com/vercel/next.js/tree/canary/examples/with-docker

I configured output: "standalone" property in next-config.js

More info in:

https://nextjs.org/docs/app/building-your-application/deploying#docker-image

πŸ—ƒοΈ Cache

I use Redis to cached values and ensure consitency across all pods.

I installed @neshca/cache-handler using npm i -D @neshca/cache-handler:

https://caching-tools.github.io/next-shared-cache/redis-stack

Adding cache-handler.js and configured incrementalCacheHandlerPath property in next-config.js

I following this example and the same pages to validate the cache using revalidateTag and revalidatePath:

https://caching-tools.github.io/next-shared-cache/redis

More info in:

https://nextjs.org/docs/app/building-your-application/deploying#caching-and-isr

πŸ“Έ Image optimization

I installed sharp using npm install sharp

More info in:

https://nextjs.org/docs/app/building-your-application/deploying#image-optimization

πŸ‹ Kubernetes

In the k8s folder, there are all the Kubernetes manifests to create two pods with the Next.js web application and one pod with a Redis server.

πŸƒ Run

πŸ₯‡ Option 1

Run dev mode with npm run dev

πŸ₯ˆ Option 2

Run in Docker using Docker compose with docker compose -f "compose.yaml" up -d --build

πŸ₯‰ Option 3

Run in Kubernetes:

  1. Execute docker build -t nextjs-docker . to create Docker image

  2. Execute kubectl apply -f k8s\configmaps\nextjs.yaml

  3. Execute kubectl apply -f k8s\deployments\nextjs.yaml

  4. Execute kubectl apply -f k8s\deployments\redis.yaml

  5. Execute kubectl apply -f k8s\services\nextjs.yaml

  6. Execute kubectl apply -f k8s\services\redis.yaml

  7. Access to http://localhost:3000

About

NextJS app with 2 pods sharing cache on redis


Languages

Language:TypeScript 77.0%Language:Dockerfile 9.0%Language:JavaScript 7.9%Language:CSS 6.1%