Remotely access a private cluster using a bastion host Stay organized with collections Save and categorize content based on your preferences.
This tutorial shows you how to access aprivate cluster in Google Kubernetes Engine (GKE)over the internet by using abastion host.
You can create GKE private clusters withno client access to the public endpoint.This access option improves the cluster security by preventing all internetaccess to the control plane. However, disabling access to the public endpointprevents you from interacting with your cluster remotely, unless you add the IPaddress of your remote client as anauthorized network.
This tutorial shows you how to set up abastion host, which is a special-purposehost machine designed to withstand attack. The bastion host usesTinyproxy toforward client traffic to the cluster. You useIdentity-Aware Proxy (IAP) tosecurely access the bastion host from your remote client.
Note: This tutorial provides instructions for working with this app: Tinyproxy. The instructions might not represent newer versions of the app. For more information, see the documentation:Tinyproxy.Objectives
- Create a private cluster with no access to the public endpoint.
- Deploy a Compute Engine virtual machine (VM) to act as a bastion hostin the cluster subnet.
- Use IAP to connect a remote client to the cluster overthe internet.
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
- 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, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 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 GKE, Compute Engine, Identity-Aware Proxy 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.Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
After initializing the gcloud CLI, update it and install the required components:
gcloudcomponentsupdategcloudcomponentsinstallalpha beta
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 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 GKE, Compute Engine, Identity-Aware Proxy 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.Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Toinitialize the gcloud CLI, run the following command:
gcloudinit
After initializing the gcloud CLI, update it and install the required components:
gcloudcomponentsupdategcloudcomponentsinstallalpha beta
Create a private cluster
Create a new private cluster with no client access to the public endpoint. Placethe cluster in its own subnet. You can do this using the Google Cloud CLI orthe Google Cloud console.
gcloud
Run the following command:
gcloudcontainerclusterscreate-autoCLUSTER_NAME\--location=CONTROL_PLANE_LOCATION\--create-subnetwork=name=SUBNET_NAME\--enable-master-authorized-networks\--enable-private-nodes\--enable-private-endpointReplace the following:
CLUSTER_NAME: the name of the new cluster.CONTROL_PLANE_LOCATION: the Compute Engineregion of the control plane of yourcluster.SUBNET_NAME: the name of the new subnetwork in whichyou want to place the cluster.
Console
Create a Virtual Private Cloud subnetwork
Go to theVPC networks page in the Google Cloud console.
Click thedefault network.
In theSubnets section, clickAdd subnet.
On theAdd a subnet dialog, specify the following:
- Name: A name for the new subnet.
- Region: A region for the subnet. This must be the same as thecluster region.
- IP address range: Specify
10.2.204.0/22or another range thatdoesn't conflict with other ranges in the VPC network. - ForPrivate Google Access, select theOn option.
ClickAdd.
Create a private cluster
Go to theGoogle Kubernetes Engine page in the Google Cloud console.
ClickCreate.
ClickConfigure forGKE Autopilot.
Specify aName andRegion for the new cluster. The region must bethe same as the subnet.
In theNetworking section, select thePrivate cluster option.
Clear theAccess control plane using its external IP address checkbox.
From theNode subnet drop-down list, select the subnet you created.
Optionally, configure other settings for the cluster.
ClickCreate.
You can also use a GKE Standard cluster with the--master-ipv4-cidr flagspecified.
Create a bastion host VM
Create a Compute Engine VM within the private cluster internal networkto act as a bastion host that can manage the cluster.
gcloud
Create a Compute Engine VM:
gcloudcomputeinstancescreateINSTANCE_NAME\--zone=COMPUTE_ZONE\--machine-type=e2-micro\--network-interface=no-address,network-tier=PREMIUM,subnet=SUBNET_NAMEReplace the following:
INSTANCE_NAME: the name of the VM.COMPUTE_ZONE: the Compute Engine zone forthe VM. Place this in the same region as the cluster.
Note: We recommend that you create the VM in the same VPC subnet as the cluster.If you create the VM in a different subnet, add the VM IP address rangeas anauthorized networkfor the cluster.SUBNET_NAME: the subnetwork in which you wantto place the VM.
Console
Go to theVM instances page in the Google Cloud console.
ClickCreate instance.
Specify the following:
- Name: the name of your VM.
- Region andZone: the region and zone of your VM. Use the sameregion as your cluster.
- Machine type: a machine type. Choose a small machine type, suchas
e2-micro. - ForNetwork interfaces, select the same VPC networkand subnet as the cluster.
- Optionally, configure other settings for the instance.
ClickCreate.
Create firewall rule
To allow IAP to connect to your bastion host VM,create a firewall rule.
Deploy the proxy
Note: Some commands in this section require administrator privileges.With the bastion host and the private cluster configured, you must deploya proxy daemon in the host to forward traffic to the cluster control plane.For this tutorial, you installTinyproxy.
Start a session into your VM:
gcloudcomputesshINSTANCE_NAME--tunnel-through-iap--project=PROJECT_IDInstall Tinyproxy:
sudoaptinstalltinyproxyOpen the Tinyproxy configuration file:
sudovi/etc/tinyproxy/tinyproxy.confIn the file, do the following:
- Verify that the port is
8888. Search for the
Allowsection:/Allow 127Add the following line to the
Allowsection:Allow localhost
- Verify that the port is
Save the file and restart Tinyproxy:
sudoservicetinyproxyrestartExit the session:
exit
Connect to your cluster from the remote client
After configuring Tinyproxy, you must set up the remote client with clustercredentials and specify the proxy. Do the following on the remote client:
Get credentials for the cluster:
gcloudcontainerclustersget-credentialsCLUSTER_NAME\--location=CONTROL_PLANE_LOCATION\--project=PROJECT_IDReplace the following:
CLUSTER_NAME: the name of the private cluster.CONTROL_PLANE_LOCATION: the Compute Enginelocation of the control plane of yourcluster. Provide a region for regional clusters, or a zone for zonal clusters.PROJECT_ID: the ID of the Google Cloud projectof the cluster.
Tunnel to the bastion host using IAP:
gcloudcomputesshINSTANCE_NAME\--tunnel-through-iap\--project=PROJECT_ID\--zone=COMPUTE_ZONE\--ssh-flag="-4 -L8888:localhost:8888 -N -q -f"Specify the proxy:
exportHTTPS_PROXY=localhost:8888kubectlgetnsThe output is a list of namespaces in the private cluster.
Stop listening on the remote client
If you want to revert the change on the remote client at any time,you should end the listener process on TCP port 8888. The command to do thisis different depending on the client operating system.
netstat-lnpt|grep8888|awk'{print $7}'|grep-o'[0-9]\+'|sort-u|xargssudokillTroubleshooting
Firewall restrictions in enterprise networks
If you're on an enterprise network with a strict firewall, you might not be ableto complete this tutorial without requesting an exception. If you request anexception, the source IP range for the bastion host is35.235.240.0/20 bydefault.
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 bastion host that you deployed in this tutorial:
gcloudcomputeinstancesdeleteINSTANCE_NAME\--zone=COMPUTE_ZONEDelete the cluster:
gcloudcontainerclustersdeleteCLUSTER_NAME\--location=CONTROL_PLANE_LOCATIONDelete the subnet:
gcloudcomputenetworkssubnetsdeleteSUBNET_NAME\--region=CONTROL_PLANE_LOCATION
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 2026-02-18 UTC.