×

启用轮询分片算法

round-robin 分片算法仅为技术预览功能。技术预览功能不受 Red Hat 生产服务级别协议 (SLA) 的支持,并且可能功能不完整。Red Hat 不建议在生产环境中使用它们。这些功能可让客户提前访问即将推出的产品功能,从而能够在开发过程中测试功能并提供反馈。

有关 Red Hat 技术预览功能的支持范围的更多信息,请参阅 技术预览功能支持范围

默认情况下,Argo CD 应用控制器使用非统一的基于哈希的legacy分片算法将集群分配给分片。这可能导致集群分布不均匀。您可以启用round-robin分片算法以实现更均衡的集群分布。

在 Red Hat OpenShift GitOps 中使用round-robin分片算法具有以下优点:

  • 确保更均衡的工作负载分配

  • 防止分片过载或利用不足

  • 优化计算资源效率

  • 降低瓶颈风险

  • 提高 Argo CD 系统的整体性能和可靠性

引入替代分片算法允许根据具体用例进行进一步定制。您可以选择最符合部署需求的算法,从而在不同的操作场景中获得更大的灵活性和适应性。

为了利用 GitOps 中替代分片算法的优势,在部署过程中启用分片至关重要。

在 Web 控制台中启用轮询分片算法

您可以使用 OpenShift Container Platform Web 控制台启用round-robin分片算法。

先决条件
  • 您已在 OpenShift Container Platform 集群上安装了 Red Hat OpenShift GitOps Operator。

  • 您可以访问 OpenShift Container Platform Web 控制台。

  • 您可以访问具有cluster-admin权限的集群。

步骤
  1. 在 Web 控制台的**管理员**视角中,转到**Operators** → **已安装的 Operators**。

  2. 从已安装的 Operators 中单击**Red Hat OpenShift GitOps**,然后转到**Argo CD**选项卡。

  3. 单击您要启用round-robin分片算法的 Argo CD 实例,例如openshift-gitops

  4. 单击**YAML**选项卡并编辑 YAML 文件,如下例所示

    启用轮询分片算法的 Argo CD 实例示例
    apiVersion: argoproj.io/v1beta1
    kind: ArgoCD
    metadata:
      name: openshift-gitops
      namespace: openshift-gitops
    spec:
      controller:
        sharding:
          enabled: true (1)
          replicas: 3 (2)
        env: (3)
          - name: ARGOCD_CONTROLLER_SHARDING_ALGORITHM
            value: round-robin
        logLevel: debug (4)
    1 sharding.enabled参数设置为true以启用分片。
    2 将副本数设置为所需值,例如3
    3 将分片算法设置为round-robin
    4 将日志级别设置为debug,以便您可以验证每个集群连接到哪个分片。
  5. 单击**保存**。

    将出现成功通知警报:openshift-gitops 已更新到版本

    如果您编辑默认的openshift-gitops实例,则会显示**托管资源**对话框。再次单击**保存**以确认更改。

  6. 通过执行以下步骤,验证是否已启用分片且分片算法为round-robin

    1. 转到**工作负载** → **StatefulSets**。

    2. 从**项目**下拉列表中选择您安装 Argo CD 实例的命名空间。

    3. 单击**-application-controller**,例如**openshift-gitops-application-controller**,然后转到**Pods**选项卡。

    4. 观察已创建的应用程序控制器 Pod 的数量。它应与设置的副本数相对应。

    5. 单击要检查的控制器 Pod,然后转到**日志**选项卡以查看 Pod 日志。

      控制器 Pod 日志片段示例
      time="2023-12-13T09:05:34Z" level=info msg="ArgoCD Application Controller is starting" built="2023-12-01T19:21:49Z" commit=a3vd5c3df52943a6fff6c0rg181fth3248976299 namespace=openshift-gitops version=v2.9.2+c5ea5c4
      time="2023-12-13T09:05:34Z" level=info msg="Processing clusters from shard 1"
      time="2023-12-13T09:05:34Z" level=info msg="Using filter function:  round-robin" (1)
      time="2023-12-13T09:05:34Z" level=info msg="Using filter function:  round-robin"
      time="2023-12-13T09:05:34Z" level=info msg="appResyncPeriod=3m0s, appHardResyncPeriod=0s"
      1 查找"Using filter function: round-robin"消息。
    6. 在日志**搜索**字段中,搜索processed by shard以验证跨分片的集群分布是否均匀,如下例所示。

      确保您将日志级别设置为debug以查看这些日志。

      控制器 Pod 日志片段示例
      time="2023-12-13T09:05:34Z" level=debug msg="ClustersList has 3 items"
      time="2023-12-13T09:05:34Z" level=debug msg="Adding cluster with id= and name=in-cluster to cluster's map"
      time="2023-12-13T09:05:34Z" level=debug msg="Adding cluster with id=068d8b26-6rhi-4w23-jrf6-wjjfyw833n23 and name=in-cluster2 to cluster's map"
      time="2023-12-13T09:05:34Z" level=debug msg="Adding cluster with id=836d8b53-96k4-f68r-8wq0-sh72j22kl90w and name=in-cluster3 to cluster's map"
      time="2023-12-13T09:05:34Z" level=debug msg="Cluster with id= will be processed by shard 0" (1)
      time="2023-12-13T09:05:34Z" level=debug msg="Cluster with id=068d8b26-6rhi-4w23-jrf6-wjjfyw833n23 will be processed by shard 1" (1)
      time="2023-12-13T09:05:34Z" level=debug msg="Cluster with id=836d8b53-96k4-f68r-8wq0-sh72j22kl90w will be processed by shard 2" (1)
      
      1 在此示例中,3 个集群连续连接到分片 0、分片 1 和分片 2。

      如果集群数“C”是分片副本数“R”的倍数,则每个分片必须具有相同数量的分配集群“N”,等于“C”除以“R”。前面的示例显示 3 个集群和 3 个副本;因此,每个分片分配 1 个集群。

