Skip to content

Commit 75b5793

Browse files
authored
New Feature: Multi Cluster TargetGroupBindings (#3853)
* implement multicluster support * correct documentation
1 parent b40a257 commit 75b5793

File tree

32 files changed

+1897
-96
lines changed

32 files changed

+1897
-96
lines changed

apis/elbv2/v1alpha1/targetgroupbinding_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ type TargetGroupBindingSpec struct {
109109
// targetGroupARN is the Amazon Resource Name (ARN) for the TargetGroup.
110110
TargetGroupARN string `json:"targetGroupARN"`
111111

112+
// MultiClusterTargetGroup Denotes if the TargetGroup is shared among multiple clusters
113+
// +optional
114+
MultiClusterTargetGroup bool `json:"multiClusterTargetGroup,omitempty"`
115+
112116
// targetType is the TargetType of TargetGroup. If unspecified, it will be automatically inferred.
113117
// +optional
114118
TargetType *TargetType `json:"targetType,omitempty"`

apis/elbv2/v1beta1/targetgroupbinding_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ type TargetGroupBindingSpec struct {
127127
// +kubebuilder:validation:MinLength=1
128128
TargetGroupARN string `json:"targetGroupARN"`
129129

130+
// MultiClusterTargetGroup Denotes if the TargetGroup is shared among multiple clusters
131+
// +optional
132+
MultiClusterTargetGroup bool `json:"multiClusterTargetGroup,omitempty"`
133+
130134
// targetType is the TargetType of TargetGroup. If unspecified, it will be automatically inferred.
131135
// +optional
132136
TargetType *TargetType `json:"targetType,omitempty"`

config/crd/bases/elbv2.k8s.aws_targetgroupbindings.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ spec:
6060
spec:
6161
description: TargetGroupBindingSpec defines the desired state of TargetGroupBinding
6262
properties:
63+
multiClusterTargetGroup:
64+
description: MultiClusterTargetGroup Denotes if the TargetGroup is
65+
shared among multiple clusters
66+
type: boolean
6367
networking:
6468
description: networking provides the networking setup for ELBV2 LoadBalancer
6569
to access targets in TargetGroup.
@@ -233,6 +237,10 @@ spec:
233237
- ipv4
234238
- ipv6
235239
type: string
240+
multiClusterTargetGroup:
241+
description: MultiClusterTargetGroup Denotes if the TargetGroup is
242+
shared among multiple clusters
243+
type: boolean
236244
networking:
237245
description: networking defines the networking rules to allow ELBV2
238246
LoadBalancer to access targets in TargetGroup.

config/rbac/role.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ kind: ClusterRole
44
metadata:
55
name: controller-role
66
rules:
7+
- apiGroups:
8+
- ""
9+
resources:
10+
- configmaps
11+
verbs:
12+
- create
13+
- delete
14+
- get
15+
- update
716
- apiGroups:
817
- ""
918
resources:

controllers/elbv2/targetgroupbinding_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type targetGroupBindingReconciler struct {
8787
// +kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch
8888
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;list;watch
8989
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
90+
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;delete;create;update
9091
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
9192
// +kubebuilder:rbac:groups="discovery.k8s.io",resources=endpointslices,verbs=get;list;watch
9293

docs/guide/ingress/annotations.md

Lines changed: 61 additions & 44 deletions
Large diffs are not rendered by default.

docs/guide/service/annotations.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
| [service.beta.kubernetes.io/aws-load-balancer-security-groups](#security-groups) | stringList | | |
5353
| [service.beta.kubernetes.io/aws-load-balancer-manage-backend-security-group-rules](#manage-backend-sg-rules) | boolean | true | If `service.beta.kubernetes.io/aws-load-balancer-security-groups` is specified, this must also be explicitly specified otherwise it defaults to `false`. |
5454
| [service.beta.kubernetes.io/aws-load-balancer-inbound-sg-rules-on-private-link-traffic](#update-security-settings) | string | |
55-
| [service.beta.kubernetes.io/aws-load-balancer-listener-attributes.${Protocol}-${Port}](#listener-attributes) | stringMap | |
55+
| [service.beta.kubernetes.io/aws-load-balancer-listener-attributes.${Protocol}-${Port}](#listener-attributes) | stringMap | |
56+
| [service.beta.kubernetes.io/aws-load-balancer-multi-cluster-target-group](#multi-cluster-target-group) | boolean | false | If specified, the controller will only operate on targets that exist within the cluster, ignoring targets from other sources. |
5657

5758
## Traffic Routing
5859
Traffic Routing can be controlled with following annotations:
@@ -289,6 +290,20 @@ for proxy protocol v2 configuration.
289290
service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled
290291
```
291292

293+
- <a name="multi-cluster-target-group">`service.beta.kubernetes.io/aws-load-balancer-multi-cluster-target-group`</a> Allows you to share the created Target Group ARN with other Load Balancer Controller managed clusters.
294+
295+
!!!warning ""
296+
This feature does not offer any Deletion Protection. Deleting the service will still delete the Target Group. If you need to support
297+
Target Groups shared with multiple clusters, it's recommended to use an out-of-band Target Group that is not managed by a Load Balancer Controller.
298+
299+
!!!note ""
300+
- It is not recommended to change this value frequently, if ever. The recommended way to set this value is on creation of the service.
301+
302+
!!!example
303+
```
304+
service.beta.kubernetes.io/aws-load-balancer-multi-cluster-target-group: "true"
305+
```
306+
292307
293308
## AWS Resource Tags
294309
The AWS Load Balancer Controller automatically applies following tags to the AWS resources it creates (NLB/TargetGroups/Listener/ListenerRule):

docs/guide/targetgroupbinding/targetgroupbinding.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,37 @@ spec:
9292
...
9393
```
9494

95+
## MultiCluster Target Group
96+
TargetGroupBinding CRD supports sharing the same target group ARN among multiple clusters. Setting this flag will ensure the controller only operates on targets within the cluster.
97+
98+
!!!tip ""
99+
The default value is false, meaning that the controller assumes full control over the target group ARN and will deregister any targets that are not found within the cluster.
100+
To set this flag for TGBs managed by the controller use either:
101+
ALB: alb.ingress.kubernetes.io/multi-cluster-target-group: "true"
102+
NLB: service.beta.kubernetes.io/aws-load-balancer-multi-cluster-target-group: "true"
103+
104+
105+
!!!warning ""
106+
It is not recommended to change this value after TGB creation. Changing between shared / not shared might lead to leaked targets.
107+
108+
!!!warning ""
109+
Only use this flag if you intend to share the target group ARN in multiple clusters. This flag will slow down reconciles and put a small additonal load on the kubernetes control plane.
110+
111+
112+
## Sample YAML
113+
```yaml
114+
apiVersion: elbv2.k8s.aws/v1beta1
115+
kind: TargetGroupBinding
116+
metadata:
117+
name: my-tgb
118+
spec:
119+
serviceRef:
120+
name: awesome-service # route traffic to the awesome-service
121+
port: 80
122+
targetGroupARN: <arn-to-targetGroup>
123+
multiClusterTargetGroup: "true"
124+
```
125+
95126

96127
## Reference
97128
See the [reference](./spec.md) for TargetGroupBinding CR

docs/guide/use_cases/blue_green/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Multiple target groups can be attached to the same forward action of a listener
1111
In addition to the weighted target group, AWS announced the advanced request routing feature in 2019. Advanced request routing gives developers the ability to write rules (and route traffic) based on standard and custom HTTP headers and methods, the request path, the query string, and the source IP address. This new feature simplifies the application architecture by eliminating the need for a proxy fleet for routing, blocks unwanted traffic at the load balancer, and enables the implementation of A/B testing.
1212

1313
## Overview
14-
The ALB is configured to split traffic using annotations on the ingress resrouces. More specifically, the [ingress annotation](../../../guide/ingress/annotations.md#actions) `alb.ingress.kubernetes.io/actions.${service-name}` configures custom actions on the listener.
14+
The ALB is configured to split traffic using annotations on the ingress resources. More specifically, the [ingress annotation](../../../guide/ingress/annotations.md#actions) `alb.ingress.kubernetes.io/actions.${service-name}` configures custom actions on the listener.
1515

1616
The body of the annotation is a JSON document that identifies an action type, and configures it. The supported [actions](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#rule-action-types) are `redirect`, `forward`, and `fixed-response`.
1717

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# MultiCluster Target Groups
2+
3+
The load balancer controller assumes full control over the configured target groups. When a target group is registered with the controller it de registers any targets not currently in the cluster. Target groups that have MultiCluster support enabled can be associated to multiple Kubernetes clusters or support arbitrary targets from other sources.
4+
5+
6+
## Overview
7+
8+
When enabled, MultiCluster mode supports multiple methods, and every cluster associated with a target group has one of these methods. It's recommended to use new resources when configuring MutliCluster mode. There is a period of time when MultiCluster must take a snapshot of the cluster state in order to support the selected mode. This data is stored into ConfigMap, which resides in the same namespace as your load balancer resources. ConfigMap stores snapshots of managed targets at `aws-lbc-targets-$TARGET_GROUP_BINDING_NAME`
9+
10+
When using an ALB, you must specify this annotation in the ingress or service:
11+
12+
`alb.ingress.kubernetes.io/multi-cluster-target-group: "true"`
13+
14+
When using an NLB, you specify this annotation in your service:
15+
16+
`service.beta.kubernetes.io/aws-load-balancer-multi-cluster-target-group: "true"`
17+
18+
When using any out-of-band TargetGroupBindings, you must specify this field in the spec:
19+
20+
`multiClusterTargetGroup: true`
21+
22+
23+
### Example
24+
25+
We will be setting up an echoserver in two clusters in order to demonstrate MultiCluster mode. See the full echoserver example in the 'Examples' tab.
26+
27+
The following ingress configures the Target Group Binding as MultiCluster. We will take the created Target Group and share it in a second cluster.
28+
29+
```
30+
apiVersion: networking.k8s.io/v1
31+
kind: Ingress
32+
metadata:
33+
name: echoserver
34+
namespace: echoserver
35+
annotations:
36+
alb.ingress.kubernetes.io/multi-cluster-target-group: "true"
37+
alb.ingress.kubernetes.io/scheme: internet-facing
38+
alb.ingress.kubernetes.io/tags: Environment=dev,Team=test
39+
spec:
40+
ingressClassName: alb
41+
rules:
42+
- http:
43+
paths:
44+
- path: /
45+
pathType: Exact
46+
backend:
47+
service:
48+
name: echoserver
49+
port:
50+
number: 80
51+
```
52+
53+
Verify that MultiCluster is enabled by verifying that the created Target Group Binding is marked as MultiCluster.
54+
55+
```
56+
kubectl -n echoserver get targetgroupbinding k8s-echoserv-echoserv-cc0122e143 -o yaml
57+
apiVersion: elbv2.k8s.aws/v1beta1
58+
kind: TargetGroupBinding
59+
metadata:
60+
annotations:
61+
elbv2.k8s.aws/checkpoint: cKay81gadoTtBSg6uVVginqtmCVG-1ApTvYN4YLD37U/_4kBy3Yg64qrXzjvIb2LlC3O__ex1qjozynsqHXmPgo
62+
elbv2.k8s.aws/checkpoint-timestamp: "1729021572"
63+
creationTimestamp: "2024-10-15T19:46:06Z"
64+
finalizers:
65+
- elbv2.k8s.aws/resources
66+
generation: 1
67+
labels:
68+
ingress.k8s.aws/stack-name: echoserver
69+
ingress.k8s.aws/stack-namespace: echoserver
70+
name: k8s-echoserv-echoserv-cc0122e143
71+
namespace: echoserver
72+
resourceVersion: "79121011"
73+
uid: 9ceaa2ea-14bb-44a5-abb0-69c7d2aac52c
74+
spec:
75+
ipAddressType: ipv4
76+
multiClusterTargetGroup: true <<< HERE
77+
networking:
78+
ingress:
79+
- from:
80+
- securityGroup:
81+
groupID: sg-06a2bd7d790ac1d2e
82+
ports:
83+
- port: 32197
84+
protocol: TCP
85+
serviceRef:
86+
name: echoserver
87+
port: 80
88+
targetGroupARN: arn:aws:elasticloadbalancing:us-east-1:565768096483:targetgroup/k8s-echoserv-echoserv-cc0122e143/6816b87346280ee7
89+
targetType: instance
90+
vpcID: vpc-0a7ef5bd8943067a8
91+
```
92+
93+
In another cluster, you can now register that Target Group ARN in a Target Group Binding.
94+
95+
```
96+
apiVersion: elbv2.k8s.aws/v1beta1
97+
kind: TargetGroupBinding
98+
metadata:
99+
name: MyTargetGroupBinding
100+
namespace: echoserver
101+
spec:
102+
serviceRef:
103+
name: echoserver
104+
port: 80
105+
multiClusterTargetGroup: true
106+
targetType: instance
107+
ipAddressType: ipv4
108+
networking:
109+
ingress:
110+
- from:
111+
- securityGroup:
112+
groupID: $SG_FROM_ABOVE
113+
ports:
114+
- port: 32197
115+
protocol: TCP
116+
targetGroupARN: $TG_FROM_ABOVE
117+
```
118+
119+
The configured TargetGroup should have targets from both clusters available to service traffic.
120+
121+

0 commit comments

Comments
 (0)