helm3使用入門


helm3使用入門

什么是 Helm
Helm 為團隊提供了在 Kubernetes 內部創建、安裝和管理應用程序時需要協作的工具,有點類似於 Ubuntu 中的 APT 或 CentOS 中的 YUM。

有了 Helm,開發者可以:

  • 查找要安裝和使用的預打包軟件(Chart)
  • 輕松創建和托管自己的軟件包
  • 將軟件包安裝到任何 K8s 集群中
  • 查詢集群以查看已安裝和正在運行的程序包
  • 更新、刪除、回滾或查看已安裝軟件包的歷史記錄

……
Helm 組件及相關術語
helm

  • Helm 是一個命令行下的客戶端工具。主要用於 Kubernetes 應用程序 Chart 的創建、打包、發布以及創建和管理本地和遠程的 Chart 倉庫。

Chart

  • Helm 的軟件包,采用 TAR 格式。類似於 APT 的 DEB 包或者 YUM 的 RPM 包,其包含了一組定義 Kubernetes 資源相關的 YAML 文件。

Repoistory

  • Helm 的軟件倉庫,Repository 本質上是一個 Web 服務器,該服務器保存了一系列的 Chart 軟件包以供用戶下載,並且提供了一個該 Repository 的 Chart 包的清單文件以供查詢。Helm 可以同時管理多個不同的 Repository。

Release

  • 使用 helm install 命令在 Kubernetes 集群中部署的 Chart 稱為 Release。可以理解為 Helm 使用 Chart 包部署的一個應用實例。

安裝 Helm3
二進制安裝

$ wget https://get.helm.sh/helm-v3.1.2-linux-amd64.tar.gz
$ tar -zxvf helm-v3.1.2-linux-amd64.tar.gz
$ cd linux-amd64
$ mv helm /usr/local/bin/
$ helm --help

腳本安裝

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh

命令補全

# 在~/.bashrc追加
source <(helm completion bash)

其他安裝詳情

快速開始

添加常用倉庫

$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/
$ helm repo update # Make sure we get the latest list of charts
$ helm repo list

微軟也提供了stable倉庫的鏡像:

  • stable: http://mirror.azure.cn/kubernetes/charts/

列出stable倉庫可以安裝的charts

helm search repo stable
NAME CHART VERSION APP VERSION DESCRIPTION
stable/acs-engine-autoscaler 2.2.2 2.1.1 DEPRECATED Scales worker nodes within agent pools
stable/aerospike 0.2.8 v4.5.0.5 A Helm chart for Aerospike in Kubernetes
stable/airflow 4.1.0 1.10.4 Airflow is a platform to programmatically autho...
stable/ambassador 4.1.0 0.81.0 A Helm chart for Datawire Ambassador
# ... and many more

安裝stable倉庫的一個mysql的chart

$ helm install stable/mysql --generate-name

查看詳情

$ helm show all stable/mysql

查看chart怎么寫的 

 解壓縮:

 

 查看目錄結構:

 

$ helm pull stable/mysql

$ ls 
mysql-1.6.6.tgz

$ tar -zxvf mysql-1.6.6.tgz

$ yum install -y tree $ tree mysql
mysql ├── Chart.yaml ├── README.md ├── templates │   ├── configurationFiles
-configmap.yaml │   ├── deployment.yaml │   ├── _helpers.tpl │   ├── initializationFiles-configmap.yaml │   ├── NOTES.txt │   ├── pvc.yaml │   ├── secrets.yaml │   ├── serviceaccount.yaml │   ├── servicemonitor.yaml │   ├── svc.yaml │   └── tests │   ├── test-configmap.yaml │   └── test.yaml └── values.yaml

查看用helm安裝的release

$ helm ls
NAME             VERSION   UPDATED                   STATUS    CHART
smiling-penguin  1         Wed Sep 28 12:59:46 2016  DEPLOYED  mysql-0.1.0

卸載release

$ helm uninstall smiling-penguin

查看status

$ helm status smiling-penguin
Status: UNINSTALLED

Chart 的使用

下面我們通過一個完整的示例來學習如何使用 Helm 創建、打包、分發、安裝、升級及回退Kubernetes應用。

創建一個名為 mychart 的 Chart

官網的例子

$ helm create mychart   #創建一個helm項目

 該命令在當前目錄創建了一個 mychart 目錄,該目錄結構如下所示:

$ tree mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

介紹:

  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

Templates 目錄下 YAML 文件模板(go template語法)填充的值默認都是在 values.yaml 里定義的,比如在 deployment.yaml 中定義的容器鏡像:

