我对入口和负载均衡器在Kubernetes中的角色感到非常困惑。
据我所知,Ingress用于将来自internet的传入流量映射到集群中运行的服务。
负载均衡器的作用是将流量转发到主机。在这方面,入口与负载均衡器有什么不同?另外,与Amazon ELB和ALB相比,kubernetes内部的负载均衡器的概念是什么?
我对入口和负载均衡器在Kubernetes中的角色感到非常困惑。
据我所知,Ingress用于将来自internet的传入流量映射到集群中运行的服务。
负载均衡器的作用是将流量转发到主机。在这方面,入口与负载均衡器有什么不同?另外,与Amazon ELB和ALB相比,kubernetes内部的负载均衡器的概念是什么?
当前回答
pod有自己的IP:PORT,但它本质上是动态的,如果删除或重新部署会发生变化。
服务被分配ClusterIPor NodePort(创建服务资源的虚拟机中的端口),可以映射到一组pod或其他后端[参见:无头服务]
要访问正确的Pod,请使用ClusterIP(从集群内部) NodePort可以用于从集群外部访问pods
LoadBalancer[外部/内部]:由云提供商提供,指向ClusterIP或NodePort。您可以通过LB的IP访问该服务
LB ~> SERVICE(ClusterIP或NodePort) ~> POD
入口资源是集群的入口点。负载均衡可以侦听入路规则,并路由到特定的服务。[请看这个例子]
LB(Ingress-managed) ~> SERVICE(ClusterIP或NodePort) ~> POD
其他回答
Feature |
Ingress |
Load Balancer |
---|---|---|
Protocal | HTTP level (Network layer 7) | Network layer 4 |
Additional Features | cookie-based session affinity, Ingress rules, Resource backends, Path types | Only balance the load |
Dependency | Ingress controller need to be running. Different Kubernetes environments use different implementations of the controller, but several don’t provide a default controller at all. | No dependency, Built-in support with K8 |
YAML manifest | There is separate API for it. apiVersion: networking.k8s.io/v1 |
type: LoadBalancer |
How it work | Client connected to one of the pods through Ingress controller. The client first performed a DNS lookup of example.com, and the DNS server (or the local operating system) returned the IP of the Ingress controller. The client then sent an HTTP request to the Ingress controller and specified example.com in the Host header. From that header, the controller determined which service the client is trying to access, looked up the pod IPs through the Endpoints object associated with the service, and forwarded the client’s request to one of the pods. | The load balancer redirects traffic to the node port across all the nodes. Clients connect to the service through the load balancer’s IP. |
我强烈推荐阅读NodePort vs LoadBalancer vs Ingress?知识++
我发现了一篇非常有趣的文章,它解释了NodePort、LoadBalancer和Ingress之间的区别。
从文章的内容来看:
loadbalance:
A LoadBalancer service is the standard way to expose a service to the internet. On GKE, this will spin up a Network Load Balancer that will give you a single IP address that will forward all traffic to your service. If you want to directly expose a service, this is the default method. All traffic on the port you specify will be forwarded to the service. There is no filtering, no routing, etc. This means you can send almost any kind of traffic to it, like HTTP, TCP, UDP, Websockets, gRPC, or whatever. The big downside is that each service you expose with a LoadBalancer will get its own IP address, and you have to pay for a LoadBalancer per exposed service, which can get expensive!
入口:
Ingress is actually NOT a type of service. Instead, it sits in front of multiple services and act as a “smart router” or entrypoint into your cluster. You can do a lot of different things with an Ingress, and there are many types of Ingress controllers that have different capabilities. The default GKE ingress controller will spin up a HTTP(S) Load Balancer for you. This will let you do both path based and subdomain based routing to backend services. For example, you can send everything on foo.yourdomain.example to the foo service, and everything under the yourdomain.example/bar/ path to the bar service. Ingress is probably the most powerful way to expose your services, but can also be the most complicated. There are many types of Ingress controllers, from the Google Cloud Load Balancer, Nginx, Contour, Istio, and more. There are also plugins for Ingress controllers, like the cert-manager, that can automatically provision SSL certificates for your services. Ingress is the most useful if you want to expose multiple services under the same IP address, and these services all use the same L7 protocol (typically HTTP). You only pay for one load balancer if you are using the native GCP integration, and because Ingress is “smart” you can get a lot of features out of the box (like SSL, Auth, Routing, etc)
短版:
在Kubernetes中,对象定义定义所需的状态,而控制器则监视对象定义以实现该状态。
入口:
“Ingress”对象,它自己做的很少,但定义了L7负载平衡规则 “入口控制器”,监视入口对象的状态,以根据在入口对象中定义的规则创建L7 LB配置
loadbalance:
"Service"类型为"LoadBalancer"的对象,允许将服务附加到LoadBalancer “负载均衡器控制器”,根据服务对象中定义的规则创建负载均衡器
入口
进入对象:
一个kubernetes对象,它自己不做任何事情,因为默认情况下不包括入口控制器。Ingress对象只是通过指定请求路径、请求域和目标kubernetes服务来描述将第7层流量路由到集群的方法,而添加服务对象实际上可能会创建服务,因为默认情况下kubernetes中包含了服务控制器。
入口控制器:
Kubernetes部署/DaemonSet +服务:
1. listens on specific ports (usually 80 and 443) for web traffic
2. Watches for the creation, modification, or deletion of Ingress Resources
3. Creates internal L7 routing rules based on desired state indicated by Ingress Objects
例如,Nginx入口控制器可以:
使用服务在端口80和443监听传入的流量 注意Ingress对象的创建,并将所需的状态转换为新的服务器{}部分,这些部分将动态地放入nginx.conf中
loadbalance
负载均衡器控制器:
负载均衡器控制器可以在AWS和GKE等平台上配置,并提供一种通过创建外部负载均衡器分配外部ip的方法。此功能可用于:
部署负载均衡器控制器(如果尚未部署) 设置服务类型为“LoadBalancer” 在服务中设置适当的注释以配置负载均衡器
服务类型:
当服务类型设置为LoadBalancer并且存在云提供的负载均衡器控制器时,该服务将使用云提供商的负载均衡器对外公开。NodePort和ClusterIP服务(外部负载均衡器路由到的服务)自动创建,从而分配服务外部ip和或DNS。
的关系
入口控制器服务通常配置为LoadBalancer类型,以便http和https请求可以通过外部ip代理/路由到特定的内部服务。
然而,这并不严格需要LoadBalancer。因为,通过使用hostNetwork或hostPort,您可以在技术上将主机上的端口绑定到服务(允许您通过主机的外部ip:port访问它)。虽然官方不建议这样做,因为它会占用实际节点上的端口。
参考文献
https://kubernetes.io/docs/concepts/configuration/overview/#services
https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/
https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#external-load-balancer-providers
https://kubernetes.io/docs/concepts/services-networking/ingress/ https://kubernetes.io/docs/concepts/architecture/cloud-controller/ https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.3/
由于入口控制器运行在集群内部,具有查询和列出服务的clusterrole权限,因此它有能力发现未在外部公开的服务。这有助于根据入口规则将来自外部源的请求路由到正确的服务。
Ingress基于L7规则(header, uri等)在内部将流量路由到多个服务
负载均衡器将外部流量路由到单个服务。