Skip to content

Commit

Permalink
#11 all custom services deployed in the k8s
Browse files Browse the repository at this point in the history
- api gateway in k8s
  • Loading branch information
maciejgz committed Oct 5, 2023
1 parent 6d7f91b commit 9eae8af
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 19 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Case study of the search problem in the microservice environment when data is st
- [x] Dockerize projects
- [x] Run projects locally
- [x] Run projects in Docker Compose
- [ ] Run all projects in Kubernetes
- [x] Run all projects in Kubernetes
- [ ] Run all the 3rd party services in K8S
- [ ] Add distributed tracing with Sleuth and Zipkin
- [ ] Enable scaling of the services with load balancing over API Gateway
- [x] Enable scaling of the services with load balancing over API Gateway
- [ ] Run Spring Boot Admin in local environment
- [ ] Enable K8S ingress to make app available over the internet
- [ ] Deploy all the services to the cloud
Expand Down Expand Up @@ -123,7 +123,7 @@ Linux:
```

### Kubernetes
K8S scripts shold be runned in the following order from the project root directory:
K8S scripts should be run in the following order from the project root directory:
- [k8s](k8s) - directory with global configuration:
- special role and privileges
- ingress
Expand All @@ -137,6 +137,11 @@ kubectl apply -f k8s
kubectl apply -f ms-admin/k8s
```

- [ms-api-gateway](ms-api-gateway) - directory with configuration for the admin module [ms-api-gateway](ms-api-gateway/k8s):
```docker
kubectl apply -f ms-api-gateway/k8s
```

