# helmchart 教學
helm chart 是一個能夠簡化k8s的一個套件,能透過 go 語言轉化k8s yaml檔案的變數。
以下是範例:
建立一個新的資料夾後在那個資料夾底下用 terminal 輸入
```bash
helm create 你要取的名字
```
你就會看到這個指令長出的東西如下圖:

templates底下的 yaml file 主要就是裝著你原本寫好的 k8s yaml file,包括常使用的 deployment, ingress, service...etc,所以你其實可以把他幫你建好的template 底下的 deployment.yaml, service.yaml... etc砍掉,放進你原本就已經用 k8s 寫好的 deployment, service.yaml 檔案
## helmchart 的環境變數: values.yaml
values.yaml 是放在跟template 資料夾同一層,values.yaml 檔如下,這邊的值都可以帶入到 templates 底下的 yaml 檔案,只要你在你本來的yaml 檔(例如 deployment.yaml 檔案)原本是輸入值的地方帶入 {{.Values.xxxx}}
```yaml
# Default values for test.
# 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
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
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {
```
ex:假設你今天要帶入上方 image 底下的 repository nginx 的值,就輸入 {{.Values.image.repository}},該地方就會帶入 nginx 的字串
下方為示範service.yaml 用helm 寫出來的範例
```yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: {{.Values.labels}}
name: {{.Values.metadata.name}}
namespace: {{.Values.namespace}}
spec:
ports:
- name: https
port: 3000
targetPort: 3000
protocol: TCP
selector:
app: {{.Values.labels}}
type: {{.Values.service.type}}
```
## if else in helmchart
```yaml
devlatest: dev
prdlatest: prd
env:
enviornment: dev
```
如果今天你希望在 xxxx.yaml 透過不同條件輸入不同值,你可以用 go 語言的 if else 來判斷,假設今天values.yaml 檔有上方 devlatest, sitlatest, prdlatest 等值,然後你希望 env 底下的 enviornment 在dev 狀態時 imageTag 可以用devlatest,那你就可以在deploymeny.yaml 本來輸入image 的地方帶入下方
```yaml
# {{- .... -}} 橫線的意思:縮行 ex: {{10 -}} < {{- 12}} === 10<12
# {{-23 }} < {{- 45}} === 23 <45
# 這是 if 的用法 eq: ===, gt: >, ge: >=, lt: <, le: <=, ne: !=
{{ if eq .Values.enviornment "dev"}}
image: "{{ .Values.image.repository }}:{{ .Values.devlatest | default .Chart.AppVersion }}"
{{- end }}
# else 的用法
{{ else }}
image: "{{ .Values.image.repository }}:{{ .Values.prdlatest | default .Chart.AppVersion }}"
{{- end }}
```
## with in helmchart
```yaml
# with 的用法: 如果 value 存在,就往裡面做
{{ with .Values.XXXX }}
條件成立做的事情
{{ end }}
```
## helm install/uninstall/upgrade
當你處理好所有template資料夾底下的yaml 檔案,
你就可以在Chart.yaml這個位置底下下指令
```yaml
helm install --namespace=你的namespace --name-template 你要取的服務名字 .
```
下此指令就會直接將你template資料夾底下的yaml 檔案全部起起來,然後你再輸入下方指令
```yaml
helm list --namespace=dde
```
你就可以看到你所起起來的服務。
如果今天你要卸載的話,你可以輸入下方
```yaml
helm uninstall 你服務名字 --namespace=你的namespace
```
就可以成功卸載。
如果今天你希望能夠繼續使用本來的服務但你只是想要更換values.yaml 裡面的環境變數
假設你要換 values.yaml 檔案底下的 image 字串
```yaml
helm upgrade 你的服務名稱 . --install --set image=xxxx --namespace=你的namespace
```
## 教學影片連結
https://web.microsoftstream.com/video/0b178463-7e9d-4c92-8a81-a25f2024dcbb