创建混合集群

在 Anthos clusters on Bare Metal 中,混合集群担任管理员集群和用户集群的双重角色。它们运行工作负载,同时管理其他集群以及自身。

在资源受限的情况下,使用混合集群可使您免于运行单独的管理员集群,并提供可用性高 (HA) 的可靠性。在高可用性混合集群中,如果一个节点发生故障,其他节点将取代此节点。

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

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

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

前提条件

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

如需查看创建混合集群的分步说明,请参阅 Anthos clusters on Bare Metal 快速入门

启用 SELinux

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

Anthos clusters on Bare Metal 仅支持 RHEL 和 CentOS 系统中的 SELinux。

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

  1. 使用 gcloud auth application-default login 以用户身份登录 gcloud:
  2. 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 密钥文件的路径。
  3. 获取要用于创建集群的 Cloud 项目 ID:
  4. export CLOUD_PROJECT_ID=$(gcloud config get-value project)
    

使用 bmctl 创建混合集群

登录 gcloud 并设置项目后,您可以使用 bmctl 命令创建集群配置文件。请注意,在此示例中,所有服务帐号都由 bmctl create config 命令自动创建:

bmctl create config -c HYBRID_CLUSTER_NAME --enable-apis \
    --create-service-accounts --project-id=CLOUD_PROJECT_ID

以下示例展示了如何为与项目 ID my-gcp-project 关联且名为 hybrid1 的混合集群创建配置文件:

bmctl create config -c hybrid1 --create-service-accounts --project-id=my-gcp-project

该文件会写入 bmctl-workspace/hybrid1/hybrid1.yaml。

除了自动启用 API 和创建服务帐号外,您还可以为现有服务帐号提供适当的 IAM 权限。也就是说,您可以在上一步中跳过 bmctl 命令中的自动服务帐号创建部分:

bmctl create config -c hybrid1

修改集群配置文件

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

  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. 您必须使用 Connect 将集群注册到项目队列

    • 如果您使用自动 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
      # containerRuntime specifies which container runtime to use for scheduling containers on nodes.
      # containerd and docker are supported.
      containerRuntime: containerd
    ....
    

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

    默认容器运行时环境为 containerd。或者,您也可以使用 Docker。如需详细了解如何更改运行时,请参阅我们的更改容器运行时指南

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

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

使用 bmctl 命令部署集群:

bmctl create cluster -c CLUSTER_NAME

CLUSTER_NAME 指定您在上一部分中创建的集群名称。

以下示例命令为名为 hybrid1 的集群创建配置文件:

bmctl create cluster -c hybrid1

混合集群配置示例

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