Dev./Kubernetes & Helm

Helm: 차트 작성

Ivan'show 2023. 10. 5.
728x90
반응형

코드로 차트를 작성해 보면서 헬름 차트 작성법을 익혀보자.

 

내 코드로 작성해보기

values.yaml 파일과 templates 폴더 아래에 있는 모든 내용 삭제하고, 다시 환경별로 코드를 사용하기 위한 작업을 진행해 주자.

# templates/configmap.yaml

apiVersion: v1
kind: configMap
metadata:
    name: {{ .Release.Name }}-configmap
data:
    myvalue: "Hello World"
helm install tiger ./mychart
# result
kimminhyeok@Ivans-Mac k8s % helm install tiger ./mychart
NAME: tiger
LAST DEPLOYED: Wed Sep 13 13:36:54 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

Value 값을 넣어주기

# templates/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
data:
    myvalue: "Hello World"
    drink: {{ .Values.favoriteDrink }}
# values.yaml
favoriteDrink: coffee
# result
kimminhyeok@Ivans-Mac k8s % helm install cafe ./mychart
NAME: cafe
LAST DEPLOYED: Wed Sep 13 13:38:41 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

리스트 확인하기

helm list --all
#
kimminhyeok@Ivans-Mac k8s % helm list --all
NAME    NAMESPACE       REVISION        UPDATED                               STATUS   CHART           APP VERSION
cafe    default         1               2023-09-13 13:38:41.545085 +0900 KST  deployed mychart-0.1.0   1.16.0     
tiger   default         1               2023-09-13 13:36:54.216738 +0900 KST  deployed mychart-0.1.0   1.16.0

모두 삭제하기

helm uninstall tiger cafe
kimminhyeok@Ivans-Mac k8s % helm uninstall tiger cafe  
release "tiger" uninstalled
release "cafe" uninstalled

직접 생성하지 않고 어떻게 생성이 될지 확인 가능

helm install --debug --dry-run cafe ./mychart
# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
data:
    myvalue: "Hello World"
    drink: {{ quote .Values.favoriteDrink }}
    food: {{ quote .Values.favoriteFood }}
# values.yaml
favoriteDrink: coffee
favoriteFood: pizza
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 13:47:17 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favoriteDrink: coffee
favoriteFood: pizza

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
data:
    myvalue: "Hello World"
    drink: coffee
    food: pizza

추가 옵션 값과 인덴트로 코드 구조 변경

# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
data:
    myvalue: "Hello World"
    {{- with .Values.favorites }}
    drink: {{ quote .drink | default "tea" }}
    food: {{ .food | upper | quote }}
    {{- if eq .drink "coffee" }}
    mug: true
    {{- end }}
    {{- end }}
# values.yaml
favorites:
  drink: coffee
  food: pizza
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 14:07:53 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favorites:
  drink: coffee
  food: pizza

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "PIZZA"
    mug: true
  • value 에 “” 추가, default 값 삽입
  • upper case 적용
  • favorites 로 묶음
  • if 문 추가
  • {{- 로 앞에 있는 빈 공간 삭제
  • with 로 반복되는 내용 저장

변수 스코핑 & 리스트 & 튜플

# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
data:
    myvalue: "Hello World"
    {{- $relname := .Release.Name -}}
    {{- with .Values.favorites }}
    drink: {{ quote .drink | default "tea" }}
    food: {{ .food | upper | quote }}
    release: {{ $relname }}
    {{- end }}
    toppings: |-
        {{- range .Values.pizzaTopings }}
        - {{ . | title | quote }}
        {{- end }}
    sizes: |-
        {{- range tuple "small" "medium" "large" }}
        - {{ . }}
        {{- end }}
# values.yaml
favorites:
  drink: coffee
  food: pizza

pizzaTopings:
  - onions
  - cheeze
  - pineapple
  - tomato souce
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 14:16:51 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favorites:
  drink: coffee
  food: pizza
pizzaTopings:
- onions
- cheeze
- pineapple
- tomato souce

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "PIZZA"
    release: cafe
    toppings: |-
        - "Onions"
        - "Cheeze"
        - "Pineapple"
        - "Tomato Souce"
    sizes: |-
        - small
        - medium
        - large

labels & Key-value & 인덱스 적용

# templates/configMap.yaml
{{- define "mychart.labels" }}
    labels:
        generator: helm
        data: {{ now | htmlDate }}
{{-end}}

apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    {{- template "mychart.labels" }}
data:
    myvalue: "Hello World"
    {{- $relname := .Release.Name -}}
    {{- range $key, $val := .Values.favorites }}
    {{ $key }}: {{$val | quote}}
    {{- end }}
    toppings: |-
        {{- range $index, $topping := .Values.pizzaTopings }}
        {{ $index }}: {{ $topping | title | quote }}
        {{- end }}
    sizes: |-
        {{- range tuple "small" "medium" "large" }}
        - {{ . }}
        {{- end }}
# values.yaml
favorites:
  drink: coffee
  food: pizza

pizzaTopings:
  - onions
  - cheeze
  - pineapple
  - tomato souce
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 14:23:31 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favorites:
  drink: coffee
  food: pizza
pizzaTopings:
- onions
- cheeze
- pineapple
- tomato souce

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
    labels:
        generator: helm
        data: 2023-09-13
data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "pizza"
    toppings: |-
        0: "Onions"
        1: "Cheeze"
        2: "Pineapple"
        3: "Tomato Souce"
    sizes: |-
        - small
        - medium
        - large

template 파일로 레이블 이전

# templates/_helpers.tpl
{{- define "mychart.labels" }}
    labels:
        generator: helm
        data: {{ now | htmlDate }}
{{- end }}
# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    {{- template "mychart.labels" }}
data:
    myvalue: "Hello World"
    {{- $relname := .Release.Name -}}
    {{- range $key, $val := .Values.favorites }}
    {{ $key }}: {{$val | quote}}
    {{- end }}
    toppings: |-
        {{- range $index, $topping := .Values.pizzaTopings }}
        {{ $index }}: {{ $topping | title | quote }}
        {{- end }}
    sizes: |-
        {{- range tuple "small" "medium" "large" }}
        - {{ . }}
        {{- end }}

템플릿 파일에 _ 를 쓰면 인스톨 할 때 컴파일에서 제외되고 yaml 파일에서 템플릿 파일을 호출할 떄 실행된다.

그렇기 때문에 특정 변수 값이 필요할 떄는 props 처럼 값을 넘겨줘야 한다.

# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    labels:
        {{ template "mychart.labels" . }}
data:
    myvalue: "Hello World"
    {{- $relname := .Release.Name -}}
    {{- range $key, $val := .Values.favorites }}
    {{ $key }}: {{$val | quote}}
    {{- end }}
    toppings: |-
        {{- range $index, $topping := .Values.pizzaTopings }}
        {{ $index }}: {{ $topping | title | quote }}
        {{- end }}
    sizes: |-
        {{- range tuple "small" "medium" "large" }}
        - {{ . }}
        {{- end }}
# templates/_helpers.tpl
{{- define "mychart.labels" }}
    labels:
        generator: helm
        data: {{ now | htmlDate }}
        chart: {{ .Chart.Name }}
        version: {{ .Chart.Version }}
{{- end }}
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 14:36:51 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favorites:
  drink: coffee
  food: pizza
pizzaTopings:
- onions
- cheeze
- pineapple
- tomato souce

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
    labels:
        
    labels:
        generator: helm
        data: 2023-09-13
        chart: mychart
        version: 0.1.0
data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "pizza"
    toppings: |-
        0: "Onions"
        1: "Cheeze"
        2: "Pineapple"
        3: "Tomato Souce"
    sizes: |-
        - small
        - medium
        - large

template 으로 앱 내용 삽입하기

# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    labels:
        {{ template "mychart.labels" . }}
data:
    myvalue: "Hello World"
    {{- $relname := .Release.Name -}}
    {{- range $key, $val := .Values.favorites }}
    {{ $key }}: {{$val | quote}}
    {{- end }}
    toppings: |-
        {{- range $index, $topping := .Values.pizzaTopings }}
        {{ $index }}: {{ $topping | title | quote }}
        {{- end }}
    sizes: |-
        {{- range tuple "small" "medium" "large" }}
        - {{ . }}
        {{- end }}
    {{ template "mychart.app" . }}
# templates/_helpers.tpl
{{- define "mychart.labels" }}
    labels:
        generator: helm
        data: {{ now | htmlDate }}
        chart: {{ .Chart.Name }}
        version: {{ .Chart.Version }}
{{- end }}

{{- define "mychart.app" -}}
app_name: {{ .Chart.Name }}
app_version: "{{ .Chart.Version }}"
{{- end -}}
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 14:40:28 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favorites:
  drink: coffee
  food: pizza
pizzaTopings:
- onions
- cheeze
- pineapple
- tomato souce

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
    labels:
        
    labels:
        generator: helm
        data: 2023-09-13
        chart: mychart
        version: 0.1.0
data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "pizza"
    toppings: |-
        0: "Onions"
        1: "Cheeze"
        2: "Pineapple"
        3: "Tomato Souce"
    sizes: |-
        - small
        - medium
        - large
    app_name: mychart
app_version: "0.1.0"

인덴트가 망가져 있는게 보인다.

인덴트를 잘 집어 넣을려면 templates 가 아니라 include 를 사용한다.

# templates/configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    labels:
        {{ include "mychart.labels" . }}
data:
    myvalue: "Hello World"
    {{- $relname := .Release.Name -}}
    {{- range $key, $val := .Values.favorites }}
    {{ $key }}: {{$val | quote}}
    {{- end }}
    toppings: |-
        {{- range $index, $topping := .Values.pizzaTopings }}
        {{ $index }}: {{ $topping | title | quote }}
        {{- end }}
    sizes: |-
        {{- range tuple "small" "medium" "large" }}
        - {{ . }}
        {{- end }}
{{ include "mychart.app" . | indent 8 }}
# result
kimminhyeok@Ivans-Mac k8s % helm install --debug --dry-run cafe ./mychart
install.go:200: [debug] Original chart version: ""
install.go:217: [debug] CHART PATH: /Users/kimminhyeok/workspace/k8s/mychart

NAME: cafe
LAST DEPLOYED: Wed Sep 13 14:46:00 2023
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
favorites:
  drink: coffee
  food: pizza
pizzaTopings:
- onions
- cheeze
- pineapple
- tomato souce

HOOKS:
MANIFEST:
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
    name: cafe-configmap
    labels:
        
    labels:
        generator: helm
        data: 2023-09-13
        chart: mychart
        version: 0.1.0
data:
    myvalue: "Hello World"
    drink: "coffee"
    food: "pizza"
    toppings: |-
        0: "Onions"
        1: "Cheeze"
        2: "Pineapple"
        3: "Tomato Souce"
    sizes: |-
        - small
        - medium
        - large
        app_name: mychart
        app_version: "0.1.0"
728x90
반응형

댓글