세로형
Recent Posts
Recent Comments
Link
04-18 14:10
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

꿈 많은 사람의 이야기

사용자 정의 kubernetes helm 생성 및 배포하기 - django helm으로 배포하기 본문

Data Engineering 및 Infra

사용자 정의 kubernetes helm 생성 및 배포하기 - django helm으로 배포하기

이수진의 블로그 2020. 4. 19. 15:16

포스팅 개요

이번 포스팅은 지난 포스팅 kubernetes의 helm 소개글 이후(https://lsjsj92.tistory.com/582), 단순히 stable repo에 있는 kubernetes를 helm으로 배포하는 것이 아니라, 사용자만의 kubernetes helm 환경을 생성하고 helm으로 배포하는 내용을 작성합니다.

그 중 정말 간단하게 살펴볼 수 있도록 Python Django app을 helm kubernetes로 배포하도록 해보겠습니다.

본 글은 아래 링크를 참조하였습니다.

또한, 본 포스팅에서 사용한 helm chart 내용은 아래 저의 github repo에 올려두었습니다.

 

 


포스팅 본문

kubernetes를 helm으로 만들어서 배포하려면 먼저, helm이 설치되어 있어야합니다. helm을 설치하는 방법은 포스팅 개요나, 제 블로그의 지난 글을 보시면 나와 있습니다. 

저는 저만의 helm 환경을 만들기 위해서 먼저 아래와 같은 디렉토리를 생성하여서 해당 디렉토리에서 작업하였습니다.

이 디렉토리 경로는 여러분들의 몫입니다. 아무대서나 하셔도 됩니다.

 

helm 환경 생성

이제 사용자 정의 helm을 생성해야 합니다. 그러기 위해서는 helm chart를 만들어주어야 하는데요. helm이 설치되어 있다면 helm 이라는 명령어가 사용가능합니다. 이 helm 명령어를 이용해서 사용자 정의 helm chart 환경을 만들어줍니다.

위 사진과 같이 helm create soojin-django 를 하게 되면 soojin-django 라는 이름으로 helm 환경이 만들어지게 됩니다. helm 환경은 helm chart를 담고 있으므로 create 한 이름의 디렉토리가 만들어지게 됩니다.

여기서 생성된 디렉토리에는 아래와 같은 파일과 디렉토리가 존재합니다.

helm으로 생성된 각 파일과 디렉토리의 역할은 아래와 같습니다.

  • chart.yaml : 해당 helm chart에 대한 정보가 담겨있습니다.
  • values.yaml : 해당 helm chart에서 사용하는 각종 값들에 대한 정의가 담겨있습니다.
  • chart 디렉토리 : 의존하는 chart에 대한 정보가 담겨있습니다.
  • templates : kubernetes르 정의하는 metifest file이 정의되어 있는 폴더입니다. 
  • README.md : 사용자가 읽을 수 있는 README 파일

등이 있으며, 자세한 내용은 개요에 helm chart 링크를 걸어놓았으니 참고바랍니다.

helm chart 파일 수정하기

저는 여기서 Chart.yaml 파일과 values.yaml 파일을 수정하도록 하겠습니다. chart.yaml 파일은 단순히 이 helm chart에 대한 설명을 담고 있는 환경입니다. 또한, values.yaml은 helm chart에서 사용하는 각종 값들에 대한 정의가 담겨있습니다. 

chart.yaml 파일 수정

먼저, chart.yaml 파일부터 수정합니다. 여기에는 helm에 대한 설명을 넣으므로 버전 정보, 이름 등을 수정해서 넣어줍니다. 저는 아래와 같이 수정하였습니다.

apiVersion: v1
name: soojin-django
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: latest

 

values.yaml 파일 수정

다음은 values.yaml 파일을 수정하였습니다. helm에서 사용되는 각종 값들에 대한 정의가 담겨있습니다. 예를 들어 replica를 몇 대를 할 것인지, 내가 사용하려는 image는 무엇인지, service의 clusterip는 어떻게 설정할 것인지 등에 대한 정보가 담겨있습니다. 저는 아래와 같이 내용을 수정하였습니다.

replicaCount: 1

image:
  repository: lsjsj92/kubernetes-django-soojin
  tag: latest
  pullPolicy: IfNotPresent

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: ClusterIP
  port: 8000

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths: []
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}

제가 사용하려는 docker image는 일전에 올려두었던 lsjsj92/kubernetes-django-soojin docker image입니다. 이러한 정보를 values.yaml 파일에 넣어주면 됩니다.

 

이와 같이 helm을 구성하기 위해서는 각종 셋팅을 해주어야 하는데요. 이것을 정확하게 어떻게 할 수 있는지는 공식 문서를, 예시를 보고 싶으면 helm repo github을 보시면 됩니다. 

위 주소는 모드 개요에 참고 링크로 걸어두었으니 보시면 되겠습니다.

 

helm chart 적용해서 생성하기

이제 이렇게 수정한 helm chart를 helm 환경에 적용해 kubernetes 환경을 구축하겠습니다. helm 환경에 적용하는 방법은 간단합니다. helm install 명령어를 입력하면 됩니다.

helm install soojin ./soojin-django/ 를 입력하게 되면 제가 방금 입력한 soojin-django 환경을 기반으로 helm 기반의 kubernetes를 생성해줍니다.

 

적용이 되었는지는 아래와 같이

helm ls 명령어와 kubectl get pods --all-namespaces 명령어로 확인할 수 있습니다.

실제로 위 처럼 명령어를 입력하였더니 soojin 이라는 이름으로 helm이 만들어졌으며, namespace는 default로 kubernetes pod가 만들어진 것을 확인할 수 있습니다. 

 

그럼 django app을 실행하였으니, 웹 환경에 들어가봐야겠죠? 웹 페이지에 들어가기 위해서 먼저 ip 정보 등을 확인해줍니다.

kubectl get svc를 입력하니까 soojin-soojin-django 이름의 환경이 8000번 포트에 떠있는 것을 확인할 수 있습니다. 그럼 이제 local 환경에서 port forward를 통해 해당 helm kubernetes django 환경에 접속합니다.

kubectl port-forward svc/soojin-soojin-django 8080:8000 명령어를 입력하게 되면 local 8080포트가 hem 환경으로 만들어진 kubernetes 8000번 포트와 포워딩 되도록 합니다. 실제로 아래와 같이 127.0.0.1:8080을 입력하게 되면

 

 

django app 화면을 볼 수 있습니다!

 

helm 환경 지우기

이제 제가 만든 helm 환경을 kubernetes에서 지우도록 하겠습니다. 아래와 같은 명령어를 순차적으로 입력해줍니다.

helm을 이용하면 kubernetes를 쉽게 배포하고 삭제할 수 있는데요. 아까 helm install로 설치했다면 이제 helm uninstall 명령어로 해당 환경을 지울 수 있습니다. 그러면 kubectl get pods --all-namespaces 등의 명령어를 보시면 Terminating 이라는 것을 볼 수 있게되고 시간이 조금 지나면 완전히 해당 pod는 사라진 것을 확인할 수 있을겁니다.

 


마무리

이번 포스팅에서는 사용자 지정 helm 환경을 생성하고 이것을 kubernetes에 적용하는 것을 확인해보았습니다.

이런 것들을 응용하면 얼마든지 kubernetes을 쉽게 관리할 수 있을겁니다.

부디 도움되기실 바랍니다. 감사합니다.

반응형
그리드형
Comments