Search
moon
sun

매니페스트 패키지 관리

HELM CHART 작성 방법

Helm CLI가 설치 되어 있으면, helm create 명령어로 새로운 차트를 생성할 수 있다.
Markdown
복사
•
Helm의 기본 구조
$ tree somaz somaz ├── Chart.yaml ├── charts ├── templates │ ├── NOTES.txt │ ├── _helpers.tpl │ ├── deployment.yaml │ ├── hpa.yaml │ ├── ingress.yaml │ ├── service.yaml │ ├── serviceaccount.yaml │ └── tests │ └── test-connection.yaml └── values.yaml 4 directories, 10 files
Shell
복사
•
helm completion bash (bash에 대한 자동 완성 스크립트 생성)
vi ~/.bashrc # 맨밑에 작성 sudo bash -c 'helm completion bash > /etc/bash_completion.d/helm' source ~/.bashrc
Shell
복사

Helm Chart Install

•
-f 옵션을 주고 overide.values.yaml을 사용하여 Install 할 수 있다.
helm install -f <오버라이딩 파일경로> <Release 이름> <차트 경로> ex) helm install -f myvalues.yaml myredis ./redis
Shell
복사
•
helm install (upgrade)할때 —set을 사용하면 values.yaml 필드를 오버라이딩 할 수 있다.
helm install --set <오버라이딩 필드>=<값> <Release 이름> <차트 경로> ex) helm install --set image=redis:latest myredis ./redis > 위와 같이 작성하면 image 필드가 redis:latest로 변경이 된다. image: redis:stable -> image: redis:latest
Shell
복사

Helm Chart 문법

