{{announcement.body}}
{{announcement.title}}

Quickly Deploy REST Microservice in Kubernetes

DZone 's Guide to

Quickly Deploy REST Microservice in Kubernetes

This article will help you to start with minikube and let you easily deploy Java and SpringBoot microservice to Kubernetes environment.

· Microservices Zone ·
Free Resource

This technology blog is about how quickly we can create spring boot based microservice and then quickly and efficiently deploy it in the Kubernetes environment. There are different plugins available, one of them is fabric8 maven plugin where we used to deploy spring boot application to Kubernetes or Openshift environment with 'mvn fabric8: deploy'. Now it is rebranded to Eclipse Jkube. For more details check Eclipse foundation jkube Kubernetes-maven-plugin.

We have used this jkube plugin to deploy spring boot microservice application in Kubernetes.

The code is available in my personnel GitHub link. We will discuss important sections of this project. This example has been forked from fabric8-quickstarts and modified for Kubernetes using the Jkube plugin. We have used minikube which is a tool or utility to run single-node Kubernetes cluster on our laptop or desktop. Minikube is a gem for developers and students who don't have the luxury of the Kubernetes cluster. I found it very stable too and I have installed a minikube in Fedora 31.

1. Kubernetes-maven-plugin

2. cxf-spring-boot-starter-jaxrs dependency with undertow as underlying container.

3. deployment.yml with resource requests and limits. This is within jkube folder.

4. service.yml which creates a load balancer service to access the REST web services externally.

5. endpoint.xml which defines a CXF JAX-RS service and its implementation is defined at HelloServiceImpl.java.

6. In ./m2/settings.xml set following so that Maven resolve Jkube plugin goals. 

Java
 




x


1
 <pluginGroups>
2
   ---
3
        <pluginGroup>org.eclipse.jkube</pluginGroup>
4
   ---
5
 </pluginGroups>
6
 
          



So we covered important sections of the sample project.

Now let us test this in minikube.

1.  Start minikube. We can start minikube with profile testfuse. The profile helps us to have multiple instances of minikube. We have set the profile as restPOC.

2. Then we can create Kubernetes namespace 'testfuse' where we will deploy our application. Next, we set the context to 'testfuse' namespace so that we don't have to provide namespace while executing commands using kubectl utility. 

Java
 




x


 
1
[[chandrashekhar@localhost ~]$ minikube version
2
minikube version: v1.9.2
3
commit: 93af9c1e43cab9618e301bc9fa720c63d5efa393
4
[chandrashekhar@localhost ~]$ minikube start -p restPOC
5
[chandrashekhar@localhost ~]$ kubectl create ns testfuse
6
[chandrashekhar@localhost ~]$ kubectl config set-context $(kubectl config current-context) --namespace=testfuse
7
 
          



3.  Now go the location of pom.xml in the sample project and execute the following commands from the Linux terminal. Make sure minikube is installed. We would have to first point docker repository to minikube one with profile restPOC.

Java
 




x


1
[chandrashekhar@localhost spring-boot-cxf-jaxrs-minikube]$ eval  $(minikube -p restPOC docker-env)
2
[chandrashekhar@localhost spring-boot-cxf-jaxrs-minikube]$ mvn k8s:build
3
[chandrashekhar@localhost spring-boot-cxf-jaxrs-minikube]$ mvn k8s:resource
4
[chandrashekhar@localhost spring-boot-cxf-jaxrs-minikube]$ mvn k8s:deploy



4.  Check pods and services created in Kubernetes. Check external-IP is pending.

Java
 




x


 
1
[chandrashekhar@localhost ~]$ kubectl get deployment
2
NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
3
spring-boot-cxf-jaxrs-xml   1/1     1            1           11m
4
[chandrashekhar@localhost ~]$ 
5
 
          
6
[chandrashekhar@localhost ~]$ kubectl get pods
7
NAME                                         READY   STATUS    RESTARTS   AGE
8
spring-boot-cxf-jaxrs-xml-6696b75b95-kbhf7   1/1     Running   0          17m
9
[chandrashekhar@localhost ~]$
10
[chandrashekhar@localhost ~]$ kubectl get service
11
NAME                        TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
12
kubernetes                  ClusterIP      10.96.0.1       <none>          443/TCP          2d3h
13
spring-boot-cxf-jaxrs-xml   LoadBalancer   10.107.31.213   <pending>    8080:30206/TCP   2d3h



5. To access this loadbalancer service, we have to enable tunnel in minikube so that it accepts external requests. Run this from a different terminal and keep it running. Note that we have provided profile restPOC.

Java
 




x


1
[chandrashekhar@localhost ~]$ minikube tunnel -p restPOC
2
 
          



6.  Once the tunnel is created. Check external-ip. Finally access service with external-ip.

Java
 




x


 
1
 
          
2
[chandrashekhar@localhost ~]$ kubectl get service
3
NAME                        TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
4
kubernetes                  ClusterIP      10.96.0.1       <none>          443/TCP          2d3h
5
spring-boot-cxf-jaxrs-xml   LoadBalancer   10.107.31.213   10.107.31.213   8080:30206/TCP   2d3h
6
[chandrashekhar@localhost ~]$ 
7
[chandrashekhar@localhost ~]$ curl -v http://10.107.31.213:8080/services/helloservice/sayHello/FIS
8
*   Trying 10.107.31.213:8080...
9
* TCP_NODELAY set
10
* Connected to 10.107.31.213 (10.107.31.213) port 8080 (#0)
11
> GET /services/helloservice/sayHello/FIS HTTP/1.1
12
> Host: 10.107.31.213:8080
13
> User-Agent: curl/7.66.0
14
> Accept: */*
15
> 
16
* Mark bundle as not supporting multiuse
17
< HTTP/1.1 200 OK
18
< Connection: keep-alive
19
< Content-Type: text/plain
20
< Content-Length: 49
21
< X-Application-Context: application
22
< Date: Fri, 01 May 2020 18:11:39 GMT
23
< 
24
* Connection #0 to host 10.107.31.213 left intact
25
Hello FIS, Welcome to CXF RS Spring Boot World!!!



7.  Finally undeploy using maven command k8s:undeploy from the location of pom.xml in the sample project. We see that all Kubernetes resources are cleanly removed. 

Java
 




x



1
[chandrashekhar@localhost spring-boot-cxf-jaxrs-minikube]$ mvn k8s:undeploy
2
[chandrashekhar@localhost ~]$ kubectl get deployment -n testfuse
3
No resources found in testfuse namespace.
4
[chandrashekhar@localhost ~]$ kubectl get pods -n testfuse
5
No resources found in testfuse namespace.
6
[chandrashekhar@localhost ~]$ 



That is it, I believe this article will help you to start with minikube and let you easily deploy Java and SpringBoot microservice to Kubernetes environment.

Topics:
cxf ,kubernetes ,microservice ,minikube ,rest ,springboot ,tutorial ,webservice

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}