springboot-mongodb-k8s-parth
Brief Introduction
Hello Friends,
I have created REST API using Springboot and Spring cloud application which performs CRUD operations (GET,POST,UPDATE,DELETE) , where data is saved into mongo db. Both Spring boot and Mongo db are hosted in kubernetes env(GCP-Google Cloud Provider) where Springboot is expose via LoadBalancer service to internet and Mongo is expose via Cluster IP service. Spring boot app will connect to mongo db via "mongo-service" and end user can connect to API using "springboot-service" Loadbalancer External IP.
Lets understand all moving parts of code and deployment in brief.
ClientController.java – It has code which performs GET,POST,UPDATE,DELETE.
Client.java- It is the Client document schema which we will store in mongo db. private Long id;
private String clientName;
private String clientType;
private Integer clientID;
private Integer gfcid;
private String clientRegion;
ClientRepo.java – It is repo which performs CRUD operations in mongo db.
application.yml- It has db connection information where all db details are fetch from kubernetes configmap and secretes.Pls make sure to set default mongo db port-27017.
#Once code is ready then login to GCP , follow below steps to make deployment up and running in GCP Kubernetes env,
a) Login to GCP(Google Cloud Provider),go to Kubernetes Engine menu-->Select Cluster-->Create New Cluster--> Connect (make sure kubernetes in up ,run command - "kubectl get nodes" to check)
b) Clone the git code using cmd-
git clone https://github.com/parthec2/springboot-mongodb-k8s-parth.git , it will download whole code in kube cluster
c) Run - Cd springbootmongodb-k8s-parth, then Build code using cmd-
mvn install -DskipTests
b) Once jar is created, then create image using DockerFile by command -
docker build ./ –t parthec2/spring-mongo-service
c) Push jar to docker hub by cmd-
docker push parthec2/spring-mongo-service OR docker push parthec2/spring-mongo-service:1.0 ( Login to docker hub, it should have push image to your docker hub)
d) Now run - cd src/main/resources and create secrets and configmap by using below commands
kubectl create -f mongo-secret.yml
kubectl create -f mongo-config.yml
e) Create mongo db persistent volumen and claim using below commands:
kubectl create -f mongo-pv.yaml
kubectl create -f mongo-pvc.yaml
g)Create mongo db deployment(pods) and service(type-ClusterIP) using below commands:
kubectl create -f mongo-deployment.yaml (mongo db user name and pwd are set using env variables which are read from secrets)
kubectl create -f mongo-service.yml
Check mongo pods health using beow command-
kubectl exec -it pod/ -- /bin/sh
#mongo -u test -p test@123 (where username is "test" and password is "test@123" which is stored in secret file in base64 encryption)
#show dbs
#use admin
#show collections
#db.client.find().pretty(); --It will show empty as we have not inserted any doc yet,
i) Create cluster role otherwise springboot-service will give Forbidden error(403) which accessing mongo service
#create rolebinding
kubectl create rolebinding default-view --clusterrole=view --serviceaccount=default:default --namespace=default
j)Now,create spring boot deployment(2 Pods) and service(type-LoadBalancer) using below commands:
kubectl create -f deployment.yml (It create 4 env variables which will be used by application.yml file)
Get External IP of springboot-service by using command
kubectl get svc/springboot-service
HTTP GET
The GET URL will be form as below- :
Hit in Postman tool OR browser from your laptop/mobile device
http://35.202.38.42:8080/api/getAllClients
It will return json with list of clients as below if we have inserted data
[
{"id":1,"clientName":"ABC-BANK","clientType":"INTERNAL","clientID":44444,"gfcid":5000022,"clientRegion":"CCD"},
{"id":2,"clientName":"XYZ-BANK","clientType":"INTERNAL","clientID":555555,"gfcid":7222,"clientRegion":"ASD"}
]
HTTP POST Go to Postman tool, select Post as option from drop down and add url- http://34.68.140.183:8080/addClient Go to body--select raw-type-JSON
{
"id": 4,
"clientName":"EFG-Client",
"clientType":"EXTERNAL",
"clientID": 232323,
"gfcid": 12345,
"clientRegion": "SSB"
}
Similary, you can try search by client id(getClient/{id}) and delete by client id (deleteClient/{id}).
Pls email me at [email protected] for any queries/feedback/suggestions,,,,
Keep coding,,Spread knowledge,,,Have a Beautiful day, :)