创建混合集群

在 Google Distributed Cloud 中,混合集群执行管理员集群和用户集群的双重角色。他们运行工作负载,同时管理其他集群及自己。

混合集群无需在资源受限的场景中运行单独的管理员集群,并且可以提供高可用性 (HA) 可靠性。在高可用性混合集群中,如果一个节点发生故障,则其他节点会取代它。

混合集群与独立集群的区别在于,它们也可以管理其他集群。独立集群无法创建或管理其他集群。

但是,创建混合集群时,您需要在灵活性与安全性之间进行一定的权衡。由于混合集群自行管理,因此在同一集群上运行工作负载会增加敏感管理数据(如 SSH 密钥)面临安全风险的风险。

您可以使用 bmctl 命令创建具有高可用性 (HA) 控制平面的混合集群。bmctl 命令可以在单独的工作站上运行,也可以在其中一个混合集群节点上运行。

前提条件

  • 从 Cloud Storage 下载了最新的 bmctl (gs://anthos-baremetal-release/bmctl/1.29.100-gke.251/linux-amd64/bmctl)。
  • 运行 bmctl 的工作站与目标混合集群中的所有节点有网络连接。
  • 运行 bmctl 的工作站与目标混合集群的控制平面 VIP 建立了网络连接。
  • 用于创建混合集群的 SSH 密钥可供 root 用户使用,或者 SUDO 用户可以访问目标混合集群中的所有节点。
  • 连接注册服务帐号已配置为与 Connect 搭配使用。

如需了解创建混合集群的分步说明,请参阅 Google Distributed Cloud 快速入门

启用 SELinux

如果要启用 SELinux 来保护容器,则必须确保在所有宿主机上以 Enforced 模式启用 SELinux。从 Google Distributed Cloud 1.9.0 版或更高版本开始,您可以在创建集群或升级之前或之后启用或停用 SELinux。在 Red Hat Enterprise Linux (RHEL) 上,SELinux 默认处于启用状态。如果宿主机上停用了 SELinux,或者不确定,请参阅使用 SELinux 保护容器,了解如何启用 SELinux。

Google Distributed Cloud 仅在 RHEL 系统中支持 SELinux。

登录 gcloud 并创建集群配置文件

  1. 使用 gcloud auth application-default login 以用户身份登录 gcloud CLI:

    gcloud auth application-default login
    

    您需要拥有 Project Owner 或 Editor 角色,才能使用自动启用 API 和服务帐号创建功能,如以下部分所述。

    您还可以向用户添加以下 IAM 角色:

    • Service Account Admin
    • Service Account Key Admin
    • Project IAM Admin
    • Compute Viewer
    • Service Usage Admin

    或者,如果您已有具备这些角色的服务账号,请运行以下命令:

    export GOOGLE_APPLICATION_CREDENTIALS=JSON_KEY_FILE
    

    JSON_KEY_FILE 替换为服务帐号 JSON 密钥文件的路径。

  2. 获取要用于创建集群的 Google Cloud 项目 ID:

    export CLOUD_PROJECT_ID=$(gcloud config get-value project)
    

使用 bmctl 创建集群配置文件

登录 gcloud CLI 并设置项目后,您可以使用 bmctl 命令创建集群配置文件。

如需创建集群配置文件,请执行以下操作:

  1. 运行以下命令来创建集群配置文件,创建所有需要的服务帐号,并启用所有必需的 API:

    bmctl create config -c CLUSTER_NAME</var> --enable-apis \
        --create-service-accounts --project-id=CLOUD_PROJECT_ID
    

    请替换以下内容:

    • CLUSTER_NAME:您正在创建的集群的名称。
    • CLOUD_PROJECT_ID:您的 Google Cloud 项目的 ID。

    默认情况下,配置文件会写入 bmctl-workspace/CLUSTER_NAME/CLUSTER_NAME.yaml

    如果您已启用 API 并创建服务帐号,则可以在不启用 API 并创建服务帐号的情况下创建配置文件。此外,您也无需使用 --project-id 标志。但是,您需要在配置文件中手动指定凭据。

    如需详细了解命令选项,请参阅创建配置

修改集群配置文件

现在您已拥有了集群配置文件,请对其进行修改以执行以下更改:

  1. 提供 SSH 私钥以访问混合集群节点:

    # bmctl configuration variables. Because this section is valid YAML but not a valid Kubernetes
    # resource, this section can only be included when using bmctl to
    # create the initial admin/hybrid cluster. Afterwards, when creating user clusters by directly
    # applying the cluster and node pool resources to the existing cluster, you must remove this
    # section.
    gcrKeyPath: bmctl-workspace/.sa-keys/my-gcp-project-anthos-baremetal-gcr.json
    sshPrivateKeyPath: /path/to/your/ssh_private_key
    gkeConnectAgentServiceAccountKeyPath: bmctl-workspace/.sa-keys/my-gcp-project-anthos-baremetal-connect.json
    gkeConnectRegisterServiceAccountKeyPath: bmctl-workspace/.sa-keys/my-gcp-project-anthos-baremetal-register.json
    cloudOperationsServiceAccountKeyPath: bmctl-workspace/.sa-keys/my-gcp-project-anthos-baremetal-cloud-ops.json
    
  2. 舰队注册集群。您在 bmctl create config 命令中指定的项目 ID 将自动添加到集群配置文件的 gkeConnect.projectID 字段中。此项目即为舰队宿主项目

    • 如果您使用自动 API 启用和服务帐号创建功能创建了配置文件,则可以跳过此步骤。
    • 如果您未使用自动启用 API 和服务帐号创建功能来创建配置文件,请在集群配置文件的相应 gkeConnectAgentServiceAccountKeyPathgkeConnectRegisterServiceAccountKeyPath 字段中引用下载的服务帐号 JSON 密钥。
  3. 更改配置以指定 hybrid 集群类型而不是 admin

    spec:
      # Cluster type. This can be:
      #   1) admin:  to create an admin cluster. This can later be used to create user clusters.
      #   2) user:   to create a user cluster. Requires an existing admin cluster.
      #   3) hybrid: to create a hybrid cluster that runs admin cluster components and user workloads.
      #   4) standalone: to create a cluster that manages itself, runs user workloads, but does not manage other clusters.
      type: hybrid
    
  4. 更改配置以指定多节点、高可用性的控制平面。您需要指定奇数个节点,以通过多数仲裁实现高可用性:

      # Control plane configuration
      controlPlane:
        nodePoolSpec:
          nodes:
          # Control plane node pools. Typically, this is either a single machine
          # or 3 machines if using a high availability deployment.
          - address: 10.200.0.4
          - address: 10.200.0.5
          - address: 10.200.0.6
    
  5. 指定集群节点的 pod 密度:

    ....
    # NodeConfig specifies the configuration that applies to all nodes in the cluster.
    nodeConfig:
      # podDensity specifies the pod density configuration.
      podDensity:
        # maxPodsPerNode specifies at most how many pods can be run on a single node.
        maxPodsPerNode: 250
    ....
    

    对于混合集群,允许的 maxPodsPerNode 值为 32-250(HA 集群)和 64-250(非 HA 集群)。如果未指定 maxPodsPerNode,则默认值为 110。集群创建后,此值无法更新。

    pod 密度也受集群的可用 IP 资源的限制。如需了解详情,请参阅 pod 网络

使用集群配置创建混合集群

使用 bmctl 命令创建集群:

bmctl create cluster -c CLUSTER_NAME

CLUSTER_NAME 替换为您在上一部分创建集群配置文件时使用的名称。

以下示例展示了用于创建名为 hybrid1 的集群的命令:

bmctl create cluster -c hybrid1

混合集群配置示例

如需查看混合集群配置示例,请参阅集群配置示例中的混合集群