Search code examples
kuberneteskubernetes-ingressk3s

Call endpoint of Pod A from Pod B when running on the same node


I have created 2 services and for each of the service there is a corresponding deployment. All these are in the same PC. After applying the YAML, the services and deployments are running properly.

sudo npm kubectl apply -f deployment.yaml

Now, if I try to curl to the ingress IP address, then I get 404 page not found as the response.

sudo kubectl get ingress -o wide
sudo curl <IP address of ingress>

If I try to curl to the IP address of the employee service or employee deployment, it gives that the connection was refused after waiting for sometime. Which ever port I mentioned for the employee service and deployment I try, I always get error instead of the proper response.

Can someone from the community please assist. I need to call the /emplist endpoint of myshop pod from employee pod.

myshop.js

const express = require('express')
const app = express()
const port = 3000

app.get('/emplist', (req, res) => {
    res.status(200).send( empListObj )
});

app.listen(port, '0.0.0.0', () => {
  console.log(`Listening on port ${port}`)
})

employee.js

const axios = require('axios').default;

const express = require('express')
const app = express()
const port = 3001

app.get('/', (req, res) => {
  
  axios({
      method: 'get',
      url: 'http://myshopservice/emplist'
    })
    .then(function (response) {
        res.status(200).send(response.data.empList)
    })
    .catch(function (error) {
        res.status(200).send(`Error because ${error}`);
    })
})

app.listen(port, '0.0.0.0', () => {
  console.log(`Listening on port ${port}`)
})

Now the corresponding deployment.yaml is:

---
apiVersion: networking.k8s.io/v1 
kind: Ingress 
metadata: 
  name: myshop-ingress 
  annotations: 
    nginx.ingress.kubernetes.io/rewrite-target: / 
spec: 
  rules: 
  - host: myshop.com 
    http: 
      paths: 
      - path: / 
        pathType: Prefix 
        backend: 
          service: 
            name: myshopservice 
            port: 
              number: 4000 

---

apiVersion: v1
kind: Service
metadata:
  name: myshopservice
spec:
  selector:
    app: myshop
  ports:
    - protocol: "TCP"
      port: 4000
      targetPort: 3000
      nodePort: 30000
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myshop
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myshop
  template:
    metadata:
      labels:
        app: myshop
    spec:
      containers:
        - name: myshop
          image: mydockerhubid/myshop:1
          ports:
            - containerPort: 3000

---
apiVersion: v1
kind: Service
metadata:
  name: employeeservice
spec:
  selector:
    app: employee
  ports:
    - protocol: "TCP"
      port: 4001
      targetPort: 3001
      nodePort: 30001
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee
spec:
  replicas: 1
  selector:
    matchLabels:
      app: employee
  template:
    metadata:
      labels:
        app: employee
    spec:
      containers:
        - name: hud
          image: mydockerhubid/employee:1
          ports:
            - containerPort: 3001

---

Solution

  • Looks like there is issue with ingress mainly. i would suggest first to check the services status if it's running or not at <Loadbalancer IP:Port/path> (Both services)

    Your app is listening on one specific path but you are not requesting that path in curl. If you want to do it that way you can also do it however i would suggest try simple way first and thn play with route/path based routing.

    Plus your ingress is sending requests at myshop service but it only listening or have route : /emplist

    So try once ingress by removing annotation and host as you are on local.

    If you want to keep host you have to add entry locally into /etc/hosts for host and ingress IP mapping and you should be using the or try curl on domain instead ingress IP then.

    Are you running any ingress controller? Nginx or so? : https://kubernetes.github.io/ingress-nginx/

    ---
    apiVersion: networking.k8s.io/v1 
    kind: Ingress 
    metadata: 
      name: myshop-ingress 
    spec: 
      rules: 
      - http: 
          paths: 
          - path: / 
            pathType: Prefix 
            backend: 
              service: 
                name: myshopservice 
                port: 
                  number: 4000 
    

    Check this very simple example for ingress routing and controller setup : https://dev.to/xaviergeerinck/creating-a-kubernetes-nginx-ingress-controller-and-create-a-rule-to-a-sample-application-4and