Kubernetes

[Cloud] 12. using Kubernetes Helm

트리스탄1234 2023. 2. 12. 15:13
728x90
반응형

Helm Ovierview

In Kubernetes, there is a Package Managing Tool called Helm. This tool is similar to npm of node.js, it is a tool that makes it possible to distribute packages of Kuberentes. The structure of this Helm is as follows.

Helm uses a Package Format called Chart, which is a set of files that define Kubernetes Resources. You can think of it as yaml + templates = helm.

반응형

In Helm, packed archives are called helm charts. The helm chart is composed of templates and value.yaml files. The combination of these two creates a single package and is deployed on the Kubernetes cluster.

Here's what you can do with Helm.

  • A new chart can be created Charts can be used to create chart archive (tgz) files.
  • You can interact (import, etc.) with Repositories where charts are stored.
  • Charts can be installed/uninstalled on Kubernetes Cluster.
  • You can manage the Release Cycle for installed charts through Helm.

Now let's look at the three concepts that make up Helm.

  • Chart: Helm's package and includes resources that run applications and services in Kubernetes Cluster.
  • Repository : A repository where Charts (Kubernetes Packages) are collected and shared.
  • Release: This is an instance of the chart, and one chart can be installed multiple times in the same cluster, and a new release is created each time it is installed.

Inside structure of Chart

Charts are made up of files in directories. The directory name will be the name of the chart. For example, a chart called WordPress is saved in the wordpress/ directory, and its structure is as follows.

wordpress/
Chart.yaml # A YAML file containing information about the chart
LICENSE # OPTIONAL: A plain text file containing the license for the chart
README.md # OPTIONAL: A human-readable README file
values.yaml # The default configuration values for this chart
values.schema.json # OPTIONAL: A JSON Schema for imposing a structure on the values.yaml file
charts/ # A directory containing any charts upon which this chart depends.
crds/ # Custom Resource Definitions
templates/ # A directory of templates that, when combined with values,
# will generate valid Kubernetes manifest files.
templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes

Helm uses Chart/ directory, templates/ directory, and specified file name. Then let's look at the chart.yaml file. The chart.yamll file is an essential file for chart. Here's a look at its contents:

contents of Chart.yaml file

apiVersion: The chart API version (required)
name: The name of the chart (required)
version: A SemVer 2 version (required)
kubeVersion: A SemVer range of compatible Kubernetes versions (optional)
description: A single-sentence description of this project (optional)
type: The type of the chart (optional)
keywords:
- A list of keywords about this project (optional)
home: The URL of this projects home page (optional)
sources:
- A list of URLs to source code for this project (optional)
dependencies: # A list of the chart requirements (optional)
- name: The name of the chart (nginx)
version: The version of the chart ("1.2.3")
repository: (optional) The repository URL ("https://example.com/charts") or alias ("@repo-name")
condition: (optional) A yaml path that resolves to a boolean, used for enabling/disabling charts
tags: # (optional)
- Tags can be used to group charts for enabling/disabling together
import-values: # (optional)
- ImportValues holds the mapping of source values to parent key to be imported. Each item can be a string or
pair of child/parent sublist items.
alias: (optional) Alias to be used for the chart. Useful when you have to add the same chart multiple times
maintainers: # (optional)
- name: The maintainers name (required for each maintainer)
email: The maintainers email (optional for each maintainer)
url: A URL for the maintainer (optional for each maintainer)
icon: A URL to an SVG or PNG image to be used as an icon (optional).
appVersion: The version of the app that this contains (optional). Needn't be SemVer. Quotes recommended.
deprecated: Whether this chart is deprecated (optional, boolean)
annotations:
example: A list of annotations keyed by name (optional).

Templates file

Templates of Helm Chart are written in Go template language and provide various template functions. These template files are saved in the template/ directory of the chart. When Helm renders the chart, all files in the directory are transferred to the template engine.

The values ​​used by the template can be delivered in two ways. Here's how.

  • If the chart developer uses the values.yaml file, it is included in the chart, and this file contains the default values.
  • If a chart user uses a separate yaml file, use it through the helm install command.
  • If the user uses a custom value, this value overrides the value of values.yaml in the chart. So let's look at an example of a templates file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "sample.fullname" . }}
