Service Discovery with Java and Database application in Kubernetes

This blog will show how a simple Java application can talk to a database using service discovery in Kubernetes.

Service Discovery with Java and Database application in DC/OS explains why service discovery is an important aspect for a multi-container application. That blog also explained how this can be done for DC/OS.

Let’s see how this can be accomplished in Kubernetes with a single instance of application server and database server. This blog will use WildFly for application server and Couchbase for database.

This blog will use the following main steps:

  • Start Kubernetes one-node cluster
  • Kubernetes application definition
  • Deploy the application
  • Access the application

Start Kubernetes Cluster

Minikube is the easiest way to start a one-node Kubernetes cluster in a VM on your laptop. The binary needs to be downloaded first and then installed.

Complete installation instructions are available at

The latest release can be installed on OSX as:

curl -Lo minikube \
&& chmod +x minikube

It also requires kubectl to be installed. Installing and Setting up kubectl provide detailed instructions on how to setup kubectl. On OSX, it can be installed as:

curl -LO$(curl -s \
  && chmod +x ./kubectl

Now, start the cluster as:

minikube start
Starting local Kubernetes cluster...
Starting VM...
Downloading Minikube ISO
 88.71 MB / 88.71 MB [==============================================] 100.00% 0s
SSH-ing files into VM...
Setting up certs...
Starting cluster components...
Connecting to cluster...
Setting up kubeconfig...
Kubectl is now configured to use the cluster.

The kubectl version command shows more details about the kubectl client and minikube server version:

kubectl version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.4", GitCommit:"7243c69eb523aa4377bce883e7c0dd76b84709a1", GitTreeState:"clean", BuildDate:"2017-03-07T23:53:09Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.3", GitCommit:"029c3a408176b55c30846f0faedf56aae5992e9b", GitTreeState:"clean", BuildDate:"1970-01-01T00:00:00Z", GoVersion:"go1.7.3", Compiler:"gc", Platform:"linux/amd64"}

More details about the cluster can be obtained using the kubectl cluster-info command:

Kubernetes master is running at
KubeDNS is running at
kubernetes-dashboard is running at
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Kubernetes Application Definition

Application definition is defined at It consists of:

  • A Couchbase service
  • Couchbase replica set with a single pod
  • A WildFly replica set with a single pod
apiVersion: v1
kind: Service
  name: couchbase-service
    app: couchbase-rs-pod
    - name: admin
      port: 8091
    - name: views
      port: 8092
    - name: query
      port: 8093
    - name: memcached
      port: 11210
apiVersion: extensions/v1beta1
kind: ReplicaSet
  name: couchbase-rs
  replicas: 1
        app: couchbase-rs-pod
      - name: couchbase
        image: arungupta/couchbase:travel
        - containerPort: 8091
        - containerPort: 8092
        - containerPort: 8093
        - containerPort: 11210
apiVersion: extensions/v1beta1
kind: ReplicaSet
  name: wildfly-rs
    name: wildfly
  replicas: 1
        name: wildfly
      - name: wildfly-rs-pod
        image: arungupta/wildfly-couchbase-javaee:travel
        - name: COUCHBASE_URI
          value: couchbase-service
        - containerPort: 8080

The key part is where the value of the
COUCHBASE_URI environment variable is name of the Couchbase service. This allows the application deployed in WildFly to dynamically discovery the service and communicate with the database.

arungupta/couchbase:travel Docker image is created using

arungupta/wildfly-couchbase-javaee:travel Docker image is created using

Java EE application waits for database initialization to be complete before it starts querying the database. This can be seen at

Deploy Application

This application can be deployed as:

kubectl create -f ~/workspaces/kubernetes-java-sample/service-discovery.yml

The list of service and replica set can be shown using the command kubectl get svc,rs:

NAME                    CLUSTER-IP   EXTERNAL-IP   PORT(S)                                AGE
svc/couchbase-service    <none>        8091/TCP,8092/TCP,8093/TCP,11210/TCP   27m
svc/kubernetes     <none>        443/TCP                                1h
svc/wildfly-rs   <none>        8080/TCP                               21m
rs/couchbase-rs   1         1         1         27m
rs/wildfly-rs     1         1         1         27m

Logs for the single replica of Couchbase can be obtained using the command kubectl logs rs/couchbase-rs:

++ set -m
++ sleep 25
++ / couchbase-server
Starting Couchbase Server -- Web UI available at http://<ip>:8091 and logs available in /opt/couchbase/var/lib/couchbase/logs
++ curl -v -X POST -d memoryQuota=300 -d indexMemoryQuota=300
. . .
++ echo 'Type: '
++ '[' '' = WORKER ']'
++ fg 1
/ couchbase-server

Logs for the WildFly replica set can be seen using the command kubectl logs rs/wildfly-rs:

  JBoss Bootstrap Environment
  JBOSS_HOME: /opt/jboss/wildfly
. . .
06:32:08,537 INFO  [com.couchbase.client.core.node.Node] (cb-io-1-1) Connected to Node couchbase-service
06:32:09,262 INFO  [com.couchbase.client.core.config.ConfigurationProvider] (cb-computations-3) Opened bucket travel-sample
06:32:09,366 INFO  [stdout] (ServerService Thread Pool -- 65) Sleeping for 3 secs ...
06:32:12,369 INFO  [stdout] (ServerService Thread Pool -- 65) Bucket found!
06:32:14,194 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 65) RESTEASY002225: Deploying class org.couchbase.sample.javaee.MyApplication
06:32:14,195 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 65) RESTEASY002200: Adding class resource org.couchbase.sample.javaee.AirlineResource from Application class org.couchbase.sample.javaee.MyApplication
06:32:14,310 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 65) WFLYUT0021: Registered web context: /airlines
06:32:14,376 INFO  [] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "airlines.war" (runtime-name : "airlines.war")
06:32:14,704 INFO  [] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on
06:32:14,704 INFO  [] (Controller Boot Thread) WFLYSRV0051: Admin console listening on
06:32:14,705 INFO  [] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 29470ms - Started 443 of 691 services (404 services are lazy, passive or on-demand)

