使用Google IAM配置GKE服务的Web访问

42次阅读
没有评论

问题描述

在Google Kubernetes Engine(GKE)上托管一个应用程序,并希望允许来自组织内部的用户通过Web访问该应用程序。用户希望他们能够使用他们的Google账号IAM凭据登录。用户想知道是否有一种方法可以配置一个服务来暴露集群的Web端点,以便用户只需使用Google账号登录即可访问此服务。例如,在Cloud Shell中测试服务时,可以轻松进行Web预览,然后在浏览器中访问Web应用程序。用户想知道是否有一种方法可以配置此功能,以便任何在其组织中获得授权的用户都可以访问他们应用程序的Web界面。

解决方案

请注意以下操作可能受版本差异影响,执行前建议做好备份。

配置Google Cloud Identity-Aware Proxy (IAP) 和 Ingress

为了实现在GKE上通过Google账号登录访问应用程序的功能,您可以配置Google Cloud Identity-Aware Proxy (IAP) 以及使用Ingress暴露服务。以下是实现此目标的步骤:

  1. 创建一个名为 secure-ingress.yaml 的YAML文件,并将以下内容复制到文件中:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-secure-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "gce"
    kubernetes.io/ingress.allow-http: "false"
    kubernetes.io/ingress.global-static-ip-name: my-external-ip
spec:
  tls:
  - secretName: my-secret-cert
  backend:
    serviceName: my-service-be-web
    servicePort: 1234
---
kind: Service
apiVersion: v1
metadata:
  name: my-service-be-web
  namespace: default
  annotations:
    beta.cloud.google.com/backend-config: '{"default": "my-service-be-conf"}'
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 1234
      targetPort: 1234
      name: my-port-web
---
apiVersion: cloud.google.com/v1beta1
kind: BackendConfig
metadata:
  name: my-service-be-conf
  namespace: default
spec:
  iap:
    enabled: true
    oauthclientCredentials:
      secretName: my-secret-oath
  1. secure-ingress.yaml 文件中,您需要替换以下部分:
  2. my-external-ip:将其替换为您的外部IP地址的名称。
  3. my-secret-cert:将其替换为存储TLS证书的Secret的名称。
  4. my-service-be-web:将其替换为要暴露的后端服务的名称。
  5. my-app:将其替换为应用程序的标签或选择器。
  6. my-secret-oath:将其替换为包含OAuth客户端凭据的Secret的名称。

  7. 使用以下命令将配置应用到集群中:

kubectl apply -f secure-ingress.yaml

配置Google Cloud Identity-Aware Proxy (IAP)

为了配置IAP,您需要按照以下步骤操作:

  1. 打开Google Cloud Console,并导航到您的项目。
  2. 在左侧导航栏中,选择“安全性” > “身份验证”。
  3. 在Identity-Aware Proxy部分,找到您刚才创建的Ingress服务,并启用IAP。
  4. 在OAuth客户端凭据部分,选择您创建的OAuth客户端凭据的Secret。
  5. 配置其他IAP设置,如访问权限和登录选项,以满足您的需求。

完成上述步骤后,您的应用程序将通过Google账号IAM凭据进行保护,只有经过授权的用户才能访问。

总结

通过配置Google Cloud Identity-Aware Proxy (IAP) 和 Ingress,您可以实现在GKE上通过Google账号IAM凭据登录访问您的应用程序的功能。使用IAP可以确保只有经过授权的用户可以访问您的Web界面,从而增强了应用程序的安全性和访问控制。

正文完