labels:
{{- include "sample.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "sample.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "sample.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "sample.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}

review value.yaml file

Now let's take a look at the value.yaml file. Below is an example of a value.yaml file.

# Default values for sample.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: nginx
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: ""
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: ""
podAnnotations: {}
podSecurityContext: {}
# fsGroup: 2000
securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000
service:
type: ClusterIP
port: 80
ingress:
enabled: false
className: ""
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
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
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}

If you want to use a value other than the value provided by the default value.yaml, you can create a separate yaml file and use it when installing the chart.

Values ​​defined in values.yaml can be accessed through the .Values ​​object in the template. In the above example, the service type can be accessed as .Values.service.type. The name of the values.yaml file included in the chart cannot be changed, and any separate yaml file name that can be specified with the helm command can be created.

Install Helm

To install Helm, install Helm via below command on Master Node and check the installed version.

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11156 100 11156 0 0 38075 0 --:--:-- --:--:-- --:--:-- 38075
root@master-VirtualBox:~# chmod 700 get_helm.sh
root@master-VirtualBox:~# ./get_helm.sh
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
root@master-VirtualBox:~# helm version
version.BuildInfo{Version:"v3.9.2", GitCommit:"1addefbfe665c350f4daf868a9adc5600cc064fd", GitTreeState:"clean", GoVersion:"go1.17.12"}

Helm commands

First, add the repository using the command below.

root@master-VirtualBox:~# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories

The second command, Helm search command, is a command to search charts, and two source types can be searched.

  • The helm search hub searches in the Artifact Hub, which includes helm charts in multiple repositories.
  • The helm search repo is searched in repositories added to the local helm client using helm repo add.

Let's search the chart in wordpress.

root@master-VirtualBox:~# 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-ak... 15.0.14 6.0.1 WordPress is the world's most popular blogging ...
https://artifacthub.io/packages/helm/bitnami/wo... 15.0.14 6.0.1 WordPress is the world's most popular blogging ...
https://artifacthub.io/packages/helm/groundhog2... 0.6.2 6.0.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/camptocamp... 0.6.10 4.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

Now let's install one of the charts we searched for. How to use is as follows. helm install 'release name' 'chart name' Then, let's install one of the searched charts.

root@master-VirtualBox:~# helm install my-wordpress bitnami/wordpress
NAME: my-wordpress
LAST DEPLOYED: Tue Aug 9 13:55:08 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: wordpress
CHART VERSION: 15.0.14
APP VERSION: 6.0.1

If you install the chart through helm, the deployment proceeds immediately. Let's check the created resources through the command below.

root@master-VirtualBox:~# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-wordpress-64d77cb56f-nmvpw 0/1 Pending 0 2m47s
pod/my-wordpress-mariadb-0 0/1 Pending 0 2m47s
pod/wordpress-6cffd787fd-wws7s 0/1 CrashLoopBackOff 38 (4m11s ago) 20h
pod/wordpress-mysql-5b9c9bc7bb-2ps92 0/1 Pending 0 20h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d20h
service/my-wordpress LoadBalancer 10.110.229.24 192.168.72.103 80:31992/TCP,443:30739/TCP 2m47s
service/my-wordpress-mariadb ClusterIP 10.105.114.217 <none> 3306/TCP 2m47s
service/wordpress NodePort 10.111.106.227 <none> 8080:30019/TCP 20h
service/wordpress-mysql ClusterIP None <none> 3306/TCP 20h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-wordpress 0/1 1 0 2m47s
deployment.apps/wordpress 0/1 1 0 20h
deployment.apps/wordpress-mysql 0/1 1 0 20h
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-wordpress-64d77cb56f 1 1 0 2m47s
replicaset.apps/wordpress-6cffd787fd 1 1 0 20h
replicaset.apps/wordpress-mysql-5b9c9bc7bb 1 1 0 20h
NAME READY AGE
statefulset.apps/my-wordpress-mariadb 0/1 2m47s
root@master-VirtualBox:~#

To delete a package installed through helm, use the command below to delete it.

root@master-VirtualBox:~/wordpress# helm uninstall my-wordpress
release "my-wordpress" uninstalled

When upgrading, you can update using the helm upgrade command. If you want to create your own chart, run the create command as shown below, edit the necessary files, and then proceed with the installation using helm install.

oot@master-VirtualBox:~/wordpress# helm create mychart
root@master-VirtualBox:~/wordpress# tree mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml

To check whether the created chart is written properly or not, you can use the helm lint command to check it.

