jQuery call REST API not working in Docker/Kubernetes

2/8/2020

This is a Java Spring boot REST API application, using an index.html to present the UI web page to user. When the index.html is displayed, it would trigger the logic from the Javascript/jQuery to make a REST api call(coded as below) to the backend service in the Java controller class to get 2 random generated numbers:

$.ajax({
    url: "http://localhost:8080/multiplications/random"

The program is working fine when run it as a Spring Boot app in Eclipse!

However, it's not working after I used the .jar file to build a Docker image file then deployed it using Kubernetes/minikube(I'm new to Docker/Kubernetes).

here's the dockfile to build the image file using the .jar:

FROM openjdk:latest

ADD target/social-multiplication-v3-0.3.0-SNAPSHOT.jar app.jar

ENTRYPOINT ["java","-jar","app.jar"]

EXPOSE 8080

Here's the deployment.yaml file:

---
kind: Service
apiVersion: v1
metadata:
  name: multiplicationservice
spec:
  selector: 
      app: multiplication
  ports:
     - protocol: "TCP"
       port:  80
       targetPort:  8080
       nodePort:  30001
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mdeploy
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: multiplication
  template:
    metadata:
      labels:
        app: multiplication
    spec:
      containers:
      - name: multiplication
        image: huxianjun/multiplication
        ports:
        - containerPort: 80

and the IP address of the host where the application being deployed in Kubernetes:

$ minikube ip
192.168.99.101

At the end, I can get to the index.html page from browser by folllowing URL:

http://192.168.99.101:30001/

The page is being displayed as expected - What NOT working is, the following REST api call didn't occur thus the 2 numbers not returned from the call and displayed on the page:

$.ajax({
    url: "http://localhost:8080/multiplications/random"

My guess is, is it caused by the 'localhost' & the port'8080' not aligned with those port# defined in the deployment.yaml file? or even something conflict to the 'EXPOSE 8080' in the docfile?

-- Roy Hu
docker
java
kubernetes
rest
spring-boot

2 Answers

2/8/2020

Try run sudo lsof -i :8080, if you use linux. It'll show all your available ports. If you don't see 8080 port, your application's port aren't available and visible for your localhost. That's because Docker containers are "closed/isolated" for all external processes and files. Moreover, EXPOSE 8080 instruction in Dockerfile is not enough. Try docker run -p 8080:8080 YOUR_CREATED_IMAGE_NAME. It will build redirection from docker container localhost:8080 to Your localhost:8080

-- Август
Source: StackOverflow

2/8/2020

In your case, you are calling the $.ajax command from your browser which is in your host machine, hence, those API calls will be sent from your local machine but not within your docker container.

To solve the problem, you can update the URL to be using http://192.168.99.101:30001/ like this

$.ajax({
    url: "http://192.168.99.101:30001/multiplications/random"
-- Nguyen Lam Phuc
Source: StackOverflow