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

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

6.2.1. 了解 OpenShift Container Platform 集群容量工具

集群容量工具模拟一系列调度决策,以确定在资源耗尽前集群中可以调度多少个输入 pod 实例,从而提供更加准确的估算。

注意

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

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

您可以从命令行中以单机实用程序的方式来运行集群容量分析工具,也可以作为 OpenShift Container Platform 集群里 pod 中的一个作业来运行。以 pod 内作业的方式运行时,可以将它运行多次而无需干预。

6.2.2. 在命令行中运行集群容量工具

您可以从命令行运行 OpenShift Container Platform 集群容量工具,以估算可以调度到集群的 pod 数量。

先决条件

  • 下载并安装 cluster-capacity 工具
  • 创建一个示例 pod 规格文件,工具将使用该文件来估算资源用量。podspeclimitsrequests 的形式指定资源要求。集群容量工具在估算分析时会考虑 pod 的资源要求。

    pod 规格输入的示例如下:

    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

流程

在命令行中运行该工具:

  1. 运行以下命令:

    $ ./cluster-capacity --kubeconfig <path-to-kubeconfig> \ 1
        --podspec <path-to-pod-spec> 2
    1
    指定 Kubernetes 配置文件的路径。
    2
    指定示例 pod 规格文件的路径

    您还可以添加 --verbose 选项,以输出集群的各个节点上可以调度多少个 pod 的详细描述:

    $ ./cluster-capacity --kubeconfig <path-to-kubeconfig> \
        --podspec <path-to-pod-spec> --verbose
  2. 查看输出结果,其类似于以下示例:

    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)

    在上例中,集群中估计可以调度的 pod 数量是 52。

6.2.3. 以 pod 中作业的方式运行集群容量工具

若以 pod 中作业的方式运行集群容量工具,其优点是可以在无需用户干预的前提下多次运行。以作业方式运行集群容量工具需要使用 ConfigMap

先决条件

下载并安装 cluster-capacity 工具

流程

运行集群容量工具:

  1. 创建集群角色:

    $ cat << EOF| oc create -f -
    kind: ClusterRole
    apiVersion: v1
    metadata:
      name: cluster-capacity-role
    rules:
    - apiGroups: [""]
      resources: ["pods", "nodes", "persistentvolumeclaims", "persistentvolumes", "services"]
      verbs: ["get", "watch", "list"]
    EOF
  2. 创建服务帐户:

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

    $ oc adm policy add-cluster-role-to-user cluster-capacity-role \
        system:serviceaccount:default:cluster-capacity-sa
  4. 定义并创建 pod 规格:

    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
  5. 集群容量分析使用名为 cluster-capacity-configmapConfigMap 挂载到卷中,从而将输入 pod 规格文件 pod.yaml 挂载到卷 test-volume 的路径 /test-pod 上。

    如果还没有创建 ConfigMap,请在创建作业前创建:

    $ oc create configmap cluster-capacity-configmap \
        --from-file=pod.yaml=pod.yaml
  6. 使用以下作业规格文件示例创建作业:

    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 在集群中运行。
    ConfigMappod.yaml 键与 pod 规格文件相同,但这不是强制要求。如果这样做,输入 pod 规格文件可作为 /test-pod/pod.yaml 在 pod 中被访问。
  7. 以 pod 中作业的方式运行集群容量镜像:

    $ oc create -f cluster-capacity-job.yaml
  8. 检查作业日志,以查找在集群中可调度的 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)