Tomislav Mikulin
Tomislav Mikulin

Reputation: 5936

server configuration in java apps for services in kubernetes

I've got a problem concerning configuration settings in a couple of java apps.

I'm using minikube version: v0.21.0 and docker for mac.

I dockerized all the apps, but the communication part between the apps is what is troubling me.

Since I've got that the applications need to communicate together through the server.url option (in the application.propertions for java) I'm not sure how to make that work.

The main app has also an angular frontend, in which I have to define the url of the two server java apps.

I know that apps in k8 communicate through services (with their virtual IP), and I'v done that for each app.

But in the front end app (config.js)

angular.module('app').constant("CONFIG", {
      app1_server_URL: "http://app1:8080/app1/",
      app2_server_URL: "http://app2:8095/"
});

So, when I get to the front end page, when I try to log in, through the chrome console, it tries to go to http://app1:8080/app1/, but is says the app can't be found? Is seems that the DNS resolving cant find the service named app1? Is resolves the DNS (when I look through my browser) but it looks for http://app1:8080/app1/ outside of the cluster?

And if I make a service for all my apps that looks something like this:

apiVersion: v1
kind: Service
metadata:
  name: app1
  labels:
    app: app1
spec:
  selector:
    app: app1
  ports:
  - protocol: TCP
    port: 8080
    name: http

Can I write in the java application properties that the url can be reached like this:

server.url=http://app1

I'm not sure what I'm doing wrong so any advice would be helpful.

Tnx, Tom

Upvotes: 0

Views: 135

Answers (1)

Jose Armesto
Jose Armesto

Reputation: 13749

Your angular application is a frontend application. That means that the code gets executed on the browser of the user. So requests from the angular application to the java applications come from outside the Kubernetes cluster.

But you have created a kubernetes Service without specifying the ServiceType, which will create a ClusterIP kind of Service. This kind of service won't allow requests coming from outside the cluster, like requests from a frontend application. You need a way to allow traffic coming from the outside of the cluster. There are several ways of achieving that, depending on your needs. The NodePort service type exposes the service on each node’s IP at a static port.

Let's assume that you create the following Service

apiVersion: v1
kind: Service
metadata:
  name: app1
  labels:
    app: app1
spec:
  type: NodePort
  selector:
    app: app1
  ports:
  - protocol: TCP
    targetPort: 80 # Port exposed by Pods behind the Service
    port: 8080 # Port used when connecting from inside the cluster
    nodePort: 30140 # Port used when connecting from outside the cluster
    name: http

That way, your angular application could send requests to http://192.168.99.100:30140, or whatever your minikube ip's and whatever port has been allocated as the NodePort (30140 in this example).

There are other ways of getting traffic into the cluster apart from Services, like using the Ingress object.

Upvotes: 1

Related Questions