8.2. 估算 OpenShift Container Platform 节点可以容纳的 pod 数量

作为集群管理员,您可以使用 OpenShift Cluster Capacity Tool 查看可以调度的 pod 数量,以便在资源耗尽前增加当前资源,并确保将来的 pod 可以被调度。此容量来自于集群中的节点主机,包括 CPU、内存和磁盘空间等。

8.2.1. 了解 OpenShift Cluster Capacity Tool

OpenShift Cluster Capacity Tool 模拟一系列调度决策,以确定在资源耗尽前集群中可以调度多少个输入 pod 实例,以提供更准确的估算。

注意

因为它不计算节点间分布的所有资源,所以它所显示的剩余可分配容量是粗略估算值。它只分析剩余的资源,并通过估算集群中可以调度多少个具有给定要求的 pod 实例来估测仍可被消耗的可用容量。

另外,根据选择和关联性条件,可能仅支持将 pod 调度到特定的节点集合。因此,可能很难估算集群还能调度多少个 pod。

您可以从命令行运行 OpenShift Cluster Capacity Tool 作为独立实用程序,或者在 OpenShift Container Platform 集群内的 pod 中作为作业运行。作为 pod 中的作业运行该工具,您可以在不干预的情况下多次运行它。

8.2.2. 在命令行中运行 OpenShift Cluster Capacity Tool

您可以从命令行运行 OpenShift Cluster Capacity Tool,以估算可调度到集群中的 pod 数量。

您可以创建一个示例 pod spec 文件,工具使用它来估算资源使用情况。pod 规格将其资源要求指定为 limitsrequests。集群容量工具在估算分析时会考虑 pod 的资源要求。

先决条件

  1. 运行 OpenShift Cluster Capacity Tool,它可作为来自红帽生态系统目录中的容器镜像。
  2. 创建 pod spec 文件示例:

    1. 创建一个类似以下示例的 YAML 文件:

      apiVersion: v1
      kind: Pod
      metadata:
        name: small-pod
        labels:
          app: guestbook
          tier: frontend
      spec:
        containers:
        - name: php-redis
          image: gcr.io/google-samples/gb-frontend:v4
          imagePullPolicy: Always
          resources:
            limits:
              cpu: 150m
              memory: 100Mi
            requests:
              cpu: 150m
              memory: 100Mi
    2. 创建集群角色:

      $ oc create -f <file_name>.yaml

      例如:

      $ oc create -f pod-spec.yaml

流程

在命令行中使用集群容量工具:

  1. 在终端中登录到 Red Hat Registry:

    $ podman login registry.redhat.io
  2. 拉取集群容量工具镜像:

    $ podman pull registry.redhat.io/openshift4/ose-cluster-capacity
  3. 运行集群容量工具:

    $ podman run -v $HOME/.kube:/kube:Z -v $(pwd):/cc:Z  ose-cluster-capacity \
    /bin/cluster-capacity --kubeconfig /kube/config --<pod_spec>.yaml /cc/<pod_spec>.yaml \
    --verbose

    其中:

    <pod_spec>.yaml
    指定要使用的 pod 规格。
    详细
    输出有关集群中每个节点上可以调度多少个 pod 的详细描述。

    输出示例

    small-pod pod requirements:
    	- CPU: 150m
    	- Memory: 100Mi
    
    The cluster can schedule 88 instance(s) of the pod small-pod.
    
    Termination reason: Unschedulable: 0/5 nodes are available: 2 Insufficient cpu,
    3 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't
    tolerate.
    
    Pod distribution among nodes:
    small-pod
    	- 192.168.124.214: 45 instance(s)
    	- 192.168.124.120: 43 instance(s)

    在上例中,集群中预计可以调度的 pod 数量为 88。

8.2.3. 将 OpenShift Cluster Capacity Tool 作为 pod 中的作业运行

通过以 pod 中的作业形式运行 OpenShift Cluster Capacity Tool,您可以多次运行该工具,而无需用户干预。您可以使用 ConfigMap 对象以作业的形式运行 OpenShift Cluster Capacity Tool。

先决条件

下载并安装 OpenShift Cluster Capacity Tool

流程

