Knative 확장 및 커스터마이징
1️⃣ Custom Event Sources 생성 (외부 이벤트 처리자 만들기)
Knative Eventing은 Custom Event Source를 만들어 외부 이벤트 소스를 처리할 수 있습니다. 이를 통해 Kafka, GitHub 등 외부 시스템과 통합하여 이벤트 기반 시스템을 구축할 수 있습니다.
1. Custom Event Source 기본 구조
apiVersion: sources.knative.dev/v1beta1
kind: MyCustomSource
metadata:
name: custom-source
spec:
eventType: my-event
eventSource: my-source
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-consumer
MyCustomSource
: 새로운 이벤트 소스를 정의.eventType
과eventSource
는 외부 이벤트의 타입과 출처를 나타냅니다.sink
는 이벤트가 전달될 Knative Service를 정의합니다.
2. 외부 시스템과 연결
apiVersion: sources.knative.dev/v1beta1
kind: MyCustomSource
metadata:
name: custom-source
spec:
eventType: github-push
eventSource: github.com
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: github-webhook-handler
- GitHub에서 발생하는 push 이벤트를 처리하는 예시.
2️⃣ 커스텀 리소스 정의(CRDs) 활용 (Knative의 확장성)
Knative는 **Custom Resource Definitions(CRDs)**를 사용하여 자체적인 리소스를 정의하고, 이를 통해 Knative의 기능을 확장할 수 있습니다.
1. CRD로 새로운 리소스 생성
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: myresource.knative.dev
spec:
group: knative.dev
names:
kind: MyResource
plural: myresources
singular: myresource
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
name:
type: string
MyResource
라는 커스텀 리소스를 정의하여 확장할 수 있습니다.
2. CRD 기반 리소스 생성
apiVersion: knative.dev/v1
kind: MyResource
metadata:
name: my-custom-resource
spec:
name: "example"
- 위의 CRD를 기반으로 실제 리소스를 생성하고, 이를 관리할 수 있습니다.
3️⃣ 외부 시스템과의 연동 (외부 API, 데이터베이스와의 연결)
Knative는 외부 시스템(예: API, 데이터베이스)과 쉽게 연동하여 이벤트를 처리하거나 데이터를 관리할 수 있습니다.
1. 외부 API 호출을 통한 데이터 처리
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: api-consumer
spec:
template:
spec:
containers:
- image: gcr.io/my-project/api-consumer
env:
- name: API_URL
value: "https://api.example.com/data"
- 외부 API에서 데이터를 가져오는 Knative Service를 설정.
2. 외부 데이터베이스와의 연결
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: db-consumer
spec:
template:
spec:
containers:
- image: gcr.io/my-project/db-consumer
env:
- name: DB_URL
value: "mysql://user:password@db.example.com"
- 외부 MySQL 데이터베이스에 연결하여 데이터를 처리하는 예시.
4️⃣ Knative Autoscaler 확장
Knative는 자동 확장(Auto-scaling) 기능을 제공합니다. 또한, Custom Autoscaler를 통해 확장 및 축소 규칙을 커스터마이징할 수 있습니다.
1. Knative Autoscaler 기본 설정
apiVersion: autoscaling.knative.dev/v1alpha1
kind: HorizontalPodAutoscaler
metadata:
name: knative-autoscaler
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: knative-service
minReplicas: 1
maxReplicas: 5
targetCPUUtilizationPercentage: 80
targetCPUUtilizationPercentage
를 설정하여 CPU 사용량에 따라 자동으로 Pod를 확장/축소할 수 있습니다.
2. 커스텀 Autoscaler 사용
apiVersion: autoscaling.knative.dev/v1
kind: Autoscaler
metadata:
name: custom-autoscaler
spec:
custom:
threshold: 75
cooldownPeriod: 30s
- Threshold와 CooldownPeriod를 커스터마이즈하여 보다 세밀한 자동 스케일링 정책을 적용.
5️⃣ Custom Ingress 및 Gateway
Knative는 Ingress 및 Gateway를 사용하여 외부와의 트래픽을 처리할 수 있습니다. 이러한 리소스를 커스터마이징하여 복잡한 라우팅 규칙을 설정할 수 있습니다.
1. Custom Ingress 설정
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: custom-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 8080
myapp.example.com
에서/api
로 시작하는 경로를 api-service로 라우팅하는 예시.
2. Gateway 설정 (Istio와 함께 사용)
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: custom-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "myapp.example.com"
- Istio Gateway를 사용하여 외부 요청을 Knative 서비스로 처리.
6️⃣ Knative Security (인증 및 권한 부여 설정, API 보안)
Knative는 보안 관련 설정을 제공하여 애플리케이션의 인증 및 API 보안을 강화할 수 있습니다.
1. 인증 및 권한 부여 설정
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: knative-admin
namespace: default
subjects:
- kind: ServiceAccount
name: default
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
RoleBinding
을 사용하여 ServiceAccount에 대한 권한을 설정.
2. API 보안 (JWT 인증)
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: secure-api
spec:
template:
spec:
containers:
- image: gcr.io/my-project/secure-api
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: api-secret
key: api-key
- API Key를 환경 변수로 전달하여 API 보안을 설정.
- JWT 인증을 통해 애플리케이션과 통신하는 클라이언트의 인증을 강화.
마지막 수정일자