Skip to content

Latest commit

 

History

History
76 lines (59 loc) · 2.37 KB

6_discovery.adoc

File metadata and controls

76 lines (59 loc) · 2.37 KB

Step 6: Service Discovery & Load-balancing

Now we need two endpoints to experiment with as one needs to another another. In this case, our Spring Boot app will be calling the Node.js app.

And let’s also experiment with two namespaces to make it a wee bit more interesting.

$ kubectl create -f kubefiles/yourspace-namespace.yml

and then load a Node.js endpoint into that new namespace

$ cd hello/nodejs
# test it locally
$ npm start
$ curl localhost:8000
$ ctrl-c
# build the docker image and test it
$ docker build -t 9stepsawesome/mynode:v1 .
$ docker run -it -p 8000:8000 9stepsawesome/mynode:v1
$ curl $(minikube --profile 9steps ip):8000
$ ctrl-c
# deploy it into minikube/minishift
$ cd ../.. # to the main 9stepsawesome directory
$ kubectl create -f kubefiles/mynode-deployment.yml -n yourspace
$ kubectl create -f kubefiles/mynode-service.yml -n yourspace
$ kubectl get all -n yourspace

Now invoke the "callinganother" endpoint on the myboot service

$ curl $(minikube --profile 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}" -n myspace)/callinganother
Node Hello on mynode-7dc8bddd4d-spv5h 0

The magic is simply knowing the correct URL

    // <servicename>.<namespace>.svc.cluster.local
    String url = "http://mynode.yourspace.svc.cluster.local:8000/";

If you are within the same namespace then you can simply use

    String url = "http://mynode:8000/";

2 replicas of mynode

$ kubectl scale deployment/mynode --replicas=2 -n yourspace

and now invoke the "callinganother" again and again

$ curl $(minikube --profile 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}")/callinganother
Node Hello on mynode-7dc8bddd4d-spv5h 4
$ curl $(minikube --profile 9steps ip):$(kubectl get service/myboot -o jsonpath="{.spec.ports[*].nodePort}")/callinganother
Node Hello on mynode-7dc8bddd4d-6gb8j 1

If you wish to update the Node.js endpoint code, just remember the -n yourspace parameter

$ docker build -t 9stepsawesome/mynode:v1 .
$ kubectl delete pod -l app=mynode -n yourspace

The delete will cause another deployment based on the new image.

And if you wish to "switch" namespaces and avoid typing the -n yourspace all the time, just remember the kubectx and kubens tips mentioned way back in Step 1