8.2.3. OpenShift Cluster Capacity Tool を Pod 内のジョブとして実行する
ConfigMap オブジェクトを使用することで、OpenShift Cluster Capacity Tool を Pod 内のジョブとして実行できます。これにより、ユーザーの介入なしにツールを複数回実行できます。
前提条件
-
cluster-capacityリポジトリーから OpenShift Cluster Capacity Tool をダウンロードしてインストールしてください。関連情報セクションのリンクを参照してください。
手順
クラスターロールを作成します。
以下のような 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"]次のコマンドを実行して、クラスターのロールを作成します。
$ oc create -f <file_name>.yaml以下に例を示します。
$ oc create sa cluster-capacity-sa
サービスアカウントを作成します。
$ oc create sa cluster-capacity-sa -n defaultロールをサービスアカウントに追加します。
$ oc adm policy add-cluster-role-to-user cluster-capacity-role \ system:serviceaccount:<namespace>:cluster-capacity-saここでは、以下のようになります。
- <namespace>
- Pod が配置されている namespace を指定します。
Pod 仕様を定義して、作成します。
以下のような YAML ファイルを作成します。
apiVersion: v1 kind: Pod metadata: name: small-pod labels: app: guestbook tier: frontend spec: securityContext: runAsNonRoot: true seccompProfile: type: RuntimeDefault 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 securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL]以下のコマンドを実行して Pod を作成します。
$ oc create -f <file_name>.yaml以下に例を示します。
$ oc create -f pod.yaml
以下のコマンドを実行して config map オブジェクトを作成します。
$ oc create configmap cluster-capacity-configmap \ --from-file=pod.yaml=pod.yamlクラスター容量分析は、
cluster-capacity-configmapという名前の config map オブジェクトを使用してボリュームにマウントされ、入力 Pod 仕様ファイルpod.yamlはパス/test-podのボリュームtest-volumeにマウントされます。ジョブ仕様ファイルの以下のサンプルを使用して、ジョブを作成します。
以下のような 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 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ここでは、以下のようになります。
spec.template.spec.containers.env-
Cluster Capacity Tool がクラスター内で Pod として実行されていることを示す、必須の環境変数を指定します。
ConfigMapのpod.yamlキーはPod仕様ファイル名と同じですが、これは必須ではありません。これを実行することで、入力 Pod 仕様ファイルは/test-pod/pod.yamlとして Pod 内でアクセスできます。
次のコマンドを実行して、クラスター容量イメージを Pod 内のジョブとして実行します。
$ oc create -f cluster-capacity-job.yaml
検証
ジョブログを確認し、クラスター内でスケジュールできる 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)