[k8s] Helm으로 Kubernetes 관리하기: 설치부터 차트 배포까지

림예·2024년 7월 21일

k8s

목록 보기
5/5
post-thumbnail

1. Helm 구성 및 사용

1.1 Helm 다운로드 및 설치

1.1.1 윈도우 설치

  • Chocolatey 설치 : PowerShell 을 열어서 아래 명령을 수행 합니다. 이미 google cloud sdk 를 설치 하면서 설치 되었을수 있습니다.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
  • Helm 설치
choco install kubernetes-helm

1.1.2 Mac / LINUX 설치

  • 수동 설치 방법
# helm 다운로드
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3

# 실행권한 변경
chmod 700 get_helm.sh

# helm 설치
./get_helm.sh

# 버전 확인
helm version

# Helm Repository 추가
helm repo add stable https://charts.helm.sh/stable

# Repository 업데이트 
helm repo update
  • Brew 설치
brew install helm

1.2 Mysql Helm 차트 다운로드 및 설치

1.3.1 mysql helm 검색

helm search repo stable/mysql

NAME                CHART VERSION    APP VERSION    DESCRIPTION
stable/mysql        1.6.3            5.7.28         Fast, reliable, scalable, and easy to use open-...
stable/mysqldump    2.6.0            2.4.1          A Helm chart to help backup MySQL databases usi...

1.3.2 피키지 메타 정보 보기

helm show chart stable/mysql

apiVersion: v1
appVersion: 5.7.28
description: Fast, reliable, scalable, and easy to use open-source relational database
  system.
home: https://www.mysql.com/
icon: https://www.mysql.com/common/logos/logo-mysql-170x115.png
keywords:
- mysql
- database
- sql
maintainers:
- email: o.with@sportradar.com
  name: olemarkus
- email: viglesias@google.com
  name: viglesiasce
name: mysql
sources:
- https://github.com/kubernetes/charts
- https://github.com/docker-library/mysql
version: 1.6.3

1.3.3 mysql helm 차트 설치 및 Deployment

helm install mysql stable/mysql 

AME: mysql-1588321002
LAST DEPLOYED: Fri May  1 08:16:55 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1588321002.default.svc.cluster.local

To get your root password run:

    MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)

i99OpY3CRp
To connect to your database:

1. Run an Ubuntu pod that you can use as a client:

    kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il

2. Install the mysql client:

    $ apt-get update && apt-get install mysql-client -y

3. Connect using the mysql cli, then provide your password:
    $ mysql -h mysql -p

To connect to your database directly from outside the K8s cluster:
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306

    # Execute the following command to route the connection:
    kubectl port-forward svc/mysql-1588321002 3306

    mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
helm ls

NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS         C
HART            APP VERSION
mysql-1588321701        default         1               2020-05-01 17:28:25.322363879 +0900 +09 deployed       m
ysql-1.6.3      5.7.28

1.3.4 helm 차트 uninstall

heml list

NAME                    NAMESPACE       REVISION        UPDATED                                 STATUS         C
HART            APP VERSION
mysql-1588321701        default         1               2020-05-01 17:28:25.322363879 +0900 +09 deployed       m
ysql-1.6.3      5.7.28


helm uninstall mysql-1588321701
release "mysql-1588321701" uninstalled

1.3 Helm 차트 만들기

1.3.1 Helm 차트 생성

helm create nginxstd

1.3.2 Template 파일 수정

  • Charts.yaml 파일 수정
apiVersion: v2
name: nginx-std
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
  • Template/deployment.yaml 파일 생성
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.container.name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.container.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.container.name }}
        environment: {{ .Values.environment }}
    spec:
      containers:
        - name: {{ .Values.container.name }}
          image: {{ .Values.container.image }}:{{ .Values.container.tag }}
          ports:
            - containerPort: {{ .Values.container.port }}
          env:
            - name: environment
              value: {{ .Values.environment }}
  • template/service.yaml 파일 생성
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.container.name }}-service
  labels:
    app: {{ .Values.container.name }}
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: {{ .Values.container.port }}
  selector:
    app: {{ .Values.container.name }}
  type: LoadBalancer
  • values.yaml 파일 생성
environment: development
container:
  name: nginx
  port: 80
  image: nginx
  tag: latest
replicas: 2

1.3.3 테스트 하기

  • K8s 오브젝트 생성
helm install nginxstd ./nginxstd
  • 삭제
# 확인
kubectl get all
helm list

# 삭제 
helm uninstall nginxstd

1.4 패키지 및 리포지토리 생성

1.4.1 패키지 생성

helm package ./nginxstd

mkdir prod

mv ./nginxstd-0.1.0.tgz ./prod/

1.4.2 helm 리포지토리 파일 생성

# 리포지토리 파일 생성 (index.yaml)
helm repo index ./prod

# 파일 생성 확인
cat ./prod/index.yaml

1.5 Helm 패키지 및 Repository 구성하기

1.5.1 Github.com Repository 생성

  • repository 생성

image-20210807010226171

  • github page 설정

image-20210807010420974

1.5.2 Git repository 생성 및 동기화

cd prod

git init

git add .

git branch -m main

git commit -am "initial commit"

git remote add origin https://github.com/dangtong76/helm-prod.git

git push origin main

1.5.3 Helm 리포지토리 구성 및 추가

  • Git page 로 서비스 되는 Git 리포지토리를 Helm 리포지토리에 추가
helm repo add helm-prod https://dangtong76.github.io/helm-prod
  • 추가확인
helm repo list

helm search repo nginx

1.5.4 Helm 리포지토리에 redis 추가

  • redis 안정버전 차트를 로컬 prod 디렉토리에 다운로드
helm search repo redis

helm fetch stable/redis -d ./prod
  • index.yaml 갱싱
helm repo index ./prod
  • git 업데이트
git status

git add .

git commit -a -m "add redis"

git push origin main
  • helm update 수행
helm repo update

helm search repo redis

업데이트 없이 "helm search repo redis" 를 검색하면 검색이 되지 않습니다.

1.6 Helm 차트 업그레이드

1.6.1 Repository 를 통한 Helm 인스톨

helm list

helm install nginxstd helm-prod/nginx-std
# 또는 
helm install helm-prod/nginx-std --generate-name

#확인
helm status nginxstd
kubectl get all 

1.6.2 helm 메니페스트를 통한 차트 변경 및 업데이트

  • stage-values.yaml 파일 생성
environment: development
replicas: 4
  • helm upgrade 로 차트 변경 적용
helm upgrade -f ./nginxstd/stage-values.yaml nginxstd helm-prod/nginx-std
  • helm history 로 확인
helm history
  • RollBack 수행
helm rollback nginxstd 1
  • Rollback 확인
helm history nginxstd

helm helm status nginxstd

kubectl get po 

1.6.4 Helm CLI 옵션을 통한 업그레이드

  • 현재 차트의 value 를 화인
helm show values helm-prod/nginx-std

environment: development
container:
  name: nginx
  port: 80
  image: nginx:1.7.9
  tag: hello
replicas: 2
  • CLI 옵션을 통한 업그레이드
helm upgrade --set replicas=4 --set environment=dev nginxstd helm-prod/nginx-std
  • 확인
helm history

helm status nginxstd

kubectl get po

1.7 삭제

helm uninstall nginxstd




profile
Think big 🌏

0개의 댓글