image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
$ cat mychart/values.yaml|grep repository
  repository: nginx

 

以上變量值是在 create chart 的時候就自動生成的默認值,你可以根據實際情況進行修改。

編寫應用的介紹信息

打開 mychart/Chart.yaml:

 

apiVersion: v2
name: mychart
description: A Helm chart for Kubernetes
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: 1.16.0 

編寫應用具體部署信息

編輯 mychart/values.yaml,它默認會在 Kubernetes 部署一個 Nginx。下面是 mychart 應用的 values.yaml 文件的內容:

 

# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: nginx
  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: 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

nodeSelector: {}

tolerations: []

affinity: {}

檢查依賴和模板配置是否正確

$ helm lint mychart/     #檢查依賴和模版配置是否正確
==> Linting mychart/
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed

如果文件格式錯誤,可以根據提示進行修改。

將應用打包

$ helm package mychart
Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz

mychart 目錄會被打包為一個 mychart-0.1.0.tgz 格式的壓縮包,該壓縮包會被放到當前目錄下。

如果你想看到更詳細的輸出,可以加上 --debug 參數來查看打包的輸出。

離線部署
注意: ~/.kube/config不存在的情況下要用 helm --kubeconfig 指定配置文件

# 方式一
$ helm install demo-test ./mychart

# 可根據不同的配置來install,默認是values.yaml
# helm install demo-test ./mychart -f ./mychart/values-prod.yaml

# 方式二
$ helm install demo-test ./mychart-0.1.0.tgz

$ helm list

# 升級
# $ helm upgrade demo-test ./mychart-0.2.0.tgz

$ helm uninstall demo-test

將應用發布到 Repository

harbor1.6+ 支持存儲 helm charts,這里使用 helm 安裝 harbor

這里為了簡化測試操作,我關閉了數據卷的掛載並使用的是 NodePort 方式進行訪問。

 

 

 

 

 

$ helm repo add goharbor https://helm.goharbor.io

$ helm repo update

# 查看harbor chart的各個版本
$ helm search repo harbor -l

# --version選擇chart的版本
$ helm install harbor goharbor/harbor --set persistence.enabled=false --set expose.type=nodePort --set expose.tls.enabled=false --set externalURL=http://192.168.4.82:30002
$ kubectl get svc

參數說明:

  • persistence.enabled=false 關閉存儲,為了方便操作,真實使用時需要掛在存儲
  • expose.type=nodePort 使用 NodePort 訪問
  • expose.tls.enabled=false 關閉tls
  • externalURL=http://192.168.4.82:30002 設置登錄 harbor 的外部鏈接,ip是某個node的ip
  • harbor 裝好之后,我們訪問 http://192.168.4.82:30002 進行登錄 harbor, harbor 的默認賬號密碼是 admin/Harbor12345

新建一個項目 chart_repo,設置公開

新建一個用戶test,密碼Test1234,加入到項目的成員里面,並賦予權限

 

 

安裝使用 helm-push 插件

helm plugin install https://github.com/chartmuseum/helm-push

超時沒裝成功,直接下載:

$ mkdir ~/.local/share/helm/plugins/helm-push

$ wget https://github.com/chartmuseum/helm-push/releases/download/v0.8.1/helm-push_0.8.1_linux_amd64.tar.gz

$ tar -zxvf helm-push_0.8.1_linux_amd64.tar.gz -C ~/.local/share/helm/plugins/helm-push

$ helm push --help

添加repo

 

 

#注釋:helm repo add --ca-file <ca file> --cert-file <cert file> --key-file <key file>     --username <username> --password <password> <repo name> http://10.13.84.187:30002/chartrepo/chart_repo

$ helm repo add myrepo http://192.168.4.86:30002/chartrepo/chart_repo  #倉庫名為:myrepo

push

 

$ helm push mychart/ myrepo -u test -p Test1234
Pushing mychart-0.1.0.tgz to myrepo...
Done.

查找chart:mychart

$ helm repo update

$ helm search repo mychart

NAME              CHART VERSION    APP VERSION    DESCRIPTION                
myrepo/mychart    0.1.0            1.16.0          A Helm chart for Kubernetes

部署應用

注意: ~/.kube/config不存在的情況下要用 helm --kubeconfig 指定配置文件

helm install demo-test  --dry-run --debug myrepo/mychart

helm install demo-test  myrepo/mychart

 

# helm install --ca-file <ca file> --cert-file <cert file> --key-file <key file>     --username=<username> --password=<password> --version 0.2.0 <repo name>/mychart

