-1

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?

  • have you set up an ingress object `kubectl get ingress`? is your container exposing port 3000? – about14sheep Apr 02 '23 at 00:15
  • I do not have an ingress object! I'm not sure how that works. I'll look into it. I believe my container is exposing port 3000 because i can go to localhost:3000 and see the node application. – Patricia Zhang Apr 02 '23 at 00:25
  • Is an ingress rule something like this?? apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-node-app-ingress spec: rules: - host: my-node-app-service http: paths: - path: /* pathType: Prefix backend: service: name: my-node-app-service port: number: 3000 – Patricia Zhang Apr 02 '23 at 00:34
  • although you have named the service `my-node-app-service` it is still serving at `localhost:3000`. [you can learn more about ingress object here](https://kubernetes.io/docs/concepts/services-networking/ingress/) – about14sheep Apr 02 '23 at 00:41
  • I know in Docker I was able to do a post request like this but looks like it's not as simple in Kubernetes? my-node-app-service doesn't map to localhost? – Patricia Zhang Apr 02 '23 at 00:51
  • this might help: https://stackoverflow.com/questions/65123401/how-to-access-hosts-localhost-from-inside-kubernetes-cluster – about14sheep Apr 02 '23 at 00:55
  • any update on this ? feel free to update the status of question if any of below answer resolves you issues or do share your solution if manage to resolve. – Harsh Manvar Apr 10 '23 at 08:57

2 Answers2

0

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">
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
0

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

S.Tushinov
  • 548
  • 5
  • 13