전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]

티스토리 뷰

Agile&DevOps/Tekton

4. Pipeline & PipelineRun

Happy@Cloud 2019. 12. 13. 21:12

Pipeline과 Pipelinerun의 관계는 아래와 같습니다. 

- PipelineRun은 PipelineResource를 참조할 수 있습니다.

- PipelineRun은 Pipeline을 실행하면서 파라미터를 넘길 수 있습니다.

- Pipeline은 여러개의 Task를 가질 수 있습니다. 

* Pipeline에서 정의된 Task는 PipelineRun에 의해 Pipeline이 실행되면서 수행되기 때문에 Taskrun은 정의하지 않습니다. 

 

그럼 예제를 통해 알아 보도록 하겠습니다.

1. Pipeline YAML 작성

아래 소스로 tutorial-pipeline.yaml파일을 생성합니다.

apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
  name: tutorial-pipeline
  namespace: tekton
spec:
  resources:
    - name: source-repo
      type: git
    - name: web-image
      type: image
  tasks:
    - name: build-docker-image
      taskRef:
        name: build-image
      params:
        - name: pathToDockerFile
          value: deployment/Dockerfile
        - name: pathToContext
          value: /workspace/hello1 # 
      resources:
        inputs:
          - name: hello1
            resource: source-repo
        outputs:
          - name: builtImage
            resource: web-image
    - name: deploy-web
      taskRef:
        name: deploy-image
      resources:
        inputs:
          - name: hello1
            resource: source-repo
          - name: image
            resource: web-image
            from:
              - build-docker-image
      params:
        - name: path
          value: /workspace/hello1/deployment/deploy.yaml #configure: may change according to your source
        - name: yamlPathToImage
          value: "spec.template.spec.containers[0].image"

- task가 2개 정의 되어 있습니다. build-docker-image는 build-image TASK로 연결했고, deploy-web은 deploy-image TASK로 연결되어 있습니다. 

- 각 task에서는 Task를 실행할 때 넘길 parameter값이 정의 되어 있습니다. 즉, Pipeline이 Taskrun의 역할을 하는 것입니다. 

- deploy-web TASK의 input resource를 build-docker-image TASK의 output을 이용한다는 의미입니다. 

 이렇게 하려면 반드시, build-docker-image TASK는 output 리소스가 아래와 같이 정의 되어 있어야 합니다.

 

1) Ordering: Pipeline은 Task의 실행 순서를 정의할 수 있습니다.

실행 순서를 정하는 방법은 위에서와 같이 1) 'from'항목을 input resource에 정의하는 방법과, 2) runAfter항목을 Task에 정의하는 방법이 있습니다. 둘 다 없으면 동시에 수행이 됩니다. 

위 예에서는 from항목으로 순서를 정했습니다. deploy-web TASK는 build-docker-image TASK의 output 리소스를 사용하는것으로 정의되어 있으므로, build-docker-image수행 후에 deploy-web이 수행됩니다.

runAfter항목의 사용 예제는 아래와 같습니다.

 

2) 재시도: retries 항목으로 실패 시 재시도 횟수를 정의합니다. 

 

3) 조건문: conditions항목으로 Task를 실행할지 말지를 정의합니다.

세부적인 내용은 이 글을 실습한 후 아래 링크를 보시기 바랍니다.

https://happycloud-lee.tistory.com/91

 

2. PipelineRun YAML작성

아래 소스를 참조하여 tutorial-pipelinerun.yaml 파일을 작성합니다.

apiVersion: tekton.dev/v1alpha1
kind: PipelineRun
metadata:
  name: tutorial-pipeline-run
  namespace: tekton
spec:
  serviceAccountName: tutorial-service
  pipelineRef:
    name: tutorial-pipeline
  resources:
    - name: source-repo
      resourceRef:
        name: git-hello1
    - name: web-image
      resourceRef:
        name: image-hello1

- tutorial-pipeline을 실행하면서 pipeline resource를 넘깁니다. 

 

3. 실행하기

$ kubectl apply -f tutorial-pipeline.yaml

$ kubectl apply -f tutorial-pepelinerun.yaml

아래 명령으로 실행을 모니터링 합니다. 

$ watch tkn pr list

완료가 되면 아래 명령으로 로그를 확인해 봅니다.

$ tkn pr log tutorial-pipeline-run

※ 재실행하기

yaml의 내용을 수정한 후 다시 적용하려면 pipeline run 리소스를 삭제하고 kubectl apply -f <yaml path>을 수행해야 합니다. 

예를 들어 tutorial-pipeline.yaml 파일을 수정했다면 아래와 같이 해야 수정 사항이 적용 됩니다.

$ tkn pr delete turorial-pipeline-run

$ kubectl apply -f tutorial-pipeline.yaml

$ kubectl apply -f tutorial-pepelinerun.yaml

 

4. 실행결과 확인

pod, service, ingress를 확인해 봅니다.

$ k get pod && k get svc && k get ing

ingress를 보니, k8s cluster내의 아무 node IP로도 접근할 수 있네요.

node ip를 본 후 웹 브라우저에서 접근해 봅니다.

$ k get node -o wide

 

수고하셨습니다. 

 

 

'Agile&DevOps > Tekton' 카테고리의 다른 글

6. Conditions  (0) 2019.12.13
5. Pipeline Resource  (0) 2019.12.13
3. Task & TaskRun  (0) 2019.12.13
2. Tekton 설치하기  (0) 2019.12.13
1. Tekton 이란 ?  (0) 2019.12.12
댓글

전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]