# 紀錄一下Helm v3怎麼用
###### tags: `kubernetes`

## 由來
原本公司是使用ECS來部署服務, 最近為了跨平台, 選擇了kubernetes, 作為基礎架構
好處是不用被AWS綁定, 可在地端與各個雲平台運行服務, 而公司內的服務很多, 大多數服務已容器化
為了做版本控制, 與快速部署服務至Kubernetes上, 所以使用的helm來管理各種服務
因內部服務多為無狀態的web sevice, 但是又有很多專案, 所以根據需求做了一個template的template
新專案導入helm時可以直接clone下來使用, 其實跟helm create差不多意思
## 簡述
helm是kubernetes的套件管理員, 運行在kubernetes上的服務通常透過多項資源組成, 如deplyment, service, configmap等等, helm提供一個方法樣板化底下的資源, 透過values控制此服務的設定, 讓其他使用者快速地使用服務, 不用去設定各項細節, 即其中的相依性
### 優點
- 方便使用者快速的安裝軟體
- 可讓其他chart再利用
- Predefine一些客製化的設定
- 針對部署做版本控制, 可方便的rollback, upgrade
### 缺點
- 針對一些沒透出的設定, 需自行下載chart後, 在template中更改
- 專案更新時, 可能發生錯誤(本身沒遇過)
- helm install/upgrade 要分開使用, 在pipeline中, 需重覆寫多餘部分
## 常用指令
### helm repo
```
## 加入repo 設定為 stable
$ helm repo add stable https://charts.helm.sh/stable
## 看本地有啥repo
$ helm repo list
```
### helm install
```
## 安裝軟體在cluster上
$ helm install stable/mysql --generate-name
## 看下剛剛裝的東西
$ helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mysql-1610447545 default 1 2021-01-12 18:32:27.956112 +0800 CST deployed mysql-1.6.9 5.7.30
## 解安裝
$ helm uninstall mysql-1610447545
```
#### 注意事項
- 需指定namespace, 如果都沒看到可使用--all-namespaces, 查看所有namespace
### helm pull
```
## 下載chart包到本地
$ helm pull stable/mysql
## tar包, 解壓後就是chart
$ ll
-rw-r--r-- 1 sj.huang GOGORO\Domain Users 11589 Jan 26 11:51 mysql-1.6.9.tgz
```
### helm search
```
## 看看有多少公開的REPO
$ helm search hub
## 看看這個repo 有啥軟體
$ helm search repo stable
```
### helm show
```
## 看看這個軟體有啥可以給我客製化的, 我通常會倒成檔案, install時直接使用
$ helm show values stable/mariadb > config.yaml
## 安裝時使用客製化參數
$ helm install -f config.yaml stable/mariadb
```
### helm create
```
## 客製化自己的chart, create會幫你開一個固定的樣板, 你再去修改
$ helm create deis-workflow
## 確定自己做的chart沒錯
$ helm lint
```
### helm pull
下載chart 到本地
```
$ helm pull ingress-nginx/ingress-nginx
$ ll
-rw-r--r-- 1 sj.huang GOGORO\Domain Users 23322 Jan 13 15:54 ingress-nginx-3.20.0.tgz
```
## chart 製作方法
```
## 透過指令做出一個目錄結構
$ helm create myfirstchart
Creating myfirstchart
$ ll
total 0
drwxr-xr-x 7 sj.huang GOGORO\Domain Users 224 Jan 26 11:53 myfirstchart
$ tree myfirstchart
myfirstchart ## 專案名
├── Chart.yaml ## 安裝名字, 版本, 維護者, 專案連結等等訊息
├── charts ## 會使用到的其他chart, 同常是專案的相依或是組成元件之一
├── templates ## 模板都寫在這, 會依照values.yaml再套用到此目錄的資源內
│ ├── NOTES.txt ## ???幫解答???
│ ├── _helpers.tpl ## ???幫解答???
│ ├── deployment.yaml ## 以下都是各種資源的樣板
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── service.yaml
│ ├── serviceaccount.yaml
│ └── tests ## 服務部署完成後的測試, web服務同常會在這邊跑一個pod去curl服務, 確定服務有正常運行
│ └── test-connection.yaml
└── values.yaml ## 設定變數的位置, 開發者會透出一些變數讓使用者設定
3 directories, 10 files
```
template使用的是go tempalte的語法, 下列針對一些常用的語法說明
### Go template
#### if
`deployment.yaml`
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "myfirstchart.fullname" . }}
labels:
{{- include "myfirstchart.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "myfirstchart.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "myfirstchart.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "myfirstchart.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 }}
```
#### Ref
- https://golang.org/pkg/text/template/
- https://helm.sh/docs/chart_template_guide/functions_and_pipelines/
-