简体   繁体   English

在 GKE 中使用共享网络创建集群

[英]Create cluster with Shared Network in GKE

I'm trying to create a cluster in GKE project-1 with shared network of project-2.我正在尝试在 GKE 项目 1 中创建一个具有项目 2 共享网络的集群。

Roles given to Service account:赋予服务帐户的角色:
project-1: Kubernetes Engine Cluster Admin, Compute Network Admin, Kubernetes Engine Host Service Agent User项目 1:Kubernetes 引擎集群管理员,计算网络管理员,Kubernetes 引擎主机服务代理用户
project-2: Kubernetes Engine Service Agent, Compute Network User, Kubernetes Engine Host Service Agent User项目2:Kubernetes引擎服务代理,计算网络用户,Kubernetes引擎主机服务代理用户

Service Account is created under project-1.服务帐户在 project-1 下创建。 API & Services are enabled in both Projects. API & 服务在两个项目中都启用。

But I am getting this error persistently.但我一直收到这个错误。 Error: googleapi: Error 403: Kubernetes Engine Service Agent is missing required permissions on this project.错误:googleapi:错误 403:Kubernetes 引擎服务代理缺少此项目所需的权限。 See Troubleshooting |请参阅故障排除 | Kubernetes Engine Documentation | Kubernetes 发动机文档 | Google Cloud for more info: required “container.hostServiceAgent.use” permission(s) for “projects/project-2”., forbidden谷歌云了解更多信息:“projects/project-2”需要“container.hostServiceAgent.use”权限。,禁止

data "google_compute_network" "shared_vpc" {
    name = "network-name-in-project-2"
    project = "project-2"
}

 
data "google_compute_subnetwork" "shared_subnet" {
    name = "subnet-name-in-project-2"
    project = "project-2"
    region = "us-east1"
}

 # cluster creation under project 1
 # project 1 specified in Provider 
resource "google_container_cluster" "mowx_cluster" {
    name = var.cluster_name
    location = "us-east1"
    initial_node_count = 1
 
    master_auth {
        username = ""
        password = ""
 
        client_certificate_config {
            issue_client_certificate = false
        }
    }
 
    remove_default_node_pool = true
    cluster_autoscaling {
        enabled = false
    }
 
    # cluster_ipv4_cidr = var.cluster_pod_cidr
    ip_allocation_policy {
        cluster_secondary_range_name = "pods"
        services_secondary_range_name = "svc"
    }
 
    network = data.google_compute_network.shared_vpc.id
    subnetwork = data.google_compute_subnetwork.shared_subnet.id
}

This is a community wiki answer based on the discussion in the comments and posted for better visibility.这是基于评论中讨论的社区 wiki 答案,并发布以提高可见性。 Feel free to expand it.随意扩展它。

The error you encountered:您遇到的错误:

Error: googleapi: Error 403: Kubernetes Engine Service Agent is missing required permissions on this project. See Troubleshooting | Kubernetes Engine Documentation | Google Cloud for more info: required “container.hostServiceAgent.use” permission(s) for “projects/project-2”., forbidden

means that the necessary service agent was not created:表示未创建必要的服务代理:

roles/container.serviceAgent - Kubernetes Engine Service Agent: roles/container.serviceAgent /container.serviceAgent - Kubernetes 引擎服务代理:

Gives Kubernetes Engine account access to manage cluster resources.授予 Kubernetes 引擎帐户访问权限以管理集群资源。 Includes access to service accounts.包括对服务帐户的访问。

The official troubleshooting docs describe a solution for such problems:官方故障排除文档描述了此类问题的解决方案:

To resolve the issue, if you have removed the Kubernetes Engine Service Agent role from your Google Kubernetes Engine service account, add it back.要解决此问题,如果您已从 Google Kubernetes Engine 服务帐户中删除Kubernetes Engine Service Agent角色,请将其重新添加。 Otherwise, you must re-enable the Kubernetes Engine API, which will correctly restore your service accounts and permissions.否则,您必须重新启用 Kubernetes 引擎 API,这将正确恢复您的服务帐户和权限。 You can do this in the gcloud tool or the Cloud Console.您可以在 gcloud 工具或 Cloud Console 中执行此操作。

The solution above works as in your use case the account was missing so it had to be (re)created.上面的解决方案在您的用例中起作用,因为帐户丢失了,因此必须(重新)创建它。

For me, even though the gke serice account existed and had the roles Kubernetes Engine Host Service Agent and Kubernetes Engine Service Agent in the both the service and host projects, I still got the 443 error.对我来说,即使 gke serice 帐户存在并且在服务和主机项目中具有角色Kubernetes Engine Host Service AgentKubernetes Engine Service Agent ,我仍然收到 443 错误。

The problem was that the service account needed to have roles/compute.networkUser and roles/compute.instanceAdmin applied to the VPC's subnetwork binding of the VPC.问题是服务帐户需要将roles/compute.networkUserroles/compute.instanceAdmin应用于 VPC 的 VPC 的子网绑定。

See: resource google_compute_subnetwork_iam_binding See also module "shared_vpc_access"请参阅:资源google_compute_subnetwork_iam_binding另请参阅module "shared_vpc_access"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM