Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating Ingress service on kubernetes for connecting springboot [backend] to front end [angular]

I am trying to create two ingress service, one which will expose the frontend to internet and 2nd which will connect backend to frontend. This is in Minikube and I have the ingress addon enabled.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: angular-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: angular.test
    - http:
        paths:
          - path: /
            pathType: Prefix  
            backend:
              service:
                name: angular-service
                port:
                  number: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: spring-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: springboot.test
    - http:
        paths:
          - path: /exact/form-config/applicant/field?section=Additional%20Details&page-index=1&enabled-on=
            pathType: Exact
            backend:
              service:
                name: angular-service
                port:
                  number: 8080

I want to use the name of the backend host url in my angular application for springboot and I am want them to connect without using IP since IP are dynamic. Is this correct?

like image 505
DarkDead Avatar asked Oct 20 '25 04:10

DarkDead


1 Answers

Given you have a service for your Angular app like this:

apiVersion: v1
kind: Service
metadata:
  name: your-angular-app
  namespace: dev
spec:
  selector:
    app: your-angular-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      name: http

And a service for your Spring Boot app in the same namespace like this:

apiVersion: v1
kind: Service
metadata:
  name: your-spring-app
  namespace: dev
spec:
  selector:
    app: your-spring-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      name: http

Make sure that your deployments (or whatever you use to actually create your app instances in the cluster) have matching labels. Deployment example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-spring-app
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: your-spring-app # <- this must match the spec.selector.app in the service

Assuming that your Spring app offers all API endpoints with the /api prefix, you could use an Ingress like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: your-ingress
  namespace: dev
spec:
  rules:
    - host: your-site.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: your-angular-app
                port:
                 name: http
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: your-spring-app
                port:
                 name: http

In a cloud environment you would most likely need additional annotations on your Ingress like the Ingress class, but these information can be found in the Cloud provider's documentation.

like image 154
Times Avatar answered Oct 23 '25 01:10

Times