클라우드 컴퓨팅/쿠버네티스

Helm으로 매니페스트 범용화하기

espossible 2021. 10. 26. 23:35

헬름(Helm)은 쿠버네티스의 패키지 관리자이다. 헬름은 오픈 소스 소프트웨어로 공개되어 있으며, 이미 많은 패키지(Chart)가 있다. 예를들어 레디스 클러스터나 워드프레스 환경 등의 소프트웨어를 하나의 명령어로 쿠버네티스 클러스터에 배포할 수 있다.

 

또한, 롤링 업데이트 등에도 지원하는 것들이 많아 쿠버네티스에서 최적화된 설정으로 사용할 수 있는 장점이 있다.

 

헬름은 클라이언트 측에서 처리하기 때문에 kubectl과 같은 인증 정보를 사용한다.

 

기본적으로 ~/.kube/config를 사용한다.

 

시스템이 대규모로 바뀔수록 비슷한 매니페스트를 대량으로 만들어야 하므로 재사용이나 일괄 변경 작업이 어려워진다. 그래서 필요한 것이 매니페스트 범용화라는 개념이다. 매니페스트를 범용화하고 관리하는 방법을 알아보자.

 

기본적인 헬름 설치 및 사용법을 살펴보자.

헬름 설치

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh

헬름 저장소 추가

# 저장소 추가
$ helm repo add stable https://charts.helm.sh/stable
"stable" has been added to your repositories

$ helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories

# 등록된 저장소 표시
$ helm repo list
NAME   	URL                               
stable 	https://charts.helm.sh/stable     
bitnami	https://charts.bitnami.com/bitnami

# 저장소 업데이트
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈

차트 검색

- 차트 버전 : 템플릿 내용이나 초기 설정값이 변경될 때 업데이트 됨

- 앱 버전 : 실제 애플리케이션 버전

- 아티팩트 허브 : https://artifacthub.io/

# 저장소에서 차트 검색
$ helm search repo wordpress
NAME             	CHART VERSION	APP VERSION	DESCRIPTION                                       
bitnami/wordpress	12.1.24      	5.8.1      	Web publishing platform for building blogs and ...
stable/wordpress 	9.0.3        	5.3.2      	DEPRECATED Web publishing platform for building...

# 헬름 허브에서 차트 검색
$ helm search hub wordpress
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/wo...	12.1.24       	5.8.1              	Web publishing platform for building blogs and ...
https://artifacthub.io/packages/helm/groundhog2...	0.4.2         	5.8.1-apache       	A Helm chart for Wordpress on Kubernetes          
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/bitnami-ak...	12.1.18       	5.8.1              	Web publishing platform for building blogs and ...
https://artifacthub.io/packages/helm/mcouliba/w...	0.1.0         	1.16.0             	A Helm chart for Kubernetes                       
https://artifacthub.io/packages/helm/homeenterp...	0.1.0         	5.8.0-php8.0-apache	Blog server                                       
https://artifacthub.io/packages/helm/securecode...	3.3.0         	4.0                	Insecure & Outdated Wordpress Instance: Never e...
https://artifacthub.io/packages/helm/wordpressm...	1.0.0         	                   	This is the Helm Chart that creates the Wordpre...
https://artifacthub.io/packages/helm/bitpoke/wo...	0.11.0-rc.2   	0.11.0-rc.2        	Bitpoke WordPress Operator Helm Chart             
https://artifacthub.io/packages/helm/presslabs/...	0.11.0-alpha.3	0.11.0-alpha.3     	Presslabs WordPress Operator Helm Chart           
https://artifacthub.io/packages/helm/presslabs/...	0.11.1        	v0.11.1            	A Helm chart for deploying a WordPress site on ...
https://artifacthub.io/packages/helm/phntom/bin...	0.0.3         	0.0.3              	www.binaryvision.co.il static wordpress           
https://artifacthub.io/packages/helm/gh-shessel...	1.0.34        	5.8.0              	Web publishing platform for building blogs and ...
https://artifacthub.io/packages/helm/sonu-wordp...	1.0.0         	2                  	This is my custom chart to deploy wordpress and...
https://artifacthub.io/packages/helm/uvaise-wor...	0.2.0         	1.1.0              	Wordpress for Kubernetes                          
https://artifacthub.io/packages/helm/wordpress/...	0.2.0         	1.1.0              	Wordpress for Kubernetes                          
https://artifacthub.io/packages/helm/wordpress-...	1.0.0         	2                  	This is my custom chart to deploy wordpress and...
https://artifacthub.io/packages/helm/securecode...	3.3.0         	v3.8.19            	A Helm chart for the WordPress security scanner...
https://artifacthub.io/packages/helm/viveksahu2...	1.0.0         	2                  	This is my custom chart to deploy wordpress and...
https://artifacthub.io/packages/helm/presslabs/...	0.11.0-rc.2   	v0.11.0-rc.2       	Open-Source WordPress Infrastructure on Kubernetes
https://artifacthub.io/packages/helm/presslabs/...	0.11.1        	v0.11.1            	Open-Source WordPress Infrastructure on Kubernetes
https://artifacthub.io/packages/helm/six/wordress 	0.2.0         	1.1.0              	Wordpress for Kubernetes                          
https://artifacthub.io/packages/helm/wordpressm...	0.1.0         	1.1                	                                                  
https://artifacthub.io/packages/helm/presslabs/...	0.11.3        	0.11.3             	Presslabs WordPress Operator Helm Chart

