This page shows you how to deploy an external LoadBalancer Service that builds a backend service-based regional external passthrough Network Load Balancer. Before reading this page, ensure that you're familiar with the following:
To learn more about regional external passthrough Network Load Balancers in general, see Backend service-based regional external passthrough Network Load Balancer.
Before you begin
Before you start, make sure that you have performed the following tasks:
- Enable the Google Kubernetes Engine API. Enable Google Kubernetes Engine API
- To use the Google Cloud CLI for this task,
install and then
initialize the
gcloud CLI. If you previously installed the gcloud CLI, get the latest
version by running the
gcloud components updatecommand. Earlier gcloud CLI versions might not support running the commands in this document.
Requirements
This tutorial uses
spec.loadBalancerClassto create a backend service-based regional external passthrough Network Load Balancer withGCE_VM_IPNEG backends. This tutorial requires GKE version 1.33.1-gke.1779000 or later becausespec.loadBalancerClassrequires GKE version 1.33.1-gke.1779000 or later. Version 1.33.1-gke.1779000 or later also exceeds minimum GKE version requirements for NEG backends or the weighted load balancer feature.If your cluster is on a version earlier than 1.36, the
HttpLoadBalancingadd-on must be enabled in your cluster. This add-on is enabled by default. For more information see HttpLoadBalancing prerequisite.To enable Cloud Logging for an external LoadBalancer Service, your GKE cluster must use version 1.36.0-gke.2459000 or later.
To use bring your own IP (BYOIP) IPv6 addresses with a single-stack IPv6 external LoadBalancer Service, your GKE cluster must use version 1.37.1-gke.2097000 or later.
Choose a cluster
You can create a new cluster or choose an existing cluster that meets the requirements.
Create a new cluster
Autopilot
To create a new Autopilot cluster:
gcloud container clusters create-auto CLUSTER_NAME \
--release-channel=RELEASE_CHANNEL \
--cluster-version=VERSION \
--location=COMPUTE_LOCATION
Replace the following:
CLUSTER_NAME: the name of the new cluster.RELEASE_CHANNEL: the name of the GKE release channel for the cluster.VERSION: the GKE version for the cluster.COMPUTE_LOCATION: the Compute Engine region of the cluster.
To disable automatic VPC firewall rule creation for
LoadBalancer Services, include the --disable-l4-lb-firewall-reconciliation
flag. For more information, see User-managed firewall rules for GKE
LoadBalancer Services.
Standard
To create a new Standard cluster:
gcloud container clusters create CLUSTER_NAME \
--release-channel=RELEASE_CHANNEL \
--cluster-version=VERSION \
--location=COMPUTE_LOCATION
Replace the following:
CLUSTER_NAME: the name of the new cluster.RELEASE_CHANNEL: the name of the GKE release channel for the cluster.VERSION: the GKE version for the cluster.COMPUTE_LOCATION: the Compute Engine region of the cluster.
To disable automatic VPC firewall rule creation for
LoadBalancer Services, include the --disable-l4-lb-firewall-reconciliation
flag. For more information, see User-managed firewall rules for GKE
LoadBalancer Services.
Upgrade an existing cluster
Use the gcloud CLI to update an existing cluster:
gcloud container clusters upgrade CLUSTER_NAME \
--cluster-version=VERSION \
--master \
--location=COMPUTE_LOCATION
Replace the following:
CLUSTER_NAME: the name of the existing cluster.VERSION: the specific GKE version to which you want to upgrade your cluster. For more information, see Manually upgrading the control plane.COMPUTE_LOCATION: the Compute Engine region of the cluster.
To disable automatic VPC firewall rule creation for
LoadBalancer Services, include the --disable-l4-lb-firewall-reconciliation
flag. For more information, see User-managed firewall rules for GKE
LoadBalancer Services.
Deploy a sample workload
Deploy the following sample workload which provides the serving Pods for the external LoadBalancer Service.
Save the following sample Deployment as
store-deployment.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: store spec: replicas: 20 selector: matchLabels: app: store template: metadata: labels: app: store spec: containers: - image: registry.k8s.io/echoserver:1.10 imagePullPolicy: Always name: echoserver ports: - name: http containerPort: 8080 readinessProbe: httpGet: path: /healthz port: 8080 scheme: HTTPApply the manifest to the cluster:
kubectl apply -f store-deployment.yamlVerify that there are 20 serving Pods for the Deployment:
kubectl get podsThe output is similar to the following:
NAME READY STATUS RESTARTS AGE store-cdb9bb4d6-s25vw 1/1 Running 0 10s store-cdb9bb4d6-vck6s 1/1 Running 0 10s ....
Create the external LoadBalancer Service
Expose the sample workload by creating an external LoadBalancer Service.
Save the following Service manifest as
store-v1-lb-svc.yaml:apiVersion: v1 kind: Service metadata: name: store-v1-lb-svc spec: type: LoadBalancer loadBalancerClass: networking.gke.io/l4-regional-external selector: app: store ports: - name: tcp-port protocol: TCP port: 8080 targetPort: 8080Apply the manifest to the cluster:
kubectl apply -f store-v1-lb-svc.yaml
Note the following points about this sample manifest:
If your cluster runs GKE 1.33.1-gke.1779000 to 1.36, you must set the
spec.loadBalancerClassfield to thenetworking.gke.io/l4-regional-externalvalue at the time the manifest is first applied to the cluster. This field instructs GKE to create a backend service-based regional external passthrough Network Load Balancer. In GKE 1.37 and later, this configuration is the default behavior and the field is not required. Backend service-based regional external passthrough Network Load Balancers are required to support features like IPv6 traffic and weighted load balancing.GKE uses either
GCE_VM_IPNEG backends or unmanaged instance group backends, depending on the cluster's version. In clusters with version 1.32.2-gke.1652000, the backend service-based regional external passthrough Network Load Balancer usesGCE_VM_IPNEGs. In previous versions, the backend service-based regional external passthrough Network Load Balancer uses unmanaged instance groups.This
spec.loadBalancerClassfield is immutable and must be set when you create the Service. Because this field is immutable, you can't add or modify it on the manifest of an existing LoadBalancer Service. Any attempt to do so will result in an error and the Service update will fail.To create a single-stack IPv6 Service in a dual-stack cluster, set the value of the
spec.ipFamilyPolicyfield toSingleStackand set the value of thespec.ipFamiliesfield toIPv6. In a single-stack IPv6 cluster, these are the default values and you can omit them. For more information, see IPv6 LoadBalancer Services.
Create an external LoadBalancer Service on a single-stack IPv6 cluster
When you create a single-stack IPv6 external LoadBalancer Service on a
single-stack IPv6 cluster or
a dual-stack cluster, the
subnetwork used by the load balancer depends on whether your cluster's node
subnetwork uses an EXTERNAL or INTERNAL IPv6 access type. On a dual-stack
cluster, you must set the value of the spec.ipFamilyPolicy field to
SingleStack and set the value of the spec.ipFamilies field to IPv6 (on a
single-stack IPv6 cluster, these are the default values and you can omit them).
IPv6 external LoadBalancer Services always use a backend-service based
regional external passthrough Network Load Balancer in Premium Tier. The
following examples set the spec.loadBalancerClass field to
networking.gke.io/l4-regional-external explicitly, although this load
balancer variant is the default in GKE version 1.37 and later:
Clusters with external nodes (
EXTERNALGUA access type): if your cluster nodes run on an external subnetwork with Global Unicast Addresses (GUA), the external load balancer automatically uses the existing node subnetwork. Create your Service without custom subnetwork annotations:apiVersion: v1 kind: Service metadata: name: ext-ipv6-svc spec: type: LoadBalancer loadBalancerClass: networking.gke.io/l4-regional-external ipFamilyPolicy: SingleStack ipFamilies: - IPv6 selector: app: store ports: - protocol: TCP port: 8080 targetPort: 8080Clusters with internal nodes (
INTERNALULA access type): if your cluster nodes run on an internal subnetwork with Unique Local Unicast Addresses (ULA), an external load balancer cannot use that subnetwork directly. You must provision a dedicated external (EXTERNAL) IPv6 subnetwork and reference it by using thenetworking.gke.io/load-balancer-subnetannotation on your Service:Create a dedicated external IPv6 subnetwork:
gcloud compute networks subnets create SUBNET_NAME \ --stack-type=ipv6_only \ --ipv6-access-type=EXTERNAL \ --network=NETWORK_NAME \ --region=COMPUTE_REGIONReplace the following:
SUBNET_NAME: the name of the subnet to create.NETWORK_NAME: the name of the VPC network in which you create this subnet.COMPUTE_REGION: the Compute Engine region for the subnet.
Create the Service referencing your dedicated subnetwork name in the
networking.gke.io/load-balancer-subnetannotation:apiVersion: v1 kind: Service metadata: name: ext-ipv6-svc annotations: networking.gke.io/load-balancer-subnet: SUBNET_NAME spec: type: LoadBalancer loadBalancerClass: networking.gke.io/l4-regional-external ipFamilyPolicy: SingleStack ipFamilies: - IPv6 selector: app: store ports: - protocol: TCP port: 8080 targetPort: 8080
Verify the assigned Service IP address:
kubectl get svc ext-ipv6-svcThe output confirms that the
CLUSTER-IPandEXTERNAL-IPcolumns (a public GUA address) are populated:NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ext-ipv6-svc LoadBalancer 2600:2d00:0:4:1a1e:d3b3:de4d:c401 2600:1900:4000:2714:8000:0:0:0 80:31114/TCP 51s
For more information, see IPv6 LoadBalancer Services.
Use bring your own IPv6 addresses (BYOIPv6)
In GKE version 1.37.1-gke.2097000 and later, you can configure a
single-stack IPv6 external LoadBalancer Service to use
bring your own IPv6 addresses (BYOIPv6) from a
regional IPv6 sub-prefix of a public delegated prefix (PDP) in
EXTERNAL_IPV6_FORWARDING_RULE_CREATION mode.
Before you create the Service, make sure that you meet the following requirements:
- Your cluster runs GKE version 1.37.1-gke.2097000 or later.
The cluster can be single-stack IPv6 or dual-stack (the Service must be
single-stack IPv6 in both cases), and the cluster's subnet can have either
an
INTERNALor anEXTERNALIPv6 access type; an external IPv6 subnet range isn't required because the address range comes from the public delegated prefix. - You have a regional IPv6 sub-prefix in
EXTERNAL_IPV6_FORWARDING_RULE_CREATIONmode with an allocatable prefix length of/96and theANNOUNCEDstatus, in the same region as your cluster. For instructions, see Create IPv6 sub-prefixes. The GKE service agent (
service-PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.com) has the Compute Public IP Admin role (roles/compute.publicIpAdmin) on the project that owns the public delegated prefix. To grant the role, run the following command:gcloud projects add-iam-policy-binding PDP_PROJECT_ID \ --member="serviceAccount:service-PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.com" \ --role="roles/compute.publicIpAdmin"Replace the following:
PDP_PROJECT_ID: the ID of the project that owns the public delegated prefix.PROJECT_NUMBER: the project number of the project that contains your cluster.
The Service is a single-stack IPv6 Service (
spec.ipFamilyPolicy: SingleStackandspec.ipFamilies: ["IPv6"]) that sets thespec.loadBalancerClassfield tonetworking.gke.io/l4-regional-externaland uses Premium Tier. Dual-stack Services, IPv4 Services, and Standard Tier aren't supported with BYOIPv6.The Service doesn't include the
networking.gke.io/load-balancer-subnetannotation, which can't be combined with thenetworking.gke.io/ip-collection-v6annotation.
Choose whether to allocate an ephemeral external BYOIPv6 address range or use a reserved static external BYOIPv6 address range:
Ephemeral external BYOIPv6 address range: add the
networking.gke.io/ip-collection-v6annotation to your Service manifest and set it to the full resource URL or the partial resource path of your regional sub-prefix:apiVersion: v1 kind: Service metadata: name: ext-byoipv6-svc annotations: networking.gke.io/ip-collection-v6: "projects/PROJECT_ID/regions/REGION/publicDelegatedPrefixes/SUB_PREFIX_NAME" spec: type: LoadBalancer loadBalancerClass: networking.gke.io/l4-regional-external ipFamilyPolicy: SingleStack ipFamilies: - IPv6 selector: app: store ports: - name: tcp-port protocol: TCP port: 80 targetPort: 8080Replace the following:
PROJECT_ID: the ID of the project that contains the sub-prefix.REGION: the region of your cluster.SUB_PREFIX_NAME: the name of your regional sub-prefix inEXTERNAL_IPV6_FORWARDING_RULE_CREATIONmode.
If you later change the
networking.gke.io/ip-collection-v6annotation, GKE re-creates the IPv6 forwarding rule with a new address from the new prefix, which briefly disrupts traffic to the Service. If you remove the annotation, the Service reverts to a Google-provided address from a subnet with an external IPv6 range. If your cluster's subnet has an internal IPv6 range, add thenetworking.gke.io/load-balancer-subnetannotation at the same time.Static external BYOIPv6 address range: reserve a regional static external
/96IPv6 address range from the sub-prefix and reference it by using thenetworking.gke.io/load-balancer-ip-addressesannotation:Create a regional static external IPv6 address range from your sub-prefix:
gcloud compute addresses create STATIC_BYOIPV6_NAME \ --region=REGION \ --ip-version=IPV6 \ --endpoint-type=NETLB \ --ip-collection=projects/PROJECT_ID/regions/REGION/publicDelegatedPrefixes/SUB_PREFIX_NAMEReplace the following:
STATIC_BYOIPV6_NAME: a name for the static IPv6 address resource.REGION: the region of your cluster.PROJECT_ID: the ID of the project that contains the sub-prefix.SUB_PREFIX_NAME: the name of your regional sub-prefix inEXTERNAL_IPV6_FORWARDING_RULE_CREATIONmode.
The
networking.gke.io/ip-collection-v6annotation is optional when you use a static external BYOIPv6 address range; if you set this annotation, the annotation must reference the same sub-prefix that you reserved the address range from.Reference the reserved static IPv6 address resource name in your Service manifest:
apiVersion: v1 kind: Service metadata: name: ext-static-byoipv6-svc annotations: networking.gke.io/load-balancer-ip-addresses: STATIC_BYOIPV6_NAME spec: type: LoadBalancer loadBalancerClass: networking.gke.io/l4-regional-external ipFamilyPolicy: SingleStack ipFamilies: - IPv6 selector: app: store ports: - name: tcp-port protocol: TCP port: 80 targetPort: 8080
For more information, see Bring your own IPv6 addresses (BYOIPv6).
Enable weighted load balancing
To distribute new connections proportionally to nodes based on how many serving,
ready, and non-terminating Pods are present on each node, enable weighted load
balancing by adding the networking.gke.io/weighted-load-balancing:
"pods-per-node" annotation to the Service manifest.
Add the
networking.gke.io/weighted-load-balancing: "pods-per-node"annotation to thestore-v1-lb-svc.yamlService manifest, and ensure that you also set theexternalTrafficPolicy: Localvalue so that it looks like this:apiVersion: v1 kind: Service metadata: name: store-v1-lb-svc annotations: networking.gke.io/weighted-load-balancing: "pods-per-node" spec: type: LoadBalancer loadBalancerClass: networking.gke.io/l4-regional-external externalTrafficPolicy: Local selector: app: store ports: - name: tcp-port protocol: TCP port: 8080 targetPort: 8080Apply the manifest to the cluster:
kubectl apply -f store-v1-lb-svc.yaml
Note the following about this example on weighted load balancing:
The Service manifest uses
externalTrafficPolicy: Local. If you don't need to enable weighted load balancing, you can also useexternalTrafficPolicy: Cluster. For details about how theexternalTrafficPolicydefines node grouping, which nodes pass their load balancer health checks, and how packets are processed, see LoadBalancer Service concepts.If you enable weighted load balancing, GKE doesn't prevent you from using
externalTrafficPolicy: Cluster, butexternalTrafficPolicy: Clustereffectively disables weighted load balancing because the packet might be routed, after the load balancer, to a different node.
You can also enable weighted load balancing on an existing external LoadBalancer
Service by using kubectl edit svc service-name. The
kubectl edit command opens the existing load balancer's Service manifest in
your configured text editor, where you can modify the manifest and save changes.
When you edit an existing external LoadBalancer Service, note the following
points:
The existing external LoadBalancer Service must have resulted in the creation of a backend service-based regional external passthrough Network Load Balancers. For clusters running GKE versions earlier than 1.37, this requirement means the existing external LoadBalancer Service must have the
spec.loadBalancerClassfield set tonetworking.gke.io/l4-regional-externalwhen the manifest was first applied to the cluster.Adding the
networking.gke.io/weighted-load-balancing: "pods-per-node"annotation to an existing external LoadBalancer Service that uses a target pool-based regional external passthrough Network Load Balancer has no effect.When updating the existing external LoadBalancer Service manifest, make sure to set the
externalTrafficPolicy: Localvalue. UsingexternalTrafficPolicy: Clustereffectively disables weighted load balancing because the packet might be routed, after the load balancer, to a different node.
Disable weighted load balancing
To distribute new connections to nodes regardless of how many serving Pods are
present on each node, disable weighted load balancing by removing the
networking.gke.io/weighted-load-balancing: "pods-per-node" annotation from the
Service manifest.
Enable logging for backend services
You can enable, disable, and view logs for a backend service-based regional external passthrough Network Load Balancer.
You configure Cloud Logging by creating an L4LBConfig custom resource that specifies your logging settings. The L4LBConfig resource is linked to a Kubernetes Service in the Service manifest. After you apply the manifest, the GKE controller configures logging for the backend service. If you don't link an L4LBConfig resource to the Service, the controller stops managing the logging configuration and leaves the backend service in its last known state. For more information, see Understand coast behavior.
You can configure the following fields:
enabled: if set totrue, enables logging for this Service and logs are available in Cloud Logging. Otherwise, logging is disabled for this Service.sampleRate: specifies a value from0through1,000,000(default), where0means no packets are logged and1,000,000means 100% of packets are logged. ThesampleRatefield is optional, and is relevant only if the value of theenablefield is set totrue.optionalMode: controls which optional metadata fields are included in the generated logs for the load balancer. The field accepts one of the following string values:EXCLUDE_ALL_OPTIONAL: default setting ifoptionalModeis not specified. When set, no optional fields are included in the logs, providing the most concise log format.INCLUDE_ALL_OPTIONAL: enables logging for all available optional fields, providing the most detailed log entries.CUSTOM: specifies a custom list of optional fields which are included in the logs. With theCUSTOMmode, you must also use theoptionalFieldsparameter to provide a comma-separated list of the fields that you want.
optionalFields: comma-separated string of field names. Defines the exact optional metadata fields that you want to include in the generated logs. This field is used only whenoptionalModeis set toCUSTOM, otherwise any values are ignored.
To configure Cloud Logging, create the L4LBConfig resource in the same namespace as your Service resource. The following example L4LBConfig manifest enables Cloud Logging and sets the sample rate to 50% of the requests for a given load balancer Service.
Save the following manifest as
svc-l4lb-config.yaml:apiVersion: networking.gke.io/v1 kind: L4LBConfig metadata: name: svc-l4lb-config namespace: default spec: logging: enabled: true # must be included sampleRate: 500000 optionalMode: "INCLUDE_ALL_OPTIONAL"Apply the L4LBConfig manifest to your cluster:
kubectl apply -f svc-l4lb-config.yamlAdd the
networking.gke.io/l4lb-configannotation to thestore-v1-lb-svc.yamlService manifest:apiVersion: v1 kind: Service metadata: name: store-v1-lb-svc annotations: cloud.google.com/l4-rbs: "enabled" networking.gke.io/l4lb-config: "svc-l4lb-config" spec: type: LoadBalancer selector: app: store ports: - name: tcp-port protocol: TCP port: 8080 targetPort: 8080Apply the Service manifest to your cluster:
kubectl apply -f store-v1-lb-svc.yaml
Verify the external LoadBalancer Service and its components
Verify that your Service is running:
kubectl get svc store-v1-lb-svcThe output is similar to the following:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE store-v1-lb-svc LoadBalancer 10.44.196.160 35.193.28.231 8080:32466/TCP 11mGKE assigned an
EXTERNAL_IPfor the regional external passthrough Network Load Balancer.Test connecting to the load balancer:
curl EXTERNAL_IP:PORTReplace the following:
EXTERNAL_IP: the allocated IP address for the regional external passthrough Network Load Balancer.PORT: the allocated port number for the regional external passthrough Network Load Balancer.
The output is similar to the following:
Hostname: store-v1-lb-svc-cdb9bb4d6-hflxd Pod Information: -no pod information available- Server values: server_version=nginx: 1.13.3 - lua: 10008 Request Information: client_address=10.128.0.50 method=GET real path=/ query= request_version=1.1 request_scheme=http request_uri=EXTERNAL_IP Request Headers: accept=*/* host=EXTERNAL_IP user-agent=curl/7.81.0 Request Body: -no body in request-Check your LoadBalancer Service and its set of annotations describing its Google Cloud resources:
kubectl describe svc store-v1-lb-svcThe output is similar to the following:
Name: store-v1-lb-svc Namespace: default Labels: <none> Annotations: cloud.google.com/neg-status: {"network_endpoint_groups":{"0":"k8s2-qvveq1d8-default-my-service-ext-5s55db85"},"zones":["us-central1-c"]} #This annotation appears in the output only if the service uses NEG backends. networking.gke.io/weighted-load-balancing: pods-per-node #This annotation appears in the output only if weighted load balancing is enabled. service.kubernetes.io/backend-service: k8s2-qvveq1d8-default-my-service-ext-5s55db85 service.kubernetes.io/firewall-rule: k8s2-qvveq1d8-default-my-service-ext-5s55db85 service.kubernetes.io/firewall-rule-for-hc: k8s2-qvveq1d8-default-my-service-ext-5s55db85-fw service.kubernetes.io/healthcheck: k8s2-qvveq1d8-default-my-service-ext-5s55db85 service.kubernetes.io/tcp-forwarding-rule: a808124abf8ce406ca51ab3d4e7d0b7d Selector: app=store Type: LoadBalancer IP Family Policy: SingleStack IP Families: IPv4 IP: 10.18.102.23 IPs: 10.18.102.23 LoadBalancer Ingress: 35.184.160.229 Port: tcp-port 8080/TCP TargetPort: 8080/TCP NodePort: tcp-port 31864/TCP Endpoints: 10.20.1.28:8080,10.20.1.29:8080 Session Affinity: None External Traffic Policy: Local HealthCheck NodePort: 30394 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ADD 4m55s loadbalancer-controller default/my-service-extThere are several fields that indicate that a backend service-based regional external passthrough Network Load Balancer and its Google Cloud resources were successfully created:
Eventsfield. This field is empty when the LoadBalancer Service and its resources were created successfully. If an error has occurred, it is listed here.List of
Annotationsenabled: GKE adds the following list of read-only annotations to the Service manifest. Each annotation whose name begins withservice.kubernetes.io/is used to indicate the name of a Google Cloud resource created as part of or to support the load balancer.- The
networking.gke.io/weighted-load-balancing: pods-per-nodeannotation indicates that weighted load balancing was applied and the load balancer distributes traffic to backend Pods based on the number of Pods running on each node. - The
service.kubernetes.io/backend-serviceannotation indicates the name of the load balancer's backend service. - The
service.kubernetes.io/healthcheckannotation indicates the name of the load balancer health check used by the backend service. - The
service.kubernetes.io/tcp-forwarding-ruleorservice.kubernetes.io/udp-forwarding-ruleannotation indicates the name of the load balancer's forwarding rule. - The
service.kubernetes.io/firewall-ruleannotation indicates the name of the firewall rule created to permit traffic to the cluster nodes. Source ranges for this firewall rule are customizable usingspec.loadBalancerSourceRanges[]. For additional detail about firewall rules for LoadBalancer Services, see Firewall rules and source IP address allowlist. - The
service.kubernetes.io/firewall-rule-for-hcannotation indicates the name of the firewall rule required for load balancer health checks. The
cloud.google.com/neg-statusannotation indicates both the NEGs that are used by the load balancer and their zones. This annotation is only present when both of the following are true:- The cluster was running GKE version 1.32.2-gke.1652000 or later when the manifest was applied to the cluster.
- The
spec.loadBalancerClassfield that was set to thenetworking.gke.io/l4-regional-externalvalue was present in the Service manifest when it was applied to the cluster.
- The
Verify that load balancer resources and firewall rules have been created for the external LoadBalancer Service:
To see the forwarding rule, run the following command:
gcloud compute forwarding-rules describe FWD_RULE_NAME \ --region=REGION_NAMEReplace the following:
FWD_RULE_NAME: the forwarding rule name provided by either theservice.kubernetes.io/tcp-forwarding-ruleorservice.kubernetes.io/udp-forwarding-ruleread-only annotations. To check these annotations, runkubectl describe svc SERVICE_NAME.REGION_NAME: the Google Cloud region containing the cluster. For zonal clusters, the region contains the zone used by the cluster.
To see the backend service, run the following command:
gcloud compute backend-services describe BACKEND_SERVICE_NAME \ --region=REGION_NAMEReplace the following:
BACKEND_SERVICE_NAME: the name of the backend service provided by theservice.kubernetes.io/backend-serviceread-only annotation. To check this read-only annotation, runkubectl describe svc SERVICE_NAME.REGION_NAME: the Google Cloud region containing the cluster. For zonal clusters, the region contains the zone used by the cluster.
To see the load balancer health check, run the following command:
gcloud compute health-checks describe HEALTH_CHECK_NAME \ --region=REGION_NAMEReplace the following:
HEALTH_CHECK_NAME: the load balancer's health check name. The name of the health check is provided by theservice.kubernetes.io/healthcheckread-only annotation. To check this read-only annotation, runkubectl describe svc SERVICE_NAME.REGION_NAME: the Google Cloud region containing the cluster. For zonal clusters, the region contains the zone used by the cluster.
To see the firewall rules, run the following commands:
gcloud compute firewall-rules describe FIREWALL_RULE_NAMEgcloud compute firewall-rules describe HEALTH_CHECK_FIREWALL_RULE_NAMEReplace the following:
FIREWALL_RULE_NAME: the name of the firewall rule that permits traffic to the load balancer. The name of this firewall rule is provided by theservice.kubernetes.io/firewall-ruleread-only annotation. To check this read-only annotation, runkubectl describe svc SERVICE_NAME.HEALTH_CHECK_FIREWALL_RULE_NAME: the name of the firewall rule that permits health checks of the load balancer's backends (the cluster's nodes). The name of this firewall rule is provided by theservice.kubernetes.io/firewall-rule-for-hcread-only annotation. To check this read-only annotation, runkubectl describe svc SERVICE_NAME.
To see the load balancer NEGs, run the following command:
gcloud compute network-endpoint-groups describe NEG_NAME \ --zone=ZONE_NAMEReplace the following:
NEG_NAME: the load balancer's NEG name. The name of the NEG is provided by thecloud.google.com/neg-statusread-only annotation. To check this read-only annotation, run thekubectl describe svc SERVICE_NAMEcommand. The annotation contains structured data with information about the NEG names and zones that are used by the load balancer. For zonal clusters, this annotation contains information about one NEG. For regional clusters, this annotation contains information about a NEG in each zone that the cluster is located in.ZONE_NAME: the Google Cloud zone that contains the NEG.
Verify that Cloud Logging has been enabled for the backend service:
kubectl get svc SERVICE_NAME --output yamlReplace the following:
SERVICE_NAME: the name of the backend service.
In the output, ensure that the
Status.Conditionsfield hasType: LoggingConfigManagedandReason: Reconciled.
Delete the external LoadBalancer Service
To delete the sample store-v1-lb-svc external LoadBalancer Service, use the
following command:
kubectl delete service store-v1-lb-svc
GKE automatically removes all load balancer resources that it created for the external LoadBalancer Service.
Migrate to GCE_VM_IP NEG backends
External LoadBalancer Services create backend service-based regional external passthrough Network Load Balancers if they are created in GKE 1.37 or later, specify the networking.gke.io/l4-regional-external value for the spec.loadBalancerClass field, or include the cloud.google.com/l4-rbs: "enabled" annotation. Depending on the GKE version of the cluster, GKE uses either GCE_VM_IP network endpoint groups or unmanaged instance groups as backends:
If the Service manifest was applied to a cluster running GKE version 1.32.2-gke.1652000 or later, the resulting regional external passthrough Network Load Balancer uses
GCE_VM_IPnetwork endpoint group (NEG) backends.If the Service manifest was applied to a cluster running an earlier GKE version, the resulting regional external passthrough Network Load Balancer uses unmanaged instance group backends.
For more information, see Node grouping in About LoadBalancer Services.
You can create a new external LoadBalancer Service powered by a backend
service-based regional external passthrough Network Load Balancer that uses GCE_VM_IP NEG backends if your
existing Service uses one of the following load balancers:
- A backend service-based regional external passthrough Network Load Balancer with instance group backends
- A target pool-based regional external passthrough Network Load Balancer
To preserve an external IPv4 address when you switch to a backend
service-based regional external passthrough Network Load Balancer that uses GCE_VM_IP NEG backends
(IPv6 external LoadBalancer Services don't require this migration):
If you haven't already, upgrade your cluster to GKE version 1.32.2-gke.1652000 or later.
Identify the external LoadBalancer Service that you want to switch to a backend service-based regional external passthrough Network Load Balancer using
GCE_VM_IPNEG backends. Describe the Service using the following command:kubectl describe svc SERVICE_NAME -n SERVICE_NAMESPACEReplace the following:
SERVICE_NAME: the name of the existing external LoadBalancer Service.SERVICE_NAMESPACE: the namespace of the existing external LoadBalancer Service.
In the command output, note the load balancer's external IPv4 address listed after
LoadBalancer Ingress.Retrieve the Service manifest for the existing LoadBalancer Service:
It's best if you have the original Service manifest that you applied to the cluster in the past. For example, you might have this in a source control repository.
If you don't have the original Service manifest:
Run the following command to get a YAML copy of the Service manifest that represents the load balancer current implementation:
kubectl get svc SERVICE_NAME -n SERVICE_NAMESPACE -o yamlCopy the manifest YAML to a text editor. Remove the
statusattribute and the followingmetadataattributes:- All of the following annotations:
- The
kubectl.kubernetes.io/last-applied-configurationannotation - All annotations that start with
service.kubernetes.io
- The
creationTimestampfinalizersresourceVersionuid
- All of the following annotations:
Ensure that the manifest includes the
spec.loadBalancerClassfield set to thenetworking.gke.io/l4-regional-externalvalue. If you're migrating from target pool-based regional external passthrough Network Load Balancer the field needs to be added.
Save the manifest file and note where you've saved it. The rest of this procedure refers to the path where you've saved the manifest as MANIFEST_FILE_PATH.
Configure a static external IPv4 address resource to hold the external IPv4 address used by the existing load balancer:
gcloud compute addresses create IP_ADDRESS_NAME --region=CLUSTER_REGION --addresses LB_EXTERNAL_IPReplace the following:
IP_ADDRESS_NAME: the name of the static external IP address. The name must adhere to name conventions for Compute Engine resources.CLUSTER_REGION: the region that contains the cluster. For zonal clusters, this is the region that contains the cluster's zone.LB_EXTERNAL_IP: the external IPv4 address used by the current load balancer, determined in the second step of this procedure.
Verify that the static external IPv4 address resource was created:
gcloud compute addresses describe IP_ADDRESS_NAME --region=CLUSTER_REGIONReplace the variables as inidicated in the previous step.
Delete the existing Service:
kubectl delete svc SERVICE_NAME -n SERVICE_NAMESPACEAdd the following annotation to the
MANIFEST_FILE_PATHService manifest file:networking.gke.io/load-balancer-ip-addresses: IP_ADDRESS_NAMEFor more information about this annotation, see Static IP addresses in LoadBalancer Service parameters.
Apply the updated Service manifest to the cluster:
kubectl apply -f MANIFEST_FILE_PATH(Optional) Release the static IPv4 address resource.
gcloud compute addresses delete IP_ADDRESS_NAME --region=CLUSTER_REGION
Troubleshoot
This section describes issues that you might encounter when you configure a backend-service based external LoadBalancer Service.
Wrong external traffic policy for weighted load balancing
If you don't set the externalTrafficPolicy: Local value when you enable
weighted load balancing, you might get a warning event when you describe the
Service using the following command:
kubectl describe svc store-v1-lb-svc`
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning UnsupportedConfiguration 4m55s loadbalancer-controller Weighted load balancing by pods-per-node has no effect with External Traffic Policy: Cluster.
To effectively enable weighted load balancing, you must set the
externalTrafficPolicy: Local value.
Error creating an IPv6 load balancer in Standard Tier
If an IPv6 external LoadBalancer Service shows a SyncExternalLoadBalancerFailed
event with the
Network tier Standard is not supported for IPv6 External Load Balancer
message, remove the cloud.google.com/network-tier: Standard annotation from
the Service manifest. IPv6 external LoadBalancer Services are supported only in
Premium Tier.
Errors when configuring bring your own IPv6 addresses (BYOIPv6)
When you configure bring your own IPv6 addresses (BYOIPv6)
for an external LoadBalancer Service, you might see
SyncExternalLoadBalancerFailed warning events when you run the
kubectl describe service SERVICE_NAME command. The
event message starts with
Error ensuring Resource for L4 External LoadBalancer, err: followed by one
of the following errors:
networking.gke.io/ip-collection-v6 is currently only supported for IPv6-only Services: make sure that your Service manifest specifiesspec.ipFamilyPolicy: SingleStackandspec.ipFamilies: ["IPv6"].static IPv6 address "ADDRESS_NAME" is a BYOIP address from IP collection "SUB_PREFIX", which is currently only supported for IPv6-only Services: make sure that your Service manifest specifiesspec.ipFamilyPolicy: SingleStackandspec.ipFamilies: ["IPv6"]. This error indicates that the Service is dual-stack or IPv4, but references a static BYOIPv6 address.cannot specify both networking.gke.io/load-balancer-subnet ("SUBNET") and networking.gke.io/ip-collection-v6 ("SUB_PREFIX") for LoadBalancer: remove thenetworking.gke.io/load-balancer-subnetannotation from the Service manifest when you use thenetworking.gke.io/ip-collection-v6annotation.invalid networking.gke.io/ip-collection-v6 "VALUE": expected projects/PROJECT/regions/REGION/publicDelegatedPrefixes/NAMEorinvalid networking.gke.io/ip-collection-v6 "VALUE": the public delegated prefix must be in the cluster region REGION: set the annotation to the full resource URL or the partial resource path (projects/PROJECT_ID/regions/REGION/publicDelegatedPrefixes/SUB_PREFIX_NAME) of a sub-prefix in the same region as your cluster. A bare sub-prefix name isn't accepted.reserved address ("ADDRESS_NAME") is not valid, err: ipCollection mismatch, expected "SUB_PREFIX_A", actual: "SUB_PREFIX_B": make sure that thenetworking.gke.io/ip-collection-v6annotation matches the public delegated prefix that you used to reserve the static IPv6 address, or omit thenetworking.gke.io/ip-collection-v6annotation when you use thenetworking.gke.io/load-balancer-ip-addressesannotation.googleapi: Error 403: Required 'compute.publicDelegatedPrefixes.use' permission for 'SUB_PREFIX', forbidden: grant the Compute Public IP Admin role (roles/compute.publicIpAdmin) to the GKE service agent (service-PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.com) on the project that owns the public delegated prefix. GKE needs this permission every time it syncs the Service, so don't remove the role after GKE creates the Service.
What's next
- For a general overview of Load Balancer Services, see LoadBalancer Services.
- For a description of Load Balancer Services parameters, see LoadBalancer Service parameters.
- Troubleshoot load balancing in GKE.