管理集群配置


Red Hat OpenShift GitOps 1.17

管理 OpenShift Container Platform 集群配置

Red Hat OpenShift Documentation Team

摘要

本文档提供了有关如何使用 OpenShift GitOps 管理 OpenShift 集群配置的内容。

使用 Red Hat OpenShift GitOps,您可以管理 OpenShift Container Platform 集群配置,使其具有以下优点:

  • 版本控制和可审核性 :提交至 Git 的配置更改提供了完整的修改历史记录。这有助于审计、合规性和责任。
  • 单一来源:Git 充当 OpenShift Container Platform 集群所需状态的确定性源。
  • 优化的性能和灾难恢复: GitOps 将 Argo CD 应用程序指向之前的提交或标签,在 Git 中带有已知良好状态,从而减少停机时间并帮助进行灾难恢复。
  • 协作和审核:Git 的协作功能使团队成员能够在将基础架构和应用程序配置更改应用到 OpenShift Container Platform 集群前对其进行审核和批准。
  • 效率和可扩展性: GitOps 简化了部署和操作工作流,实现高效管理复杂和多集群环境,同时减少了人工干预和人为错误。

执行以下任务来管理 OpenShift Container Platform 集群配置:

  1. 使用 CLI 安装 Red Hat OpenShift GitOps Operator
  2. 分析默认 Argo CD 实例
  3. 访问默认 Argo CD 实例
  4. 配置默认的 Argo CD 实例

1.1. 使用 CLI 安装 Red Hat OpenShift GitOps Operator

您可以使用 CLI 从 OperatorHub 安装 Red Hat OpenShift GitOps Operator。

注意

对于 GitOps 版本 1.10 及更新的版本,默认命名空间从 openshift-operators 改为 openshift-gitops operator

前提条件

  • 有登录凭证,才能使用 cluster-admin 权限访问 OpenShift Container Platform 集群。
  • 已安装 oc CLI

流程

  1. 创建 openshift-gitops-operator 命名空间:

    $ oc create ns openshift-gitops-operator
    Copy to Clipboard Toggle word wrap

    输出示例

    namespace/openshift-gitops-operator created
    Copy to Clipboard Toggle word wrap

    注意

    您可以通过应用 openshift.io/cluster-monitoring=true 标签,在 openshift-gitops-operator 或任何命名空间上启用集群监控:

    $ oc label namespace <namespace> openshift.io/cluster-monitoring=true
    Copy to Clipboard Toggle word wrap

    输出示例

    namespace/<namespace> labeled
    Copy to Clipboard Toggle word wrap

  2. 创建一个 OperatorGroup 对象 YAML 文件,如 gitops-operator-group.yaml

    OperatorGroup 示例

    apiVersion: operators.coreos.com/v1
    kind: OperatorGroup
    metadata:
      name: openshift-gitops-operator
      namespace: openshift-gitops-operator
    spec:
      upgradeStrategy: Default
    Copy to Clipboard Toggle word wrap

  3. OperatorGroup 应用到集群:

    $ oc apply -f gitops-operator-group.yaml
    Copy to Clipboard Toggle word wrap

    输出示例

    operatorgroup.operators.coreos.com/openshift-gitops-operator created
    Copy to Clipboard Toggle word wrap

  4. 创建一个 Subscription 对象 YAML 文件,以便为 Red Hat OpenShift GitOps Operator 订阅一个命名空间,如 openshift-gitops-sub.yaml

    订阅示例

    apiVersion: operators.coreos.com/v1alpha1
    kind: Subscription
    metadata:
      name: openshift-gitops-operator
      namespace: openshift-gitops-operator
    spec:
      channel: latest 
    1
    
      installPlanApproval: Automatic
      name: openshift-gitops-operator 
    2
    
      source: redhat-operators 
    3
    
      sourceNamespace: openshift-marketplace 
    4
    Copy to Clipboard Toggle word wrap

    1
    指定您要订阅 Operator 的频道名称。
    2
    指定要订阅的 Operator 的名称。
    3
    指定提供 Operator 的 CatalogSource 的名称。
    4
    CatalogSource 的命名空间。将 openshift-marketplace 用于默认的 OperatorHub CatalogSource。
  5. 订阅应用到集群:

    $ oc apply -f openshift-gitops-sub.yaml
    Copy to Clipboard Toggle word wrap

    输出示例

    subscription.operators.coreos.com/openshift-gitops-operator created
    Copy to Clipboard Toggle word wrap

  6. 安装完成后,验证 openshift-gitops 命名空间中的所有 pod 是否正在运行:

    $ oc get pods -n openshift-gitops
    Copy to Clipboard Toggle word wrap

    输出示例

    NAME                                                      	  READY   STATUS    RESTARTS   AGE
    cluster-785cfc5f75-669wq                                      1/1     Running   0          76s
    gitops-plugin-6664c749dd-dx64s                                1/1     Running   0          76s
    openshift-gitops-application-controller-0                     1/1     Running   0          74s
    openshift-gitops-applicationset-controller-549d7f6686-wzckt   1/1     Running   0          74s
    openshift-gitops-dex-server-5d4ffdb9b9-lb7b7                  1/1     Running   0          74s
    openshift-gitops-redis-6d65c94d4b-k9l8k                       1/1     Running   0          75s
    openshift-gitops-repo-server-79db854c58-279jr                 1/1     Running   0          75s
    openshift-gitops-server-f488b848-xntbc                        1/1     Running   0          75s
    Copy to Clipboard Toggle word wrap

  7. 验证 openshift-gitops-operator 命名空间中的 pod 是否正在运行:

    $ oc get pods -n openshift-gitops-operator
    Copy to Clipboard Toggle word wrap

    输出示例

    NAME                                                            READY   STATUS    RESTARTS   AGE
    openshift-gitops-operator-controller-manager-6fdc5cd9dc-jr9mn   2/2     Running   0          41s
    Copy to Clipboard Toggle word wrap

