简体   繁体   中英

How would I set up kubernetes Services and pods' IP addresses and ports to send some information into a pod?

The issue of ClusterIP, pod IP, nodePort, and targetPort are still a little confusing to me.

I want to set up a small test case to better evaluate use cases, but I am having a bit of trouble. At the moment, I am working with kubernetes for docker on mac.

What I'd like:

  • A pod with an application on it that listens on a port (say, 8080).
  • Pod is running on the docker-desktop node that comes with Kubernetes on Docker
  • A way to send requests from my local machine to the docker-desktop node that the pod can receive on its 8080 port, and a way to get back information the pod provides in response to the request

I am pretty sure I need something like a Service to act as the middleware between the pod and me, but I am not sure how to set something like this up.

Something like the kubernetes-dashboard api, where I can access the pod with:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/pod?namespace=default

Here, know kubernetes-dashboard is replaced by the namespace for my pods, and https is replaced with whatever port name I have configured in a Service, but I am not sure about the rest.

You should define a Service with NodePort type to access the service from the host machine.

Follow the reference --> https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/

The kubernetes dashboard example that you are referring you are actually using kubectl proxy or kubectl port forward to access it. In this case Kubernetes API Server works as proxy and forwards the request to pod.

You can just create a clusterIP type service and use the kubectl proxy or kubectl port forward mechanism to access it. Here is a guide on this.

The service would look like below as an example

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

You could also use NodePort type service to expose the pod. In this case you don't need to use kubectl proxy or kubectl port forward

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 8080
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007

You access it using http://<NODEIP>:30007 where <NODEIP> is any of the kubernetes node's IP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM