Cannot connect to service of it's own from inside pod or from other pods on Kubernetes

125 Views Asked by At

What happened? I am deploying a web service on Kubernetes with there major components

  1. Web-UI
  2. API
  3. Database

Cloud: AKS

Exposed each service using Kubernetes service at the desired port number but except API rest two services are accessible.

API deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mind-api
  namespace: mind-app
spec:
  selector:
    matchLabels:
      app: mind-api
  replicas: 1
  template:
    metadata:
      labels:
        app: mind-api
    spec:
      containers:
      - name: mind-api
        image: multiscale/mind-airflow:1.1.3
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 4000
        env:
        - name: DB_HOST
          value: #####
        - name: DB_NAME
          value: #####
        - name: DB_USER
          value: #####
        - name: DB_PORT
          value: "27017"
        - name: SOURCE_MOUNTED_DRIVE
          value: "/mind-data"
        - name: MIND_HOME
          value: "/mind-data"
        volumeMounts:
            - name: azure-mind-vol
              mountPath: /mind-data
      volumes:
        - name: azure-mind-vol
          persistentVolumeClaim:
            claimName: mind-pvc
      imagePullSecrets:
        - name: regcred

API Service

apiVersion: v1
kind: Service
metadata:
  name: web-api-service
  namespace: mind-app
  labels:
    app: mind-api
spec:
  selector:
    app: mind-api
  ports:
    - protocol: "TCP"
      port: 8086
      targetPort: 4000
  type: LoadBalancer


root@mindairflow:~/mind-api# kubectl get svc
NAME              TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)           AGE
mongo             LoadBalancer   10.0.176.106   ######   27017:31062/TCP   15d
web-api-service   LoadBalancer   10.0.171.39    #####    8086:30571/TCP    41h
web-ui-service    LoadBalancer   10.0.142.99    ######    8084:30909/TCP    16d

From API pod

/api # curl -i web-api-service:8086
curl: (7) Failed to connect to web-api-service port 8086 after 5 ms: Couldn't connect to server
/api # curl -i 10.0.171.39:8086
curl: (7) Failed to connect to 10.0.171.39 port 8086 after 0 ms: Couldn't connect to server
/api #

But accessible as localhost

/api # curl -i localhost:4000
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers,X-Access-Token,XKey,Authorization
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Content-Length: 17
ETag: W/"11-uHyBxMeNlIJAQfw7PJuBJSmOJ9E"
Date: Sun, 13 Aug 2023 06:40:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Server is running/api #

If accessing UI service from API pod is working fine

/api # curl web-ui-service:8084
<!doctype html>
<html lang="en">
<head>
<!-- <link href="https://fonts.googleapis.com/icon?family=Material+Icons|Material+Icons+Outlined" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700|Roboto:100,300,400,500,700" rel="stylesheet"> -->

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content Type" content="text/html; charset=UTF-8" />


  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="0">

  <title>MIND Web</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.png">
</head>
<body>

  <app-root></app-root>
  <script id="xo-config" type="text/javascript"></script>
<script type="text/javascript" src="runtime.js"></script><script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="styles.js"></script><script type="text/javascript" src="scripts.js"></script><script type="text/javascript" src="vendor.js"></script><script type="text/javascript" src="main.js"></script></body>
</html>
/api #

I tried with other service type as well like CLusterIP and NodePort but no luck.

Dockerfile

FROM node:16.15.1-alpine3.16

RUN apk add git
WORKDIR /
COPY . /api
WORKDIR /api
RUN npm install --legacy-peer-deps
RUN apk add vim
RUN npm install -g forever

# Environmnet variables
ENV INTERNAL_IP="localhost" \
    PUBLIC_IP="http://localhost:4000" \
    DB_HOST="localhost" \
    DB_PORT=27017 \
    DB_NAME="mongo" \
    DB_USER="<user name>" \
    DB_PASSWORD="" \
    AUTH_DB="<db name>" \
    SERVER_PORT=4000 \
    SERVER_HOST="localhost" \
    SUPER_ADMIN_USER="<email-address>" \
    MAX_LICENSE_COUNT=100 \
    SHARED_DIR_PATH="<folderpath>" \
    PLATFORM_DATA_FOLDER_NAME="<foldername>" \
    HASH_SECRET_STRING="<secretname>"

EXPOSE 4000
RUN chmod 777 -R /api
RUN chmod +x ./entrypoint.sh
#RUN chmod 755 ./entrypoint.sh
ENTRYPOINT [ "./entrypoint.sh" ]

0

There are 0 best solutions below