1.2. 分析默认的 Argo CD 实例详情

默认情况下,Operator 在 openshift-gitops 命名空间中创建 Argo CD 实例。安装后,您可以使用 OpenShift Container Platform Web 控制台查看 Operator 详情页面 并分析默认的实例配置。此分析可帮助您稍后自定义此实例的行为。该实例适用于集群配置用例,并具有升级的权限。

注意

您可以在其他命名空间中创建额外的 Argo CD 实例,以支持应用程序用例。

前提条件

  • 您有登录凭证来访问带有 cluster-admin 权限的 OpenShift Container Platform 集群。

流程

  1. 执行以下步骤之一打开 Operator 详情页面

    • 点 Operator 安装完成后可用的 View Operator 按钮。
    • Operators 菜单下的 Installed Operators,然后选择 Red Hat OpenShift GitOps Operator。
  2. 选择 Argo CD 选项卡。
  3. 单击默认实例 openshift-gitops 的名称,以查看其新页面的详细信息。
  4. 选择 YAML 选项卡来分析其配置方式。

1.3. 访问默认的 Argo CD 实例

分析默认 Argo CD 实例详情后,您可以通过 Argo CD UI 访问它,以检查它是否可用。

前提条件

  • 您有登录凭证来访问带有 cluster-admin 权限的 OpenShift Container Platform 集群。

流程

  1. 点 OpenShift Container Platform Web 控制台右上角的 Application Launcher 菜单。
  2. 从下拉列表中选择 Cluster Argo CD。Argo CD 登录页面将打开。
  3. 单击 LOG IN VIA OPENSHIFT 按钮。OpenShift Container Platform 登录页面将打开。
  4. 输入 OpenShift Container Platform 凭证。此时会打开 Authorize Access 页面。
  5. Allow selected permissions 提供请求的权限。Argo CD UI 页面将打开。

此时,UI 为空,因为您尚未创建任何 Argo CD 应用程序。您可以检查 User Info 页面,以查看用户详情。

1.4. 配置默认的 Argo CD 实例

虽然 Operator 在 openshift-gitops 命名空间中创建默认 Argo CD 实例,但您必须配置它,使其可用于部署应用程序并设置集群配置:

  • 配置基于角色的访问控制(RBAC): Argo CD 使用自己的 RBAC 配置。Operator 配置的默认权限可能不足,具体取决于用户已分配给的 OpenShift Container Platform 集群组。
  • 配置权限 :Operator 使用一组默认的 Kubernetes 权限配置默认实例。但是,这些权限在实时环境中部署应用程序时不足。因此,请确保为此默认实例提供额外的权限。

1.4.1. 配置 RBAC

您必须配置 RBAC,以提供对用户使用默认实例的足够访问权限。

实例的 defaultPolicy 是一个空字符串,这意味着没有自动分配角色。虽然用户可以登录实例,但它们没有查看任何权限,或者执行 Argo CD UI 或 CLI 中的任何任务。

