Skip to content

Commit

Permalink
Add Locality Load Balancing support (#258)
Browse files Browse the repository at this point in the history
* Add Locality Load Balancing support
  • Loading branch information
waynz0r authored Jul 11, 2019
1 parent 304dd02 commit 5761fbd
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
45 changes: 45 additions & 0 deletions config/base/crds/istio_v1beta1_istio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,51 @@ spec:
type: object
type: array
type: object
localityLB:
description: Locality based load balancing distribution or failover
settings.
properties:
distribute:
description: 'Optional: only one of distribute or failover can be
set. Explicitly specify loadbalancing weight across different
zones and geographical locations. Refer to [Locality weighted
load balancing](https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/load_balancing/locality_weight)
If empty, the locality weight is set according to the endpoints
number within it.'
items:
properties:
from:
description: Originating locality, '/' separated, e.g. 'region/zone'.
type: string
to:
description: Map of upstream localities to traffic distribution
weights. The sum of all weights should be == 100. Any locality
not assigned a weight will receive no traffic.
type: object
type: object
type: array
enabled:
description: If set to true, locality based load balancing will
be enabled
type: boolean
failover:
description: 'Optional: only failover or distribute can be set.
Explicitly specify the region traffic will land on when endpoints
in local region becomes unhealthy. Should be used together with
OutlierDetection to detect unhealthy endpoints. Note: if no OutlierDetection
specified, this will not take effect.'
items:
properties:
from:
description: Originating region.
type: string
to:
description: Destination region the traffic will fail over
to when endpoints in the 'from' region becomes unhealthy.
type: string
type: object
type: array
type: object
meshExpansion:
description: If set to true, the pilot and citadel mtls will be exposed
on the ingress gateway also the remote istios will be connected through
Expand Down
12 changes: 12 additions & 0 deletions config/samples/istio_v1beta1_istio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ spec:
accessToken: <access-token>
secure: true
cacertPath: /etc/lightstep/cacert.pem
localityLB:
enabled: false
# distribute:
# - from: "us-central1/*"
# to:
# "us-central1/*": 80
# "us-central2/*": 20
# failover:
# - from: us-east
# to: eu-west
# - from: us-west
# to: us-east
51 changes: 51 additions & 0 deletions pkg/apis/istio/v1beta1/istio_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,54 @@ type IstioCoreDNS struct {
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
}

// Describes how traffic originating in the 'from' zone is
// distributed over a set of 'to' zones. Syntax for specifying a zone is
// {region}/{zone} and terminal wildcards are allowed on any
// segment of the specification. Examples:
// * - matches all localities
// us-west/* - all zones and sub-zones within the us-west region
type LocalityLBDistributeConfiguration struct {
// Originating locality, '/' separated, e.g. 'region/zone'.
From string `json:"from,omitempty"`
// Map of upstream localities to traffic distribution weights. The sum of
// all weights should be == 100. Any locality not assigned a weight will
// receive no traffic.
To map[string]uint32 `json:"to,omitempty"`
}

// Specify the traffic failover policy across regions. Since zone
// failover is supported by default this only needs to be specified for
// regions when the operator needs to constrain traffic failover so that
// the default behavior of failing over to any endpoint globally does not
// apply. This is useful when failing over traffic across regions would not
// improve service health or may need to be restricted for other reasons
// like regulatory controls.
type LocalityLBFailoverConfiguration struct {
// Originating region.
From string `json:"from,omitempty"`
// Destination region the traffic will fail over to when endpoints in
// the 'from' region becomes unhealthy.
To string `json:"to,omitempty"`
}

// Locality-weighted load balancing allows administrators to control the
// distribution of traffic to endpoints based on the localities of where the
// traffic originates and where it will terminate.
type LocalityLBConfiguration struct {
// If set to true, locality based load balancing will be enabled
Enabled *bool `json:"enabled,omitempty"`
// Optional: only one of distribute or failover can be set.
// Explicitly specify loadbalancing weight across different zones and geographical locations.
// Refer to [Locality weighted load balancing](https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/load_balancing/locality_weight)
// If empty, the locality weight is set according to the endpoints number within it.
Distribute []*LocalityLBDistributeConfiguration `json:"distribute,omitempty"`
// Optional: only failover or distribute can be set.
// Explicitly specify the region traffic will land on when endpoints in local region becomes unhealthy.
// Should be used together with OutlierDetection to detect unhealthy endpoints.
// Note: if no OutlierDetection specified, this will not take effect.
Failover []*LocalityLBFailoverConfiguration `json:"failover,omitempty"`
}

// IstioSpec defines the desired state of Istio
type IstioSpec struct {
// Contains the intended Istio version
Expand Down Expand Up @@ -397,6 +445,9 @@ type IstioSpec struct {
// Istio CoreDNS provides DNS resolution for services in multi mesh setups
IstioCoreDNS IstioCoreDNS `json:"istioCoreDNS,omitempty"`

// Locality based load balancing distribution or failover settings.
LocalityLB *LocalityLBConfiguration `json:"localityLB,omitempty"`

networkName string
meshNetworks *MeshNetworks
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/apis/istio/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion pkg/resources/common/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *Reconciler) meshConfig() string {
"defaultConfig": defaultConfig,
"rootNamespace": "istio-system",
"connectTimeout": "10s",
"localityLbSetting": nil,
"localityLbSetting": r.getLocalityLBConfiguration(),
}

if util.PointerToBool(r.Config.Spec.UseMCP) {
Expand All @@ -117,6 +117,24 @@ func (r *Reconciler) meshConfig() string {
return string(marshaledConfig)
}

func (r *Reconciler) getLocalityLBConfiguration() *istiov1beta1.LocalityLBConfiguration {
var localityLbConfiguration *istiov1beta1.LocalityLBConfiguration

if r.Config.Spec.LocalityLB == nil || !util.PointerToBool(r.Config.Spec.LocalityLB.Enabled) {
return localityLbConfiguration
}

if r.Config.Spec.LocalityLB != nil {
localityLbConfiguration = r.Config.Spec.LocalityLB.DeepCopy()
localityLbConfiguration.Enabled = nil
if localityLbConfiguration.Distribute != nil && localityLbConfiguration.Failover != nil {
localityLbConfiguration.Failover = nil
}
}

return localityLbConfiguration
}

func (r *Reconciler) meshNetworks() string {
marshaledConfig, _ := yaml.Marshal(r.Config.Spec.GetMeshNetworks())
return string(marshaledConfig)
Expand Down
7 changes: 7 additions & 0 deletions pkg/resources/pilot/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ func (r *Reconciler) containers() []apiv1.Container {
TerminationMessagePolicy: apiv1.TerminationMessageReadFile,
}

if r.Config.Spec.LocalityLB != nil && util.PointerToBool(r.Config.Spec.LocalityLB.Enabled) {
discoveryContainer.Env = append(discoveryContainer.Env, apiv1.EnvVar{
Name: "PILOT_ENABLE_LOCALITY_LOAD_BALANCING",
Value: "1",
})
}

containers := []apiv1.Container{
discoveryContainer,
}
Expand Down

0 comments on commit 5761fbd

Please sign in to comment.