Example of using 0mq inside K8S environment with multi-language communication.
Golang <----- 0mq -----> Node.js (Web Server) (bridge) (Database Engine)
- Docker
- k8s
-
Install Ingress NGINX (Community Edition)
ingress-nginx
if you haven't. -
Build Go web server in
fiber
cd fiber
docker build -t docker -t fiber-prisma-main .
- Build Node.js Database Engine in
database-engine
cd database-engine
docker build -t fiber-prisma-db-engine .
- Move
deployment.example.yaml
todeployment.yaml
and appendDATABASE_URL
environment variable.
# deployment.yaml
31 env:
32 - name: DATABASE_URL
33 value: <Postgres Connection URL>
- Port forward nginx Ingress if you haven't. (I'm going to forward to localhost:8080)
# Find the ingress name
kubectl get pod | grep ingress-nginx-controller-
# Copy ingress name from above to here
kubectl port-forward ingress-nginx-controller-[hash]-[hash] 8080:80
- Run service
# At project root
./up.sh
- To stop service, simply run
./down.sh
Even with automation, manual setup is still required and is complicated. So I think I will implement basic deployment which can be automated.
Note: The template is not the best practice, you can improve it much better but I the idea is to prove that this work and I want to reduce any complexity where possible as state above.
Note: For some reason, you might have to run deployment command 2-3 times to make it complete for some reason.
1 Command automate deployment to GCP. Including:
- Create VPC Network, subnetwork and private network service for internal service communication.
- PostgreSQL with users.
- Create Autopilot Kubernetes Engine and deploys the images.
- Deploy pod with replicas and load balancer service.
- Install nginx helm chart and use ingress to direct traffic.
- Push docker image of
fiber
anddatabase-engine
to any registry.
- Make sure Google Cloud can access the registry.
- The easiest way is to host it in Google Cloud Image Registry, so the image is private, GCP can access it.
- Rename
config.ts
to.ts
and fill the form.
- Rename
src/config.example.ts
toconfig.ts
. - Rename
src/config.example.ts
toconfig.ts
.
- Config deployment zone and project id
pulumi config set gcp:project <Your GCP project ID>
pulumi config set gcp:zone asia-southeast1-a
Run:
cd infra && yarn up
The deployment will take a couple minute before complete. Once complete, you will be able to access the deployment.
If you access any endpoint the use database-engine
, you will notice that it will return success: false
response.
Because the automation doesn't create Database schema, you have to created one yourself.
- Go to your SQL deployment on GCP.
- Go the
GCP console
- Select
SQL
from sidebar - Select
<your deployed database>
- Grab the IP address into local
database-engine/.env
.
DATABASE_URL="postgresql://<username>:<password>@<Cloud SQL IP Adress>/prismaQueue?schema=queue"
- Enable Cloud SQL local machine access.
- Select
Edit
in GCP SQL console page - Scroll down and expand
Connections
- Check
Enable Public IP
if not enabled - Under
Authorized Networks
, clickAdd Network
- Name your network and add your current
IP Address
(Check your ip here)
- Migrate schema and table from your Prisma defination to Cloud SQL.
- Go to
database-engine
cd database-engine
- Run
npx prisma migrate <stack name>
(This repo's default isdev
)
npx prisma migrate <stack name>
After following Post Deployment process, the Database Engine should be able to query and everything should be working just fine.
If you want Ingress
IP, you can find one in GCP Console.