k8s學習記錄,包管理工具helm(十三)


文章中資料參考來源2022 雲原生Kubernetes全棧架構師

kubernetes包管理工具——Helm

安裝helm

可以通過訪問Helm官網獲取安裝文檔

1、在github上下載安裝tar包文件

tar包下載地址

2、拷貝到目標機器上【我是在本機科xue上網情況下 下載后再上傳至k8s集群機器上,如果網絡情況比較好的話也可以直接在集群上下載】

3、解壓安裝包,並移動到配置有環境變量的文件夾中【這里是移動到/usr/local/bin目錄下】

#移動完文件夾后,使用命令查看helm的版本信息
helm version

helm常用命令

#添加倉庫
helm repo add bitnami https://charts.bitnami.com/biitnami
helm repo add ali-stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
#查看helm鏡像倉庫列表
helm repo list

#查找相關包
helm search repo nginx

#拉取相關包
helm pull bitnami/nginx

#查看helm安裝的應用列表
helm list
#helm創建一個chart
helm create helm-test

helm目錄樹結構

#新建的chart的目錄結果
[root@master01 helm-test]# tree
.
├── charts  #依賴文件
├── Chart.yaml  這個 chart的版本信息
├── templates #模板文件夾
│   ├── deployment.yaml #模板文件
│   ├── _helpers.tpl  #自定義的函數或模板
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt # 這個chart的信息內容
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml  #配置全局變量或一些參數

helm中的語法

取值 .Values.replicaCount 取value.yaml中replicaCount的值

#模板文件中的參數值,下面replicas的取值是讀取的chart根目錄下values.yaml中的replicaCount配置的值
#helm-test/templates/deployment.yaml
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}


#helm-test/values.yaml

# Default values for helm-test.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1

with 取value.yaml中切片的數據

#helm-test/values.yaml
imagePullSecrets:  # [ '{"name": "a"}', '{"name": "b"}' ]
  - name: a
  - name: b

#helm-test/templates/deployment.yaml
# .Values.image.a.b.c.repository
# .Values.image.a.b.c.pullPolicy
#通過with語法,可以將前面重復的定位值省略,只寫一次即可
# with .Values.image.a.b.c
#   .repository
#   .pullPolicy
spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}  
        #  toYaml后面的 “ . ” 就代表 .Values.imagePullSecrets,對應的是values.yaml中 imagePullSecrets的切片取值“a”和“b”
        # nindent 8 代表縮進8個空格

#使用命令測試下  --dry-run 參數代表只打印,不部署,查看語法命令是否正確
helm install test --dry-run .

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "helm-test.fullname" . }}
  labels:
    {{- include "helm-test.labels" . | nindent 4 }}

#include 引用的函數或模板  templates/_helpers.tpl  這個文件

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "helm-test.fullname" -}}     # 定義helm-test.fullname 這個模板
{{- if .Values.fullnameOverride }}   #判斷如果有 .Values.fullnameOverride 這個取值
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}  #那么就取前面63個字符,去掉"-"
{{- else }}  # 否則
{{- $name := default .Chart.Name .Values.nameOverride }}   #定義變量name   如果  .Values.nameOverride為空的,那么就取默認default的值 .Chart.Name
{{- if contains $name .Release.Name }}  # 如果name中包含 .Release.Name
{{- .Release.Name | trunc 63 | trimSuffix "-" }}  # 就取.Release.Name的前63個字符,末尾去掉"-"
{{- else }}  #否則
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}  # 就以占位符的方式,輸出 .Release.Name-$name  的前63個字符,末尾去掉"-"
{{- end }}
{{- end }}
{{- end }}

# define 定義一個模板
# - 代表去掉空格(加在前面代表去掉前面的,加在后面代表去掉后面的)
# trunc 63  只能取前面63個字符【最多63個字符,because some Kubernetes name fields are limited to this (by the DNS naming spec).】
# 數字為正是從前往后取,數字為負時,從后往前取
# trimSuffix "-"  字符串末尾去掉"-"
# trimPrefix "-"  字符串前面去掉"-"
# default  定義的變量的默認值   .Chart.Name      我這里的取值為: helm-test

# contains 如果$name中包含 .Release.Name  這個Release.Name就是helm install xxx 后面跟的名稱
helm install zw --dry-run .

#如果命令后面跟的Release.Name包含了.Chart.Name,那么就以 Release.Name-Chart.Name
helm install zw-helm-test-new --dry-run .

#當我們修改了values.yaml中的fullnameOverride的值時,
nameOverride: ""
fullnameOverride: "change-values-name"

#再運行
helm install helm-test --dry-run .

#也可以通過在命令中使用--set來修改fullnameOverride的值
helm install helm-test --set fullnameOverride=set-fullnameOverride-new --dry-run .

指定名字的優先級 set >> values.yaml >> Release.Name


免責聲明!

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



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