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依赖的其他chart
  crds/               # 自定义资源的定义
  templates/          # 模板目录, 当和values 结合时,可生成有效的Kubernetes manifest文件
  templates/NOTES.txt # 可选: 包含简要使用说明的纯文本文件
  templates/deployment.yaml
  templates/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: 3
 replicas:2
 selector:
   matchLabels:
     app: {{ .Values.name }}
 template:
   metadata:
     labels:
       app: {{ .Values.name }}
   spec:
     initContainers:
       - name: init-logs
         image: busybox
         imagePullPolicy: IfNotPresent
         command:
           - sh
           - -c
           - |
             mkdir -p /var/log/leve/{{ if eq .Values.namespace "default" }}test{{ else }}dev{{ end }}/{{ .Values.name }}
             mkdir -p /opt/nfs
         volumeMounts:
           - name: app-logs
             mountPath: /var/log/leve
           {{- if .Values.nfs }}
           - name: nfs-service
             mountPath: /opt/nfs
           {{- end }}
     serviceAccountName: default
     topologySpreadConstraints:
       - maxSkew: 1
         topologyKey: zone
         whenUnsatisfiable: DoNotSchedule
         labelSelector:
           matchLabels:
             app: {{ .Values.name }}
     volumes:
       - name: app-logs
         hostPath:
           path: /var/log/leve
       {{- if eq .Values.namespace "default" }}
       - name: ssl-cert
         secret:
           secretName: keystore-secret
       {{- end }}
       {{- if .Values.nfs }}
       - name: nfs-service
         nfs:
           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 }}-SNAPSHOT
         imagePullPolicy: IfNotPresent
         volumeMounts:
           - name: app-logs
             mountPath: /var/log/leve
           {{- if eq .Values.namespace "default" }}
           - name: ssl-cert
             mountPath: /ssl/cert
             readOnly: true
           {{- end }}
           {{- if .Values.nfs }}
           - name: nfs-service
             mountPath: /opt/nfs
           {{- end }}
         ports:
           - name: rest-api
             containerPort: {{ .Values.port }}
             hostPort: {{ .Values.port }}
         readinessProbe:
           httpGet:
             path: /actuator/health/readiness
             port: {{ .Values.port }}
             scheme: {{ if eq .Values.namespace "default" -}} HTTPS {{- else -}} HTTP {{- end }}
           periodSeconds: 5
         livenessProbe:
           httpGet:
             path: /actuator/health/liveness
             port: {{ .Values.port }}
             scheme: {{ if eq .Values.namespace "default" -}} HTTPS {{- else -}} HTTP {{- end }}
           periodSeconds: 5
         startupProbe:
           httpGet:
             path: /actuator/health
             port: {{ .Values.port }}
             scheme: {{ if eq .Values.namespace "default" -}} HTTPS {{- else -}} HTTP {{- end }}
           initialDelaySeconds: 50
           periodSeconds: 5
           failureThreshold: 50
         resources:
           limits:
             cpu: '2'
             memory: {{ .Values.memory }}
           requests:
             cpu: '0.3'
             memory: {{ .Values.memory }}
         env:
           - name: SPRING_PROFILES_ACTIVE
             value: {{ if eq .Values.namespace "default" -}} "test" {{- else -}} "dev" {{- end }}
           - name: JAVA_TOOL_OPTIONS
             value: >-
               {{ 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: NodePort
  selector:
    app: {{ .Values.name }}
  ports:
    - name: rest
      port: {{ .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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值