A gRPC based microservices implementation in Go, deployed in a kubernetes cluster and consisting of two microservices:
Maths
serviceREST API
to expose theMaths
service
The Maths
microservice that:
- computes the
Addition
of two numbers - computes the
Average
of an array of numbers - computes the
Maximum
of an array of numbers - computes the
Minimum
of an array of numbers - computes the
Summation
of an array of numbers - computes the
Sorted
form of an array of numbers
A REST API
that responds to the client and routes them to the corresponding Maths
compute
To start the application without deploying to kubernetes
, we need to enter the downloaded project directory, and run the server and api in separate terminals respectively i.e.
- for the
server
, executego run server/main.go
- for the
api
, executego run api/main.go
Now test with integer 17
, integer 18
and array [48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
, via the sample api requests:
127.0.0.1:8080/add/17/18
127.0.0.1:8080/average/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
127.0.0.1:8080/max/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
127.0.0.1:8080/min/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
127.0.0.1:8080/sum/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
127.0.0.1:8080/sort/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
To deploy and start the application in kubernetes
, we need to make a few changes to ensure the api
is able to communicate with the math-service
server. These changes to be implemented from within the project directory are:
- start
kubernetes
by runningminikube start
- ensure docker reuses the Docker deamon in minikube, by running
eval $(minikube docker-env)
- update
api/main.go
by changinglocalhost:9090
tomath-service:9090
on line 35 - rebuild
api
docker image by runningdocker build -t math-service-api -f Dockerfile.api .
- rebuild
server
docker image by runningdocker build -t math-service-server -f Dockerfile.server .
- clean-up previous
kubernetes
deployments, by runningkubectl delete all --all
- deploy the
api
to kubernetes by runningkubectl create -f api.yaml
- deploy the
server
to kubernetes by runningkubectl create -f server.yaml
- copy the
api
's new kubernetes ip address after runningminikube service api-service --url
Now test again with integer 17
, integer 18
and array [48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
, via the sample api requests:
<ip-address:port>/add/17/18
<ip-address:port>/average/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
<ip-address:port>/max/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
<ip-address:port>/min/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
<ip-address:port>/sum/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
<ip-address:port>/sort/[48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
where <http://ip-address:port>
represents the result of running minikube service api-service --url
. For example, if <http://ip-address:port>
was http://192.168.99.100:32348
, then we can use the terminal
to make the api request: curl http://192.168.99.100:32348/add/17/18
. For the array
: [48, 96, 86, 68, 57, 82, 63, 70, 37, 34, 83, 27, 19, 97, 9, 17]
samples, simply copy/past the requests into a browser's address bar i.e. intead of using curl
.
In both scenarios of runing the application (i.e. with or without kubernetes), the test examples should give us results numbers similar to:
Addition is: 35
================
Average is: 55.8125
================
Maximum element is: 97
================
Minimum element is: 9
================
Sum total of the elements is: 893
================
Sorted array is: [9 17 19 27 34 37 48 57 63 68 70 82 83 86 96 97]
================