Configure networking for a basic production cluster
This tutorial is intended for cloud architects and operations administratorsinterested in deploying a web application to a Google Kubernetes Engine (GKE)cluster and exposing it with an HTTPS load balancer.
Objectives
In this tutorial, you will learn how to:
- Create a GKE cluster.
- Create a global IP address and Cloud DNS zone with Terraform.
- Configure HTTPS load balancing.
- Deploy a sample web application.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use thepricing calculator.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, seeClean up.
Before you begin
Set up your project
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud console, on the project selector page, clickCreate project to begin creating a new Google Cloud project.
Roles required to create a project
To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.Verify that billing is enabled for your Google Cloud project.
Enable the Google Kubernetes Engine, Cloud DNS APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.In the Google Cloud console, on the project selector page, clickCreate project to begin creating a new Google Cloud project.
Roles required to create a project
To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.Verify that billing is enabled for your Google Cloud project.
Enable the Google Kubernetes Engine, Cloud DNS APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission.Learn how to grant roles.
- You must own a domain name. The domain name must be no longer than 63 characters. You can useGoogle Domains or another registrar.
Set up your environment
In this tutorial, you useCloud Shell to manage resources hosted onGoogle Cloud. Cloud Shell is preinstalled with the software you need forthis tutorial, includingTerraform,kubectl and thegcloud CLI.
Set environment variables:
PROJECT_ID=$(gcloudconfigget-valueproject)gcloudconfigsetproject$PROJECT_IDgcloudconfigsetcompute/regionus-central1Clone the code repository:
gitclonehttps://github.com/GoogleCloudPlatform/kubernetes-engine-samples.gitChange to the working directory:
cdkubernetes-engine-samples/autopilot/networking-tutorial
Create a GKE cluster
The following Terraform file creates a GKE cluster:
terraform{required_version="~> 1.3"}provider"google"{}variable"region"{type=stringdescription="Region where the cluster will be created."default="us-central1"}variable"cluster_name"{type=stringdescription="Name of the cluster"default="networking-cluster"}resource"google_container_cluster""default"{name=var.cluster_namedescription="Cluster for sample web application"location=var.regionenable_autopilot=trueip_allocation_policy{}}output"region"{value=var.regiondescription="Compute region"}output"cluster_name"{value=google_container_cluster.default.namedescription="Cluster name"}The following Terraform file creates a global IP address and Cloud DNSzone:
terraform{required_version="~> 1.3"}variable"base_domain"{type=stringdescription="Your base domain"}variable"name"{type=stringdescription="Name of resources"default="networking-tutorial"}data"google_client_config""current"{}resource"google_compute_global_address""default"{name=var.name}resource"google_dns_managed_zone""default"{name=var.namedns_name="${var.name}.${var.base_domain}."description="DNS Zone for web application"}resource"google_dns_record_set""a"{name=google_dns_managed_zone.default.dns_nametype="A"ttl=300managed_zone=google_dns_managed_zone.default.namerrdatas=[google_compute_global_address.default.address]}resource"google_dns_record_set""cname"{name=join(".", compact(["www",google_dns_record_set.a.name]))type="CNAME"ttl=300managed_zone=google_dns_managed_zone.default.namerrdatas=[google_dns_record_set.a.name]}output"dns_zone_name_servers"{value=google_dns_managed_zone.default.name_serversdescription="Write these virtual name servers in your base domain."}output"domain"{value=trim(google_dns_record_set.a.name,".")}Initialize Terraform:
terraforminitView the infrastructure changes:
terraformplanWhen prompted, enter your domain, such as
my-domain.net.Apply the Terraform configuration:
terraformapply--auto-approveWhen prompted, enter your domain, such as
my-domain.net.The output is similar to the following:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Outputs:cluster_name = "networking-cluster"region = "us-central1"
Create an external Application Load Balancer
The following manifest describes a ManagedCertificate, FrontendConfig,Deployment, Service, and Ingress:
---apiVersion:networking.gke.io/v1kind:ManagedCertificatemetadata:name:networking-managed-certspec:domains:-DOMAIN_NAME-www.DOMAIN_NAME---apiVersion:networking.gke.io/v1beta1kind:FrontendConfigmetadata:name:networking-fcspec:redirectToHttps:enabled:trueresponseCodeName:MOVED_PERMANENTLY_DEFAULT---apiVersion:apps/v1kind:Deploymentmetadata:name:frontendspec:selector:matchLabels:app:frontendreplicas:2template:metadata:labels:app:frontendspec:containers:-name:echo-amd64image:us-docker.pkg.dev/google-samples/containers/gke/hello-app-cdn:1.0---apiVersion:v1kind:Servicemetadata:name:frontendspec:type:LoadBalancerselector:app:frontendports:-name:httpport:80targetPort:8080---apiVersion:networking.k8s.io/v1kind:Ingressmetadata:name:frontendannotations:networking.gke.io/managed-certificates:networking-managed-certnetworking.gke.io/v1beta1.FrontendConfig:networking-fckubernetes.io/ingress.global-static-ip-name:networking-tutorialkubernetes.io/ingress.class:gcelabels:app:frontendspec:defaultBackend:service:name:frontendport:number:80Replace
DOMAIN_NAMEwith your domain name, suchasmy-domain.net.This manifest has the following properties:
networking.gke.io/managed-certificates: the name of theManagedCertificate.networking.gke.io/v1beta1.FrontendConfig: the name of the FrontendConfigresource.kubernetes.io/ingress.global-static-ip-name: the name of the IP address.kubernetes.io/ingress.class: instructs the GKE Ingresscontroller to create an external Application Load Balancer.
Apply the manifest to your cluster:
kubectlapply-fkubernetes-manifests.yamlVerify the Ingress was created:
kubectldescribeingressfrontendThe output is similar to the following:
... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ADD 2m loadbalancer-controller default/frontend Normal CREATE 1m loadbalancer-controller ip: 203.0.113.2...It might take several minutes for the Ingress to provision.
Test application
Check the status of the SSL certificate:
kubectlgetmanagedcertificates.networking.gke.ionetworking-managed-certThe SSL certificate might take up to 30 minutes to provision. The followingoutput indicates the SSL certificate is ready:
NAME AGE STATUSnetworking-managed-cert 28m ActiveRun a
curlcommand:curl-Lvhttps://DOMAIN_NAMEThe output is similar to the following:
* Trying 34.160.115.33:443...* Connected toDOMAIN_NAME (34.160.115.33) port 443 (#0)...* TLSv1.3 (IN), TLS handshake, Certificate (11):...* Server certificate:* subject: CN=DOMAIN_NAME...> Host:DOMAIN_NAME
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the project
Delete individual resources
Delete the kubernetes resources:
kubectldelete-fkubernetes-manifests.yamlDelete the Terraform resources:
terraformdestroy--auto-approveWhen prompted, enter your domain, such as
my-domain.net.
What's next
- Learn more aboutGKE networking.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-11-25 UTC.