Review exposure of Kubernetes LoadBalancer Services

Verify that the load balancer’s actual exposure matches the service requirements.

Description

Use type = "ClusterIP" for a Service needed only inside the cluster. If private-network clients also need access, use the internal load balancer option supported by your provider and controller.

Potential impact

An unnecessary public endpoint adds cost and provides an external path to data if the application or its authentication is vulnerable.

Remediation

For intentionally public services, restrict source networks and configure authentication and TLS. After deployment, check the assigned endpoint and any other public routes, including Ingress and proxies.

Examples

Both examples require separate Pods labeled app = "my-app" that listen on port 8080. The Service port is 80.

Before

hcl
resource "kubernetes_service" "example" {
  metadata {
    name = "terraform-example"
  }

  spec {
    selector = {
      app = "my-app"
    }

    port {
      port        = 80
      target_port = 8080
    }

    type = "LoadBalancer"
  }
}

After

hcl
resource "kubernetes_service" "example" {
  metadata {
    name = "terraform-example"
  }

  spec {
    selector = {
      app = "my-app"
    }

    port {
      port        = 80
      target_port = 8080
    }

    type = "ClusterIP"
  }
}

Explanation:

  • Before: LoadBalancer requests an endpoint. Provider settings determine whether it is public or internal.
  • After: ClusterIP stops this Service from requesting a load balancer. Confirm that any unwanted external route has closed.

References