Skeleton of a NodeJS backend, using NestJS + GraphQL + NATS + Microservices + Docker + CI/CD + automated deployments with GitHub Actions using Kubernetes, Helm and AWS EKS.
The code is following Domain-Driven Design (DDD).
Useful Documentation:
- Kubernetes Tutorial
- AWS EKS - Create Kubernetes cluster on Amazon EKS
- Concept, Pros & Cons of Domain Driven Design
What has been done to the default app:
npm i -g @nestjs/cli
nest new node-skeleton
cd node-skeleton
nest generate app micro
npm i nestjs/platform-fastify
npm remove @nestjs/platform-express @types/express supertest @types/supertest
npm i @nestjs/microservices
npm i nats
npm i sinon
npm i @nestjs/config
npm i @nestjs/graphql graphql-tools graphql apollo-server-fastify @moonwalker/graphql-nats-subscriptions
npm i winston colors
CC_TEST_REPORTER_ID # CodeClimate Code Coverage Reporter ID
AWS_ACCESS_KEY_ID # AWS Access Key ID
AWS_SECRET_ACCESS_KEY # AWS Access Key Secret
KUBE_CONFIG_DATA # cat $HOME/.kube/eksctl/clusters/skeleton | base64
Using Docker:
docker-compose up
You can build & deploy out of the box to local minikube or to AWS EKS using Kubernetes.
Install kubectl;
Install Helm;
Install minikube;
Start minikube:
minikube start
Install and configure AWS CLI;
Install eksctl;
Create AWS Cluster:
eksctl create cluster -f ./build-deploy/cluster/skeleton.yaml --auto-kubeconfig
Scale Cluster if needed:
eksctl scale nodegroup --cluster=skeleton --nodes=4 --name=skeleton-nodes
Tip:
Use
--kubeconfig ~/.kube/eksctl/clusters/skeleton
flag to use AWS EKS withkubectl
orhelm
.
Use the minikube docker deamon to build the image:
eval $(minikube docker-env)
Build images:
docker build -t skeleton/api --target production-api .
docker build -t skeleton/micro --target production-micro .
- Run GitHub Action: Build Api;
- Run GitHub Action: Build Micro.
helm upgrade nats ./build-deploy/helm/nats --install --wait
helm upgrade micro ./build-deploy/helm/micro --install --wait
helm upgrade api ./build-deploy/helm/api --install --wait
Explose API:
minikube service api-service
- Run GitHub Action Deploy NATS;
- Run GitHub Action Deploy Micro;
- Run GitHub Action Deploy Api.
helm rollback nats --wait
helm rollback micro --wait
helm rollback api --wait
- Run GitHub Action Rollback NATS;
- Run GitHub Action Rollback Micro;
- Run GitHub Action Rollback Api.
helm uninstall api
helm uninstall micro
helm uninstall nats
helm uninstall api --kubeconfig ~/.kube/eksctl/clusters/skeleton
helm uninstall micro --kubeconfig ~/.kube/eksctl/clusters/skeleton
helm uninstall nats --kubeconfig ~/.kube/eksctl/clusters/skeleton
Destroy AWS EKS Cluster:
eksctl delete cluster --name skeleton
https://kubernetes.io/docs/reference/kubectl/cheatsheet/
To check the command list:
make help
Output:
Usage: make <command>
command description
------ -----------
help Help dialog.
pull Pull docker images.
build Build docker images based on docker-compose.yml file.
up Start docker containers.
upd Start docker containers in daemon mode.
upp Rebuild and start docker containers.
main Enter the 'main' docker container.
stop Stop docker containers.
down Destroy docker containers and volumes.
clean Destroy docker containers, local images and volumes.
ci Run tests in CI mode.
Listen for logs:
kubectl logs -f -l app=api --all-containers [ --kubeconfig ~/.kube/eksctl/clusters/skeleton ]
kubectl logs -f -l app=micro --all-containers [ --kubeconfig ~/.kube/eksctl/clusters/skeleton ]
Enter pods:
kubectl exec --stdin --tty <pod-name> -- /bin/sh