Please enable JavaScript to view the comments powered by Disqus.
Search
moon
sun

K8S Helm quick start, the package manager

태그
K8S
kubernates
Helm
package
공개여부
작성일자
2022/10/06
K8S의 helm을 사용하는 방법과 응용, 디버깅 방법을 알아본다.

Helm 이란 무엇인가?

•
패키지 매니저로 개발자가 k8s cluster 환경에서 application을 쉽게 배포할 수 있게 해준다.
•
Cloud Native Computing Foundation 의 2018년 졸업 프로젝트 설명

주요 개념 3가지

Chart

•
헬름의 패키지.
•
K8S 내에서 application, 도구, 서비스를 구동하는데 필요한 resource 정의
•
yum, homebrew 와 같다고 볼 수 있다.

Repository

•
Chart를 모아두고 공유하는 장소
•
K8S 패키지용

Release

•
K8S Cluster 에서 구동되는 chart 의 instance

Helm search: Chart 찾기

•
helm search hub
◦
여러 저장소에 있는 Helm chart 를 다루는 헬름의 hub
•
helm search repo
◦
Local helm client에 추가된 저장소를 검색한다.
◦
검색은 local의 범위에서 이루어지며, public network 접속이 필요하지 않다.
helm search hub wordpress
Shell
복사
URL CHART VERSION APP VERSION DESCRIPTION https://artifacthub.io/packages/helm/kube-wordp... 0.1.0 1.1 this is my wordpress package https://artifacthub.io/packages/helm/bitnami-ak... 15.2.5 6.0.2 WordPress is the world's most popular blogging ... https://artifacthub.io/packages/helm/shubham-wo... 0.1.0 1.16.0 A Helm chart for Kubernetes https://artifacthub.io/packages/helm/bitnami/wo... 15.2.5 6.0.2 WordPress is the world's most popular blogging ... https://artifacthub.io/packages/helm/sikalabs/w... 0.2.0 Simple Wordpress https://artifacthub.io/packages/helm/groundhog2... 0.6.6 6.0.2-apache A Helm chart for Wordpress on Kubernetes https://artifacthub.io/packages/helm/camptocamp... 0.6.10 4.8.1 Web publishing platform for building blogs and ... https://artifacthub.io/packages/helm/risserlabs... 0.0.1 latest open source software you can use to create a be... https://artifacthub.io/packages/helm/riftbit/wo... 12.1.16 5.8.1 Web publishing platform for building blogs and ... https://artifacthub.io/packages/helm/devops/wor... 0.9.0 1.16.0 Wordpress helm chart https://artifacthub.io/packages/helm/homeenterp... 0.5.0 5.9.3-php8.1-apache Blog server https://artifacthub.io/packages/helm/mcouliba/w... 0.1.0 1.16.0 A Helm chart for Kubernetes https://artifacthub.io/packages/helm/wordpress-... 1.0.0 1.1 This is a package for configuring wordpress and... https://artifacthub.io/packages/helm/securecode... 3.15.0 4.0 Insecure & Outdated Wordpress Instance: Never e... https://artifacthub.io/packages/helm/bitnami/wo... 2.1.11 6.0.2 WordPress for Intel is the most popular bloggin...
Shell
복사
필요한 helm 을 찾아 내가 구성한 K8S cluster 에 추가할 수 있다.
Workshop 에서도 몇몇 설피가 필요한 다른 pod, deployment 등을 helm repo add 후에 helm install 로 쉽게 설치할 수 있다.

우리는 어떻게 사용하면 좋을까?

•
Coway 는 특정 application 을 서버에 배포할 때 dev, stg, prd 3군데로 나누어 사용한다.
•
dev 팀 내부에서 주로 사용한다.
•
stg 팀 외부에서 연동 목적으로 사용하며 QA 범위가 된다.
•
prd 실제 고객들이 접속하는 곳
이렇게 요구사항이 나뉘다 보니 어떠한 application 집합 AA 가 dev, stg, prd 에 따라 스펙이 다르게 구성되어야 하는 요구사항이 있다.

Requirements

dev
stg
prd
replicas
2
2
5
memory
100m
100m
100m
CPU
768Mi
768Mi
1152Mi
HPA Scale-out RAM
90%
70%
50%
이와 같은 요구사항을 만족하기 위해 manifest는 dev, stg, prd 가 모두 다른 manifest를 만들어 적용해야 한다.
즉 집합 AA 의 배포를 만족하는 manifest 집합 BB 와 A×BA \times B 의 element가 생성된다.
이러한 결과는 관리 포인트가 늘어난다는 것을 의미한다.

How create Application manifest for K8S cluster.

다음은 가장 익숙하게 접근할 수 있는 deployment manifest 예제이다.
apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Values.appName }}-deployment namespace: {{ .Values.namespace }} labels: app: {{ .Values.appName }} spec: replicas: {{ .Values.replicas }} selector: matchLabels: app: {{ .Values.appName }} template: metadata: labels: app: {{ .Values.appName }} app.kubernetes.io/name: {{ .Values.appName }} spec: containers: - image: {{ .Values.imageName }} imagePullPolicy: Always name: {{ .Values.appName }} resources: requests: memory: {{ .Values.resources.requests.memory }} cpu: {{ .Values.resources.requests.cpu }} limits: memory: {{ .Values.resources.limits.memory }} env: - name: JVM_OPTS value: "{{ .Values.resources.jvmOpts }}" ports: - containerPort: 8080
YAML
복사
{{ .Values.* }} 은 mustache syntax 이다.
이미 많은 곳에서 template engine 으로 사용되며 동적으로 {{ }} 안에 할당되는 값을 결정할 수 있다.
그래서 우리는 dev, stg, prd 에 따라 달라지는 값들을 알맞게 정의하고 template 에서 사용할 것이다.
namespace: myapp appName: myapp healthCheck: /actuator/health initial: 10 replicas: 2 configName: myapp-config env: dev resources: replicas: min: 2 max: 5 cpu: averageUtilization: 90 requests: memory: "768Mi" cpu: "100m" limits: memory: "768Mi" jvmOpts: >- -server -XX:InitialRAMPercentage=50.0 -XX:MaxRAMPercentage=75.0
YAML
복사
dev 영역에서 사용할 value 이름을 value-dev.yaml 이라 명명했다.
위의 정의에서 확인할 수 있듯이 사용이 반복되는 값들을 mustash 로 관리하면 반복을 줄일 수 있으며 오타 등의 에러를 사전에 방지할 수 있다.
비슷하게 위의 requirements를 참고하여 value.yaml 을 만들어보자.
현재 예제에선 dev와 stg가 크게 차이가 없으므로 stg-value.yaml 은 생략한다.
namespace: myapp appName: myapp healthCheck: /actuator/health initial: 10 replicas: 5 configName: myapp-config env: prd resources: replicas: min: 5 max: 10 cpu: averageUtilization: 50 requests: memory: "1152Mi" cpu: "100m" limits: memory: "1152Mi" jvmOpts: >- -server -XX:InitialRAMPercentage=50.0 -XX:MaxRAMPercentage=75.0
YAML
복사

How to Debug my Manifest template?

현재 예제에선 deployment.yaml 하나만 정의 하였지만, 실제로 service, config-map, ingress, hpa 등 여러 resource를 사용하게 될 것이다.
이렇게 다양한 resource 를 정의하다 보면 실수 혹은, K8S manifest API 정의대로 작성하지 못할 수 있다. K8S API 서버로 전송될 때 문서 형식 이외의 이유로 YAML 파일 apply가 거부될 수 있다.
사실 이건 굉장히 까다롭고 피곤한 이슈이다.
helm 에선 이 문제를 다음의 명령어를 통해 확인할 수 있다.
•
helm lint
•
helm install --dry-run --debug 혹은 helm template --debug 로 확인할 수 있다.

가정. directory

helm ├── Chart.yaml ├── templates │ ├── acupi-configmap.yml │ ├── deployment.yaml │ ├── hpa.yml │ └── service.yaml ├── values-dev.yaml ├── values-prd.yaml └── values-stg.yaml
YAML
복사

template 결과 확인해보기

편리한 설명을 위해 작업 환경의 directory가 위와 같다고 가정하여 다음과 같은 command를 입력해보자.
helm template -f helm/values-dev.yaml helm
Shell
복사
helm template -f [path/value.yaml] [template헬름/path]
Shell
복사
그런데 이와같은 command는 꼼꼼히 확인해보지 않는다면 확인이 어렵다.

--dry-run, --debug

다음과 같이 입력해보자.
helm install -f helm/values-dev.yaml helm --debug --dry-run
Shell
복사
결과는 다음과 같다.
install.go:178: [debug] Original chart version: "" Error: INSTALLATION FAILED: must either provide a name or specify --generate-name helm.go:88: [debug] must either provide a name or specify --generate-name helm.sh/helm/v3/pkg/action.(*Install).NameAndChart helm.sh/helm/v3/pkg/action/install.go:612 main.runInstall helm.sh/helm/v3/cmd/helm/install.go:184 main.newInstallCmd.func2 helm.sh/helm/v3/cmd/helm/install.go:125 github.com/spf13/cobra.(*Command).execute github.com/spf13/cobra@v1.2.1/command.go:856 github.com/spf13/cobra.(*Command).ExecuteC github.com/spf13/cobra@v1.2.1/command.go:974 github.com/spf13/cobra.(*Command).Execute github.com/spf13/cobra@v1.2.1/command.go:902 main.main helm.sh/helm/v3/cmd/helm/helm.go:87
Shell
복사
혹은 다음의 방법도 있다.
helm lint -f helm/values-dev.yaml helm --debug
Shell
복사
==> Linting k8s/template/helm [INFO] Chart.yaml: icon is recommended [INFO] values.yaml: file does not exist [WARNING] templates/deployment.yaml: object name does not conform to Kubernetes naming requirements: "-deployment": metadata.name: Invalid value: "-deployment": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*') [WARNING] templates/service.yaml: object name does not conform to Kubernetes naming requirements: "-service": metadata.name: Invalid value: "-service": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')
Shell
복사

Helm use “if statement” - Flow control

경험상 dev에 white box를 구성하기 위한 Job이 필요했던 적이 있다.
다음 manifest의 bold 처리된 부분을 주의깊게 보자
{{- if eq .Values.deployEnv "dev" }} apiVersion: batch/v1 kind: Job metadata: name: statistics-all spec: template: metadata: labels: app.kubernetes.io/name: {{ .Values.appName }} spec: containers: - name: batch-account image: {{ .Values.imageName }} command: [sh, /data/statistics_all.sh] restartPolicy: Never backoffLimit: 3 {{- end }}
YAML
복사
dev 영역에만 Job 을 실행해보자
이 이외에도 다양한 syntax가 있으며 내 필요에 맞게 가져다 쓸 수 있다.
전부를 다루기엔 더 많은 예시가 필요하니 필요한 것을 찾으며 적용해보자.

Reference