How to expose more than 5 ports for a LoadBalancer k8s service in GCP

George Tseres
2 min readNov 4, 2021

Are you trying to create a LoadBalancer service in k8s and getting the following error when running kubectl describe service <service_name> ?

Too many ports specified. Maximum is 5

Then read along!

Creating a LoadBalancer type of service in Kubernetes (k8s) creates a load balancer in the cloud provider you are using. GCP creates a TCP load balancer and depending on the annotation you put on your service, it creates an external TCP load balancer, or an internal one. By default an external load balancer is created, but you can create an internal one instead by adding the following annotation in your service manifest:

"cloud.google.com/load-balancer-type": "Internal"

A limitation in GCP is the following though: A GCP TCP load balancer can expose up to 5 explicit ports (or all ports as noted in the documentation). The link is referencing the internal TCP load balancer documentation, but the same issue happens if you try to create an external one as well.

To overcome this in k8s, you need to have the GKE controller automatically configure the load balancer to expose all ports. To do that, as of the time of this writing, you either need to:

  • Upgrade your k8s cluster to version 1.20.6 or later, or 1.21 or later, or
  • Enable internal TCP/UDP load balancer subsetting in your cluster

You can reference the GCP documentation for further information.

Another approach would be to create multiple load balancer services and use a VIP (see proposed solution in Github for this), but we assume that you want to go with the simplest solution here.

Hope it helps! 🚀

--

--