Thursday, June 17, 2021

Calling one springboot microservice from another in kubernetes and using docker as container

 In this example we will try to undestand how springboot microservice application communicate with each other in kubernetes.

We had taken the Docker images which we had crated in below blogs

http://siddharathadhumale.blogspot.com/2021/06/calling-one-microservice-from-another.html

note:- You can download the code from the given github location.
consumer :-
https://github.com/shdhumale/springboot-docker-consumer.git
Producer:-
https://github.com/shdhumale/springboot-docker-producer.git
DockerHub:- shdhumale/springboot-docker-consumer and shdhumale/shdhumale/springboot-docker-producer

As we are having window OS we are using Docker Desktop and in build Kubernetes. If you want you can also use Minikube.
Please make sure to have this configuration of your docker desktop.

Now create follwing three yamlfile

1- siddhu-namespace.yml

1
2
3
4
kind: Namespace
apiVersion: v1
metadata:
  name: springboot-application

2- springboot-docker-consumer.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-docker-consumer
  namespace: springboot-application
spec:
  selector:
    matchLabels:
      component: springboot-docker-consumer
  template:
    metadata:
      labels:
        component: springboot-docker-consumer
    spec:
      containers:
      - name: springboot-docker-consumer
        image: shdhumale/springboot-docker-consumer:latest
        env:
        - name: discovery.type
          value: single-node
        ports:
        - containerPort: 8090
          name: http
          protocol: TCP     
 
---
 
apiVersion: v1
kind: Service
metadata:
  name: siddhuconsumer
  namespace: springboot-application
  labels:
    service: springboot-docker-consumer
spec:
  type: NodePort
  selector:
    component: springboot-docker-consumer
  ports:
  - port: 8090
    targetPort: 8090

3-springboot-docker-producer.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-docker-producer
  namespace: springboot-application
spec:
  selector:
    matchLabels:
      component: springboot-docker-producer
  template:
    metadata:
      labels:
        component: springboot-docker-producer
    spec:
      containers:
      - name: springboot-docker-producer
        image: shdhumale/springboot-docker-producer:latest
        env:
        - name: discovery.type
          value: single-node
        ports:
        - containerPort: 8091
          name: http
          protocol: TCP     
 
---
 
apiVersion: v1
kind: Service
metadata:
  name: siddhuproducer
  namespace: springboot-application
  labels:
    service: springboot-docker-producer
spec:
  type: NodePort
  selector:
    component: springboot-docker-producer
  ports:
  - port: 8091
    targetPort: 8091

As you know in our code for microservice we have used resttemplate and make a call using url as
http://siddhuproducer:8091

So we need to make sure that our Producer service name should be siddhuproducer

execute all the files in sequence

1- C:\STS-Workspace\springboot-docker-consumer\yaml>kubectl create -f siddhu-namespace.yml
2- C:\STS-Workspace\springboot-docker-consumer\yaml>kubectl create -f springboot-docker-producer.yaml
3- C:\STS-Workspace\springboot-docker-consumer\yaml>kubectl create -f springboot-docker-consumer.yaml

check both the pod is running properly

1
2
3
4
C:\STS-Workspace\springboot-docker-consumer\yaml>kubectl get pods --show-labels --namespace springboot-application -o wide
NAME                                          READY   STATUS    RESTARTS   AGE     IP         NODE             NOMINATED NODE   READINESS GATES   LABELS
springboot-docker-consumer-69cd4b98f-42ngk    1/1     Running   0          37s     10.1.0.9   docker-desktop   <none>           <none>            component=springboot-docker-consumer,pod-template-hash=69cd4b98f
springboot-docker-producer-6866686c74-zmtng   1/1     Running   0          8m12s   10.1.0.8   docker-desktop   <none>           <none>            component=springboot-docker-producer,pod-template-hash=6866686c74

Now exposed both the port using port-forward

C:\STS-Workspace\springboot-docker-consumer\yaml>kubectl port-forward springboot-docker-producer-6866686c74-zmtng 8091:8091 -n springboot-application
Forwarding from 127.0.0.1:8091 -> 8091
Forwarding from [::1]:8091 -> 8091
Handling connection for 8091

C:\STS-Workspace\springboot-docker-consumer\yaml>kubectl port-forward springboot-docker-consumer-69cd4b98f-42ngk 8090:8090 -n springboot-application
Forwarding from 127.0.0.1:8090 -> 8090
Forwarding from [::1]:8090 -> 8090
Handling connection for 8090

Open the browser and hit the url you will be able to see the producer individual working with below output

and consumer working with output from producer

Note :- I had uploaded the yml file on below locations

https://github.com/shdhumale/springboot-docker-consumer.git

No comments: