The below app just gives out its own pod name in response on a GET call to its '/' endpoint.
- Install
task
cli tool:
go install github.com/go-task/task/v3/cmd/task@latest
- cd into root directory:
cd ../load-balancer
- compile, package and publish restSimpleApp:
task compile-restSimpleApp
task package-restSimpleApp
task publish-restSimpleApp
Note: skip steps #1, #2 and #3 since, the app's image has already been pushed to dockerhub.
-
Setup a kubernetes cluster, easier way to do this is via minikube: https://kubernetes.io/docs/tutorials/hello-minikube/
-
Deploy the app
kubectl create -f build/restSimpleApp/deploy.yaml
- Get the pods
kubectl get po -l app=rest-simple-app -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
rest-simple-app-6c9dc776f5-gwnvq 2/2 Running 0 59s 10.244.0.4 eksabm-testbed1-core-dev-node-0 <none> <none>
rest-simple-app-6c9dc776f5-hkp7w 2/2 Running 0 59s 10.244.0.2 eksabm-testbed1-core-dev-node-0 <none> <none>
rest-simple-app-6c9dc776f5-vfnbx 2/2 Running 0 59s 10.244.0.3 eksabm-testbed1-core-dev-node-0 <none> <none>
- Curl the pods using their IP and listening router port
curl 10.244.0.4:9090 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-gwnvq
curl 10.244.0.3:9090 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-vfnbx
curl 10.244.0.2:9090 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-hkp7w
This app is designed to act as a proxy for any number of urls.
- compile, package and publish proxyApp:
task compile-proxyApp
task package-proxyApp
task publish-proxyApp
Note: skip steps #1 since, the app's image has already been pushed to dockerhub.
- Deploy the proxy app
kubectl create -f build/proxyApp/deploy.yaml
kubectl get po -o wide -l app=proxy-app
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
proxy-app-6c6bd559d7-rx7m2 2/2 Running 0 3m4s 10.244.0.254 eksabm-testbed1-core-dev-node-0 <none> <none>
- Deploy the proxy svc
kubectl create -f build/proxyApp/svc.yaml
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
proxy-svc ClusterIP 10.108.215.193 <none> 9092/TCP 3m52s
- Request to add all pod-urls to the proxy
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.4:9090"}' -w "\n"
proxy registered
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.2:9090"}' -w "\n"
proxy registered
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.3:9090"}' -w "\n"
proxy registered
- Request to serve proxy
curl 10.108.215.193:9092 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-gwnvq
curl 10.108.215.193:9092 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-gwnvq
Note: does not matter how many times you hit the proxy, request is redirected to same po [the first one all the time]. Can you spot the issue in the code in the branch 'setup-proxy'? What we have achieved though is to setup a proxy that redirects the request to the pod(s) behind it. Now we would like to move on to achieve load balancing!!
- Before we move on to load balancing, lets setup periodic health check of the urls we add behind our proxy
the proxy app's image 'hiteshpattanayak/proxy-app:2.0' has the health check feature setup. Also the image has been updated in 'proxyApp-deploy.yaml'.
Deploy the latest proxy app
kubectl apply -f build/proxyApp/deploy.yaml
deployment.apps/proxy-app configured
kubectl get po -l app=proxy-app
NAME READY STATUS RESTARTS AGE
proxy-app-6c6bd559d7-rx7m2 2/2 Running 0 29s
- Repeat the 'addUrl' requests again
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.4:9090"}' -w "\n"
proxy registered
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.3:9090"}' -w "\n"
proxy registered
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.2:9090"}' -w "\n"
proxy registered
## adding an invalid url
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://1.2.3.4:9090"}' -w "\n"
proxy registered
- check logs of the proxy app pod
kubectl logs -f proxy-app-555465b5b-xwz42 -c proxy-app
[GIN] 2023/04/16 - 12:33:08 | 200 | 420.363µs | 127.0.0.1 | POST "/addurl"
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
[GIN] 2023/04/16 - 12:33:14 | 200 | 1.198732ms | 127.0.0.1 | POST "/addurl"
16047-09+00 47:23:23 -> url(http://10.244.0.4:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.2:9090) is available.
[GIN] 2023/04/16 - 12:33:19 | 200 | 292.743µs | 127.0.0.1 | POST "/addurl"
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
16047-09+00 47:23:23 -> url(http://1.2.3.4:9090) is unavailable.
[GIN] 2023/04/16 - 12:33:25 | 200 | 337.648µs | 127.0.0.1 | POST "/addurl"
16047-09+00 47:23:23 -> url(http://10.244.0.4:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.2:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
16047-09+00 47:23:23 -> url(http://1.2.3.4:9090) is unavailable.
16047-09+00 47:23:23 -> url(http://10.244.0.4:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.2:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
16047-09+00 47:23:23 -> url(http://1.2.3.4:9090) is unavailable.
16047-09+00 47:23:23 -> url(http://10.244.0.4:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.2:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
16047-09+00 47:23:23 -> url(http://1.2.3.4:9090) is unavailable.
16047-09+00 47:23:23 -> url(http://10.244.0.4:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.2:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
16047-09+00 47:23:23 -> url(http://1.2.3.4:9090) is unavailable.
16047-09+00 47:23:23 -> url(http://10.244.0.4:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.2:9090) is available.
16047-09+00 47:23:23 -> url(http://10.244.0.3:9090) is available.
16047-09+00 47:23:23 -> url(http://1.2.3.4:9090) is unavailable.
-
Lets deploy
hiteshpattanayak/proxy-app:3.0
which contains implementation ofround robin
load balancing strategy. -
Deploy the latest proxy app
kubectl apply -f build/proxyApp/deploy.yaml
deployment.apps/proxy-app configured
- Repeat the 'addUrl' requests again, we do this because we do not have a persistent stirage as yet. All the urls are stored in-memory.
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.4:9090"}' -w "\n"
proxy registered
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.3:9090"}' -w "\n"
proxy registered
curl 10.108.215.193:9092/addurl --header "Content-Type: application/json" --request "POST" --data '{"url": "http://10.244.0.2:9090"}' -w "\n"
proxy registered
- Request to serve proxy now
curl 10.108.215.193:9092 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-vfnbx
curl 10.108.215.193:9092 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-hkp7w
curl 10.108.215.193:9092 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-gwnvq
curl 10.108.215.193:9092 -w "\n"
responding from a simple rest app with pod name: rest-simple-app-6c9dc776f5-vfnbx
Note: The load balancer redirects requests across pods in a round-robin manner.
What if we could redirect requests in a weighted fashion? Could set an url's (server's) capability to handle more requests/load and the load balancer could redirect request based on that? Lets find out in the next steps.