$ helm create somaz $ ls Chart.yaml charts templates values.yaml $ ls templates NOTES.txt deployment.yaml ingress.yaml serviceaccount.yaml _helpers.tpl hpa.yaml service.yaml tests
Shell
복사
•
Chart.yaml
차트 자체에 대한 메타데이터가 포함된 기본 파일이다. name, version, description, home, keywords, sources 등과 같은 필드가 포함 된다. dependencies는 package.json과 비슷한 느낌이다.
•
values.yaml
차트의 기본 구성 값을 작성한다. 다양한 환경에 대해 다양한 값으로 템플릿을 매개변수화 할 수 있는 파일이다.
해당 파일의 작성한 값들을 기준으로 template의 yaml파일로 오버라이딩 된다.
•
templates
값과 결합할때 유효한 k8s manifest를 생성하는 템플릿의 디렉터리이다. 템플릿은 Go 템플릿 언어를 사용한다.
•
templates/deployment.yaml 예시
apiVersion: apps/v1 kind: Deployment metadata: name: {{ include "mychart.fullname" . }} labels: {{- include "mychart.labels" . | nindent 4 }} spec: {{- if not .Values.autoscaling.enabled }} replicas: {{ .Values.replicaCount }} {{- end }} selector: matchLabels: {{- include "mychart.selectorLabels" . | nindent 6 }} template: metadata: {{- with .Values.podAnnotations }} annotations: {{- toYaml . | nindent 8 }} {{- end }} labels: {{- include "mychart.labels" . | nindent 8 }} {{- with .Values.podLabels }} {{- toYaml . | nindent 8 }} {{- end }} spec: {{- with .Values.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} serviceAccountName: {{ include "mychart.serviceAccountName" . }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} containers: - name: {{ .Chart.Name }} securityContext: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: {{ .Values.service.port }} protocol: TCP livenessProbe: httpGet: path: / port: http readinessProbe: httpGet: path: / port: http resources: {{- toYaml .Values.resources | nindent 12 }} {{- with .Values.volumeMounts }} volumeMounts: {{- toYaml . | nindent 12 }} {{- end }} {{- with .Values.volumes }} volumes: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.affinity }} affinity: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 8 }} {{- end }}
YAML
복사
•
문법 정리
조건절은 {{- if }} 훅은 {{- if not }} {{ end }} 로 사용할 수 있고 {{- with 키 }} {{ end }} 는 with 절 안에서는 구조상 해당 키 아래의 깂들을 사용하겠다는 의미이다. {{- toYaml 키 }} 는 키의 값을 그대로 Yaml로 프린트 해준다.
Markdown
복사
# 조건절 예시 # {{- if }} 훅은 조건이 참일 때만 실행됩니다. # {{- if not }} 훅은 조건이 거짓일 때만 실행됩니다. # {{ end }}로 조건절을 닫습니다. {{- if .Values.enabled }} enabled: true {{- else }} enabled: false {{- end }} # with 절 예시 # {{- with 키 }} {{ end }}는 해당 키 아래의 값을 사용하겠다는 의미입니다. # with 절 안에서는 키를 생략하고 값을 사용할 수 있습니다. {{- with .Values.service }} service: name: {{ .name }} # 현재 컨텍스트 내의 특정 값을 참조 (.Values.service 생략) port: {{ .port }} {{- end }} # toYaml 예시 # {{- toYaml 키 }}는 키의 값을 Yaml 형식으로 출력합니다. config: | {{ toYaml .Values.config | indent 2 }}
Markdown
복사
•
with 블록을 사용하면 반복적인 접두사 (예: .Values.service)를 줄일 수 있어 템플릿을 더 간결하게 한다.
•
indent와 nindent
◦
indent는 지정된 수의 공백으로 텍스트를 들여쓰기 한다.
◦
nindent는 indent와 동일하지만, 앞에 있는 공백을 제거 후 들여쓰기를 한다.
•
{{- ... }}
◦
{{-는 템플릿 표현식 앞의 공백을 제거한다.
◦
}}는 템플릿 표현식 뒤의 공백을 제거한다.
•
. (dot)
◦
.는 현재 컨텍스트를 나타냅니다. Helm 템플릿에서 .는 현재 값의 범위를 나타내며, 상위 값에 접근할 수 있습니다.
# range 예시 spec: containers: {{- range .Values.containers }} - name: {{ .name }} image: {{ .image }} {{- end }} # quote 예시 data: MY_VAR: {{ quote .Values.myVar }} # eq 예시 {{- if eq .Values.environment "production" }} PRODUCTION_SETTING: true {{- else }} PRODUCTION_SETTING: false {{- end }} # and, or 예시 {{- if and .Values.featureA .Values.featureB }} BOTH_FEATURES_ENABLED: true {{- end }} {{- if or .Values.featureX .Values.featureY }} EITHER_FEATURE_ENABLED: true {{- end }} # with 예시 {{- with .Values.resources }} resources: limits: cpu: {{ .cpu }} memory: {{ .memory }} {{- end }} # define과 template 예시 {{- define "myapp.fullname" -}} {{- $name := default .Chart.Name .Values.nameOverride -}} {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} {{- end -}} # 위에서 정의한 템플릿 사용 name: {{ template "myapp.fullname" . }} # include 예시 (define과 비슷하지만 파이프라인 지원) {{ include "myapp.fullname" . | upper | quote }}
YAML
복사
•
range:
◦
리스트나 맵을 순회합니다.
◦
예시에서는 .Values.containers의 각 항목에 대해 컨테이너 정의를 생성합니다.
•
quote:
◦
문자열을 따옴표로 감쌉니다. 특수 문자가 있는 문자열을 안전하게 처리할 때 유용합니다.
◦
예시에서는 MY_VAR의 값을 따옴표로 감쌉니다.
•
eq:
◦
두 값이 같은지 비교합니다.
◦
예시에서는 환경이 "production"인지 확인하고, 그에 따라 다른 설정을 적용합니다.
•
and, or:
◦
논리 연산자입니다. 여러 조건을 결합할 때 사용합니다.
◦
예시에서는 두 기능이 모두 활성화되었는지, 또는 둘 중 하나라도 활성화되었는지 확인합니다.
•
with:
◦
특정 객체의 스코프 내에서 작업할 때 사용합니다. 반복되는 접두사를 줄일 수 있습니다.
◦
예시에서는 .Values.resources 내의 값들을 간단히 참조합니다.
•
define과 template:
◦
define으로 재사용 가능한 템플릿을 정의하고, template으로 그것을 사용합니다.
◦
예시에서는 애플리케이션의 전체 이름을 생성하는 템플릿을 정의하고 사용합니다.
•
include:
◦
template과 비슷하지만 파이프라인을 지원합니다.
◦
예시에서는 정의된 템플릿을 포함시키고, 결과를 대문자로 변환한 후 따옴표로 감쌉니다.
•
define: 재사용 가능한 템플릿 블록을 정의합니다.
•
default: 기본값을 설정합니다.
•
printf: 문자열을 포맷팅합니다.
•
trunc: 문자열을 자릅니다.
•
trimSuffix: 문자열의 접미사를 제거합니다.
•
template: 정의된 템플릿 블록을 호출합니다.