一:介绍

helm:helm官网文档

Helm is an open source package manager for Kubernetes. It provides the ability to provide, share, and use software built for Kubernetes.

上述是CNCF关于helm的一个简短的介绍,意思是说helm是kubernetes的一个包管理器,提供了为k8s提供、分享、构建软件的能力。类比于redhat系列的rpm、以及ubuntu的apt,都可以快速分发并且安装软件。

基本概念

  1. chart:Helm软件包,它包含了在Kubernetes集群上运行应用、工具或服务所需的所有资源定义。chart对于k8s等同于MacOS brew、debain dpkg或redhat RPM的文件包。
  2. repo:chart仓库,收集、存储以及共享可以在k8s安装的charts。
  3. release:是Kubernetes集群中安装运行的一个chart实例。一个chart通常可以多次安装到同一个集群中,每次安装时都会创建一个新版本。
  4. Config: Chart实例化安装运行时使用的配置信息。

二:实战

准备应用

configmap-helloworld.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: helloworld-configmap
data:
  value: "Hello World"

生成 helloworld chart项目包

helm create helloworld

结构详情

helloworld/
├── Chart.yaml            // chart基本信息文件
├── charts
├── templates        // 资源模版文件夹
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   ├── serviceaccount.yaml
│   └── tests        // 测试相关文件夹
│       └── test-connection.yaml
└── values.yaml     // 参数变量文件

删除templates文件夹下文件,替换成如上configmap文件,查看最终要部署的内容

helloworld
├── Chart.yaml
├── charts
├── templates
│   └── configmap-helloworld.yaml
└── values.yaml

--dry-run 模拟执行

 helm install --dry-run hw helloworld/

读值方式

从Release读取

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}
data:
  value: "Hello World"

从values文件读取 注意:《现在配置文件里加入参数name》

name: hw-from-values
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.name }}
data:
  value: "Hello World"

从参数读取

helm install  hw --dry-run helloworld/ --set name=hw-from-parammm

私有charts仓库搭建

安装chartmuseum repo

helm repo add chartmuseum https://chartmuseum.github.io/charts

生成 helloworld chart项目包

helm install chartmuseum  chartmuseum/chartmuseum  --set persistence.enabled=true --set persistence.storageClass="rook-cephfs" --set persistence.pv.capacity.storage=20Gi --set env.open.DISABLE_API=false --set env.secret.BASIC_AUTH_USER=aaaa --set env.secret.BASIC_AUTH_PASS=bbbb

helm push插件安装

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

到此,私有repo 仓库准备完毕。

打包以及repo部署

集群添加私有repo仓库

helm repo add my-repo http://10.90.x.x:8080 

打包

helm package 

安装

helm install  hw --dry-run helloworld-0.1.0.tgz

将chart包推送到仓库

helm cm-push helloworld-0.1.0.tgz my-repo

通过仓库安装

helm repo update my-repo
helm install hw my-repo/helloworld --set name=hw-newww

templates 语法

内置方法

Release 用法 {{ .Release.Name }} {{ .Release.Namespace }} …
Values 用法 {{ .Values.name }} {{ .Values.app.image }} …
Chart 用法 {{ .Chart.Name }} {{ .Chart.Version }} …

运算符

算数运算符

  • add sub min max mod …
    values.yaml 加入参数

    num1: 1
    num2: 2

    add示例

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      sum: {{ add .Values.num1 .Values.num2 }}

    关系运算符

  • and or not eq ne lt gt …
    not示例:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      on: {{ not .Values.num1 }}

    流控制

    if else

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      {{- if eq "coffee1" "coffee" }}
      mug: "true"
      {{- end }}

    range

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Release.Name }}-configmap
    data:
      myvalue: "Hello World"
      sizes: |-
     {{- range tuple "small" "medium" "large" }}
     - {{ . | quote }}
     {{- end }}   

常用命令

安装

helm install <RELEASENAME> <CHART>

卸载

helm uninstall <RELEASENAME> 

更新

helm upgrade <RELEASENAME> <CHART> --set key=value

搜索chart

helm search rpeo

推送chart包到仓库

helm push

查看release版本

helm history

回滚

helm rollback

查看

helm show

创建chart包项目

helm create

参考:
helm仓库
官方下载

如有@侵权,请联系 2787950493@qq.com 改版。


火车站的那个她
1 声望0 粉丝