In Kubernetes, a Service is an abstract way to expose an application running on a set of Pods as a network service. It provides a stable IP address and DNS name for accessing the application.

The localPort in a Kubernetes Service refers to the port number on the local machine where the service is exposed. When you create a Service, you can specify a targetPort, which is the port number on the Pods where the application is listening. The localPort is the port number on the local machine where the Service will be accessible.

For example, if you have a Service with targetPort set to 8080 and localPort set to 30001, the application running on the Pods will be accessible at port 8080 within the cluster, and at port 30001 on the local machine.

You can expose a Service locally by using a NodePort or LoadBalancer type Service. With NodePort, the Service will be accessible on a high port number on each Node in the cluster. With LoadBalancer, the Service will be accessible through an external load balancer that forwards traffic to the localPort.

Here's an example YAML definition of a Service with localPort set to 30001:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
    nodePort: 30001
  selector:
    app: my-app

In this example, the Service named "my-service" will be accessible on port 30001 on each Node in the cluster, and it will forward traffic to Pods with the label "app: my-app" that are listening on port 8080

k8s service localport

原文地址: https://www.cveoy.top/t/topic/hJ6o 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录