Kubernetes 이야기

Keptn (활용) 본문

Kubernetes/devops

Keptn (활용)

kmaster 2022. 8. 4. 15:20
반응형

Keptn 프로젝트는 최첨단 마이크로서비스 애플리케이션 또는 오랜 기간 지속되는 모놀리스에 대해 클라우드 네이티브 전략을 채택하도록 지원하는 방법으로 시작되었다. Keptn은 자동화된 테스트, 품질 게이트 및 자동 수정 기능 등을 통해 강력한 지속적 제공을 가능하게 한다.

 

이제 아래와 같은 Keptn 활용법에 대해 알아보자.

 

1) Keptn CLI 

2) Job-executor-service 

3) Deployment with Helm

 

1) Keptn CLI

 

Keptn CLI 주요 명령어에 대해 알아보자.

Keptn CLI 를 사용하려면 먼저 Keptn API 서버에 인증을 해야 한다.

 

Keptn auth

# keptn auth --endpoint=http://10.109.83.135:80
Starting to authenticate
Successfully authenticated against http://10.109.83.135:80/api

인증 후 keptn 상태를 조회해 보자.

 

Keptn status

# keptn status
Keptn CLI is already on the latest version ( 0.18.1 )!
* Your Keptn CLI version: 0.18.1
* To disable the daily version check, please execute:
 - keptn set config AutomaticVersionCheck false

Starting to authenticate
Successfully authenticated against http://10.99.43.245:80/api
Bridge URL: http://10.99.43.245:80/bridge
Using a file-based storage for the key because the password-store seems to be not set up.

Project 생성

keptn create project hello-world --shipyard=shipyard.yaml --git-user=$GIT_USER --git-remote-url=$GIT_REPO --git-token=$GIT_TOKEN

Project 조회

# keptn get project
NAME            CREATION DATE                   SHIPYARD VERSION
test            2022-07-28T14:34:21+09:00       spec.keptn.sh/0.2.3
test2           2022-07-29T16:54:45+09:00       spec.keptn.sh/0.2.2

 

Project 삭제

# keptn delete project test2
Starting to delete project
Deleting services of project test2...
Deleting service svc
Service deleted successfully
Project deleted successfully
The Git upstream of the project will not be deleted: https://github.com/kmaster8/flask-helloworld.git
Shipyard of project test2 cannot be retrieved anymore. After deleting the project, the namespaces containing the services are still available. This may cause problems if a project with the same name is created later.

Service 생성

# keptn create service demo --project=hello-world

add-resource 생성

# keptn add-resource --project=hello-world --service=demo --stage=dev --resource=jobconfig.yaml --resourceUri=job/config.yaml

trigger 생성

keptn trigger sequence --sequence example-seq --project hello-world --service hello --stage dev

event 생성

# keptn send event -f hello.triggered.event.json

 

2) Job-executor-service

Job executor service는 Keptn을 Kubernetes 작업으로 사용하여 사용자 지정 가능한 작업을 실행하는 새로운 접근 방식이다.

 

출처 : https://github.com/keptn-contrib/job-executor-service/blob/main/docs/ARCHITECTURE.md

 

Job executor service 설치

JES_VERSION="0.2.3"
JES_NAMESPACE="keptn-jes"
TASK_SUBSCRIPTION="sh.keptn.event.remote-task.triggered" # Add the relevant events here in a comma-separated list
KEPTN_API_PROTOCOL=http # or https
KEPTN_API_HOST=<INSERT-YOUR-HOSTNAME-HERE> # e.g., 1.2.3.4.nip.io
KEPTN_API_TOKEN=<INSERT-YOUR-KEPTN-API-TOKEN-HERE>

# helm upgrade --install --create-namespace -n ${JES_NAMESPACE} job-executor-service \
    https://github.com/keptn-contrib/job-executor-service/releases/download/${JES_VERSION}/job-executor-service-${JES_VERSION}.tgz \
    --set remoteControlPlane.topicSubscription=${TASK_SUBSCRIPTION} \
    --set remoteControlPlane.api.protocol=${KEPTN_API_PROTOCOL} \
    --set remoteControlPlane.api.hostname=${KEPTN_API_HOST} \
    --set remoteControlPlane.api.token=${KEPTN_API_TOKEN}

* KEPTN_API_TOKEN은 다음에서 확인이 가능하다.

 

Project 생성 

# keptn create project test -y -s shipyard.yaml --git-user=$GIT_USER --git-remote-url=$GIT_REPO --git-token=$GIT_TOKEN
no value for --config-file was specified
 defaulting to: /root/.keptn/config

Using config file: /root/.keptn/config
Starting to create project
Project created successfully

shipyard.yaml

apiVersion: "spec.keptn.sh/0.2.2"
kind: "Shipyard"
metadata:
  name: "shipyard-jes-sample"
