본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Pod 템플릿

원문 보기 위키 갱신

agent-stack-k8s v0.32.2부터 kubernetes 플러그인에서 podTemplate을 지정할 수 있어요. podTemplate 속성은 스택 컨트롤러와 같은 네임스페이스에 있는 PodTemplate 리소스의 이름을 가리켜요. Pod 템플릿은 podSpec과 비슷하게 동작하지만, podSpec의 세부 내용을 파이프라인 정의에서 숨겨준답니다.

출처: 문서

본문

스택 운영자(Kubernetes 리소스를 만들 수 있는 사람)는 템플릿의 공유 라이브러리를 설정할 수 있어요. 이렇게 하면 pod-spec-patch처럼 스택 컨트롤러와 별개로, 그리고 그 템플릿을 사용하는 모든 파이프라인과도 별개로 pod 사양을 업데이트할 수 있고, 각 파이프라인 정의에 불필요한 플랫폼 세부 내용을 저장하지 않아도 된답니다.

사용 방법

  1. agent-stack-k8s v0.32.2 이상을 사용하고 있는지 확인하세요.
  2. 스택 컨트롤러와 같은 네임스페이스에 PodTemplate 리소스를 만드세요.
  3. kubernetes 플러그인의 podTemplate 키로 PodTemplate 리소스를 이름으로 참조하세요.

참고 사항

podTemplate은 podSpec과 비슷하게 동작해요. 스택 컨트롤러가 조정하기 전의 초기 pod 사양을 제공해요. podSpec이 제공되면 podTemplate은 무시돼요. 스텝 안에서 podTemplate을 조정하려면 podSpecPatch를 사용하세요.

초기 사양을 변경하는 다른 옵션으로는 다음이 있어요:

  • image, command 같은 스텝 속성
  • 리소스 클래스 태그
  • skip 같은 checkout 파라미터
  • pod-spec-patch 컨트롤러 설정과 podSpecPatch 플러그인 속성

spec(a PodSpec) 외에도 PodTemplate은 템플릿 안에 메타데이터를 지정할 수 있어요. 이 메타데이터는 스택 컨트롤러가 무시해요 - spec만 사용된답니다.

예시

다음 예시 매니페스트는 buildkite 네임스페이스에 go-with-cache라는 PodTemplate을 정의해요. 이 템플릿은

  • 컨테이너 이미지를 golang:latest로 설정하고,
  • Go가 caching tool을 사용하도록 구성하고,
  • 영구 볼륨 클레임을 연결하고,
  • 사용자와 그룹을 변경하는 보안 컨텍스트를 설정해요.
apiVersion: v1
kind: PodTemplate
metadata:
  name: go-with-cache
  namespace: buildkite # 참고: agent-stack-k8s와 같은 네임스페이스여야 해요
template:
  spec:
    containers:
      - name: container-0
        image: golang:latest
        env:
          - name: GOCACHEPROG
            value: "/tools/go-cacher -cache-server=http://gocached.default.svc.cluster.local:31364"
        volumeMounts:
          - name: tools
            mountPath: /tools
            readOnly: true
    volumes:
      - name: tools
        persistentVolumeClaim:
          claimName: tools-shared
    securityContext:
      runAsNonRoot: true
      runAsUser: 1000
      runAsGroup: 1001

클러스터에 PodTemplate을 만들었다면 kubernetes 플러그인에서 참조할 수 있어요. 다음은 (위에서 정의한) go-with-cache 템플릿을 여러 번 사용하는 파이프라인 정의예요:

steps:
  - label: "Go Build"
    command: go build -o /tmp/out .
    plugins:
      - kubernetes:
          podTemplate: go-with-cache

  - label: "Go Test"
    command: go test ./...
    plugins:
      - kubernetes:
          podTemplate: go-with-cache

  - label: "Go Vet"
    command: go vet
    plugins:
    - kubernetes:
        podTemplate: go-with-cache

podSpec만 사용한 동일한 파이프라인은 꽤 길어지고, 파이프라인 스텝과 크게 관련 없는 플랫폼 세부 내용(볼륨 구성 같은)이 담긴 반복적이고 깊이 중첩된 설정으로 이어져요:

steps:
- label: "Go Build"
  command: go build -o /tmp/out .
  plugins:
    - kubernetes:
        podSpec:
          containers:
            - name: container-0
              image: golang:latest
              env:
                - name: GOCACHEPROG
                  value: "/tools/go-cacher -cache-server=http://gocached.default.svc.cluster.local:31364"
              volumeMounts:
                - name: tools
                  mountPath: /tools
                  readOnly: true
          volumes:
            - name: tools
              persistentVolumeClaim:
                claimName: tools-shared
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1001

- label: "Go Test"
  command: go test ./...
  plugins:
    - kubernetes:
        podSpec:
          containers:
            - name: container-0
              image: golang:latest
              env:
                - name: GOCACHEPROG
                  value: "/tools/go-cacher -cache-server=http://gocached.default.svc.cluster.local:31364"
              volumeMounts:
                - name: tools
                  mountPath: /tools
                  readOnly: true
          volumes:
            - name: tools
              persistentVolumeClaim:
                claimName: tools-shared
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1001

- label: "Go Vet"
  command: go vet
  plugins:
  - kubernetes:
        podSpec:
          containers:
            - name: container-0
              image: golang:latest
              env:
                - name: GOCACHEPROG
                  value: "/tools/go-cacher -cache-server=http://gocached.default.svc.cluster.local:31364"
              volumeMounts:
                - name: tools
                  mountPath: /tools
                  readOnly: true
          volumes:
            - name: tools
              persistentVolumeClaim:
                claimName: tools-shared
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1001

이 예시를 줄이기 위해 다른 기법(예: YAML 앵커/앨리어스 또는 컨트롤러 pod-spec-patch 설정)을 사용할 수도 있지만, 이들은 podTemplate보다 유연성이 떨어지고, pod 사양을 변경하려면 파이프라인이나 컨트롤러 설정을 업데이트해야 해요.

더 알아보기 (Learn more)