- [ms-cms-service](ms-cms-service) - directory with configuration for the cms module [ms-cms-service](ms-cms-service/k8s):
```docker
kubectl apply -f ms-cms-service/k8s
Expand Down
2 changes: 1 addition & 1 deletion docker/db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
- "5432:5432"
volumes:
- /var/lib/docker/volumes/microservice-search/postgres:/var/lib/postgresql/data
- ./init.sh:/docker-entrypoint-initdb.d/init.sh
- ./init_db_schema.sh:/docker-entrypoint-initdb.d/init.sh
networks:
- ms-network

Expand Down
File renamed without changes.
6 changes: 1 addition & 5 deletions docker/start.bat
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
@echo off
:: docker compose up all sh files in the docker directory
set COMPOSE_PROJECT_NAME=microservice-search
docker-compose -f db.yml -p %COMPOSE_PROJECT_NAME% up -d
docker-compose -f mongodb.yml -p %COMPOSE_PROJECT_NAME% up -d
docker-compose -f kafka.yml -p %COMPOSE_PROJECT_NAME% up -d
docker-compose -f redis.yml -p %COMPOSE_PROJECT_NAME% up -d
call start_3rd_party_components.bat

set /a num_replicas=3
set /a delay_seconds=5
Expand Down
5 changes: 1 addition & 4 deletions docker/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
## docker compose up all sh files in the docker directory
docker-compose -f db.yml -p microservice-search up -d
docker-compose -f mongodb.yml -p microservice-search up -d
docker-compose -f kafka.yml -p microservice-search up -d
docker-compose -f redis.yml -p microservice-search up -d
./start_3rd_party_components.sh

for i in {1..3}; do
docker-compose -f app.yml -p %COMPOSE_PROJECT_NAME% up -d --scale ms-cms-service=$i
Expand Down
5 changes: 5 additions & 0 deletions docker/start_3rd_party_components.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set COMPOSE_PROJECT_NAME=microservice-search
docker-compose -f db.yml -p %COMPOSE_PROJECT_NAME% up -d
docker-compose -f mongodb.yml -p %COMPOSE_PROJECT_NAME% up -d
docker-compose -f kafka.yml -p %COMPOSE_PROJECT_NAME% up -d
docker-compose -f redis.yml -p %COMPOSE_PROJECT_NAME% up -d
4 changes: 4 additions & 0 deletions docker/start_3rd_party_components.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docker-compose -f db.yml -p microservice-search up -d
docker-compose -f mongodb.yml -p microservice-search up -d
docker-compose -f kafka.yml -p microservice-search up -d
docker-compose -f redis.yml -p microservice-search up -d
8 changes: 8 additions & 0 deletions ms-api-gateway/k8s/configmap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: ConfigMap
apiVersion: v1
metadata:
name: ms-api-gateway
data:
DATA_REDIS_HOST: "192.168.0.129"
application-k8s.properties: |-
spring.profiles.active=k8s
35 changes: 35 additions & 0 deletions ms-api-gateway/k8s/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: ms-api-gateway
labels:
app: ms-api-gateway
spec:
replicas: 1
selector:
matchLabels:
app: ms-api-gateway
template:
metadata:
labels:
app: ms-api-gateway
spec:
serviceAccountName: spring-cloud-kubernetes
containers:
- name: ms-api-gateway
image: ms-api-gateway:latest
ports:
- containerPort: 8080
imagePullPolicy: Never
env:
- name: SPRING_CLOUD_BOOTSTRAP_ENABLED
value: "true"
- name: SPRING_CLOUD_KUBERNETES_SECRETS_ENABLEAPI
value: "true"
- name: SPRING_PROFILES_ACTIVE
value: k8s
- name: DATA_REDIS_HOST
valueFrom:
configMapKeyRef:
name: ms-api-gateway
key: DATA_REDIS_HOST
14 changes: 14 additions & 0 deletions ms-api-gateway/k8s/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: ms-api-gateway
labels:
app: ms-api-gateway
spring-boot: "true"
spec:
ports:
- port: 8080
protocol: TCP
selector:
app: ms-api-gateway
type: LoadBalancer
64 changes: 64 additions & 0 deletions ms-api-gateway/src/main/resources/application-k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
server:
port: 8080

management:
endpoint:
gateway:
enabled: true
endpoints:
web:
exposure:
include: "*"

## API Gateway routes
spring:
cloud:
gateway:
routes:
- id: stock-service-api-1-route
uri: http://ms-stock-service:8091
predicates:
- Path=/api/stockProduct/**
- Weight=stock-api-group-1, 5
filters:
- RewritePath=/api/stockProduct/(?<segment>.*), /api/v1/stockProduct/${segment}
- name: CircuitBreaker
args:
name: stock-api-group-1-cb
fallbackUri: forward:/fallback/stock
- id: stock-service-api-2-route
uri: http://ms-stock-service:8091
predicates:
- Path=/api/stockProduct/**
- Weight=stock-api-group-1, 5
filters:
- RewritePath=/api/stockProduct/(?<segment>.*), /api/v2/stockProduct/${segment}
- name: CircuitBreaker
args:
name: stock-api-group-1-cb
fallbackUri: forward:/fallback/stock
- id: cms-service-api-1-route
uri: http://ms-cms-service:8100
predicates:
- Path=/api/cmsProduct/**
- Weight=cms-api-group-1, 5
filters:
- RewritePath=/api/cmsProduct/(?<segment>.*), /api/v1/cmsProduct/${segment}
- name: CircuitBreaker
args:
name: cms-api-group-1-cb
fallbackUri: forward:/fallback/cms
- id: cms-service-api-2-route
uri: http://ms-cms-service:8100
predicates:
- Path=/api/cmsProduct/**
- Weight=cms-api-group-1, 5
filters:
- RewritePath=/api/cmsProduct/(?<segment>.*), /api/v2/cmsProduct/${segment}
- name: CircuitBreaker
args:
name: cms-api-group-1-cb
fallbackUri: forward:/fallback/cms
data:
redis:
host: ${DATA_REDIS_HOST}
2 changes: 1 addition & 1 deletion ms-cms-service/k8s/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
labels:
app: ms-cms-service
spec:
replicas: 1
replicas: 3
selector:
matchLabels:
app: ms-cms-service
Expand Down
2 changes: 1 addition & 1 deletion ms-cms-service/k8s/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ spec:
protocol: TCP
selector:
app: ms-cms-service
type: ClusterIP
type: LoadBalancer
2 changes: 1 addition & 1 deletion ms-cms-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>1.7.0</version>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
2 changes: 1 addition & 1 deletion ms-stock-service/k8s/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
labels:
app: ms-stock-service
spec:
replicas: 1
replicas: 3
selector:
matchLabels:
app: ms-stock-service
Expand Down
2 changes: 1 addition & 1 deletion ms-stock-service/k8s/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ spec:
protocol: TCP
selector:
app: ms-stock-service
type: ClusterIP
type: LoadBalancer
4 changes: 3 additions & 1 deletion ms-stock-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=stock-service
spring.kafka.consumer.properties.spring.json.trusted.packages=*
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer

management.endpoints.web.exposure.include=*

0 comments on commit 9eae8af

Please sign in to comment.