美文网首页
helm 3 定制模板

helm 3 定制模板

作者: akka9 | 来源:发表于2020-04-09 22:54 被阅读0次

本文演示 helm create app 创建模板后,如何根据业务进行定制。

版本 helm v3.1.2

创建 conf 目录,存放项目的配置文件

mkdir -p conf

增加 namespace

默认的模板,没有 namespace 字段,需要在 configmap deployment ingress service 等地方增加

apiVersion: apps/v1
kind: Deployment
metadata:
    namespace: {{ .Release.Namespace }}

增加 configmap.yaml

cat templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ include "app.fullname" . }}
data:
{{ (.Files.Glob "conf/*.*").AsConfig | indent 2 }}
  #default.conf: {{.Files.Get "files/default.conf" | printf "%s" | indent 4}}

修改 Chart.yaml 中 name 字段

也可以不修改,可以使用 --set nameOverride=myapp 来覆盖

增加自动滚动发布

最后两行,增加 configmap 哈希校验,和 自动随机字符,每次发布都会自动滚动发布

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ include "helm-chart.fullname" . }}
  labels:
    {{- include "helm-chart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "helm-chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        rollme: {{ randAlphaNum 12 | upper | quote }}

个人习惯把所有的文件 template 合并成 一个 app.yaml

按照 configmap service ingress deployment 的顺序

测试模板

namespace=golang
app=myapp
helm template .  --namespace $namespace  \
  --name-template=$app  --set name=$app --set nameOverride=$app | less


app version 和 chart version 区别

app version 指的是 代码版本
chart version 指得是 发布版本

完整的 app.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: {{ .Release.Namespace }}
  name: cm-{{ include "app.fullname" . }}
data:
{{ (.Files.Glob "conf/*").AsConfig | indent 2 }}

---

apiVersion: v1
kind: Service
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ include "app.fullname" . }}
  labels:
    {{- include "app.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "app.selectorLabels" . | nindent 4 }}

---
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "app.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ $fullName }}
  labels:
    {{- include "app.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
        {{- range .paths }}
          - path: {{ . }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: {{ $svcPort }}
        {{- end }}
  {{- end }}
{{- end }}

---

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ include "app.fullname" . }}
  labels:
    {{- include "app.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "app.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        rollme: {{ randAlphaNum 12 | upper | quote }}
      labels:
        {{- include "app.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "app.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}

---


相关文章

网友评论

      本文标题:helm 3 定制模板

      本文链接:https://www.haomeiwen.com/subject/mwxkmhtx.html