我运行以下命令
- 使用我的公司电子邮件 ID (ldap) 向 Google Cloud 进行身份验证
- 更新本地计算机上的 kubeconfig 文件
- 使用 kube-api-proxy 从本地计算机访问 k8s 控制平面。 (我使用此代理来访问控制平面,因为 GKE 控制平面 vpc 和我的公司网络之间没有 VPC 对等)
gcloud auth login --no-launch-browser ## I use corporate email id to authenticate
gcloud container clusters get-credentials <>gke_cluster_name> --region <region> --project <gcp_project>
export https_proxy=<kube_api_proxy>:8118 ## Proxy to connect to k8s controlplane
kubectl get no
每隔 1 小时,我必须重复上述步骤来重新进行身份验证,因为我会失败并出现以下错误,否则当我尝试连接到 k8S 时
Unable to connect to the server: error executing access token command "/usr/lib64/google-cloud-sdk/bin/gcloud
config config-helper --format=json": err=exit status 1 output= stderr=ERROR: gcloud crashed (TransportError):
HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token
(Caused by ProxyError('Cannot connect to proxy.',
OSError('Tunnel connection failed: 403 Request blocked by Privoxy')))
有没有办法可以增加这个超时时间,比如说 4 小时左右,因为我有一个运行时间超过 1 小时的作业,并且由于超时而在中间失败。
最佳答案
CLI gcloud 创建有效期为 3,600 秒的 OAuth 访问 token 。这是非组织项目支持的最大生命周期。这也是您正在使用的用户身份的最长生命周期。
要延长组织的 token 生命周期,您必须从服务帐户创建凭据,并设置支持生命周期为 12 小时的 token 的组织策略约束 constraints/iam.allowServiceAccountCredentialLifetimeExtension。 link
但是,我不知道在 CLI 中使用该约束而不修改 CLI 源代码的方法,它是用 Python 编写的。我从未进行过此更改,因为编写自己的代码要容易得多。
相反,编写您自己的 token 生成器。互联网上有很多源代码示例。我写了一篇文章,其中包含源代码 link 。将我的代码中的这一行更改为所需的时间:
# Set how long this token will be valid in seconds
expires_in = 3600 # Expires in 1 hour
总结:
- 您必须属于 Google Cloud 组织。
- 您必须通过服务帐户创建凭据。
- 您必须设置组织政策约束。
- 限制必须包含允许的服务帐户的电子邮件地址。
关于authentication - 如何增加 gcloud 重新身份验证超时,目前每 1 小时过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71312808/