2.4. Server-Side Apply を使用した Prometheus リソースのカスタマイズ
Server-Side Apply は、Kubernetes リソースの共同管理を可能にする機能です。コントロールプレーンは、さまざまなユーザーおよびコントローラーが Kubernetes オブジェクト内のフィールドをどのように管理するかを追跡します。フィールドマネージャーの概念を導入し、フィールドの所有権を追跡します。この集中制御により、競合検出および解決が行われ、意図しない上書きのリスクが軽減されます。
Client-Side Apply と比較すると、より宣言的であり、最後に適用された状態ではなく、フィールド管理を追跡します。
- Server-Side Apply
- リソースの状態を更新することで、削除や再作成を必要とせずに宣言型の設定を管理します。
- フィールド管理
- ユーザーは、他のフィールドに影響を与えずに、更新するリソースのフィールドを指定できます。
- 管理対象フィールド
-
Kubernetes は、メタデータ内の
managedFields
フィールドでオブジェクトの各フィールドを管理するユーザーに関するメタデータを保存します。 - Conflicts
- 複数のマネージャーが同じフィールドを変更しようとすると、競合が発生します。アプライヤーは、上書きするか、制御を放棄するか、または管理を共有するかを選択できます。
- マージストラテジー
- Server-Side Apply は、管理しているアクターに基づいてフィールドをマージします。
手順
次の設定を使用して
MonitoringStack
リソースを追加します。MonitoringStack
オブジェクトの例apiVersion: monitoring.rhobs/v1alpha1 kind: MonitoringStack metadata: labels: coo: example name: sample-monitoring-stack namespace: coo-demo spec: logLevel: debug retention: 1d resourceSelector: matchLabels: app: demo
sample-monitoring-stack
という名前の Prometheus リソースが、coo-demo
namespace に生成されます。次のコマンドを実行して、生成された Prometheus リソースの管理対象フィールドを取得します。$ oc -n coo-demo get Prometheus.monitoring.rhobs -oyaml --show-managed-fields
出力例
managedFields: - apiVersion: monitoring.rhobs/v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:labels: f:app.kubernetes.io/managed-by: {} f:app.kubernetes.io/name: {} f:app.kubernetes.io/part-of: {} f:ownerReferences: k:{"uid":"81da0d9a-61aa-4df3-affc-71015bcbde5a"}: {} f:spec: f:additionalScrapeConfigs: {} f:affinity: f:podAntiAffinity: f:requiredDuringSchedulingIgnoredDuringExecution: {} f:alerting: f:alertmanagers: {} f:arbitraryFSAccessThroughSMs: {} f:logLevel: {} f:podMetadata: f:labels: f:app.kubernetes.io/component: {} f:app.kubernetes.io/part-of: {} f:podMonitorSelector: {} f:replicas: {} f:resources: f:limits: f:cpu: {} f:memory: {} f:requests: f:cpu: {} f:memory: {} f:retention: {} f:ruleSelector: {} f:rules: f:alert: {} f:securityContext: f:fsGroup: {} f:runAsNonRoot: {} f:runAsUser: {} f:serviceAccountName: {} f:serviceMonitorSelector: {} f:thanos: f:baseImage: {} f:resources: {} f:version: {} f:tsdb: {} manager: observability-operator operation: Apply - apiVersion: monitoring.rhobs/v1 fieldsType: FieldsV1 fieldsV1: f:status: .: {} f:availableReplicas: {} f:conditions: .: {} k:{"type":"Available"}: .: {} f:lastTransitionTime: {} f:observedGeneration: {} f:status: {} f:type: {} k:{"type":"Reconciled"}: .: {} f:lastTransitionTime: {} f:observedGeneration: {} f:status: {} f:type: {} f:paused: {} f:replicas: {} f:shardStatuses: .: {} k:{"shardID":"0"}: .: {} f:availableReplicas: {} f:replicas: {} f:shardID: {} f:unavailableReplicas: {} f:updatedReplicas: {} f:unavailableReplicas: {} f:updatedReplicas: {} manager: PrometheusOperator operation: Update subresource: status
-
metadata.managedFields
値を確認し、metadata
とspec
の一部のフィールドがMonitoringStack
リソースによって管理されていることを確認します。 MonitoringStack
リソースで制御されないフィールドを変更します。MonitoringStack
リソースによって設定されていないフィールドであるspec.enforcedSampleLimit
を変更します。prom-spec-edited.yaml
ファイルを作成します。prom-spec-edited.yaml
apiVersion: monitoring.rhobs/v1 kind: Prometheus metadata: name: sample-monitoring-stack namespace: coo-demo spec: enforcedSampleLimit: 1000
以下のコマンドを実行して YAML を適用します。
$ oc apply -f ./prom-spec-edited.yaml --server-side
注記--server-side
フラグを使用する必要があります。変更された Prometheus オブジェクトを取得し、
spec.enforcedSampleLimit
を持つmanagedFields
に、もう 1 つセクションがあることに注意してください。$ oc get prometheus -n coo-demo
出力例
managedFields: 1 - apiVersion: monitoring.rhobs/v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:labels: f:app.kubernetes.io/managed-by: {} f:app.kubernetes.io/name: {} f:app.kubernetes.io/part-of: {} f:spec: f:enforcedSampleLimit: {} 2 manager: kubectl operation: Apply
MonitoringStack
リソースによって管理されるフィールドを変更します。次の YAML 設定を使用して、
MonitoringStack
リソースによって管理されるフィールドであるspec.LogLevel
を変更します。# changing the logLevel from debug to info apiVersion: monitoring.rhobs/v1 kind: Prometheus metadata: name: sample-monitoring-stack namespace: coo-demo spec: logLevel: info 1
- 1
spec.logLevel
が追加されました。
以下のコマンドを実行して YAML を適用します。
$ oc apply -f ./prom-spec-edited.yaml --server-side
出力例
error: Apply failed with 1 conflict: conflict with "observability-operator": .spec.logLevel Please review the fields above--they currently have other managers. Here are the ways you can resolve this warning: * If you intend to manage all of these fields, please re-run the apply command with the `--force-conflicts` flag. * If you do not intend to manage all of the fields, please edit your manifest to remove references to the fields that should keep their current managers. * You may co-own fields by updating your manifest to match the existing value; in this case, you'll become the manager if the other manager(s) stop managing the field (remove it from their configuration). See https://kubernetes.io/docs/reference/using-api/server-side-apply/#conflicts
-
フィールド
spec.logLevel
はobservability-operator
によってすでに管理されているため、Server-Side Apply を使用して変更できないことに注意してください。 この変更を強制するには、
--force-conflicts
フラグを使用します。$ oc apply -f ./prom-spec-edited.yaml --server-side --force-conflicts
出力例
prometheus.monitoring.rhobs/sample-monitoring-stack serverside-applied
--force-conflicts
フラグの場合、このフィールドは強制的に変更できますが、同じフィールドがMonitoringStack
リソースでも管理されるため、Observability Operator は変更を検出し、MonitoringStack
リソースによって設定された値に戻します。注記MonitoringStack
リソースによって生成される一部の Prometheus フィールドは、logLevel
など、MonitoringStack
spec
スタンザのフィールドの影響を受けます。これらは、MonitoringStack
spec
を変更することで変更できます。Prometheus オブジェクトの
logLevel
を変更するには、次の YAML を適用してMonitoringStack
リソースを変更します。apiVersion: monitoring.rhobs/v1alpha1 kind: MonitoringStack metadata: name: sample-monitoring-stack labels: coo: example spec: logLevel: info
変更が実行されたことを確認するには、次のコマンドを実行してログレベルについてクエリーします。
$ oc -n coo-demo get Prometheus.monitoring.rhobs -o=jsonpath='{.items[0].spec.logLevel}'
出力例
info
Operator の新規バージョンが、以前にアクターによって生成および制御されるフィールドを生成する場合、アクターによって設定された値はオーバーライドされます。
たとえば、
MonitoringStack
リソースによって生成されないフィールドenforcedSampleLimit
を管理しているとします。Observability Operator がアップグレードされ、新しいバージョンの Operator がenforcedSampleLimit
の値を生成すると、以前に設定した値がオーバーライドされます。-
MonitoringStack
リソースによって生成されたPrometheus
オブジェクトには、モニタリングスタックによって明示的に設定されていないフィールドが含まれる場合があります。これらのフィールドは、デフォルト値があるために表示されます。