本页介绍如何使用 Identity-Aware Proxy (IAP) 保护 Google Kubernetes Engine (GKE) 实例的安全。
概览
在 GKE 上,IAP 通过 Ingress 集成。因此,您无需使用 VPN 即可控制员工的资源级访问权限。
在 GKE 集群中,传入流量由 HTTP(S) 负载平衡服务处理,该服务是 Cloud Load Balancing 的一个组件。HTTP(S) 负载平衡器通常由 Kubernetes Ingress 控制器配置。Ingress 控制器会从与一个或多个 Service 对象关联的 Kubernetes Ingress 对象中获取配置信息。每个 Service 对象包含用于将传入请求定向到特定 Pod 和端口的路由信息。
从 Kubernetes 版本 1.10.5-gke.3 开始,您可以通过将 Service 与 BackendConfig 对象进行关联来添加负载平衡器配置。 BackendConfig 是 kubernetes/ingress-gce 代码库中定义的一个自定义资源定义 (CRD)。
Kubernetes Ingress 控制器会从 BackendConfig 读取配置信息并相应地设置负载平衡器。BackendConfig 包含特定于 Cloud Load Balancing 的配置信息,并可用于为每个 HTTP(S) 负载平衡后端服务定义单独的配置。
准备工作
要为 GKE 启用 IAP,您需要以下各项:
- Google Cloud 启用了结算功能的控制台项目。
- 由 HTTPS 负载平衡器处理的一个或多个 GKE 实例。当您在 GKE 集群中创建 Ingress 对象时,负载平衡器应自动建立。
- 已注册到负载平衡器地址的域名。
- 用于验证所有请求都拥有一个身份的应用代码。
- 了解如何获取用户的身份。
IAP 使用 Google 管理的 OAuth 客户端对用户进行身份验证。只有组织内的用户才能访问启用了 IAP 的应用。如果您想允许组织外部的用户访问,请参阅为外部应用启用 IAP。
启用 IAP
If you haven't configured your project's OAuth consent screen, you'll be prompted to do so. To configure your OAuth consent screen, see Setting up your OAuth consent screen.
If you are running GKE clusters version 1.24 or later, you can configure
IAP and GKE by using the Kubernetes Gateway API. To do so, complete
the following steps and then follow the instructions in
Configure IAP.
Do not configure BackendConfig.
Setting up IAP access
-
Go to the
Identity-Aware Proxy page.
Go to the Identity-Aware Proxy page - Select the project you want to secure with IAP.
-
Select the checkbox next to the resource you want to grant access to.
If you don't see a resource, ensure that the resource is created and that the BackendConfig Compute Engine ingress controller is synced.
To verify that the backend service is available, run the following gcloud command:
gcloud compute backend-services list - On the right side panel, click Add principal.
-
In the Add principals dialog that appears, enter the email addresses of groups or
individuals who should have the IAP-secured Web App User role for the project.
The following kinds of principals can have this role:
- Google Account: user@gmail.com
- Google Group: admins@googlegroups.com
- Service account: server@example.gserviceaccount.com
- Google Workspace domain: example.com
Make sure to add a Google Account that you have access to.
- Select Cloud IAP > IAP-secured Web App User from the Roles drop-down list.
- Click Save.
Configuring BackendConfig
You can configure a BackendConfig for IAP by adding an iap block.
Adding an iap block to the BackendConfig
To configure the BackendConfig for IAP, you need to specify the
enabled value. Ensure
that you have the compute.backendServices.update permission and add the
iap block to BackendConfig.
For GKE versions 1.16.8-gke.3 and higher, use the cloud.google.com/v1 API version.
If you are using an earlier GKE version, use cloud.google.com/v1beta1.
apiVersion: cloud.google.com/v1 kind: BackendConfig metadata: name: CONFIG_DEFAULT namespace: my-namespace spec: iap: enabled: true
Associate a service port with a BackendConfig
You also need to associate Service ports with your BackendConfig to trigger turning on IAP. One way to make this association is to make all ports for the service default to your BackendConfig, which you can do by adding the following annotation to your Service resource:
metadata: annotations: beta.cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}'
Verify the BackendConfig
To test the configuration, run kubectl get event. If you see the message
"no BackendConfig for service port exists", then you successfully
associated a service port with your BackendConfig, but the BackendConfig
resource wasn't found. This error can occur if you haven't created the BackendConfig resource,
created it in the wrong namespace, or misspelled the reference in the Service annotation.
Turning IAP off
To turn IAP off, you must set enabled to
false in the BackendConfig. If you delete the IAP
block from BackendConfig, the settings will persist. For example, if IAP is
enabled and you delete the block then IAP will still be turned on.