차트 설치

- 각 차트마다 설정가능한 values 들이 있고, 이를 커스터마이징 하는 것이 아주 중요하다.

- 헬름 클라이언트는 헬름 저장소에서 다운로드한 차트와 values의 조합을 릴리스로 관리하고 쿠버네티스의 시크릿으로 데이터를 저장한다. 따라서 별도의 데이터베이스가 필요하지 않다.

# 설정 가능한 파라미터 표시
$ helm show values bitnami/wordpress

# README
$ helm show readme bitnami/wordpress

# 설치 명령어에 파라미터를 지정하여 설치
$ helm install sample-wordpress bitnami/wordpress --version 10.9.1 \
--set wordpressUsername=sample-user \
--set wordpressPassword=sample-pass \
--set wordpressBlogName="sample blog" \
--set persistence.size=5Gi

# values 파일을 생성하여 설치
$ helm install sample-wordpress bitnami/wordpress --version 10.9.1 \
--values values.yaml

# 테스트
$ helm test sample-wordpress

 

다음으로 개인적으로 아주 유용하다고 생각되는 템플릿 기능이다. 매니페스트의 특정 부분을 수정할 때 템플릿에서 해당하는 변수만을 수정하면 작업이 끝나기 때문에 sed 등을 사용한 매니페스트 수정 작업에 비해 실수를 방지하기가 더 유리하다. 즉, 자주 사용되는 템플릿을 기반으로 커스텀 차트를 생성하고, 파라미터 혹은 values 파일을 통해 유용한 매니페스트를 생성해낼 수 있다.

템플릿으로 매니페스트 생성

# 설치 명령어에 파라미터를 지정하여 매니페스트 생성
$ helm template sample-wordpress bitnami/wordpress --version 10.9.1 \
--set wordpressUsername=sample-user \
--set wordpressPassword=sample-pass \
--set wordpressBlogName="sample blog" \
--set persistence.size=5Gi

# values 파일을 생성하여 매니페스트 생성
$ helm template sample-wordpress bitnami/wordpress --version 10.9.1 \
--values values.yaml

 

차트

헬름에서는 차트라는 단위로 시스템을 패키징하고 차트 내용은 쿠버네티스 매니페스트 템플릿과 변수가 메인이다. 한번 생성한 차트는 쿠버네티스 버전에 맞추어 매니페스트 유지 보수를 별도로 해야한다.

# 커서틈 차트 양식 생성
$ helm create sample-charts
$ cd sample-charts
$ tree
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   ├── serviceaccount.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 10 files

$ cd ..
$ helm install sample-helm sample-charts
NAME: sample-helm
LAST DEPLOYED: Tue Oct 26 23:06:13 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=sample-charts,app.kubernetes.io/instance=sample-helm" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT
파일명 용도
templates/*.yaml 설치하는 매니페스트 템플릿
templates/tests/*.yaml 설치한 차트가 정상적으로 동작하는지 테스트하는 매니페스트 템플릿
values.yaml 사용자가 나중에 덮어 쓸 수 있는 기본값 정의
templates/NOTES.txt helm install 시 출력되는 메시지
requirements.yaml 의존하는 차트와 해당 버전 기록
templates/_helpers.tpl 릴리스 이름으로 변수를 정의하는 등의 헬퍼 변수 정의
Chart.yaml 차트 메타데이터, 의존하는 다른 차트 명시

 

기타 명령

# 설치 후 릴리스 확인
$ helm list
NAME       	NAMESPACE	REVISION	UPDATED                             	STATUS  	CHART              	APP VERSION
sample-helm	default  	1       	2021-10-26 23:06:13.158183 +0900 KST	deployed	sample-charts-0.1.0	1.16.0    

# 릴리스 삭제
$ helm uninstall sample-wordpress

 

 


출처

- [Helm 설치] : https://helm.sh/docs/intro/install/

- [책] 쿠버네티스 완벽 가이드 마사야 아오야마 지음, 박상욱 옮김