Skip to content

Commit 4851b28

Browse files
authored
fix: Switched back to using ELB instead of NLB due to various issues. Also added the ability to modify proxy protocol and traffic policy settings and return the nginx request id to the front end. (#45)
1 parent 9187c5f commit 4851b28

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

modules/kubernetes/ingress_nginx/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ No requirements.
2525
| chart\_version | The version of helm chart to use. | `string` | `"3.25.0"` | no |
2626
| connection\_idle\_timeout | The amount of time the load balancer will keep an idle connection open for. The value of nginx upstream-keepalive-timeout will also be set to this value + 5. If it were shorter than the LB timeout it could cause intermittent 502s. | `number` | `55` | no |
2727
| enable\_metrics | Enable prometheus metrics support, including adding a ServiceMonitor. | `bool` | n/a | yes |
28+
| external\_traffic\_policy | The external traffic policy to apply to the ingress service. Cluster will open a valid NodePort on all nodes even if they aren't running an ingress pod and kubernetes will handle sending the traffic to the correct pod. Local will only have valid NodePorts on the nodes running ingress pods. | `string` | `"Cluster"` | no |
2829
| namespace | Namespace to create the ingress in. | `string` | `"ingress-nginx"` | no |
2930
| replica\_count | Number of replicas of the ingress controller to create. Should be 2 or more in production. | `number` | `2` | no |
30-
| use\_network\_load\_balancer | Use an AWS NLB to load balance traffic to the cluster. Recommended. If false, will create a Classic Load Balancer. | `bool` | `true` | no |
31+
| use\_network\_load\_balancer | Use an AWS NLB to load balance traffic to the cluster. If false, will create a Classic Load Balancer. NLB is not recommended at this time due to some connection issues. | `bool` | `false` | no |
32+
| use\_proxy\_protocol | If true, will enable proxy protocol support between the Load Balancer and the nginx ingress controller. This allows nginx to know the IP of the client when using an ELB. | `bool` | `true` | no |
3133

3234
## Outputs
3335

modules/kubernetes/ingress_nginx/main.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ EOF
2424
configmap_defaults = {
2525
"proxy-real-ip-cidr" = "0.0.0.0/0"
2626
"use-forwarded-headers" = "true"
27-
"use-proxy-protocol" = "false"
27+
"use-proxy-protocol" = tostring(var.use_proxy_protocol)
2828
"log-format-escape-json" = "true"
2929
"log-format-upstream" = replace(local.log_format, "\n", "")
3030
"generate-request-id" = "true"
3131
"upstream-keepalive-timeout" = var.connection_idle_timeout + 5
3232
}
3333

34-
# Anti-affinity rules to apply. Will instruct k8s to try to not schedule 2 pods on the same node if possible.
34+
# Anti-affinity rules to apply. Will instruct k8s to try not to schedule 2 pods on the same node if possible.
3535
pod_anti_affinity = {
3636
podAntiAffinity : {
3737
preferredDuringSchedulingIgnoredDuringExecution : [
@@ -75,14 +75,17 @@ EOF
7575
namespace : "metrics"
7676
}
7777
}
78+
addHeaders : { "X-Request-Id" : "$request_id" }
79+
7880
service : {
79-
externalTrafficPolicy : "Local"
81+
externalTrafficPolicy : var.external_traffic_policy
8082

8183
annotations : {
8284
"service.beta.kubernetes.io/aws-load-balancer-backend-protocol" : "tcp"
8385
"service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout" : var.connection_idle_timeout
8486
"service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled" : "true"
8587
"service.beta.kubernetes.io/aws-load-balancer-type" : var.use_network_load_balancer ? "nlb" : "elb"
88+
"service.beta.kubernetes.io/aws-load-balancer-proxy-protocol" : var.use_proxy_protocol ? "*" : "false" # "*" is the only value that enables proxy protocol on the LB
8689
}
8790
}
8891

modules/kubernetes/ingress_nginx/variables.tf

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ variable "enable_metrics" {
2828
}
2929

3030
variable "use_network_load_balancer" {
31-
description = "Use an AWS NLB to load balance traffic to the cluster. Recommended. If false, will create a Classic Load Balancer."
31+
description = "Use an AWS NLB to load balance traffic to the cluster. If false, will create a Classic Load Balancer. NLB is not recommended at this time due to some connection issues."
3232
type = bool
33-
default = true
33+
default = false
3434
}
3535
variable "connection_idle_timeout" {
3636
description = "The amount of time the load balancer will keep an idle connection open for. The value of nginx upstream-keepalive-timeout will also be set to this value + 5. If it were shorter than the LB timeout it could cause intermittent 502s."
@@ -43,3 +43,20 @@ variable "apply_pod_anti_affinity" {
4343
type = bool
4444
default = true
4545
}
46+
47+
variable "use_proxy_protocol" {
48+
description = "If true, will enable proxy protocol support between the Load Balancer and the nginx ingress controller. This allows nginx to know the IP of the client when using an ELB."
49+
type = bool
50+
default = true
51+
}
52+
53+
variable "external_traffic_policy" {
54+
description = "The external traffic policy to apply to the ingress service. Cluster will open a valid NodePort on all nodes even if they aren't running an ingress pod and kubernetes will handle sending the traffic to the correct pod. Local will only have valid NodePorts on the nodes running ingress pods."
55+
type = string
56+
default = "Cluster"
57+
58+
validation {
59+
condition = (var.external_traffic_policy == "Local" || var.external_traffic_policy == "Cluster")
60+
error_message = "Invalid value for external_traffic_policy. Valid values are Local or Cluster."
61+
}
62+
}

0 commit comments

Comments
 (0)