Spring boot Rest Endpoint is not accessible from Angular application in GKE within same pod

2/10/2019

I am running an angular app with backend rest service from spring boot on Kubernetes in Google Cloud. The angular app tries to consume data from spring rest endpoint like:

Spring boot endpoint: http://localhost:8080/app/getDetails

This endpoint works well as if I do port forward it runs. If I expose the endpoint explicitly from service using ingress, it is accessible too.

But it is not accessible from Angular application running in the same pod as different container hosted inside nginx. (Cors is enabled)

From console it shows :

GET http://localhost:8080/app/getDetails net::ERR_CONNECTION_REFUSED

Error: [object ProgressEvent]

Any idea how to get through this?

Here is my angular code

export class MyComponent implements OnInit {
  private url: string = "http://localhost:8080/app/getDetails";

  constructor(private httpClient: HttpClient) {}

  dtls: any;

  ngOnInit() {}

  public onClick() {
    this.httpClient.get(this.url).subscribe(
      results => {
        this.dtls = results;
      },
      (err: HttpErrorResponse) => {
        console.log("Error: " + err.error);
      }
    );
  }
}

Here is nginx.conf

upstream webapp {
        server 127.0.0.1:8080;
    }
server {
  listen 80;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

Deployment.yml

apiVersion: v1
kind: Pod
metadata:
  name: myapp-demo
  labels:
    app: myapp-dtls-demo
spec:
  containers:
  - name: myapp-ui
    image: myuser/myapp-kubernetes-demo
    ports:
    - name: ui-port
      containerPort: 80
  - name: myapp-api
    image: myuser/myapp-service-kubernetes-demo

Service.yml

apiVersion: v1
kind: Service
metadata:
  name: myapp-demo-service
spec:
  ports:
  - port: 4200
    targetPort: ui-port
    name: ui
    protocol: TCP   
  selector:
    app: myapp-football-demo
  type: LoadBalancer
-- ARINDAM BANERJEE
angular
google-kubernetes-engine
spring-boot

0 Answers