NextJS with Prisma on Kubernetes
deploy as a knative service
- tags
- nextjs
- javascript
- prisma
- knative
- kubernetes
Contents
Now that we have our cluster up and running, lets look at how to build and deploy a NextJS app on it, including the database.
Create a NextJS app
We'll scaffold out a TypeScript app.
| |
Fireup a local data
| |
Install prisma
We'll add the npm packages to our project.
| |
Edit .env and change the url to be
| |
Data Models
In primsa/schema.prisma add a model:
| |
Run the migration
| |
Run prisma studio to check it out
| |
Write the post page and api
This is a basic demostration, so nothing fancy.
tsconfig.json
Add
"baseUrl" : "." and "strict":"false" to compilerOptions.
lib/prisma.ts
| |
pages/posts.ts
We'll use react-hook-form to make things simplier.
| |
A couple points.
- We query the database in
getServiceSideProps - We are fetching our API from
window.location.origin - We are totally hacking the window reload, I'm just lazy
| |
api/create.ts
This is the api request to handle the post. It runs on the server.
| |
Setup postgres on your cluster
| |
Eventually the database will be available on postgres-postgresql.default.svc.cluster.local
We can get the password using
| |
When we deploy the function we'll set the DATABASE_URL to look something like:
| |
You'll need to update that with your actual password of course.
Migrate the database
We are going to setup the database from our local enviornment. We'll
use kubectl to forward the local postgres port to the remote install.
Remember to stop your local postgres container
| |
Then, using the password that we found above, deploy the current migrations to the postgres that's being forwared on localhost. This is a different url then the previous one shown!:
| |
You can also run prisma studio that way, by the way.
Deploy the service
Build image
We need to tweaks the dockerfile from the official docs to add prisma
generate to get the client code. So, copy over that Dockerfile and
then, right after it copies the node_modules over added
RUN npx prisma generate
For me it's on like 14, right before yarn build.
Since I'm working off an M1 mac, I need to set the --platform to make
sure that the image is built using the correct binaries. An example
of this is:
| |
Create the service
| |
Once this is up and running, you should be able to interact with your service and add messages.
Previously
Next