CRでsubresourceを利用してみる。[1]
subresouce
k8sのresourceはそれぞれ独立したAPIエンドポイントを持っており、APIサーバー経由でリソースの取得・作成・変更・削除をおこなうことができる。 subresourceはリソースのプロパティにも独立したAPIエンドポイントを持たせてやる機能。 (**ただし、作成や削除はできなくて、取得や更新のみが可能。)
CRで使用できるサブリソースはstatusとscaleのみ。
cf. これで満足できない場合はk8sの独自リソースの作成としてCRを利用するのではなくて、アグリゲーションレイヤーを使ったAPIの拡張を検討したほうがよいみたい。[2]
status subresource
CRの状態を表すもの。[3]
statusはk8sにおけるコントローラと人間(クライアント)の役割分担を実現するのに重要な概念になっている。
コントローラと人間(クライアント)の役割分担とは以下を意味する。
「人間(クライアント)がspecフィールドを更新して(書いて)、コントローラがspecフィールドを取得する(読む)のと、statusフィールドを更新する(書く)。」[4][5]
scale subresource
DeploymentやReplicasetみたいにreplicaの数を表すもの。
やってみる
まずはCRD
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: # name must match the spec fields below, and be in the form: <plural>.<group> name: crontabs.stable.example.com spec: # group name to use for REST API: /apis/<group>/<version> group: stable.example.com # list of versions supported by this CustomResourceDefinition versions: - name: v1 # Each version can be enabled/disabled by Served flag. served: true # One and only one version must be marked as the storage version. storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: cronSpec: type: string image: type: string replicas: type: integer status: type: object properties: replicas: type: integer labelSelector: type: string additionalPrinterColumns: - name: Spec type: string description: The cron spec defining the interval a CronJob is run jsonPath: .spec.cronSpec - name: Replicas type: integer description: The number of jobs launched by the CronJob jsonPath: .spec.replicas - name: Age type: date jsonPath: .metadata.creationTimestamp # subresources describes the subresources for custom resources. subresources: # status enables the status subresource. status: {} # scale enables the scale subresource. scale: # specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas. specReplicasPath: .spec.replicas # statusReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Status.Replicas. statusReplicasPath: .status.replicas # labelSelectorPath defines the JSONPath inside of a custom resource that corresponds to Scale.Status.Selector. labelSelectorPath: .status.labelSelector # either Namespaced or Cluster scope: Namespaced names: # plural name to be used in the URL: /apis/<group>/<version>/<plural> plural: crontabs # singular name to be used as an alias on the CLI and for display singular: crontab # kind is normally the CamelCased singular type. Your resource manifests use this. kind: CronTab # shortNames allow shorter string to match your resource on the CLI shortNames: - ct
CRDをアプライ。
kubectl apply -f resourcedefinition.yaml
replicasに3を指定したCRを作成。
apiVersion: "stable.example.com/v1" kind: CronTab metadata: name: my-new-cron-object spec: cronSpec: "* * * * */5" image: my-awesome-cron-image replicas: 3
CRをアプライ。
kubectl apply -f my-crontab.yaml
getしてみる。
kubectl get ct my-new-cron-object NAME SPEC REPLICAS AGE my-new-cron-object * * * * */5 3 11d
REPLICASが入っていてよい感じ。 scaleさせてみる。
kubectl scale --replicas=5 ct my-new-cron-object crontab.stable.example.com/my-new-cron-object scaled
getしてみる。
kubectl get ct my-new-cron-object NAME SPEC REPLICAS AGE my-new-cron-object * * * * */5 5 11d
scaleされている☆
参考
- https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#subresources
- https://kubernetes.io/ja/docs/concepts/extend-kubernetes/api-extension/apiserver-aggregation/
- https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/#object-spec-and-status
- https://kubernetes.io/ja/docs/concepts/extend-kubernetes/#extension-patterns
- https://kubernetes.io/ja/docs/concepts/architecture/controller/