关于Helm的介绍,查看之前写的文章(务必要先看):【k8s学习】Kubernetes打包工具Helm介绍
【环境】
- MacOS
- minikube version: v1.25.2
【资源】
- Helm官网:https://helm.sh/zh/docs/
- 教程 - Using Helm and Kubernetes: https://www.baeldung.com/ops/kubernetes-helm
【Helm命令列表】
- helm version:打印客户端版本信息
- helm create:使用给定名称创建新的chart
- helm lint:验证chart是否存在问题
- helm template <chart location>:本地渲染模板
- helm install <chart name> <chart location>:安装chart
- helm list:列举发布版本,可加以参数--all
其它:
1. Helm安装
使用Homebrew工具进行安装:
brew install helm
在安装的时候出现问题:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
查看https://www.chengxulvtu.com/missing-xcrun/,需要先安装:
xcode-select --install
helm安装好了,查看版本:
helm version
version.BuildInfo{Version:"v3.8.2", GitCommit:"6e3701edea09e5d55a8ca2aae03a68917630e91b", GitTreeState:"clean", GoVersion:"go1.18.1"}
从3开始因为没有Tiller了,所以就可以直接使用了,并不需要先初始化:
Starting with Helm 3, since there is no more Tiller server, it's unnecessary to initialize Helm
2. Hello World示例
2.1 首先创建一个helm chart,名字叫hello-world:
可以看到在当前目录下新建了一些文件和文件夹:helm create hello-world
-
charts/
:这个文件夹放的是charts的一些依赖,比如当前的chart依赖于别的chart,那么别的chart就可以安装在这里面,默认为空。 -
templates/
:文件夹用来存放模版。 -
Chart.yaml
:当前hello-world chart相关的信息,比如name, version,dependencies等 -
values.yaml
:templates模块需要被替换的真实的值,这里一般存放的是默认值,可以被重写掉的。 -
.helmignore
:类似.gitignore
2.2 创建templates模版
进入/templates文件夹:因为是模版,所以有很多值是需要动态传入的,如{{ .Values.replicaCount}}
,那么就需要在values.yaml中传入。
2.3 查看values.yaml
内容精减下:
replicaCount: 1
image:
repository: nginx
tag: "latest"
pullPolicy: IfNotPresent
service:
type: NodePort
port: 80
2.4 因为只是做demo测试,所以就不用改模版了,只需要删掉一些不想要的,比如就留deployment.yaml和service.yaml也可以:
内容精减下:
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "hello-world.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "hello-world.name" . }}
helm.sh/chart: {{ include "hello-world.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "hello-world.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app.kubernetes.io/name: {{ include "hello-world.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 8080
protocol: TCP
service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ include "hello-world.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "hello-world.name" . }}
helm.sh/chart: {{ include "hello-world.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: {{ include "hello-world.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
3. 打包
3.1 首先使用helm lint来验证chart是否存在问题
比如有问题的时候会报错:3.2 通过命令helm template本地演染模板并显示
本地渲染模板并显示输出:因为我们在2.4只留了Deployment和Service的生成模版,所以这里渲染出来的就只有这两个组件。
3.3 输入helm install命令开始安装
在安装前查看kubernetes组件:开始安装helm chart:
helm install hello-world ./hello-world
helm chart安装,其实就是把模版填上values.yaml,然后再调用kubectl命令来创建组件(即kubectl apply -f <filename>
)。
使用kubectl get all
查看,可以看到通过模版deployment.yaml和service.yaml创建了以下组件:
3.4 查看,使用helm list -all
image.png
Repo相关
另外还有一部分内容与helm Repository有关,跟git或是maven差不太多的思路:
helm package ./hello-world
helm repo index my-repo/ --url https://<username>.github.io/my-repo
helm repo add my-repo https://my-pages.github.io/my-repo
helm install my-repo/hello-world --name=hello-world
helm search repo <KEYWORD>