root@master-VirtualBox:~/wordpress# helm lint mychart/
==> Linting mychart/
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed

The helm template creates a deployment manifest file by referring to the template files (deployment.yaml, service.yaml, ingress.yaml, etc.), values.yaml and templates/_helpers.tpl in the templates directory to the template engine. To check this value, you can use the command below.

oot@master-VirtualBox:~/wordpress# helm template mychart/
---
# Source: mychart/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: release-name-mychart
labels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
app.kubernetes.io/version: "1.16.0"
app.kubernetes.io/managed-by: Helm
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: release-name-mychart
labels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
app.kubernetes.io/version: "1.16.0"
app.kubernetes.io/managed-by: Helm
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: release-name-mychart
labels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
app.kubernetes.io/version: "1.16.0"
app.kubernetes.io/managed-by: Helm
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
template:
metadata:
labels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
spec:
serviceAccountName: release-name-mychart
securityContext:
{}
containers:
- name: mychart
securityContext:
{}
image: "nginx:1.16.0"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{}
---
# Source: mychart/templates/tests/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
name: "release-name-mychart-test-connection"
labels:
app.kubernetes.io/name: mychart
app.kubernetes.io/instance: release-name
app.kubernetes.io/version: "1.16.0"
app.kubernetes.io/managed-by: Helm
annotations:
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['release-name-mychart:80']
restartPolicy: Never
root@master-VirtualBox:~/wordpress#

The helm package command archives (compresses) the chart directory. The archived file name is generated as 'name_value'-'version_value'.tgz of the Chart.yaml file.

root@master-VirtualBox:~/wordpress# helm package ./mychart
Successfully packaged chart and saved it to: /root/wordpress/mychart-0.1.0.tgz

edit Chart

There are two ways to change the chart value.

  • change value.
  • edit chart

First, let's check the default values. The query format is as follows

helm show chart 'chart name'

root@master-VirtualBox:~# helm show chart bitnami/wordpress
annotations:
category: CMS
apiVersion: v2
appVersion: 6.0.1
dependencies:
- condition: memcached.enabled
name: memcached
version: 6.x.x
- condition: mariadb.enabled
name: mariadb
version: 11.x.x
- name: common
tags:
- bitnami-common
version: 1.x.x
description: WordPress is the world's most popular blogging and content management
platform. Powerful yet simple, everyone from students to global corporations use
it to build beautiful, functional websites.
keywords:
- application
- blog
- cms
- http
- php
- web
- wordpress
maintainers:
- name: Bitnami
name: wordpress
sources:
version: 15.0.14
  • Now let's see how to change the value. First of all, there are two ways to change the value as follows. How to use the set option

helm template --name myrelease --set replicaCount=10 ./helloworld

  • How to use the --value or -f option This is a method of passing the parameter to be changed to a separate yaml file and passing it during installation. For example, create a myval.yam file, enter the following content, and then run the following command
root@master-VirtualBox:~# vi myval.yaml
name: "fromValuefile"
root@master-VirtualBox:~#helm install -f myval.yaml --name newrelease --dry-run --debug ./helloworld

Second, let's see how to edit the chart. To modify and install the chart, you can download the chart from the repository, modify it, and install helm with the modified chart.

So let's first download the chart.

root@master-VirtualBox:~# helm pull --untar bitnami/wordpress
root@master-VirtualBox:~# cd wordpress/
root@master-VirtualBox:~/wordpress# ls -al
total 152
drwxr-xr-x 4 root root 4096 8월 9 14:46 .
drwx------ 11 root root 4096 8월 9 14:46 ..
-rw-r--r-- 1 root root 387 8월 9 14:46 Chart.lock
drwxr-xr-x 5 root root 4096 8월 9 14:46 charts
-rw-r--r-- 1 root root 1048 8월 9 14:46 Chart.yaml
-rw-r--r-- 1 root root 333 8월 9 14:46 .helmignore
-rw-r--r-- 1 root root 65916 8월 9 14:46 README.md
drwxr-xr-x 2 root root 4096 8월 9 14:46 templates
-rw-r--r-- 1 root root 5706 8월 9 14:46 values.schema.json
-rw-r--r-- 1 root root 46579 8월 9 14:46 values.yaml

To change the value, edit the value.yaml file, and to change the templates, edit it in the templates directory. After modifying the necessary files, you can install it through the helm install command.

728x90
반응형