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
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
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.
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.
Since you are using Docker Desktop for deploying your application to Kubernetes. You can try changing your
Service
to atype: NodePort
instead of atype: LoadBalancer
It should look something like this:
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.
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 aNodePort
I recommend this thread here in stackoverflow