-1

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.

4
  • 1
    You 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, 2021 at 9:41
  • If it's just the simple post request you need info on, see How do I send a json string request in go
    – Matt
    Feb 12, 2021 at 9: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, 2021 at 22:48
  • Sure thing, added an answer with some more detail on the kubernetes flow
    – Matt
    Feb 14, 2021 at 23:20

1 Answer 1

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.

3
  • 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, 2021 at 2: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.
    – Matt
    Feb 15, 2021 at 2:59
  • I am using minikube for my experiment and used "minikube ip" command to find the node-ip as suggested and when I use node-ip:node-port I got "Operation time out". This is why I used port-forward.
    – user226876
    Feb 15, 2021 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.