实例包括以下两个组:

  • system:cluster-admins :组仅适用于临时 kube-admin 凭证,并可以忽略。
  • cluster-admins :您可以在此组中添加用户,使他们能够执行任务,如在 Argo CD web 控制台中部署应用程序。
注意

限制默认权限:

确保始终对 defaultPolicy 参数使用空字符串或 deny-all 类型的 role,因为其中授予的权限无法撤销。因此,不建议以授予权限的方式设置 defaultPolicy 参数。

前提条件

  • 有登录凭证,才能使用 cluster-admin 权限访问 OpenShift Container Platform 集群。
  • 已安装 oc CLI

流程

  1. 查看默认实例的 Operator 配置 RBAC:

    $ oc get argocd openshift-gitops -n openshift-gitops -o=jsonpath='{.spec.rbac}'
    Copy to Clipboard Toggle word wrap

    结果

    {"defaultPolicy":"","policy":"g, system:cluster-admins, role:admin\ng, cluster-admins, role:admin\n","scopes":"[groups]"}
    Copy to Clipboard Toggle word wrap

  2. 检查 cluster-admins 组是否存在:

    $ oc get groups
    Copy to Clipboard Toggle word wrap
  3. 执行以下步骤之一:

    • 如果组不存在,请创建它并将其添加到其中:

      $ oc adm groups new cluster-admins <user>
      Copy to Clipboard Toggle word wrap

      其中:

      <user>

      表示您要添加到组中的用户。

      输出示例

      group.user.openshift.io/cluster-admins created
      Copy to Clipboard Toggle word wrap

    • 如果组存在,请检查您的用户是否在之前运行 oc get groups 命令的输出中的一部分。如果您的用户不在组中,请将您的用户添加到组中:

      $ oc adm groups add-users cluster-admins <user>
      Copy to Clipboard Toggle word wrap

      其中:

      <user>

      表示您要添加到组中的用户。

      输出示例

      group.user.openshift.io/cluster-admins added: "<user>"
      Copy to Clipboard Toggle word wrap

验证

  • 验证组 cluster-admins 是否存在,并且您的用户是否是它的一部分:

    $ oc get groups cluster-admins
    Copy to Clipboard Toggle word wrap

    输出显示 cluster-admins 组,其中包含您的用户。

重要

创建或编辑 cluster-admins 组后,请确保退出 Argo CD web 控制台,然后再次登录,以便组与您的用户关联。检查 User Info 页面,以验证您的用户是否是 Argo CD 中的 cluster-admins 组的一部分。

1.4.2. 配置权限

虽然默认 Argo CD 实例会自动配置一组默认的 Kubernetes 权限,但您需要为其提供额外权限来部署集群配置所需的所有资源。相反,如果您需要为默认实例设置更严格的权限来仅部署特定资源,您可以通过额外的配置完成此操作。

注意

有关默认 Kubernetes 权限集合的更多信息,请参阅"添加资源"。

当将默认实例用于集群配置时,为 Argo CD 应用程序控制器服务帐户提供 cluster-admin 权限。要做到这一点,您可以为 openshift-gitops-argocd-application-controller 服务帐户创建一个 ClusterRoleBinding 对象,因为默认实例使用此帐户与 Kubernetes API 交互来部署资源。

前提条件

  • 您有登录凭证来访问带有 cluster-admin 权限的 OpenShift Container Platform 集群。
  • 已安装 oc CLI

流程

  • 运行以下命令:

    $ oc adm policy add-cluster-role-to-user --rolebinding-name="openshift-gitops-cluster-admin" cluster-admin -z openshift-gitops-argocd-application-controller -n openshift-gitops
    Copy to Clipboard Toggle word wrap

    输出示例

    clusterrole.rbac.authorization.k8s.io/cluster-admin added: "openshift-gitops-argocd-application-controller"
    Copy to Clipboard Toggle word wrap

验证

  • 查看创建的 ClusterRoleBinding 对象:

    $ oc get clusterrolebinding openshift-gitops-cluster-admin -o yaml
    Copy to Clipboard Toggle word wrap

    输出示例

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: openshift-gitops-cluster-admin
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: cluster-admin
    subjects:
      - kind: ServiceAccount
        name: openshift-gitops-argocd-application-controller
        namespace: openshift-gitops
    Copy to Clipboard Toggle word wrap

法律通告

Copyright © 2025 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat