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
使用 helm get manifest 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 }}