spec:
  stages:
    - name: "production"
      sequences:
        - name: "example-seq"
          tasks:
            - name: "remote-task"

 

서비스 생성

# keptn create service hello --project test -y
Starting to create service
Service created successfully

 

add resource

# keptn add-resource --project test --service hello --stage production --resource jobconfig.yaml --resourceUri job/config.yaml
Adding resource jobconfig.yaml to service hello in stage production in project test
Resource has been uploaded.

jobconfig.yaml 의 내용은 아래와 같다. ( hello world를 출력하는 간단한 job이다. )

apiVersion: v2
actions:
  - name: "Be polite and say hello!"
    events:
      - name: "sh.keptn.event.remote-task.triggered"
    tasks:
      - name: "Greet the world"
        image: "alpine"
        cmd:
          - echo
        args:
          - "Hello World"

 

trigger

# keptn trigger sequence example-seq --project test --service hello --stage production
Triggering sequence example-seq in project test in stage production service hello

 

또는 아래와 같이 event 를 실행해도 된다.

# keptn send event -f jobevent.yaml

jobevent.yaml은 아래와 같다.

{
  "specversion": "1.0",
  "type": "sh.keptn.event.production.example-seq.triggered",
  "source": "remote-task hello",
  "datacontenttype": "application/json",
  "data": {
    "project": "test",
    "service": "hello",
    "stage": "production"
  }
}

 

 

Kubectl CLI에서 Pod 로그를 조회하면 아래와 같다.

# k logs -f -n keptn-jes job-executor-service-job-d625d6bb-45e9-413d-bce7--000-001-q8ftr
Defaulted container "job-executor-service-job-d625d6bb-45e9-413d-bce7--000-001" out of: job-executor-service-job-d625d6bb-45e9-413d-bce7--000-001, init-job-executor-service-job-d625d6bb-45e9-413d-bce7--000-001 (init)
Hello World

 

3) Deployment with Helm

Keptn은 Helm v3 를 사용하여 Kubernetes 클러스터에 서비스를 배포한다. 이것은 현재 helm-service 에 구현되어 있다. Keptn의 helm-service는 다음 배포 전략을 지원한다.

 

  • 직접 배포
  • Blue-Green 배포
  • 사용자 관리 배포

이번 예제는 https://github.com/keptn/examples/tree/0.11.0/onboarding-carts/carts ( 카트 마이크로 서비스 ) Helm Chart를 예제로 진행한다.

 

Project 생성 

# keptn create project test -y -s shipyard.yaml --git-user=$GIT_USER --git-remote-url=$GIT_REPO --git-token=$GIT_TOKEN
no value for --config-file was specified
 defaulting to: /root/.keptn/config

Using config file: /root/.keptn/config
Starting to create project
Project created successfully

shipyard.yaml

apiVersion: spec.keptn.sh/0.2.3
kind: "Shipyard"
metadata:
  name: "shipyard-sockshop"
spec:
  stages:
    - name: "production"
      sequences:
        - name: "delivery"
          tasks:
            - name: "deployment"
              properties:
                deploymentstrategy: "blue_green_service"
            - name: "release"

        - name: "delivery-direct"
          tasks:
            - name: "deployment"
              properties:
                deploymentstrategy: "direct"
            - name: "release"

 

project 생성 시 "이미 프로젝트가 있다"는 에러 메시지가 나타나면 git 저장소에 metadata.yaml 와 shipyard.yaml 을 삭제하고, stage 이름 "production"  브랜치를 삭제한 후 다시 실행한다.

 

서비스 생성

# keptn create service deploy --project test -y
Starting to create service
Service created successfully
# keptn add-resource --project=test --service=deploy --all-stages --resource=carts.tgz --resourceUri=helm/carts.tgz
Adding resource carts.tgz to all stages of project test
Resource has been uploaded.

 

Trigger

# keptn trigger delivery --project=test --service=deploy --image=keptn-examples/carts:0.13.3

또는 Event

# keptn send event -f event.json

event.json 은 아래와 같다.

# cat event.json
{
  "contenttype": "application/json",
  "data": {
    "project": "test",
    "service": "deploy",
    "stage": "production",
    "configurationChange": {
      "values": {
        "image": "keptn-examples/carts:0.13.3"
      }
    }
  },
  "source": "https://github.com/keptn/keptn/cli",
  "specversion": "1.0",
  "type": "sh.keptn.event.production.delivery.triggered"
}

 

* 테스트 시 아래의 오류가 발생하였다. charts 를 찾지 못하는데 이 부분은 좀 더 확인이 필요하다.

failed to load chart: Error when reading chart deploy from project test: Resource not found

 

참고

https://github.com/keptn-contrib/job-executor-service

https://keptn.sh/docs/0.18.x/define/delivery_sequence/

반응형
Comments