k8s-1 HELM使用详解

HELM使用详解:

1.安装:(https://www.cnblogs.com/ericte/p/13840511.html)

kubectl get po minio-io-minio-io-857df79454-sgr7p -n public -o yaml |kubectl replace --force -f -

wget https://get.helm.sh/helm-v3.9.3-linux-amd64.tar.gz
tar xf helm-v3.9.3-linux-amd64.tar.gz && mv linux-amd64/helm /usr/local/bin/helm
#for k3s
echo 'export KUBECONFIG=/etc/rancher/k3s/k3s.yaml' >> /etc/profile

----好,好:https://www.cnblogs.com/liugp/p/16663783.html
helm search hub
helm repo list    helm仓库查看
helm repo add stable   https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts    仓库添加
helm repo add jetstack https://charts.jetstack.io
helm repo remove stable    仓库删除
##  比如想查找 nginx 的 chart,使用如下命令:
helm search repo chart_name
helm search repo nginx
1.3个helm仓库常用地址官方:https://hub.helm.sh/
2.开源社镜像:
http://mirror.kaiyuanshe.cn/kubernetes/charts/
​http://mirror.azure.cn/kubernetes/charts/
3.kubernetes app商店:
https://hub.kubeapps.com/

# 添加稳定版图表仓库
helm repo add stable https://kubernetes-charts.storage.googleapis.com/
# 列出该图表库中的所有图表
helm search repo stable

helm repo update              # Make sure we get the latest list of charts
helm install stable/mysql --generate-name

常用命令
helm install mk -n monkey /data/helm-charts/
helm upgrade mk -n monkey /data/helm-charts/
helm history mk -n monkey
helm rollback mk 3 -n monkey
helm status mk -n monkey
helm ls --deleted
helm delete --purge mk
helm install try --dry-run --debug .

三、Helm 命令
helm create
在当前目录创建一个mychart目录。
Chart.yaml — Chart名称、描述和版本号
Values.yaml — Template中各变量的默认值
Template folder — 存放Chart的template文件,Tiller server会根据template里面的文件对里面的变量值进行渲染生成最后供k8s可用的resource yaml文件。
deployment.yaml/service.yaml/ingress.yaml — k8s resouce文件,里面有大量的chart语法
_helpers.tpl — 存放chart语法的变量值
NOTES.txt — 类似README,以供他人了解该chart的用途和介绍,纯文本文件,会在helm install时候作为output输出给用户看。
[apps@gd13-001-noah-helm-proxy-api-prod-342295-0 ~]$ tree mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml
3 directories, 8 files
helm package
在当前同名目录生成tgz格式压缩包,同时将压缩包放到/root/.helm/repository/local并同时更新index.yaml文件。

helm install
利用已有的chart发布一个release

helm install --name --repo --namespace --host --version --set-string

注意:这里的releaseName和namespace不一定会是最后在k8s上面创建的resource里的名称和namespace,只有在chart里引用了这里的namespace才行,不然release和k8s的namespace会有不一致的情况。

小技巧:如果加上" --dry-run --debug ", Tiller server会渲染template但是不会发送给k8s创建resource,用这种方法可以检查chart文件是否正确。

helm upgrade
升级某个已发布的release,一般是通过--set-string来更新镜像或更新replicasCount等。
helm upgrade <releaseName> <chartName> --repo <repoName> --namespace <nameSpace> --host <Tiller server> --version <chart Version> --reset-values|--reuse-values --set-string <ValuesStringList>

helm rollback
回滚release至某个版本。
helm rollback $releaseName $revision --host localhost:44134 --force

helm delete
删除某个处于DEPLOYED状态的release;如果想从release list中彻底清除则需要加上 --purge。
helm delete $releaseName --host localhost:44134

helm status
查看某个release的状态,可以查看该release发布的所有k8s resource的状态。
[apps@gd13-001-noah-helm-proxy-api-prod-342295-0 ~]$ /apps/svr/helm/bin/helm status noah-php-demo
LAST DEPLOYED: Tue Apr 30 18:52:03 2019
NAMESPACE: eric
STATUS: DEPLOYED
RESOURCES:
==> v1/Deployment
NAME                     READY  UP-TO-DATE  AVAILABLE  AGE
noah-php-demo-helm-test  3/3    3           3          22h
==> v1/Pod(related)
NAME                                     READY  STATUS   RESTARTS  AGE
noah-php-demo-helm-test-546958457-bb98x  1/1    Running  0         22h
noah-php-demo-helm-test-546958457-bsl9z  1/1    Running  0         22h
noah-php-demo-helm-test-546958457-k65zz  1/1    Running  0         22h
helm list
list出release列表;可按status,date,chart name分别列出所有的release
helm list --output json --host localhost:44134 --all --offset xxx --max 10

Flags:
  -a, --all                   show all releases, not just the ones marked DEPLOYED
  -c, --chart-name            sort by chart name
      --col-width uint        specifies the max column width of output (default 60)
  -d, --date                  sort by release date
      --deleted               show deleted releases
      --deleting              show releases that are currently being deleted
      --deployed              show deployed releases. If no other is specified, this will be automatically enabled
      --failed                show failed releases
  -m, --max int               maximum number of releases to fetch (default 256)
      --namespace string      show releases within a specific namespace
  -o, --offset string         next release name in the list, used to offset from start value
      --output string         output the specified format (json or yaml)
      --pending               show pending releases
  -r, --reverse               reverse the sort order
helm lint
检查chart文件可能存在的问题,跟helm install --name --dry-run --debug效果差不多。
helm lint <chartName>
helm history
列出某个release所有的历史版本
/apps/svr/helm/bin/helm history noah-php-demo --output table

[apps@gd13-001-noah-helm-proxy-api-prod-342295-0 ~]$ /apps/svr/helm/bin/helm history noah-php-demo --output table
REVISION        UPDATED                         STATUS          CHART           DESCRIPTION
1               Tue Apr 30 16:44:15 2019        SUPERSEDED      helm-test-0.1.5 Install complete
2               Tue Apr 30 18:00:50 2019        SUPERSEDED      helm-test-0.1.5 Upgrade complete
3               Tue Apr 30 18:41:36 2019        SUPERSEDED      helm-test-0.1.5 Rollback to 1
4               Tue Apr 30 18:47:38 2019        SUPERSEDED      helm-test-0.1.5 Upgrade complete
5               Tue Apr 30 18:49:58 2019        SUPERSEDED      helm-test-0.1.5 Upgrade complete
6               Tue Apr 30 18:52:03 2019        DEPLOYED        helm-test-0.1.5 Upgrade complete
helm search
在所有的helm list中找出想找的release,关键字一般为release name。
helm search <keyword>

更多Helm命令请查询Helm官方文档(https://helm.sh/docs/helm/#helm)。

四、Chart语法
在前面我们已经简单介绍了Chart的文件结构,包括Chart.yaml,values.yaml,templates/deployment.yaml,templates/_helpers.tpl,templates/NOTES.txt。

大家都知道template文件里会引用很多变量,而这些变量值来自何处,该用何种语法来写才能达到我们想要的引用效果,下面我们一起来看看如何写一份template文件。

以下是一份最简单的configmap文件,里面的configmap的名称引用了{{ .Release.Name }}-configmap。注意{{ 、}}两边的空格和点 " . " 。

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  myvalue: "Hello World"
Build-in 对象
注意所有的build-in对象首字母都必须大写(Release, Values, Chart等)。
Release对象
从release里拿到release的信息并渲染template文件生成k8s resource文件。
Release.Name: The release name
Release.Time: The time of the release
Release.Namespace: The namespace to be released into (if the manifest doesn’t override)
Release.Service: The name of the releasing service (always Tiller).
Release.Revision: The revision number of this release. It begins at 1 and is incremented for each helm upgrade.
Release.IsUpgrade: This is set to true if the current operation is an upgrade or rollback.
Release.IsInstall: This is set to true if the current operation is an install.
Values对象
从values.yaml里读取对应的key并渲染,{{ .Values.replicasCount }} 、 {{ .Values.image.imageName }}
Values.yaml
replicasCount: 1
business_domain: noah-php-demo.vip.vip.com
namespace: eric
name: noah-php-demo-helm-test
image:
  imageRepo: vipdocker-f9nub.vclound.com/citest
  imageName: 1.0.0_1243_b4a48dd1bf153d5fe62397566bd34e5d1d64fe01
Chart对象
从Chart.yaml文件里引用Chart name和Version。 {{ .Chart.name}} \ {{ .Chart.version }}

Capabilities
获取k8s cluster info

Capabilities
Capabilities.APIVersions is a set of versions.
Capabilities.APIVersions.Has $version indicates whether a version (batch/v1) is enabled on the cluster.
Capabilities.KubeVersion provides a way to look up the Kubernetes version. It has the following values: Major, Minor, GitVersion, GitCommit, GitTreeState, BuildDate, GoVersion, Compiler, and Platform.
Capabilities.TillerVersion provides a way to look up the Tiller version. It has the following values: SemVer, GitCommit, and GitTreeState.
删除某个key值
只需要在helm install/upgrade 时将某个key值设成null。
helm install stable/drupal --set image=my-registry/drupal:0.1.0 --set livenessProbe.exec.command=[cat,docroot/CHANGELOG.txt] --set livenessProbe.httpGet=null
以上命令会更改image,同时将liveness probe由原来的http get改成cat docroot/CHANGELOG.txt来判断。

Function/Pipeline
函数/管道,跟shell命令中的管道是一样的作用,通过一些命令来实现对输出进行格式化,比如 {{ .Values.favorite.food | upper | quote }},会将values 文件里的food值pizza用upper函数全部转成大写PIZZA。

常用的一些函数/工具有:
quote : 引用,一般不做任何事,直接原样输出。
upper : 小写转大写 {{ .Values.favorite.food | upper | quote }}
repeat 5 : 重复5次。{{ .Values.favorite.drink | repeat 5 | quote }}
title: 将首字母大写
nindent 2:缩进2个字母,用于yaml文件格式化
b64enc:转换成base-64
now:当前时间
htmlDate: 格式化时间为2016-11-02
default: default函数,设置默认值,{{ .Values.favorite.drink | default "tea" | quote }},如果vaules文件里不存在drink值,将采用默认值 “ tea ”。

操作表达式函数
eq,ne,lt,gt,and,or,not,跟shell中的含义差不多,结果返回一个boolean值(true/false)。
eq:相等
ne:不等
lt:小于
gt:大于
and :是否存在,存在则为true,否则为false
or : 不懂啥意思........ (Eric)
not: 非
Flow control
if/else
{{ if PIPELINE }}
  # Do something
{{ else if OTHER PIPELINE }}
  # Do something else
{{ else }}
  # Default case
{{ end }}
例子:
{{ if and .Values.favorite.drink (eq .Values.favorite.drink "coffee") }}mug: true{{ end }},表达式含义为当 .Values.favorite.drinkc存在并且等于“coffee时”,在template里加入key值mug: true。

注意: 上面的例子是将一条语句放在一行里处理,如果需要将上面的语句进行格式化显得更好看,就必须要注意由此而带来的空格行。

{{if and .Values.favorite.drink (eq .Values.favorite.drink "coffee")}}
  mug: true
{{end}}
此时渲染时就会发现报错并且mug: true前后多出两个空白行,正确处理方式为在大括号内加入一个 “ - ”,那么渲染时将会跳过该行。

{{- if and .Values.favorite.drink (eq .Values.favorite.drink "coffee")}}
  mug: true
{{- end}}
with
利用with函数来更改范围,如果我们一直在values.yaml里取值,我们是不是一直要加上 ".Values.favorite"前缀给每一个值,为了能简化这个步骤我们引入了with来处理相对路径和绝对路径。下面的例子就是在前面设置了scope为 .Values.favorite, 那么在 end 之前的左右值都会在 .Values.favorite 里取得所有值。
{{- with .Values.favorite }}
drink: {{ .drink | default "tea" | quote }}
food: {{ .food | upper | quote }}
{{- end }}
range
foreach操作,以下例子就是从 Values.pizzaToppings里顺序取出所有值并将首字母大写。" toppings: |- "里的 " |- " 在yaml文件里表示有多行string值。range也可以用于map,dict,list,tuples等。
  toppings: |-
    {{- range .Values.pizzaToppings }}
    - {{ . | title | quote }}
    {{- end }}
  
渲染结果:
  toppings: |-
    - "Mushrooms"
    - "Cheese"
    - "Peppers"
    - "Onions"
  
另外一种用法:
  sizes: |-
    {{- range tuple "small" "medium" "large" }}
    - {{ . }}
    {{- end }}
  
渲染结果:
  sizes: |-
    - small
    - medium
    - large
变量
我们当然也可以在template文件里定义变量供文件后面使用
  {{- $relname := .Release.Name -}}
  {{- with .Values.favorite }}
  drink: {{ .drink | default "tea" | quote }}
  food: {{ .food | upper | quote }}
  release: {{ $relname }}
  {{- end }}
  
或可以:
  toppings: |-
    {{- range $index, $topping := .Values.pizzaToppings }}
      {{ $index }}: {{ $topping }}
    {{- end }}
Named templates
一般是以下划线 _ 开头和以tpl为后缀作为我们的文件,类似于_helpers,tpl
我们在_helpers,tpl里定义了一个mychart.labels
{{/* Generate basic labels */}}
{{- define "mychart.labels" }}
  labels:
    generator: helm
    date: {{ now | htmlDate }}
{{- end }}
  
在template里我们就可以直接引用
metadata:
  name: {{ .Release.Name }}-configmap
  {{- template "mychart.labels" }}
  
渲染后的结果:
metadata:
  name: running-panda-configmap
  labels:
    generator: helm
    date: 2016-11-02
更多高级用法
从其他File中引用,在chart文件里建subchart等功能请见helm官方chart developing文档(https://helm.sh/docs/chart_template_guide/#the-chart-template-developer-s-guide)。
posted @ 2020-05-12 20:16  冰冷的火  阅读(176)  评论(0)    收藏  举报