# Microservice Architecture with Spring Cloud, Prometheus, Grafana, and Keycloak using Kubernetes

When I started research on building microservice architecture with completely opensource software, I established the following as key **guiding principles:**

-   Opensource technology stack, which is proven & battle-tested for production systems
-   Applying [API Gateway pattern](https://microservices.io/patterns/apigateway.html) for Microservices
-   Wider developer community support with active support (in case there are issues or troubleshooting needed)
-   Good documentation with easily accessible learning material

These principles helped me to choose the technology stack, which has been outlined in the next section.

## Technology Stack

| Technology | Usage |
| --- | --- |
| [Spring Cloud Gateway](https://spring.io/projects/spring-cloud-gateway) | As API Gateway with support for handing cross-cutting concerns such as security, monitoring/metrics, and resiliency |
| [Spring Boot](https://spring.io/projects/spring-boot) | For microservices development |
| [Grafana](https://grafana.com/) | For observability with dashboard and visualizing monitoring metrics like system CPU, memory, disk and I/O utilization, etc.  
**Datasource added for visualization:**  
Prometheus, Jaeger, Loki |
| [Prometheus](https://prometheus.io/) | For capturing data metrics & alerts with time-series data |
| [Keycloak](https://www.keycloak.org/) | For identity and access management (opensource) |
| [Jaeger](https://www.jaegertracing.io/) | For end-to-end distributed tracing |
|   
[Resilience4j](https://resilience4j.readme.io/docs) | For fault-tolerance management (similar to Netflix Hystrix but lightweight) |
| [Loki](https://grafana.com/docs/loki/latest/getting-started/get-logs-into-loki/) | For Logs Management built at [Grafana Lab](https://github.com/grafana).  
Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheuss. |
| [Kubernetes](https://kubernetes.io/) | For container orchestration, deployment, scaling and container management |

This led me to create a simple microservices oriented application implementing the following use-cases:

-   3 or more microservices as per business domain (applying [domain-driven design](https://martinfowler.com/tags/domain%20driven%20design.html) – didn’t name microservices to keep it open)
-   Authenticated application secured with Login flow (with token generation & validation)
-   Service routing to the intended microservice
-   Managing non-functional aspects such as caching, monitoring, distributed tracing, logging, etc.

## Architecture Flow

![Figure A - Microservices Architecture Flow](https://github.com/kuldeepsingh99/spring-cloud-gateway/raw/main/img/arch.PNG)

Figure A – Microservices Architecture Flow

## Deployed Stack

-   Deployment steps followed in detail available by [clicking here](https://github.com/kuldeepsingh99/spring-cloud-gateway).
-   Kubernetes YAML configuration files are available by [clicking here](https://github.com/kuldeepsingh99/spring-cloud-gateway/tree/main/deployment).
-   Microservices are referred as: **ms-1, ms-2, ms-3**

![Figure B - PODs Deployed to Kubernetes](https://github.com/kuldeepsingh99/spring-cloud-gateway/raw/main/img/pod.PNG)

Figure B – PODs Deployed to Kubernetes

## Working Code

-   [https://github.com/kuldeepsingh99/spring-cloud-gateway](https://github.com/kuldeepsingh99/spring-cloud-gateway)

To conclude, it was an interesting journey with a great learning curve putting these technologies together to build a completely opensource based microservices architecture. Feel free to share your comments.
