Knative 실무 적용 사례

Knative 실무 적용 사례

1️⃣ 서버리스 웹 애플리케이션 배포

Knative를 사용하면 컨테이너 기반 서버리스 웹 애플리케이션을 손쉽게 배포할 수 있습니다.

1. Knative Service를 활용한 웹 애플리케이션 배포

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: knative-web-app
spec:
  template:
    spec:
      containers:
        - image: gcr.io/my-project/web-app
          ports:
            - containerPort: 8080
  • gcr.io/my-project/web-app 컨테이너 이미지를 서버리스 서비스로 배포.
  • 필요할 때만 Pod가 생성되고, 요청이 없으면 자동으로 스케일 다운(0개)됨.

2. 배포 후 외부 접속 확인

kubectl get ksvc
  • 생성된 Knative Service의 URL을 확인하고 브라우저에서 접속.

2️⃣ 이벤트 기반 시스템 구성 (Kafka, Pub/Sub와 연동)

Knative Eventing을 사용하면 이벤트 기반 애플리케이션을 구축할 수 있습니다.

1. Kafka 이벤트 소스 구성

apiVersion: sources.knative.dev/v1beta1
kind: KafkaSource
metadata:
  name: kafka-source
spec:
  consumerGroup: knative-group
  bootstrapServers:
    - my-kafka-broker:9092
  topics:
    - my-topic
  sink:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: event-consumer
  • Kafka에서 my-topic의 메시지를 읽어 event-consumer 서비스로 전달.

2. Google Cloud Pub/Sub와 Knative 연동

apiVersion: sources.knative.dev/v1
kind: CloudPubSubSource
metadata:
  name: pubsub-source
spec:
  topic: my-topic
  sink:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: event-handler
  • Google Cloud Pub/Sub의 my-topic에서 발생하는 이벤트를 Knative Service(event-handler)로 전달.

3️⃣ 마이크로서비스 아키텍처에서 Knative 사용 사례

Knative는 마이크로서비스 기반 애플리케이션에서 다음과 같은 역할을 수행할 수 있습니다.

Knative를 활용한 마이크로서비스 패턴

  • API Gateway: Knative Service를 API Gateway로 사용.
  • 이벤트 기반 서비스: Knative Eventing으로 비동기 메시지 처리.
  • 서버리스 마이크로서비스: 요청이 있을 때만 실행되는 비동기 서비스 구축.

1. 다중 Knative Service로 구성된 마이크로서비스 예제

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: user-service
spec:
  template:
    spec:
      containers:
        - image: gcr.io/my-project/user-service

---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: order-service
spec:
  template:
    spec:
      containers:
        - image: gcr.io/my-project/order-service
  • user-serviceorder-service를 각각 Knative Service로 배포.
  • 요청이 없을 때는 자동으로 스케일 다운됨.

4️⃣ CI/CD 파이프라인에서 Knative Build 활용

Knative Build를 사용하면 자동화된 컨테이너 빌드 및 배포가 가능합니다.

1. Knative Build Template 구성

apiVersion: build.knative.dev/v1alpha1
kind: BuildTemplate
metadata:
  name: docker-build
spec:
  steps:
    - name: build
      image: gcr.io/kaniko-project/executor
      args:
        - "--dockerfile=/workspace/Dockerfile"
        - "--destination=gcr.io/my-project/my-app"
  • Dockerfile을 기반으로 컨테이너 이미지를 자동 빌드.
  • Google Container Registry(GCR)로 푸시.

2. Knative Build 트리거 설정

apiVersion: triggers.tekton.dev/v1beta1
kind: Trigger
metadata:
  name: git-trigger
spec:
  template:
    spec:
      steps:
        - name: build
          image: gcr.io/kaniko-project/executor
          args:
            - "--destination=gcr.io/my-project/my-app"
  • Git에서 코드가 변경되면 자동으로 Knative Build가 실행됨.

5️⃣ Knative와 Istio 통합 (서비스 메쉬와의 결합)

Knative는 Istio와 결합하여 트래픽 관리 및 서비스 메시 기능을 강화할 수 있습니다.

1. Istio Gateway와 Knative Service 연동

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: knative-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - "*.example.com"
  • *.example.com 도메인으로 Knative Service 접근 가능.

2. Istio VirtualService로 트래픽 분배

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: knative-route
spec:
  hosts:
    - my-app.example.com
  gateways:
    - knative-gateway
  http:
    - route:
        - destination:
            host: my-app
          weight: 70
        - destination:
            host: my-app-v2
          weight: 30
  • my-app.example.com 트래픽을 my-app(70%)과 my-app-v2(30%)로 분산.

6️⃣ Multi-cluster 및 Cross-cluster 배포 전략

Knative를 멀티 클러스터 환경에서 운영할 수도 있습니다.

1. Multi-cluster Knative 배포

  • 클러스터 1: knative-serving-1
  • 클러스터 2: knative-serving-2
kubectl apply -f knative-service.yaml --context=cluster-1
kubectl apply -f knative-service.yaml --context=cluster-2
  • 서로 다른 클러스터에서 동일한 Knative 서비스 배포 가능.

2. Knative Federation을 활용한 Cross-cluster 배포

  • Istio Multi-cluster Mesh와 Knative를 결합하여 여러 클러스터 간 트래픽 라우팅 가능.

7️⃣ Knative로 마이크로서비스 인프라 구성

Knative는 클라우드 네이티브 환경에서 마이크로서비스 운영을 최적화할 수 있습니다.

Knative 기반 마이크로서비스 아키텍처

  1. API Gateway 역할
  2. 서버리스 이벤트 처리
  3. CI/CD 자동화
  4. 멀티 클러스터 운영

1. Knative + Istio + Kafka를 활용한 아키텍처

Knative 마이크로서비스 아키텍처


8️⃣ 클라우드 네이티브 애플리케이션을 위한 서버리스 아키텍처 구현

Knative는 클라우드 네이티브 서버리스 애플리케이션을 위한 필수 도구입니다.

클라우드 네이티브 서버리스 특징

  • 자동 스케일링: 트래픽에 따라 자동으로 확장/축소.
  • 이벤트 기반 실행: 필요한 순간에만 실행.
  • 비용 절감: 사용한 만큼만 요금 지불.

1. 클라우드 네이티브 서버리스 앱 예제

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: cloud-native-app
spec:
  template:
    spec:
      containers:
        - image: gcr.io/my-project/cloud-app
          env:
            - name: ENVIRONMENT
              value: "production"
  • 클라우드 환경에서 자동 스케일링되는 서버리스 애플리케이션 배포.

RSS Feed
마지막 수정일자