运行集群容量工具:

  1. 创建集群角色:

    1. 创建一个类似以下示例的 YAML 文件:

      kind: ClusterRole
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
        name: cluster-capacity-role
      rules:
      - apiGroups: [""]
        resources: ["pods", "nodes", "persistentvolumeclaims", "persistentvolumes", "services", "replicationcontrollers"]
        verbs: ["get", "watch", "list"]
      - apiGroups: ["apps"]
        resources: ["replicasets", "statefulsets"]
        verbs: ["get", "watch", "list"]
      - apiGroups: ["policy"]
        resources: ["poddisruptionbudgets"]
        verbs: ["get", "watch", "list"]
      - apiGroups: ["storage.k8s.io"]
        resources: ["storageclasses"]
        verbs: ["get", "watch", "list"]
    2. 运行以下命令来创建集群角色:

      $ oc create -f <file_name>.yaml

      例如:

      $ oc create sa cluster-capacity-sa
  2. 创建服务帐户:

    $ oc create sa cluster-capacity-sa -n default
  3. 将角色添加到服务帐户:

    $ oc adm policy add-cluster-role-to-user cluster-capacity-role \
        system:serviceaccount:<namespace>:cluster-capacity-sa

    其中:

    <namespace>
    指定 pod 所在的命名空间。
  4. 定义并创建 pod 规格:

    1. 创建一个类似以下示例的 YAML 文件:

      apiVersion: v1
      kind: Pod
      metadata:
        name: small-pod
        labels:
          app: guestbook
          tier: frontend
      spec:
        containers:
        - name: php-redis
          image: gcr.io/google-samples/gb-frontend:v4
          imagePullPolicy: Always
          resources:
            limits:
              cpu: 150m
              memory: 100Mi
            requests:
              cpu: 150m
              memory: 100Mi
    2. 运行以下命令来创建 pod:

      $ oc create -f <file_name>.yaml

      例如:

      $ oc create -f pod.yaml
  5. 运行以下命令来创建配置映射对象:

    $ oc create configmap cluster-capacity-configmap \
        --from-file=pod.yaml=pod.yaml

    集群容量分析使用名为 cluster-capacity-configmap 的配置映射对象挂载到卷中,将输入 pod 规格文件 pod.yaml 挂载到卷 test-volume 的路径 /test-pod

  6. 使用以下作业规格文件示例创建作业:

    1. 创建一个类似以下示例的 YAML 文件:

      apiVersion: batch/v1
      kind: Job
      metadata:
        name: cluster-capacity-job
      spec:
        parallelism: 1
        completions: 1
        template:
          metadata:
            name: cluster-capacity-pod
          spec:
              containers:
              - name: cluster-capacity
                image: openshift/origin-cluster-capacity
                imagePullPolicy: "Always"
                volumeMounts:
                - mountPath: /test-pod
                  name: test-volume
                env:
                - name: CC_INCLUSTER 1
                  value: "true"
                command:
                - "/bin/sh"
                - "-ec"
                - |
                  /bin/cluster-capacity --podspec=/test-pod/pod.yaml --verbose
              restartPolicy: "Never"
              serviceAccountName: cluster-capacity-sa
              volumes:
              - name: test-volume
                configMap:
                  name: cluster-capacity-configmap
      1
      必要的环境变量,使集群容量工具知道它将作为一个 pod 在集群中运行。
      ConfigMap 对象的 pod.yaml 键与 Pod spec 文件名称相同,但这不是必须的。如果这样做,输入 pod 规格文件可作为 /test-pod/pod.yaml 在 pod 中被访问。
    2. 运行以下命令,以 pod 中作业的形式运行集群容量镜像:

      $ oc create -f cluster-capacity-job.yaml

验证

  1. 检查作业日志,以查找在集群中可调度的 pod 数量:

    $ oc logs jobs/cluster-capacity-job

    输出示例

    small-pod pod requirements:
            - CPU: 150m
            - Memory: 100Mi
    
    The cluster can schedule 52 instance(s) of the pod small-pod.
    
    Termination reason: Unschedulable: No nodes are available that match all of the
    following predicates:: Insufficient cpu (2).
    
    Pod distribution among nodes:
    small-pod
            - 192.168.124.214: 26 instance(s)
            - 192.168.124.120: 26 instance(s)