Setting up prisma GraphQL on kubernetes
This article is part of the series `Practical guide to microservices using GraphQL`, You can find a full list of articles at the end below.
Prerequisites
You need to have access to a kubernetes cluster and `kubectl` installed.
You need to install helm https://helm.sh/ installed on your kubernetes cluster.
So, to get started building our graphql microservices we need to take care of our database and database access layer.
For the data access layer we will be using prisma graphQL and until now it can work with any of these databases
- PostgreSql
- MySql
- Mongodb
In this article lets go with MongoDB
Installing MongoDb on Kubernetes
We will be using this helm chart to install MongoDB.
Once you have helm up and running on kubernetes it’s fairly simple to install MongoDB using this simple command:
helm install --name mongodb --namespace prisma stable/mongodb
It will be installed to prisma namespace.
We will use this command in the post-installation notes to get the root password for the database to be used for prisma configuration later on.
kubectl get secret --namespace prisma mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode
Also kubernetes provides dns name for the mongodb connection url
MongoDB can be accessed via port 27017 on the following DNS name from within your cluster:mongodb.prisma.svc.cluster.local
In the same namespace we can use only mongodb
part of the dns name
You can also use this command to check if mongodb pod is up and running
kubectl get po -n prisma
Outputs something like this:
NAME READY STATUS RESTARTS AGE
mongodb-68f66b89fd-5bwsp 1/1 Running 0 4m
Setting up Prisma
First, We need to make the deployment and config file:
Then instruct kuberentes to apply these config files:
kubectl apply -f ./config.yml
kubectl apply -f ./deployment.yml
after a minute or two we should have the prisma pod up and running
kubectl get po -n prisma
NAME READY STATUS RESTARTS AGE
mongodb-68f66b89fd-5bwsp 1/1 Running 0 52m
prisma-f59c75f7d-pr24t 1/1 Running 0 2m
Exposing prisma server to network
For that we need to setup a kubernetes service, and since we don’t have any kind of security we will make prisma server only exposed to kubernetes internal network:
kubectl apply -f ./service.yml
Once we apply the service config, we can forward traffic from our localhost on port 5000
to the service on port 4466
using this command:
kubectl port-forward svc/prisma 5000:4466 -n prismaOutput
Forwarding from 127.0.0.1:5000 -> 4466
Forwarding from [::1]:5000 -> 4466
Handling connection for 5000
And if we visit http://localhost:5000 you will be able to see the graphql playground for prisma.
And from now on you can go to prisma documentation and try to setup a new prisma database https://www.prisma.io/docs/1.29/get-started/01-setting-up-prisma-new-database-JAVASCRIPT-a002/#configure-your-prisma-api
You may also need to setup prisma cli to continue along with the guide:
npm install -g prisma
In the next article we will setup the API for a microservices on top of a prisma database.
Articles in this series
- Practical guide to microservices using GraphQL [Introduction]
- Setting up prisma GraphQL on kubernetes