• 헬름차트 테스트 - helm template
    아키텍처 및 기술 공부/헬름 2025. 3. 30. 17:46
    반응형

    헬름(Helm)은 쿠버네티스(Kubernetes) 애플리케이션을 패키징하고 배포하는 도구다. helm template 명령어는 헬름차트로 쿠버네티스 리소스를 생성하기 전에, 로컬에서 차트가 올바르게 렌더링 되는지 확인 할 수 있는 명령어다.

     

     

     

    1. helm template 설명

     helm template은 헬름 차트를 쿠버네티스 리소스 YAML로 변환한다. 클러스터에 배포하지 않고도 헬름 차트가 올바르게 렌더링되는지 확인할 수 있다. 즉 차트 내부 변수({ .Values... }, { .Release...} 등)의 내용을 반영한 YAML을 확인할 수 있다.

     

     

     

    2. 기본 사용법

    helm template <RELEASE_NAME> <CHART_PATH>

     

    예시는 다음과 같다.

    helm template onestone .

    onestone은 헬름의 릴리스(.Release.Name 변수) 이름이다. .(현재폴더)은 차트의 위치다. 이 명령을 실행하면 현재폴더(.) 내의 헬름 템플릿 파일들이 YAML 형식으로 변환되어 출력된다.

     

     

     

    3. 주요 옵션 정리

    1) -f: 특정 values 파일 적용

    헬름은 기본적으로 values.yaml을 사용하지만, 다른 값을 적용하고 싶다면 -f 옵션을 사용한다.

    helm template onestone . -f values-dev.yaml

    여러 개의 values 파일을 사용할 수도 있다.

    helm template onestone . -f values.yaml -f values-dev.yaml

     

    2) --set: 특정 값 직접 설정

    helm template onestone . --set replicaCount=3

    이렇게 하면 values.yaml을 수정하지 않고도 특정 값을 변경하여 테스트할 수 있다.

    중첩된 값 설정 예시

    helm template onestone . --set image.repository=nginx --set image.tag=1.21
     

     

    3) --namespace: 특정 네임스페이스 지정

    Helm 차트가 특정 네임스페이스에서 동작하는지 미리 확인할 수 있다.

    helm template onestone . --namespace test

    이렇게 하면 metadata.namespace: staging이 자동으로 추가된다.

     

    4) --output-dir: 결과를 파일로 저장

    기본적으로 helm template은 콘솔에 출력하지만, 결과를 파일로 저장할 수도 있다.

    helm template onestone . --output-dir ./output

    이 명령을 실행하면 ./output 디렉터리에 YAML 파일들이 생성됩니다.

     

     

     

    4. 테스트를 위한 차트 생성

    // mkdir로 helm 폴더를 만들고 helm 폴더로 이동한다.
    mkdir helm
    cd helm
    // 현재(.)폴더에 헬름차트 생성
    helm create .
    // helm create로 만든 테스트 헬름 차트 구조
    
    helm/
    ├── Chart.yaml  
    ├── charts/     
    ├── templates/   
    │       ├──NOTES.txt
    │       ├──_helpers.tpl
    │       ├──deployment.yaml
    │       ├──hpa.yaml
    │       ├──ingress.yaml
    │       ├──service.yaml
    │       ├──serviceaccount.yaml 
    │       └──tests/
    │            └── test-connection.yaml
    ├──.helmignore
    └──values.yaml

     

     

     

    4. 테스트

     

    렌더링되지 않은 차트를 다음 명령어로 렌더링된 차트를 만들어보자.

    helm template onestone .

     

    metadata.name이 "onestone-."으로 만들어졌는데, _helpers.tpl파일에 "..serviceAccountName"이  ".ReleaseName + Chart.Name"이라 그렇다. 헷갈리므로 Chart.yaml의 name을 .에서 testApp으로 바꿔주자.

     

     

     

     

    5. --set과 함께 -f를 여러번 사용하면 차트에 어떻게 반영될까?

    테스트를 위해 values-dev.yaml파일을 생성한다.

    // values-dev.yaml
    serviceAccount:
      create: true
      automount: true
      annotations: {}
      name: "values-test"

     

     

    values.yaml의 내용은 다음과 같다.

    // values.yaml
    ...
    serviceAccount:
      create: true
      automount: true
      annotations: {}
      name: ""
    ...

     

    serviceaccount는 values파일의 serviceAccount.name이 ""이라면 ".ReleaseName Chart.Name"이 노출되고, 값이 지정되어 있다면, serviceAccount.name의 값이 노출된다.

     

     

    (1) 다음 template명령어로 테스트를 해보자.

    아래 명령어는 values-dev.yaml을 먼저 선언했으므로 values.yaml이 values-dev.yaml의 내용을 덮어쓰기 하므로, onestone-testApp이 노출된다.

    helm template onestone . -f values-dev.yaml -f values.yaml

     

     

    아래 명령어는 values.yaml을 먼저 선언했으므로 values-dev.yaml이 values.yaml의 내용을 덮어쓰기 하므로, values-test가 노출된다.

    helm template onestone . -f values.yaml -f values-dev.yaml

     

     

    (2) yaml파일 하위의 모든 내용을 덮어쓰기 하나?

    다음 테스트를 위해 values-dev.yaml의 값을 수정해본다.

    // values-dev.yaml
    serviceAccount:
      # create: true 제거
      # automount: true 제거
      annotations: {}
      name: "values-test"

     

    다음 명령어로 values-dev.yaml이 values값을 덮어쓰기하게 만든다.

    helm template onestone . -f values.yaml -f values-dev.yaml

     

    잘 나오는 것을 확인할 수 있다. 결국 values-dev.yaml은 values.yaml의 값을 덮어쓰기(overwrite)하지만, 기본적인 동작은 merge이다.

     

    마지막 테스트를 위해, values-dev.yaml, values.yaml의 값을 모두 변경해본다.

    // values.yaml
    ...
    serviceAccount:
      automount: true
      name: ""
      annotations: {}
    ...
    // values-dev.yaml
    serviceAccount:
      create: true
      name: "values-test"

     

    다음명령어로 기대하는 결과는 다음과 같다.

    helm template onestone . -f values.yaml -f values-dev.yaml
    serviceAccount:
      create: true
      automount: true
      name: "values-test"
      annotations: {}

     

    원하는 결과대로 잘 나오는 것을 확인할 수 있다.

     

     

     

    반응형

    댓글

Designed by Tistory.