使用 CLI 启用轮询分片算法

您可以使用命令行界面启用round-robin分片算法。

先决条件
  • 您已在 OpenShift Container Platform 集群上安装了 Red Hat OpenShift GitOps Operator。

  • 您可以访问具有cluster-admin权限的集群。

步骤
  1. 运行以下命令以启用分片并将副本数设置为所需值

    $ oc patch argocd <argocd_instance> -n <namespace> --patch='{"spec":{"controller":{"sharding":{"enabled":true,"replicas":<value>}}}}' --type=merge
    示例输出
    argocd.argoproj.io/<argocd_instance> patched
  2. 运行以下命令将分片算法配置为round-robin

    $ oc patch argocd <argocd_instance> -n <namespace> --patch='{"spec":{"controller":{"env":[{"name":"ARGOCD_CONTROLLER_SHARDING_ALGORITHM","value":"round-robin"}]}}}' --type=merge
    示例输出
    argocd.argoproj.io/<argocd_instance> patched
  3. 运行以下命令验证 Argo CD 应用程序控制器 Pod 的数量是否与设置的副本数相对应

    $ oc get pods -l app.kubernetes.io/name=<argocd_instance>-application-controller -n <namespace>
    示例输出
    NAME                                        READY   STATUS    RESTARTS   AGE
    <argocd_instance>-application-controller-0   1/1     Running   0          11s
    <argocd_instance>-application-controller-1   1/1     Running   0          32s
    <argocd_instance>-application-controller-2   1/1     Running   0          22s
  4. 运行以下命令验证是否已启用分片且分片算法为round-robin

    $ oc logs <argocd_application_controller_pod> -n <namespace>
    示例输出片段
    time="2023-12-13T09:05:34Z" level=info msg="ArgoCD Application Controller is starting" built="2023-12-01T19:21:49Z" commit=a3vd5c3df52943a6fff6c0rg181fth3248976299 namespace=<namespace> version=v2.9.2+c5ea5c4
    time="2023-12-13T09:05:34Z" level=info msg="Processing clusters from shard 1"
    time="2023-12-13T09:05:34Z" level=info msg="Using filter function:  round-robin" (1)
    time="2023-12-13T09:05:34Z" level=info msg="Using filter function:  round-robin"
    time="2023-12-13T09:05:34Z" level=info msg="appResyncPeriod=3m0s, appHardResyncPeriod=0s"
    1 查找"Using filter function: round-robin"消息。
  5. 通过执行以下步骤验证跨分片的集群分布是否均匀

    1. 运行以下命令将日志级别设置为debug

      $ oc patch argocd <argocd_instance> -n <namespace> --patch='{"spec":{"controller":{"logLevel":"debug"}}}' --type=merge
      示例输出
      argocd.argoproj.io/<argocd_instance> patched
    2. 查看日志并搜索processed by shard以观察每个集群连接到哪个分片,运行以下命令

      $ oc logs <argocd_application_controller_pod> -n <namespace> | grep "processed by shard"
      示例输出片段
      time="2023-12-13T09:05:34Z" level=debug msg="Cluster with id= will be processed by shard 0" (1)
      time="2023-12-13T09:05:34Z" level=debug msg="Cluster with id=068d8b26-6rhi-4w23-jrf6-wjjfyw833n23 will be processed by shard 1" (1)
      time="2023-12-13T09:05:34Z" level=debug msg="Cluster with id=836d8b53-96k4-f68r-8wq0-sh72j22kl90w will be processed by shard 2" (1)
      
      1 在此示例中,3 个集群连续连接到分片 0、分片 1 和分片 2。

      如果集群数“C”是分片副本数“R”的倍数,则每个分片必须具有相同数量的分配集群“N”,等于“C”除以“R”。前面的示例显示 3 个集群和 3 个副本;因此,每个分片分配 1 个集群。