Access Application

The kubectl proxy command starts a proxy to the Kubernetes API server. Let’s start a Kubernetes proxy to access our application:

kubectl proxy
Starting to serve on

Expose the WildFly replica set as a service using:

kubectl expose --name=wildfly-service rs/wildfly-rs

The list of services can be seen again using kubectl get svc command:

kubectl get svc
NAME                CLUSTER-IP   EXTERNAL-IP   PORT(S)                                AGE
couchbase-service    <none>        8091/TCP,8092/TCP,8093/TCP,11210/TCP   41m
kubernetes     <none>        443/TCP                                1h
wildfly-service   <none>        8080/TCP                               5s

Now, the application is accessible at:

curl http://localhost:8001/api/v1/proxy/namespaces/default/services/wildfly-service/airlines/resources/airline

A formatted output looks like:

    "travel-sample": {
      "country": "United States",
      "iata": "Q5",
      "callsign": "MILE-AIR",
      "name": "40-Mile Air",
      "icao": "MLA",
      "id": 10,
      "type": "airline"
    "travel-sample": {
      "country": "United States",
      "iata": "TQ",
. . .
     "name": "Airlinair",
      "icao": "RLA",
      "id": 1203,
      "type": "airline"

Now, new pods may be added as part of Couchbase service by scaling the replica set. Existing pods may be terminated or get rescheduled. But the Java EE application will continue to access the database service using the logical name.

This blog showed how a simple Java application can talk to a database using service discovery in Kubernetes.

For further information check out:

Arun Gupta

Arun is a technology enthusiast, avid runner, author of a best-selling book, globe trotter, a community guy, Java Champion, JavaOne Rockstar, JUG Leader, Minecraft Modder, Devoxx4Kids-er, and a Red Hatter.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button