# 在部署前使用 --dry-run 參數驗證 Chart 的配置,並不執行安裝
$ helm install demo-test  --dry-run --debug myrepo/mychart

# 沒問題就install
$ helm install demo-test  myrepo/mychart

NAME: demo-test
LAST DEPLOYED: Tue Apr 21 11:06:27 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80

測試:

export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")
echo #POD_NAME
kubectl --namespace default port-forward $POD_NAME 8080:80    #打開端口映射,注意不能退出,否則下面訪問nginx不通。

訪問nginx:在開一個linux終端

curl http://127.0.0.1:8080   #在開一個linux終端

使用下面的命令列出的所有已部署的 Release 以及其對應的 Chart。

$ helm list

NAME                      NAMESPACE    REVISION    UPDATED                                    STATUS      CHART                      APP VERSION
demo-test                 default      1           2020-04-21 11:06:27.672714497 +0800 CST    deployed    mychart-0.1.0              1.16.0  

從上面 helm list 輸出的結果中我們可以看到有一個 REVISION(更改歷史)字段,該字段用於表示某一個 Release 被更新的次數,我們可以用該特性對已部署的 Release 進行回滾。

查看狀態

升級應用

修改 mychart/Chart.yaml 文件

 

改成:

 

 

 

$vim mychart/Chart.yaml
apiVersion: v2 name: mychart description: A Helm chart
for Kubernetes type: application # 改了下面兩行 version: 0.2.0 appVersion: 1.17.0

上傳chart

 

 

$ helm push mychart/ myrepo -u test -p Test1234
Pushing mychart-0.2.0.tgz to myrepo...
Done.

$ helm repo update

$ helm search repo mychart -l

NAME              CHART VERSION    APP VERSION    DESCRIPTION                
myrepo/mychart    0.2.0            1.17.0          A Helm chart for Kubernetes
myrepo/mychart    0.1.0            1.16.0          A Helm chart for Kubernetes

現在用 helm upgrade 命令將已部署的 demo-test 升級到新版本。你可以通過 --version 參數指定需要升級的版本號,如果沒有指定版本號,則缺省使用最新版本。

  

$ helm -n default upgrade demo-test myrepo/mychart

Release "demo-test" has been upgraded. Happy Helming!
NAME: demo-test
LAST DEPLOYED: Tue Apr 21 11:33:12 2020
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=demo-test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80

$ helm list

NAME                      NAMESPACE    REVISION    UPDATED                                    STATUS      CHART                      APP VERSION
demo-test                 default      2           2020-04-21 11:33:12.376727722 +0800 CST    deployed    mychart-0.2.0              1.17.0

完成后,可以看到已部署的 demo-test 被升級到chart的 0.2.0 版本。

回退應用

如果更新后的程序由於某些原因運行有問題,需要回退到舊版本的應用。可以使用 helm history 命令查看一個 Release 的所有變更記錄

 

$ helm history demo-test 

REVISION    UPDATED                     STATUS        CHART            APP VERSION    DESCRIPTION     
1           Tue Apr 21 11:06:27 2020    superseded    mychart-0.1.0    1.16.0          Install complete
2           Tue Apr 21 11:33:12 2020    deployed      mychart-0.2.0    1.17.0          Upgrade complete

其次,我們可以使用下面的命令對指定的應用進行回退。

 

 

$ helm rollback demo-test 1

Rollback was a success! Happy Helming!

注:其中的參數 1 是 helm history 查看到 Release 的歷史記錄中 REVISION 對應的值。
查看

 

$ helm list

NAME         NAMESPACE    REVISION    UPDATED                                    STATUS      CHART            APP VERSION
demo-test    default      3           2020-04-21 11:40:50.782129072 +0800 CST    deployed    mychart-0.1.0    1.16.0

$ helm history demo-test 

REVISION    UPDATED                     STATUS        CHART            APP VERSION    DESCRIPTION     
1           Tue Apr 21 11:06:27 2020    superseded    mychart-0.1.0    1.16.0          Install complete
2           Tue Apr 21 11:33:12 2020    superseded    mychart-0.2.0    1.17.0          Upgrade complete
3           Tue Apr 21 11:40:50 2020    deployed      mychart-0.1.0    1.16.0          Rollback to 1   

刪除應用

helm uninstall demo-test

helm直接打印和生成yaml內容

helm template .             #打印到屏幕上
helm template . > one.yaml  #輸出到文件里

 

 

參考

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM