下载helm包
wget https://get.helm.sh/helm-v3.9.4-linux-amd64.tar.gz
解压和移动
tar -zxvf helm-v3.9.4-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
验证
helm version
2.1、创建chart
helm create [release-name]
栗子:helm create sp-boot
这时候会生成一个sp-boot文件夹,里面定义了各种文件模板
2.2、裁剪文件模板,剩下需要的
2.3、编辑values.yaml
app: sp-de
dep:
name: sp-dep
replicas: 2
port: 80
2.4、编辑deploy和service.yaml
deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.dep.name }}
spec:
replicas: {{ .Values.dep.replicas }}
selector:
matchLabels:
app: {{ .Values.app }}
template:
metadata:
labels:
app: {{ .Values.app }}
spec:
containers:
- name: grateful-close
image: k8-sp:3
command: ["/bin/sh", "-c", "java -jar spring-boot.jar"]
# listener port or quit
ports:
- containerPort: {{ .Values.dep.port }}
lifecycle:
preStop:
exec:
# kubectl delete is default 30s
command: ["/bin/sh","-c","kill `ps -ef|grep java|grep -v grep|awk '{print $2}'`"]
service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: {{ .Values.app }}
ports:
- protocol: TCP
port: 8080
targetPort: {{ .Values.dep.port }}
2.5、语法检查+打包
helm lint sp-boot
helm package sp-boot
2.6、安装
helm install [release-name] [pkgname]
或:helm install [pkgname] --generate-name
栗子:helm install spsp sp-boot-0.1.0.tgz
2.7、查看安装的应用
2.8、查看k8s pod
方式一,修改spboot文件夹中的内容然后
helm upgrade [release-name] [your-app]
栗子:helm upgrade spboot-test ./spboot
方式二,打包后更新
helm upgrade [release-name] [pkgname]
栗子:helm upgrade spboot-test spboot-1.0.0.tgz
方式三,指定内容更新(注:这种更新方式会覆盖values.yaml的值,卸载再安装后还原)
helm upgrade spboot-test ./spboot --set sp.images=sp:3
有时候k8s的yaml文件有过多重复的地方,或者一些业务无关的声明,可以使用模板提取出来,简化yaml文件
4.1、定义模板(在_helpers.tpl中声明)
{{- define "container.define" -}}
containers:
- name: spboot
image: {{ .Values.sp.images }}
volumeMounts:
- mountPath: /cache
name: temp-volume
# port has name so service can use it
ports:
- containerPort: {{ .Values.sp.pod.port }}
name: {{ .Values.sp.pod.portname }}
volumes:
- name: temp-volume
emptyDir: {}
{{- end -}}
4.2 引用模板( deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: sp-dev-dep
spec:
selector:
matchLabels:
name: {{ .Values.sp.pod.label }}
replicas: {{ .Values.sp.pod.replicas }}
template:
metadata:
labels:
name: {{ .Values.sp.pod.label }}
spec:
{{ include "container.define" . | indent 6 }}