skip to Main Content

I have a yaml file for Kubernetes that looks like this:

apiVersion: v1
kind: Service
metadata:
  name: my-node-app-service
spec:
  selector:
    app: my-node-app
  ports:
    - name: http
      port: 3000
      targetPort: 3000
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-node-app
  template:
    metadata:
      labels:
        app: my-node-app
    spec:
      containers:
        - name: node-app
          image: <my image>
          ports:
            - containerPort: 3000

Here’s the line for the POST request from an HTML page:

<form action="http://my-node-app-service:3000/" method="post">

It doesn’t work and gives me a Server Not Found error in my browser.

I expected the POST request to go through successfully and write to my database, but it looks like where I’m posting to is wrong. Where should I post to?

2

Answers


  1. If your HTML is running in the same Kubernetes cluster then ideally it should be working, both service, deployment & HTML should be in same namespace of k8s too.

    If your HTML hosted outside of the k8s cluster it won’t work that, service-name that you have used is correct way but only resolves internally.

    Run command

    kubectl get svc 
    

    as your service is type:LoadBalancer you will see the ExternalIP

    Use that IP to hit the service. IP mostly available if you are running K8s on cloud provider GCP, AWS etc.

    <form action="http://IPX.XX.X.X:3000/" method="post">
    

    You can also Map this IP to your Domain if you want, Alternative way is to use ingress to expose your service and use that Domain in HTML.

    <form action="http://example.com:3000/" method="post">
    
    Login or Signup to reply.
  2. Since you are using Docker Desktop for deploying your application to Kubernetes. You can try changing your Service to a type: NodePort instead of a type: LoadBalancer

    It should look something like this:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-node-app-service
    spec:
      selector:
        app: my-node-app
      ports:
        - name: http
          nodePort: 30000 # Will be accessible on localhost:30000
          port: 3000
          targetPort: 3000
      type: NodePort
    

    After adjusting the service you will able to reach your application on http://localhost:30000

    Additionally since your application is now listening on http://localhost:30000

    You can set the form of your application to send a POST request to your node.js application by adjusting it in the following way.

    <form action="http://localhost:30000/" method="post">
    

    And this should resolve your case for sending a post request to your application and writing the form data into the database.

    p.s. If you’re interested in the difference between a LoadBalancer and a NodePort I recommend this thread here in stackoverflow

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search