启用 Argo CD 应用程序控制器的分片动态缩放

分片的动态缩放仅为技术预览功能。技术预览功能不受 Red Hat 生产服务级别协议 (SLA) 的支持,可能功能不完整。Red Hat 不建议在生产环境中使用它们。这些功能提供对即将推出的产品功能的早期访问,使客户能够在开发过程中测试功能并提供反馈。

有关 Red Hat 技术预览功能的支持范围的更多信息,请参阅 技术预览功能支持范围

默认情况下,Argo CD 应用程序控制器会无限期地将集群分配给分片。如果您使用的是round-robin分片算法,则此静态分配可能导致分片分布不均,尤其是在添加或删除副本时。您可以启用分片的动态缩放,以便根据 Argo CD 应用程序控制器在给定时间管理的集群数量自动调整分片的数量。这确保了分片平衡良好并优化了计算资源的使用。

启用动态缩放后,您无法手动修改分片计数。系统会根据 Argo CD 应用程序控制器在给定时间管理的集群数量自动调整分片的数量。

在 Web 控制台中启用分片的动态缩放

您可以使用 OpenShift Container Platform Web 控制台启用分片的动态缩放。

先决条件
  • 您可以访问具有cluster-admin权限的集群。

  • 您可以访问 OpenShift Container Platform Web 控制台。

  • 您已在 OpenShift Container Platform 集群上安装了 Red Hat OpenShift GitOps Operator。

