I am new to kubernetes. I have implemented a webserver inside a pod and set a Nodeport service for that pod. I want to send a POST request with a custom message (in json) to a pod after it has been created and ready to use. I want to use the go client library for that matter. Could you please let me know how I can do that? Which part of the library come to help? Thanks.
Asked
Active
Viewed 277 times
-1
-
1You don't need to use the kubernetes client to do that, it's just a normal http request to the app running the the pod, but via the nodeport. – Matt Feb 12 '21 at 09:41
-
If it's just the simple post request you need info on, see [How do I send a json string request in go](https://stackoverflow.com/questions/24455147/how-do-i-send-a-json-string-in-a-post-request-in-go/24455606#24455606) – Matt Feb 12 '21 at 09:45
-
Hi @Matt, many thanks for your reply. Would you please elaborate your answer? the part that new and made me confused is "but via the nodeport". – user226876 Feb 14 '21 at 22:48
-
Sure thing, added an answer with some more detail on the kubernetes flow – Matt Feb 14 '21 at 23:20
1 Answers
1
Say the go server runs on locally, you normally use http://localhost:3000
to access it. The pod then has a containerPort
of 3000
.
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-web-deployment
labels:
app: GoWeb
spec:
replicas: 1
selector:
matchLabels:
app: GoWeb
template:
metadata:
labels:
app: GoWeb
spec:
containers:
- name: go-web
image: me/go-web:1.0.1
ports:
- containerPort: 3000
The Service is then an abstraction of that pod, that describes how to access 1 or many Pods running that service.
The nodePort
of the service is 31024
.
apiVersion: v1
kind: Service
metadata:
name: go-web-service
spec:
type: NodePort
selector:
app: GoWeb
ports:
- port: 3000
nodePort: 31024
The application is published on http://node-ip:node-port
for the public to consume. Kubernetes manages the mappings between the node and the container in the background.
| User | -> | Node:nodePort | -> | Pod:containerPort |
The Kubernetes internal Service and Pod IP's are not often available to the outside world (unless you specifically set a cluster up that way). Whereas the nodes themselves will often carry an IP address that is routable/contactable.

Matt
- 68,711
- 7
- 155
- 158
-
Thanks a lot. That helped me to communicate with my pod. But I have to enable port-forward in order to send/receive traffic from the pod. Is there a way that we don't need to enable port-forward? – user226876 Feb 15 '21 at 02:16
-
Enable port forward? You mean with `kubectl port-forward`? That's not a part of the mechanism described above. A port forward is a way of tunnelling a connection to a Pod via the kubernetes API to test/debug things. Maybe read through the [Service docco](https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types). – Matt Feb 15 '21 at 02:59
-
I am using minikube for my experiment and used "minikube ip" command to find the node-ip as suggested and when I use http://node-ip:node-port I got "Operation time out". This is why I used port-forward. – user226876 Feb 15 '21 at 03:19