Kubernetes - Send HTTP post request from Pod to ListenAndServe

1/6/2020

(pretty new in Kubernetes, sorry in advance for possible mistakes)

I'm using kind (kind) for my two-node (master and worker) cluster with a StatefulSet of 10 pods and I have a node.js that simulates an HTTP server that gets HTTP GET requests that runs on each pod.

I'm trying to set a connection for sending an HTTP POST with an idle massage whenever the pod is idle (after handling the clients' HTTP GET) to NextEndpoint function in roundrobin.go with ListenAndServe for a new load balancing algorithm I want to implement in roundrobin.gothat’s based on the pods sending an idle massage to the LB whenever they finish handling an HTTP get request sent from an external python script.

I'm getting this error (from the pod logs):

ECONNREFUSED 172.17.0.3:8081 where 172.17.0.3 is the local IP of the control plane node.

Edit

I've created a pod webr-master running on the control plane node (master) and its container is a nodeJS that receives Http POST from pods in the statefulset successfully. Now I need to send an Http POST with the message received by webr-master to the ListenAndServe function that's being called currently in Run function in stateful_set.go, which seems to be doing nothing.

  1. Where should I call ListenAndServe?
  2. Am I meant to expose another port on webr-masters' container so that communication in the direction webr-master-to-kubernetes would work?
  3. What exactly does containerPort field of a pod mean?

Code excerpts:

In roundrobin.go:

type idleHandler struct{
    lb LoadBalancer
    svcPort proxy.ServicePortName
}

func (ih *idleHandler) handler(w http.ResponseWriter, r *http.Request) {
    //something something
}

In function NextEndpoint:

    handlerStruct := &idleHandler{lb: lb, svcPort: svcPort}
    http.HandleFunc("/", handlerStruct.handler)

    if err := http.ListenAndServe("172.17.0.3:8081", nil); err != nil {
    log.Fatal("ListenAndServe: ", err)
    }

In server.js:

const http = require('http');
const fs = require('fs');
const url = require('url');

//HTTP POST
var postData = JSON.stringify({msg: 'IDLE'})

var options = {
  hostname: '172.17.0.3',   //control plane node
  port: 8081,              //NodePort
  path: '/',
  method: 'POST',
  headers: {
       'Content-Type': 'application/json',
       'Content-Length': postData.length
     }
};

function handleRequest(request, response) {

      //HTTP GET response here...

      //HTTP POST
      var req = http.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});
    //post IDLE

    req.end(postData);
}

Thank you.

-- Racheli a
kubernetes
kubernetes-networking
kubernetes-pod
kubernetes-statefulset
node.js

0 Answers