Helm架構
node
圖片來自IBM Developer Blog。linux
部署Helm
本文只展現二進制方式安裝,其餘方式可查看官方文檔。nginx
下載安裝包git
[root@K8S-PROD-M1 workspace]# wget https://get.helm.sh/helm-v3.4.1-linux-amd64.tar.gz
解壓安裝包
[root@K8S-PROD-M1 workspace]# tar -zxvf helm-v3.4.1-linux-amd64.tar.gz
[root@K8S-PROD-M1 workspace]# mv linux-amd64/helm /usr/local/bin/helm
與K8S 交互
採用與執行kubectl命令時的與K8S集羣進行交互的配置文件:/root/.kube/config。github
Chart倉庫操做
經常使用倉庫:docker
gitlab:https://charts.gitlab.ioapi
harbor:https://helm.goharbor.io架構
elastic:https://helm.elastic.comide
bitnami:https://charts.bitnami.com/bitnamiwordpress
倉庫操做:
[root@K8S-PROD-M1 ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@K8S-PROD-M1 ~]# helm repo add brigade https://brigadecore.github.io/charts
[root@K8S-PROD-M1 ~]# helm repo add bitnami https://charts.bitnami.com/bitnami
[root@K8S-PROD-M1 ~]# helm repo add harbor https://helm.goharbor.io
[root@K8S-PROD-M1 ~]# helm repo update
[root@K8S-PROD-M1 ~]# helm repo list
[root@K8S-PROD-M1 ~]# helm search repo stable
[root@K8S-PROD-M1 ~]# helm search repo brigade
[root@K8S-PROD-M1 ~]# helm repo remove aliyun
[root@K8S-PROD-M1 ~]# helm search hub
[root@K8S-PROD-M1 ~]# helm search hub wordpress
其餘命令
heml env
helm help
經常使用選項
--dry-run
-g, --generate-name
--no-hooks
測試Helm
安裝應用
[root@K8S-PROD-M1 ~]# helm search repo nginx
[root@K8S-PROD-M1 ~]# helm install my-nginx bitnami/nginx
[root@K8S-PROD-M1 ~]# helm install my-nginx bitnami.nginx.tgz
[root@K8S-PROD-M1 ~]# helm install my-nginx bitnami/nginx/
[root@K8S-PROD-M1 ~]# helm install --set name=prod my-nginx bitnami/nginx/
[root@K8S-PROD-M1 ~]# helm install --set-string long_int=123 my-nginx bitnami/nginx/
[root@K8S-PROD-M1 ~]# helm install --set-file my_script=dothings.sh my-nginx bitnami/nginx/
[root@K8S-PROD-M1 ~]# helm install --set foo=bar --set bar=foo my-nginx bitnami/nginx/
[root@K8S-PROD-M1 ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
my-nginx default 1 2020-11-30 15:29:44.362452632 +0800 CST deployed nginx-8.2.0 1.19.5
[root@K8S-PROD-M1 ~]# helm status my-nginx
NAME: my-nginx
LAST DEPLOYED: Mon Nov 30 15:29:44 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
...
[root@K8S-PROD-M1 ~]# helm history my-nginx
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Mon Nov 30 15:29:44 2020 deployed nginx-8.2.0 1.19.5 Install complete
[root@K8S-PROD-M1 ~]# helm rollback my-nginx 1
[root@K8S-PROD-M1 ~]# helm uninstall my-nginx
[root@K8S-PROD-M1 ~]# helm list --all
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
my-nginx default 1 2020-11-30 15:29:44.362452632 +0800 CST uninstalled nginx-8.2.0 1.19.5
[root@K8S-PROD-M1 ~]# helm delete my-nginx
release "my-nginx" uninstalled
自定義參數安裝應用
Helm 中支持使用自定義yaml文件和 --set命令參數對要安裝的應用進行參數配置,首先查看可配置參數:
[root@K8S-PROD-M1 ~]# helm show values bitnami/nginx
使用自定義values.yaml文件安裝應用
[root@K8S-PROD-M1 nginx]# cat > values.yaml << EOF
image:
registry: docker.io
repository: bitnami/nginx
resources:
limits:
cpu: 1000m
memory: 512Mi
requests:
cpu: 1000m
memory: 512Mi
EOF
helm install -f values.yaml bitnami/nginx
使用--set配置參數進行安裝應用
--set參數是在使用helm命令時候添加的參數,能夠在執行helm安裝與更新應用時使用,多個參數間用」,「隔開。
若是配置文件和--set同時使用,則--set設置的參數會覆蓋配置文件中的參數配置。
對於--set配置參數,Helm官方對於不一樣的配置類型給出了不一樣的寫法,以下:
[root@K8S-PROD-M1 nginx]# helm install --set 'registry.registry=docker.io,registry.repository=bitnami/nginx' bitnami/nginx
升級應用
[root@K8S-PROD-M1 nginx]# cat > values.yaml << EOF
service.type: NodePort
service.nodePorts.http: 30002
EOF
[root@K8S-PROD-M1 nginx]# helm upgrade -f values.yaml my-nginx bitnami/nginx -n default
[root@K8S-PROD-M1 nginx]# helm get values my-nginx
渲染模板
查看經過指定的參數渲染的Kubernetes部署資源模板:
[root@K8S-PROD-M1 ~]# helm template bitnami/nginx
開發Chart
建立chart
[root@K8S-PROD-M1 ~]# mkdir -p /root/workspace/helm
[root@K8S-PROD-M1 ~]# cd /root/workspace/helm
[root@K8S-PROD-M1 helm]# helm create chart-demo
[root@K8S-PROD-M1 helm]# tree chart-demo/
chart-demo/
├── charts #該目錄保存其餘依賴的 chart(子 chart)
├── Chart.yaml
├── templates #chart配置模板,用於渲染最終的Kubernetes YAML文件
│ ├── deployment.yaml #Kubernetes deployment 配置
│ ├── _helpers.tpl #用於建立模板時的幫助類
│ ├── hpa.yaml
│ ├── ingress.yaml #Kubernetes ingress配置
│ ├── NOTES.txt #用戶運行helm install時候的提示信息
│ ├── serviceaccount.yaml #Kubernetes serviceaccount配置
│ ├── service.yaml #Kubernetes service配置
│ └── tests
│ └── test-connection.yaml
└── values.yaml # 定義chart模板中的自定義配置的默認值,能夠在執行helm install或helm update的時候覆蓋
3 directories, 10 files
修改Chart
上一步建立的是一個標準的chart目錄結構,能夠編輯相應配置從而建立本身的chart。
[root@K8S-PROD-M1 helm]# helm lint chart-demo/
==> Linting chart-demo/
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
[root@K8S-PROD-M1 helm]# helm package chart-demo/Successfully packaged chart and saved it to: /root/workspace/helm/chart-demo-0.1.0.tgz