步骤
  1. 在 OpenShift Container Platform Web 控制台的**管理员**视角中,转到**Operators** → **已安装的 Operators**。

  2. 从**已安装的 Operators**列表中,选择 Red Hat OpenShift GitOps Operator,然后单击**ArgoCD**选项卡。

  3. 选择要为其启用分片动态缩放的 Argo CD 实例名称,例如openshift-gitops

  4. 单击**YAML**选项卡,然后编辑并配置以下spec.controller.sharding属性

    启用动态缩放的 Argo CD YAML 文件示例
    apiVersion: argoproj.io/v1beta1
    kind: ArgoCD
    metadata:
      name: openshift-gitops
      namespace: openshift-gitops
    spec:
      controller:
        sharding:
          dynamicScalingEnabled: true (1)
          minShards: 1 (2)
          maxShards: 3 (3)
          clustersPerShard: 1 (4)
    1 dynamicScalingEnabled设置为true以启用动态缩放。
    2 minShards设置为所需的最小分片数。该值必须设置为1或更大。
    3 maxShards设置为所需的最大分片数。该值必须大于minShards的值。
    4 clustersPerShard设置为每个分片所需的集群数。该值必须设置为1或更大。
  5. 单击**保存**。

    将出现成功通知警报:openshift-gitops 已更新到版本

    如果您编辑默认的openshift-gitops实例,则会显示**托管资源**对话框。再次单击**保存**以确认更改。

验证

通过检查命名空间中的 Pod 数量来验证是否启用了分片。

  1. 转到**工作负载** → **StatefulSets**。

  2. 从**项目**下拉列表中选择部署 Argo CD 实例的命名空间,例如openshift-gitops

  3. 单击名称为 Argo CD 实例名称的StatefulSet对象的名称,例如openshift-gitops-apllication-controller

  4. 单击**Pods**选项卡,然后验证 Pod 的数量是否等于或大于您在 Argo CD YAML文件中设置的minShards值。

使用 CLI 启用分片的动态缩放

您可以使用 OpenShift CLI (oc) 启用分片的动态缩放。

先决条件
  • 您已在 OpenShift Container Platform 集群上安装了 Red Hat OpenShift GitOps Operator。

  • 您可以访问具有cluster-admin权限的集群。

步骤
  1. 使用oc工具以具有cluster-admin权限的用户身份登录集群。

  2. 运行以下命令启用动态缩放

    $ oc patch argocd <argocd_instance> -n <namespace> --type=merge --patch='{"spec":{"controller":{"sharding":{"dynamicScalingEnabled":true,"minShards":<value>,"maxShards":<value>,"clustersPerShard":<value>}}}}'
    示例命令
    $ oc patch argocd openshift-gitops -n openshift-gitops --type=merge --patch='{"spec":{"controller":{"sharding":{"dynamicScalingEnabled":true,"minShards":1,"maxShards":3,"clustersPerShard":1}}}}' (1)
    1 此示例命令为openshift-gitops命名空间中的openshift-gitops Argo CD 实例启用动态缩放,并将最小分片数设置为1,最大分片数设置为3,每个分片的集群数设置为1minShardclustersPerShard的值必须设置为1或更大。maxShard的值必须等于或大于minShard的值。
    示例输出
    argocd.argoproj.io/openshift-gitops patched
验证
  1. 检查 Argo CD 实例的spec.controller.sharding属性

    $ oc get argocd <argocd_instance> -n <namespace> -o jsonpath='{.spec.controller.sharding}'
    示例命令
    $ oc get argocd openshift-gitops -n openshift-gitops -o jsonpath='{.spec.controller.sharding}'
    启用分片动态缩放时的示例输出
    {"dynamicScalingEnabled":true,"minShards":1,"maxShards":3,"clustersPerShard":1}
  2. 可选:通过检查 OpenShift Container Platform Web 控制台中 Argo CD 实例的配置YAML文件中配置的spec.controller.sharding属性来验证是否启用了动态缩放。

  3. 检查 Argo CD 应用控制器 Pod 的数量

    $ oc get pods -n <namespace> -l app.kubernetes.io/name=<argocd_instance>-application-controller
    示例命令
    $ oc get pods -n openshift-gitops -l app.kubernetes.io/name=openshift-gitops-application-controller
    示例输出
    NAME                                           READY   STATUS    RESTARTS   AGE
    openshift-gitops-application-controller-0      1/1     Running   0          2m  (1)
    
    1 Argo CD 应用控制器 Pod 的数量必须大于或等于minShard的值。