Back-end/docker, kubernetes

Helm과 설치 가이드

이안_ian 2023. 8. 28. 17:05
반응형

Helm이란?

helm은 deployment, service, ingress 등 yaml 파일들을 하나의 package로 관리하기 위한 도구이다.

기본적으로 배포한 애플리케이션을 삭제하기 위해선 deployment.yaml, service.yaml, ingress.yaml를

각각 삭제 해줘야 하지만 helm을 사용하면 설치와 삭제가 모두 한 번에 가능하다.

주요 개념 3가지

  • Chart: helm package에 해당하는 부분으로, 이 패키지에는 쿠버네티스 클러스터 내에서 애플리케이션, 툴, 서비스 등 구동에 필요한 모든 리소스가 정의되어 있다. 즉 설치 스크립트라고 볼 수 있다.
  • Repository: Chart를 모아두고 공유하는 공간이다.
  • Release: 쿠버네티스 클러스터에서 구동되는 Chart의 인스턴스이다. 일반적으로 하나의 Chart는 동일한 Cluster 내에 여러 번 설치될 수 있다. 이때 설치될 때마다 새로운 Release가 생성된다.

Template과 Value

helm은 기본적으로 템플릿 개념을 사용한다. 템플릿으로 파일을 만들어 놓고 밸류 값을 채워 넣어서

쿠버네티스 리소스를 정의한 yaml 파일을 생성한다.

아래와 같은 형태의 파일이 제공된다.

template.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ .Values.name }}-deployment
spec:
  replicas: {{ .Values.replicaCount }}
  minReadySeconds: 5
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      name: {{ .Values.name }}-pod
      labels:
        app: {{ .Values.name }}
    spec:
      containers:
      - name: {{ .Values.name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "test-chart.fullname" . }}
  labels:
    {{- include "test-chart.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "test-chart.selectorLabels" . | nindent 4 }}

이렇게 만들어진 template에 values.yaml의 데이터를 읽어 들여서 매핑하여 만들어준다.

특히나 지속적으로 반복되는 name의 경우 이러한 방법을 썼을 때 굉장히 간편해진다.

 

values.yaml

name: testapp
image:
  repository: nginx
  
service:
  type: ClusterIP
  port: 80

이런식으로 해당 값만 작성한 yaml 파일을 생성하면된다.

 

이러한 기본적인 기초 지식으로 이제 실습을 진행해보자.

Helm 설치

리눅스 환경

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

윈도우 환경

https://github.com/helm/helm/releases

 

Releases · helm/helm

The Kubernetes Package Manager. Contribute to helm/helm development by creating an account on GitHub.

github.com

위 url에 접속하여 windows 용으로 다운받아 압축 해제한다.

그리고 아래와 같이 환경변수를 만들어준다.

시스템 변수 > path에도 변수 값을 추가해준다.

그리고 cmd에서 helm version으로 설치된 것을 확인하면 된다.

 

Helm Chart Repository 초기화, 업데이트

helm repo add stable https://charts.helm.sh/stable

helm repo update

사용 명령어

//helm chart 목록 조회
helm search repo stable

//helm chart 검색
helm search repo <word>

//Repo 추가
helm repo add [레포지토리 이름] [레포지토리 주소]

//local에 등록된 repo조회
helm repo list

//등록했던 repo 삭제
helm repo remove <name>

##chart를 이용한 package 설치 및 삭제##
//[RELEASE-NAME]은 사용자가 정의하는 이름, [CHART-NAME]은 repo에서 확인한 chart이름
helm install [RELEASE-NAME] [CHART-NAME] [flags]

//[NAME]을 따로 지정하지 않고 자동생성
helm install [CHART-NAME] --generate-name

//패키지 삭제
helm uninstall [RELEASE-NAME]

//차트수정
helm upgrade [RELEASE-NAME] [Chart 생성 경로]

Chart 직접 만들기

helm create [RELEASE-NAME] [Chart 생성 경로]
ex) helm create test-chart ./test-chart/

현재 위치에 지정한 이름으로 폴더가 생기며 필요한 파일들이 하위에 생성된다.

거기서 values.yaml 파일을 아래와 같이 수정하고 배포 가능하다.

name: testapp
image:
  repository: nginx

service:
  type: ClusterIP
  port: 80

 

이제 미니쿠베를 실행시킨 다음 아래 명령어로 실행 및 확인을 해 볼 수 있다.

helm install test-chart --generate-name

//적용 확인하기
helm ls
kubectl get all

삭제는 위 명령어에서 확인!

Chart 검사하기

values.yaml에 정의한 대로 나올 결과물을 미리 알아보자.

 

문법적으로 이상이 없는지 확인

helm lint <Chart.yaml 경로>

 

templates을 기반으로 변수들 참조하여 리소스 배포시 결과 보기

helm template <Chart.yaml 경로>

 

반응형