简体   繁体   English

Kubernetes 中的默认负载平衡

[英]Default Load Balancing in Kubernetes

I've recently started working with Kubernetes clusters.我最近开始使用 Kubernetes 集群。 The flow of network calls for a given Kubernetes service in our cluster is something like the following:我们集群中给定 Kubernetes 服务的网络调用流程类似于以下内容:

External Non-K8S Load Balancer -> Ingress Controller -> Ingress Resource -> Service -> Pod外部非 K8S 负载均衡器 -> Ingress Controller -> Ingress Resource -> Service -> Pod

For a given service, there are two replicas.对于给定的服务,有两个副本。 By looking at the logs of the containers in the replicas, I can see that calls are being routed to different pods.通过查看副本中容器的日志,我可以看到调用被路由到不同的 pod。 As far as I can see, we haven't explicitly set up any load-balancing policies anywhere for our services in Kubernetes.据我所知,我们还没有在任何地方为 Kubernetes 中的服务明确设置任何负载平衡策略。

I've got a few questions:我有几个问题:

1) Is there a default load-balancing policy for K8S? 1) K8S 有默认的负载均衡策略吗? I've read about kube-proxy and random routing.我已经阅读了 kube-proxy 和随机路由。 It definitely doesn't appear to be round-robin.它绝对不是循环的。 2) Is there an obvious way to specify load balancing rules in the Ingress resources themselves? 2) 有没有明显的方法可以在 Ingress 资源本身中指定负载均衡规则? On a per-service basis?以每项服务为基础?

Looking at one of our Ingress resources, I can see that the 'loadBalancer' property is empty:查看我们的 Ingress 资源之一,我可以看到“loadBalancer”属性为空:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
ingress.kubernetes.io/rewrite-target: /
kubectl.kubernetes.io/last-applied-configuration: |
  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"ingress.kubernetes.io/rewrite-target":"/","nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"example-service-ingress","namespace":"member"},"spec":{"rules":[{"host":"example-service.x.x.x.example.com","http":{"paths":[{"backend":{"serviceName":"example-service-service","servicePort":8080},"path":""}]}}]}}
nginx.ingress.kubernetes.io/rewrite-target: /
  creationTimestamp: "2019-02-13T17:49:29Z"
  generation: 1
  name: example-service-ingress
  namespace: x
  resourceVersion: "59178"
  selfLink: /apis/extensions/v1beta1/namespaces/x/ingresses/example-service-ingress
  uid: b61decda-2fb7-11e9-935b-02e6ca1a54ae
spec:
  rules:
  - host: example-service.x.x.x.example.com
    http:
      paths:
      - backend:
          serviceName: example-service-service
          servicePort: 8080
status:
  loadBalancer:
    ingress:
    - {}

I should specify - we're using an on-prem Kubernetes cluster, rather than on the cloud.我应该指定 - 我们使用的是本地 Kubernetes 集群,而不是在云上。

Cheers!干杯!

The "internal load balancing" between Pods of a Service has already been covered in this question from a few days ago . Service Pods 之间的“内部负载均衡” 这个问题几天前就已经讲过了。

Ingress isn't really doing anything special (unless you've been hacking in the NGINX config it uses) - it will use the same Service rules as in the linked question. Ingress 并没有真正做任何特别的事情(除非你在它使用的 NGINX 配置中进行了黑客攻击)——它将使用与链接问题相同的服务规则。

If you want or need fine-grained control of how pods are routed to within a service, it is possible to extend Kubernetes' features - I recommend you look into the traffic management features of Istio , as one of its features is to be able to dynamically control how much traffic different pods in a service receive.如果您想要或需要对 Pod 如何在服务内路由进行细粒度控制,可以扩展 Kubernetes 的功能 - 我建议您查看Istio的流量管理功能,因为它的功能之一是能够动态控制服务中不同 Pod 接收的流量。

I see two options that can be used with k8s:我看到两个可以与 k8s 一起使用的选项:

  1. Use istio's traffic management and create a DestinationRule .使用istio 的流量管理并创建DestinationRule It currently supports three load balancing modes:目前支持三种负载均衡模式:

    • Round robin循环赛
    • Random随机的
    • Weighted least request加权最小请求

      apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule ... spec: ... subsets: - name: test ... trafficPolicy: loadBalancer: simple: ROUND_ROBIN
  2. Use lb_type in envoy proxy with ambassador on k8s.使用lb_type特使代理大使的K8S。 More info about ambassador is in https://www.getambassador.io .有关大使的更多信息,请访问 https://www.getambassador.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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