helm使用说明和实例
Helm 概述
helm:一个简化Kubernetes应用程序部署的软件包管理器。
几乎每种编程语言和操作系统都有自己的软件包管理器,以帮助安装和维护软件。Helm 提供了与许多您可能已经熟悉的软件包管理器(如 Debian 的 apt 或 Python 的 pip)相同的基本功能集。
Helm 可以:
- 安装软件。
- 自动安装软件依赖项。
- 升级软件。
- 配置软件部署。
- 从存储库获取软件包。
helm目录结构
chart是一个组织在文件目录中的集合。目录名称就是chart名称(没有版本信息)
mservice-charts/Chart.yaml # 包含了chart信息的YAML文件LICENSE # 可选: 包含chart许可证的纯文本文件README.md # 可选: 可读的README文件values.yaml # chart 默认的配置值values.schema.json # 可选: 一个使用JSON结构的values.yaml文件charts/ # 包含chart依赖的其他chartcrds/ # 自定义资源的定义templates/ # 模板目录, 当和values 结合时,可生成有效的Kubernetes manifest文件templates/NOTES.txt # 可选: 包含简要使用说明的纯文本文件templates/deployment.yamltemplates/service.yaml
mservice-charts是我们创建的一个charts。
deployment.yaml和service.yaml是通用的模板文件,将值抽取出来做成了变量。
values.yaml文件是默认加载的,其它的像dev-values.yaml文件需要根据选择用 -f 导入。
快速部署微服务到k8s
创建模板
包括三个文件:deployment.yaml、service.yaml、values.yaml
deployment.yaml: 对应k8s的deployment文件
service.yam:对应k8s的service
values:保存上面两个文件中的变量
文件内容如下:
deployment.yaml
apiVersion: apps/v1
kind: {{ .Values.kind }}
metadata:name: {{ .Values.name }}namespace: {{ .Values.namespace }}labels:app: {{ .Values.name }}
spec:revisionHistoryLimit: 3replicas:2selector:matchLabels:app: {{ .Values.name }}template:metadata:labels:app: {{ .Values.name }}spec:initContainers:- name: init-logsimage: busyboximagePullPolicy: IfNotPresentcommand:- sh- -c- |mkdir -p /var/log/leve/{{ if eq .Values.namespace "default" }}test{{ else }}dev{{ end }}/{{ .Values.name }}mkdir -p /opt/nfsvolumeMounts:- name: app-logsmountPath: /var/log/leve{{- if .Values.nfs }}- name: nfs-servicemountPath: /opt/nfs{{- end }}serviceAccountName: defaulttopologySpreadConstraints:- maxSkew: 1topologyKey: zonewhenUnsatisfiable: DoNotSchedulelabelSelector:matchLabels:app: {{ .Values.name }}volumes:- name: app-logshostPath:path: /var/log/leve{{- if eq .Values.namespace "default" }}- name: ssl-certsecret:secretName: keystore-secret{{- end }}{{- if .Values.nfs }}- name: nfs-servicenfs:path: /nfs_data/service/{{ .Values.namespace }}/{{ .Values.name }}server: nfs.leve.com{{- end }}containers:- name: {{ .Values.name }}image: nexus.lll.com:6000/{{ .Values.name }}:2.0.0-{{ .Values.version }}-SNAPSHOTimagePullPolicy: IfNotPresentvolumeMounts:- name: app-logsmountPath: /var/log/leve{{- if eq .Values.namespace "default" }}- name: ssl-certmountPath: /ssl/certreadOnly: true{{- end }}{{- if .Values.nfs }}- name: nfs-servicemountPath: /opt/nfs{{- end }}ports:- name: rest-apicontainerPort: {{ .Values.port }}hostPort: {{ .Values.port }}readinessProbe:httpGet:path: /actuator/health/readinessport: {{ .Values.port }}scheme: {{ if eq .Values.namespace "default" -}} HTTPS {{- else -}} HTTP {{- end }}periodSeconds: 5livenessProbe:httpGet:path: /actuator/health/livenessport: {{ .Values.port }}scheme: {{ if eq .Values.namespace "default" -}} HTTPS {{- else -}} HTTP {{- end }}periodSeconds: 5startupProbe:httpGet:path: /actuator/healthport: {{ .Values.port }}scheme: {{ if eq .Values.namespace "default" -}} HTTPS {{- else -}} HTTP {{- end }}initialDelaySeconds: 50periodSeconds: 5failureThreshold: 50resources:limits:cpu: '2'memory: {{ .Values.memory }}requests:cpu: '0.3'memory: {{ .Values.memory }}env:- name: SPRING_PROFILES_ACTIVEvalue: {{ if eq .Values.namespace "default" -}} "test" {{- else -}} "dev" {{- end }}- name: JAVA_TOOL_OPTIONSvalue: >-{{ if eq .Values.namespace "default" -}}-Xlog:gc*,age*=trace,gc+heap=trace,ref*=debug,safepoint:file=/var/log/leve/test/{{ .Values.name }}/gc-%p-%t.log:tags,uptime,time,level:filecount=2,filesize=50m{{- else -}}-Xlog:gc*,age*=trace,gc+heap=trace,ref*=debug,safepoint:file=/var/log/leve/dev/{{ .Values.name }}/gc-%p-%t.log:tags,uptime,time,level:filecount=2,filesize=50m{{- end }}
service.yaml
apiVersion: v1
kind: Service
metadata:name: {{ .Values.name }}namespace: {{ .Values.namespace }}labels:app: {{ .Values.name }}secured: {{ if eq .Values.namespace "default" }}'true'{{- else }}'false'{{- end }}
spec:type: NodePortselector:app: {{ .Values.name }}ports:- name: restport: {{ .Values.port }}targetPort: {{ .Values.port }}nodePort: {{ .Values.nodePort }}
values.yaml
# 命名空间,例如:dev
namespace:
# 服务名称,不要带下划线或者横杠,统一命名,例如:authghost,vanityfair
name:
# 服务端口号,例如:8118
port:
# k8s上面的service端口号,例如:31208
nodePort:
# 镜像版本号,可以在jenkins上面看 示例:12126-3
version:
# pod的内存大小 要带单位 示例:700Mi
memory: 700Mi
# 是否开启nfs挂载,示例:true
# 开启nfs挂载后,默认挂载nfs服务器的/nfs_data/service/<命名空间>/<服务名称>,k8s上面的pod的挂载路径为/opt/nfs
# nfs服务器上面的挂载路径要自己创建
nfs: false
# 默认为 Deployment 某些服务为 StatefulSet
kind: Deployment
部署微服务
修改values.yaml文件里的变量到对应的值,
运行下面的命令:
helm upgrade --install user /opt/mservice-charts -n dev -f /opt/mservice-charts/values.yaml
要是运行失败可以在后面添加参数 --debug 来显示日志
user:服务名字,helm通过这个名字来管理
更新微服务
已经部署的微服务实例,快速更新镜像版本:
helm upgrade user /opt/mservice-charts/ -n dev --reuse-values --set version=13583-1
user: 微服务名
version:values.yaml文件内定义的微服务版本号
删除微服务
helm delete -n dev user
helm常用命令
检查配置
helm lint ./leve-charts -f leve-charts/dev-valus.yaml --namespace dev --set name=user,port=8118,nodePort=31208
根据配置输出yaml
helm template dev-user ./leve-charts -f leve-charts/dev-valus.yaml --namespace dev --set name=user,port=8118,nodePort=31208
修改配置
helm upgrade dev-faces ./leve-charts -f leve-charts/dev-valus.yaml --namespace dev --set name=faces,port=8118,nodePort=31208
参考
命令详情:https://helm.sh/zh/docs/intro/using_helm/
charts:https://helm.sh/zh/docs/topics/charts/
helm模板的管道用法:https://helm.sh/zh/docs/chart_template_guide/control_structures/
文件下载:https://download.csdn.net/download/xgw1010/90963543