-1

I have a kubernetes cluster having two deployments ui-service-app and user-service-app. Both of the deployments are exposed through Cluster IP services namely ui-service-svc and user-service-svc. In addition there is a Ingress for accessing both of my applications inside those deployments from outside the cluster.

Now I want to make a api call from my application inside ui-service-app to user-service-app. Currently I am using the ingress-ip/user to do so. But there should be some way to do this internally?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-service-app
  labels:
    app: user-service-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: user-service-app
  template:
    metadata:
      labels:
        app: user-service-app
    spec:
      containers:
      - name: user-service-app
        image: <MY-IMAGE-URL>
        imagePullPolicy: Always
        ports:
        - containerPort: 3000
        livenessProbe:
          httpGet:
            path: /ping
            port: 3000
        readinessProbe:
          httpGet:
            path: /ping
            port: 3000
          
          

---
apiVersion: "v1"
kind: "Service"
metadata:
  name: "user-service-svc"
  namespace: "default"
  labels:
    app: "user-service-app"
spec:
  type: "ClusterIP"
  selector:
    app: "user-service-app"
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 3000

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ui-service-app
  labels:
    app: ui-service-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ui-service-app
  template:
    metadata:
      labels:
        app: ui-service-app
    spec:
      containers:
      - name: ui-service-app
        image: <MY-IMAGE-URL>
        imagePullPolicy: Always
        ports:
        - containerPort: 3000

---
apiVersion: "v1"
kind: "Service"
metadata:
  name: "ui-service-svc"
  namespace: "default"
  labels:
    app: "ui-service-app"
spec:
  type: "ClusterIP"
  selector:
    app: "ui-service-app"
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 3000
  


---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: awesome-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  defaultBackend:
    service:
      name: ui-service-svc
      port:
        number: 80
  rules:
  - http:
      paths:      
      - path: /login
        pathType: Prefix
        backend:
          service:
            name: ui-service-svc
            port:
              number: 80
      - path: /user(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: user-service-svc
            port:
              number: 80

UPDATE 1: THIS IS THE ERROR PAGE WHEN I CHANGE THE URL IN REACT APP TO HTTP://USER-SERVICE-SVC

1 Answers1

1

Use the service name of the associated service.

From any other pod in the same namespace, the hostname user-service-svc will map to the Service you've defined, so http://user-service-svc would connect you to the web server of the user-service-app Deployment (no port specified, because your Service is mapping port 80 to container port 3000).

From another namespace, you can use the hostname <service>.<namespace>.svc.cluster.local, but that's not relevant to what you're doing here.

See the Service documentation for more details.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • I have tried it. It isn't working. http://user-service-svc gives me network error. – Yashvardhan Nathawat Jun 16 '22 at 06:08
  • Basically my ui-service-app is a react based app which is a simple UI for my application. There are buttons through which I register user and let them login. This is done by api calls to the user-service-app. But when I use the http://ingress-ip/user as the api call for registering or loging the user in my react app, everything works fine. But when I change this to http://user-service-svc then I get network error. Adding the screenshot of the error to my question now. @Iarsks – Yashvardhan Nathawat Jun 16 '22 at 06:19