확장 가능한 Composition
확장 가능한 Composition (Scalable Composition)
XRD는 복합 리소스에 쿠버네티스 scale 하위 리소스(subresource)를 노출할 수 있습니다. scale 하위 리소스를 노출하면 kubectl scale, Horizontal Pod Autoscaler(HPA), KEDA를 포함한 표준 쿠버네티스 스케일링 도구가 복합 리소스의 전체 스키마를 몰라도 그 복합 리소스를 제어할 수 있습니다.
출처: 문서
본문
예시 개요 (Example overview)
이 가이드는 쿠버네티스 Deployment를 감싸는 MyApp 복합 리소스를 생성하여 scale 하위 리소스를 노출하는 방법을 보여 줍니다.
사용자가 MyApp을 생성하면 크로스플레인은 Deployment를 프로비저닝하고 복합 리소스와 Deployment 사이에 레플리카(replica) 수를 연결합니다. 그러면 표준 쿠버네티스 스케일링 도구가 MyApp의 전체 스키마를 몰라도 레플리카 수를 구동할 수 있습니다.
MyApp XR 예시는 다음과 같습니다.
apiVersion: example.org/v1alpha1
kind: MyApp
metadata:
name: my-app
spec:
replicas: 1
배후에서 크로스플레인은:
- 요청된 레플리카 수로 Deployment(composed resource)를 생성합니다.
- Deployment에서 관찰된 레플리카 수를 MyApp의
status.replicas에 다시 씁니다. - Deployment가 건강해지면 MyApp을 ready로 표시합니다.
MyApp이 scale 하위 리소스를 노출하므로 전체 스키마를 몰라도 확장할 수 있습니다.
kubectl scale myapp/my-app --replicas=3
Horizontal Pod Autoscaler와 KEDA는 동일한 scale 하위 리소스를 사용하여 MyApp을 직접 대상으로 삼을 수도 있습니다.
사전 요구 사항 (Prerequisites)
이 가이드에는 다음이 필요합니다.
- 쿠버네티스 클러스터
- 쿠버네티스 클러스터에 설치된 크로스플레인
함수 설치 (Install the functions)
리소스를 구성하고 복합 리소스와 composed resources 사이의 필드를 패치하려면 function-patch-and-transform을 설치합니다.
apiVersion: pkg.crossplane.io/v1
kind: Function
metadata:
name: function-patch-and-transform
spec:
package: xpkg.crossplane.io/crossplane-contrib/function-patch-and-transform:v0.10.3
kubectl apply -f https://docs.crossplane.io/latest/manifests/guides/scalable-composition/fn-pat.yaml
이 가이드는 또한 function-auto-ready를 사용합니다. 이 함수는 composed resources가 건강할 때 자동으로 ready로 표시합니다.
apiVersion: pkg.crossplane.io/v1
kind: Function
metadata:
name: function-auto-ready
spec:
package: xpkg.crossplane.io/crossplane-contrib/function-auto-ready:v0.6.3
kubectl apply -f https://docs.crossplane.io/latest/manifests/guides/scalable-composition/fn-auto-ready.yaml
크로스플레인이 함수를 설치했는지 확인하세요.
kubectl get pkg
NAME INSTALLED HEALTHY PACKAGE AGE
function.pkg.crossplane.io/function-auto-ready True True xpkg.crossplane.io/crossplane-contrib/function-auto-ready:v0.6.3 2s
function.pkg.crossplane.io/function-patch-and-transform True True xpkg.crossplane.io/crossplane-contrib/function-patch-and-transform:v0.10.3 16s
XRD 구성 (Configure the XRD)
XRD의 subresources 필드에서 버전별로 scale 하위 리소스를 구성합니다.
apiVersion: apiextensions.crossplane.io/v2
kind: CompositeResourceDefinition
metadata:
name: myapps.example.org
spec:
group: example.org
names:
kind: MyApp
plural: myapps
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.replicas
name: DESIRED
type: string
- jsonPath: .status.replicas
name: CURRENT
type: string
name: v1alpha1
served: true
referenceable: true
subresources:
scale:
specReplicasPath: .spec.replicas
statusReplicasPath: .status.replicas
labelSelectorPath: .status.labelSelector
schema:
openAPIV3Schema:
properties:
spec:
properties:
replicas:
type: integer
status:
properties:
replicas:
type: integer
labelSelector:
type: string
kubectl apply -f https://docs.crossplane.io/latest/manifests/guides/scalable-composition/xrd-scale.yaml
XRD가 존재하는지 확인하세요.
kubectl get xrd
NAME ESTABLISHED OFFERED AGE
myapps.example.org True 6s
scale 블록에는 세 개의 필드가 있습니다.
specReplicasPath: 원하는 레플리카 수를 담고 있는 복합 리소스 spec의 필드에 대한 JSON 경로. 이 필드는 XRD 스키마에 존재해야 합니다.statusReplicasPath: 관찰된 레플리카 수를 담고 있는 복합 리소스 status의 필드에 대한 JSON 경로. 이 필드는 XRD 스키마에 존재해야 합니다.labelSelectorPath: 직렬화된 라벨 선택자를 담고 있는 status의 문자열 필드에 대한 선택적 JSON 경로. Horizontal Pod Autoscaler에 필요합니다.
중요 크로스플레인은
scale구성을 생성된 CRD로 전파합니다. Composition 작성자는 예를 들어 복합 리소스에서 composedDeployment로spec.replicas를 패치하는 방식으로 스케일링 로직을 구현해야 합니다.
Composition에서 스케일링 구현 (Implement scaling in a Composition)
XRD에서 scale 하위 리소스를 활성화한 후, Composition의 composed resources에 레플리카 수를 연결합니다. 다음 예시는 function-patch-and-transform을 사용하여 복합 리소스에서 Deployment로 spec.replicas를 전달합니다.
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: myapp
spec:
compositeTypeRef:
apiVersion: example.org/v1alpha1
kind: MyApp
mode: Pipeline
pipeline:
- step: patch-and-transform
functionRef:
name: function-patch-and-transform
input:
apiVersion: pt.fn.crossplane.io/v1beta1
kind: Resources
resources:
- name: deployment
base:
apiVersion: apps/v1
kind: Deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.29.7-alpine
ports:
- containerPort: 80
patches:
- type: FromCompositeFieldPath
fromFieldPath: spec.replicas
toFieldPath: spec.replicas
- type: ToCompositeFieldPath
fromFieldPath: status.readyReplicas
toFieldPath: status.replicas
- step: automatically-detect-readiness
functionRef:
name: function-auto-ready
kubectl apply -f https://docs.crossplane.io/latest/manifests/guides/scalable-composition/composition-scale.yaml
Composition이 존재하는지 확인하세요.
kubectl get compositions
NAME XR-KIND XR-APIVERSION AGE
myapp MyApp example.org/v1alpha1 3s
Composition은 또한 자동 스케일러와 kubectl scale --current-replicas가 올바른 레플리카 수를 읽도록 현재 레플리카 수를 status.replicas(그리고 사용한다면 status.labelSelector)에 다시 써야 합니다.
MyApp 복합 리소스 생성 (Create a MyApp composite resource)
XRD와 Composition이 준비되면 MyApp 복합 리소스를 생성합니다.
apiVersion: example.org/v1alpha1
kind: MyApp
metadata:
name: my-app
spec:
replicas: 1
현재 레플리카 수를 확인하세요.
kubectl get myapp/my-app
NAME DESIRED CURRENT SYNCED READY COMPOSITION AGE
my-app 1 1 True True myapp 108s
scale 하위 리소스 사용 (Use the scale subresource)
XRD와 Composition을 적용한 후 kubectl scale로 복합 리소스를 확장합니다.
kubectl scale myapp/my-app --replicas=3
현재 레플리카 수를 확인하세요.
kubectl get myapp/my-app
NAME DESIRED CURRENT SYNCED READY COMPOSITION AGE
my-app 3 3 True True myapp 3m26s