helm 的安裝使用
helm用途
1. 建立新的chart 2. chart 打包成tgz 格式 3. 上傳chart 到 chart 殘酷或從倉庫中下載chart 4. 在k8s叢集中安裝或解除安裝chart 5. 管理用helm安裝的 chart的釋出週期
重要概念
1. chart 包含了建立k8s的一個應用例項的必要資訊 2. config: 包含了應用釋出配置資訊 3. release: 是一個chart 及其配置的一個執行例項
helm基本使用
倉庫
helm的repo倉庫和 docker registry比較類似,chart庫可以用來儲存和共享打包chart的位置,我們安裝了helm後,預設的倉庫是google一個地址,可以使用 helm repo list
來檢視倉庫配置
root@master1 tools]# helm repo list
新增倉庫地址
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/
更新
更新
[root@master1 tools]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "ali-incubator" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈Happy Helming!⎈
從chart倉庫中查詢 nginx
[root@master1 tools]# helm search repo nginx
NAME CHART VERSION APP VERSION DESCRIPTION
檢視chart/mysql的資訊
[root@master1 tools]# helm show chart bitnami/mysql
安裝
[root@master1 tools]# helm install nginx bitnami/nginx
檢視詳細的values值
[root@master1 tools]# helm show values bitnami/nginx
自定義配置
[root@master1 tools]# cat 1.yaml service: type: ClusterIP
安裝執行測試
helm install nginx -f 1.yaml bitnami/nginx --dry-run
更新升級
[root@master1 tools]# cat 1.yaml service: type: NodePort
helm upgrade -f 1.yaml nginx bitnami/nginx
檢視歷史記錄
[root@master1 tools]# helm ls NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION nginx default 2 2021-06-14 17:43:21.358066771 +0800 CST deployed nginx-9.1.0 1.21.0 [root@master1 tools]# helm history nginx REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Mon Jun 14 17:41:34 2021 superseded nginx-9.1.0 1.21.0 Install complete 2 Mon Jun 14 17:43:21 2021 deployed nginx-9.1.0 1.21.0 Upgrade complete
回滾到上一個版本
[root@master1 tools]# helm rollback nginx 1 Rollback was a success! Happy Helming!
[root@master1 tools]# helm history nginx REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Mon Jun 14 17:41:34 2021 superseded nginx-9.1.0 1.21.0 Install complete 2 Mon Jun 14 17:43:21 2021 deployed nginx-9.1.0 1.21.0 Upgrade complete
[root@master1 tools]# helm rollback nginx 1 Rollback was a success! Happy Helming!
[root@master1 tools]# helm ls NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION nginx default 3 2021-06-14 17:47:30.537248127 +0800 CST deployed nginx-9.1.0 1.21.0
[root@master1 tools]# helm history nginx REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Mon Jun 14 17:41:34 2021 superseded nginx-9.1.0 1.21.0 Install complete 2 Mon Jun 14 17:43:21 2021 superseded nginx-9.1.0 1.21.0 Upgrade complete 3 Mon Jun 14 17:47:30 2021 deployed nginx-9.1.0 1.21.0 Rollback to 1
helm模版之內建函式和values
常見chart包
[root@master1 tools]# helm create helloworld Creating helloworld [root@master1 tools]# tree helloworld/ helloworld/ ├── charts ├── Chart.yaml ├── templates │ ├── deployment.yaml │ ├── _helpers.tpl │ ├── hpa.yaml │ ├── ingress.yaml │ ├── NOTES.txt │ ├── serviceaccount.yaml │ ├── service.yaml │ └── tests │ └── test-connection.yaml └── values.yaml 3 directories, 10 files
使用helmgetmanifest name獲取詳細資訊
[root@master1 tools]# helm get manifest nginx --- # Source: nginx/templates/server-block-configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-server-block labels: app.kubernetes.io/name: nginx helm.sh/chart: nginx-9.1.0 app.kubernetes.io/instance: nginx app.kubernetes.io/managed-by: Helm data: server-blocks-paths.conf: |- include "/opt/bitnami/nginx/conf/server_blocks/ldap/*.conf"; include "/opt/bitnami/nginx/conf/server_blocks/common/*.conf"; --- # Source: nginx/templates/svc.yaml apiVersion: v1 kind: Service metadata: name: nginx labels: app.kubernetes.io/name: nginx helm.sh/chart: nginx-9.1.0 app.kubernetes.io/instance: nginx app.kubernetes.io/managed-by: Helm spec: type: LoadBalancer externalTrafficPolicy: "Cluster" ports: - name: http port: 80 targetPort: http selector: app.kubernetes.io/name: nginx app.kubernetes.io/instance: nginx --- # Source: nginx/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx labels: app.kubernetes.io/name: nginx helm.sh/chart: nginx-9.1.0 app.kubernetes.io/instance: nginx app.kubernetes.io/managed-by: Helm spec: replicas: 1 selector: matchLabels: app.kubernetes.io/name: nginx app.kubernetes.io/instance: nginx template: metadata: labels: app.kubernetes.io/name: nginx helm.sh/chart: nginx-9.1.0 app.kubernetes.io/instance: nginx app.kubernetes.io/managed-by: Helm spec: serviceAccountName: default affinity: podAffinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - podAffinityTerm: labelSelector: matchLabels: app.kubernetes.io/name: nginx app.kubernetes.io/instance: nginx namespaces: - "default" topologyKey: kubernetes.io/hostname weight: 1 nodeAffinity: containers: - name: nginx image: docker.io/bitnami/nginx:1.21.0-debian-10-r0 imagePullPolicy: "IfNotPresent" env: - name: BITNAMI_DEBUG value: "false" ports: - name: http containerPort: 8080 livenessProbe: tcpSocket: port: http periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 6 readinessProbe: tcpSocket: port: http initialDelaySeconds: 5 periodSeconds: 5 timeoutSeconds: 3 successThreshold: 1 failureThreshold: 3 resources: limits: {} requests: {} volumeMounts: - name: nginx-server-block-paths mountPath: /opt/bitnami/nginx/conf/server_blocks volumes: - name: nginx-server-block-paths configMap: name: nginx-server-block items: - key: server-blocks-paths.conf path: server-blocks-paths.conf
內建物件
{{ .Release.Name }}將relases的名字插入到模版中, Release是helm的內建物件,
Release: 這個物件描述了release本身 1. Release.Name release 名稱 2. Release.Time release 時間 3. Release.Namespace: release 的namespace 4. Release.Service: release 的服務名稱 5. Release.Revision: release修訂版本,從1開始累加 6. Release.isUpgrade: 如果當前操作是升級或者回滾, 則將其設定為true 7. Release.isInstall: 如果當前操作是安裝, 則設定為true vlues: 從values.yaml 檔案和使用者提供的檔案傳入模版的值,預設情況下, values是空. chart: chart.yaml檔案的內容, 所有的chart物件都是將從該檔案訪問,例如 {{ .Chart.Name }} {{ .Chart.Version }} Files: 這提供了對 chart中所有非特殊檔案的訪問, 雖然無法使用它來訪問模版,但是可以使用它來訪問chart中的其他檔案 Template: 包含有關正在執行的當前模版資訊
helm模版之管道與控制結構
模版函式
apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }} data: nacosserver: "172.16.230.111" k8s: {{ quote .Values.k8s.apiserver }} python: {{ .Values.python }}
模版函式遵循的語法是:functionName arg1 arg2 ,在上面的模版中, quote.Values.apiserver呼叫了quote函式並將後面的值作為一個引數傳遞給它.quote函式是模版中的一個值渲染成字串
[root@master1 tools]# helm install fengjianconfigmap apiserver --dry-run NAME: fengjianconfigmap LAST DEPLOYED: Mon Jun 14 18:53:50 2021 NAMESPACE: default STATUS: pending-install REVISION: 1 TEST SUITE: None HOOKS: MANIFEST: --- # Source: apiserver/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: fengjianconfigmap data: nacosserver: "172.16.230.111" k8s: "master1" python: list
把k8s指的值變成大寫
apiVersion: v1 kind: ConfigMap metadata: name: {{ .Release.Name }} data:
# 如果values.yaml 預設沒有定義hello的值,預設是空, 可以使用default 定義一個值 nacosserver: {{ .Values.hello | default "I don't" | quote }} # 轉成字串,再轉成大寫 k8s: {{ .Values.k8s.apiserver | quote | upper }}
# 轉成字串,並且重複3次 python: {{ .Values.python | repeat 3 | quote }}