Connect back-end with front-end over rest api call running on kubernetes

9/28/2019

I have a two tier application. The frontend calls the webapi layer through simple http rest call http://mywebapi:5000/ My working docker compose code is below and the application works

version: '3'

services:
  webfrontend:
    image: webfrontend
    build: ./nodeexpress-alibaba-ci-tutorial
    ports:
      - "3000:3000"
    networks: 
      - my-shared-network  

  mywebapi:
    image: mywebapi
    build: ./dotnetcorewebapi-alibaba-ci-tutorial
    ports:
      - "5000:5000"
    networks: 
      - my-shared-network  
networks:
  my-shared-network: {}      

Now I'm trying to get this to work on kubernetes. I have created two deployments and two services-loadbalancer for webfrontend and clusterip for mywebapi

But, after deploying, I find that the data from mywebapi is not reaching the frontend. I can view the frontend on the browser through the load balancer public ip.

Mywebapi deployment yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '3'
  creationTimestamp: '2019-09-28T13:31:32Z'
  generation: 3
  labels:
    app: mywebapi
    tier: backend
  name: mywebapi
  namespace: default
  resourceVersion: '1047268388'
  selfLink: /apis/apps/v1beta2/namespaces/default/deployments/mywebapi
  uid: 493ab5e0-e1f4-11e9-9a64-d63fe9981162
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: mywebapi
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        aliyun.kubernetes.io/deploy-timestamp: '2019-09-28T14:36:01Z'
      labels:
        app: mywebapi
    spec:
      containers:
        - image: >-
            registry-intl-vpc.ap-southeast-1.aliyuncs.com/devopsci-t/mywebapi:1.0
          imagePullPolicy: Always
          name: mywebapi
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 2
  conditions:
    - lastTransitionTime: '2019-09-28T14:51:18Z'
      lastUpdateTime: '2019-09-28T14:51:18Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2019-09-28T14:49:55Z'
      lastUpdateTime: '2019-09-28T14:51:19Z'
      message: ReplicaSet "mywebapi-84cf98fb4f" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 3
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2

Mywebapi service yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: '2019-09-28T13:31:33Z'
  name: mywebapi-svc
  namespace: default
  resourceVersion: '1047557879'
  selfLink: /api/v1/namespaces/default/services/mywebapi-svc
  uid: 49e21207-e1f4-11e9-9a64-d63fe9981162
spec:
  clusterIP: None
  ports:
    - name: mywebapiport
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: mywebapi
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

I even tried updating the http rest call url to http://mywebapi-svc:5000/ but still does not work. In the webfrontend pod logs I find the below error

Got error: getaddrinfo ENOTFOUND mywebapi-svc mywebapi-svc:5000

Sincerely appreciate all help

Thanks

............................................................

Update..

Changed mywebapi-svc to disable headless. Current YAML is below. Problem still the same..

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: '2019-09-29T15:21:44Z'
  name: mywebapi-svc
  namespace: default
  resourceVersion: '667545270'
  selfLink: /api/v1/namespaces/default/services/mywebapi-svc
  uid: d84503ee-e2cc-11e9-93ec-a65f0b53b1fa
spec:
  clusterIP: 172.19.0.74
  ports:
    - name: mywebapiport
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: mywebapi-default
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
-- Arnab
alibaba-cloud
kubernetes

1 Answer

9/28/2019

It's because you're using headless service

I guess you don't need a headless service for this job. Because DNS server won't return a single IP address if there are more than one pod match by the label selector. Remove the spec.ClusterIP field from your service.

apiVersion: v1
kind: Service
metadata:
  name: mywebapi-svc
  namespace: default
spec:
  # clusterIP: None <-- remove
  ports:
    - name: mywebapiport
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: mywebapi
  type: ClusterIP

Now you can call at http://service-name.namespace-name.svc:port endpoint from your front-end. It should work.

In case your webapi is stateful, then you should use statefulset instead of deployment.

-- Kamol Hasan
Source: StackOverflow