• 2.HelmTemplate Guidance and Enable Ingress for Blazor Server


    演示资源已提交k8s-helm-tpl - Repos (azure.com)

    目录

    前言

    Enable Ingress

    1. 更新values.yaml和ingress.yaml 

    2. Helm Upgrade

    ​3. SwitchHosts 访问

    Helm Template YAML

    Chart.yaml

    values.yaml 

    _helpers.tpl

    NOTES.txt


    前言

    书接上文,我们毫不夸张的演示了如何使用HelmTemplate 将一个新建的Blazor Server项目3分钟打包成Helm Chart并部署至k8s

    但并没有介绍helm template的具体细节,接下来我们为部署到Cluster的service开启ingress,并且看下helm template自动生成的YAML文件是如何工作的。

    Enable Ingress

    !首先需要确保你的cluster有可用的 ingress-nginx

    1. 更新values.yaml和ingress.yaml 

    Enable Ingress Commit 95e27417: Enable ingress - Repos (azure.com)

    简单修改下values.yaml和ingress.yaml 即可: 

    了解过Blazor Server 的朋友知道,它是在服务端使用对象状态图来记录页面状态,并根据传来的页面事件更新状态图,之后将变化部分通过SignalR推送至浏览器渲染响应。

    因此这里根据官方文档Blazor Server Kubernetes Deploy添加部分 annotations,以实现粘性会话。

    2. Helm Upgrade

     3. SwitchHosts 访问

    至此,一个Helm Chart所需的内容我们已基本实现。接下来,我们回头看下Helm Template为我们生成的文件都有什么作用。

    Helm Template YAML

    在Helm Template 使用一套简单易懂的语法规则,我们可以用来定义变量并引用;也可以通过.Values.xxx 来访问values.yaml中提供的配置参数;还可以使用range等关键字来访问一个数组变量等等。

    Helm Template同样会生成deployment.yaml, ingress.yaml等常规文件,但其中只有少量的直接定义,大多配置项定义在了以下几个yaml里,并通过特定的语法规则进行引用。

    Chart.yaml

    Chart.yaml包含了对Helm Chart的定义,例如chart name,description,version等。

    1. apiVersion: v2
    2. name: helm-tpl-test-blazor-demo
    3. description: A balzor demo helm chart for Kubernetes
    4. # A chart can be either an 'application' or a 'library' chart.
    5. #
    6. # Application charts are a collection of templates that can be packaged into versioned archives
    7. # to be deployed.
    8. #
    9. # Library charts provide useful utilities or functions for the chart developer. They're included as
    10. # a dependency of application charts to inject those utilities and functions into the rendering
    11. # pipeline. Library charts do not define any templates and therefore cannot be deployed.
    12. type: application
    13. # This is the chart version. This version number should be incremented each time you make changes
    14. # to the chart and its templates, including the app version.
    15. # Versions are expected to follow Semantic Versioning (https://semver.org/)
    16. version: 1.0.1
    17. # This is the version number of the application being deployed. This version number should be
    18. # incremented each time you make changes to the application. Versions are not expected to
    19. # follow Semantic Versioning. They should reflect the version the application is using.
    20. appVersion: 1.0.1

    values.yaml 

    Helm Template会为我们自动生成deployment,ingress,serviceaccount等模板,values.yaml中为它们所需要的配置参数提供统一的定义入口。

    例如我们前面演示用到的,image,imagePullSecrets,ingress configuration等等。

    我们也可以在其中定义一些deployment.yaml所需要的内容,以便创建或覆盖程序运行所需要的环境变量。

    1. # Default values for helm-tpl-test-blazor-demo.
    2. # This is a YAML-formatted file.
    3. # Declare variables to be passed into your templates.
    4. replicaCount: 2
    5. image:
    6. repository: docker.io/phone8848/blazor-demo
    7. pullPolicy: IfNotPresent
    8. # Overrides the image tag whose default is the chart appVersion.
    9. tag: "v1"
    10. imagePullSecrets:
    11. - name: phone8848-auth
    12. nameOverride: ""
    13. fullnameOverride: ""
    14. serviceAccount:
    15. # Specifies whether a service account should be created
    16. create: true
    17. # Annotations to add to the service account
    18. annotations: {}
    19. # The name of the service account to use.
    20. # If not set and create is true, a name is generated using the fullname template
    21. name: ""
    22. podAnnotations: {}
    23. podSecurityContext: {}
    24. # fsGroup: 2000
    25. service:
    26. type: ClusterIP
    27. port: 80
    28. ingress:
    29. enabled: true
    30. annotations:
    31. kubernetes.io/ingress.class: nginx
    32. nginx.ingress.kubernetes.io/affinity: "cookie"
    33. nginx.ingress.kubernetes.io/session-cookie-name: "affinity"
    34. nginx.ingress.kubernetes.io/session-cookie-expires: "14400"
    35. nginx.ingress.kubernetes.io/session-cookie-max-age: "14400"
    36. hosts:
    37. - host: k8s-helm-tpl-test.local
    38. paths:
    39. - /
    40. tls:
    41. - secretName: helm-demo-tls
    42. hosts:
    43. - k8s-helm-tpl-test.local
    44. resources: {}
    45. # We usually recommend not to specify default resources and to leave this as a conscious
    46. # choice for the user. This also increases chances charts run on environments with little
    47. # resources, such as Minikube. If you do want to specify resources, uncomment the following
    48. # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
    49. # limits:
    50. # cpu: 100m
    51. # memory: 128Mi
    52. # requests:
    53. # cpu: 100m
    54. # memory: 128Mi

    _helpers.tpl

    _helpers.tpl 主要用来定义各种name,deployment name, ingress name, labels name等等。

    1. {{/* vim: set filetype=mustache: */}}
    2. {{/*
    3. Expand the name of the chart.
    4. */}}
    5. {{- define "helm-tpl-test-blazor-demo.name" -}}
    6. {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
    7. {{- end }}
    8. {{/*
    9. Create a default fully qualified app name.
    10. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
    11. If release name contains chart name it will be used as a full name.
    12. */}}
    13. {{- define "helm-tpl-test-blazor-demo.fullname" -}}
    14. {{- if .Values.fullnameOverride }}
    15. {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
    16. {{- else }}
    17. {{- $name := default .Chart.Name .Values.nameOverride }}
    18. {{- if contains $name .Release.Name }}
    19. {{- .Release.Name | trunc 63 | trimSuffix "-" }}
    20. {{- else }}
    21. {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
    22. {{- end }}
    23. {{- end }}
    24. {{- end }}
    25. {{/*
    26. Create chart name and version as used by the chart label.
    27. */}}
    28. {{- define "helm-tpl-test-blazor-demo.chart" -}}
    29. {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
    30. {{- end }}
    31. {{/*
    32. Common labels
    33. */}}
    34. {{- define "helm-tpl-test-blazor-demo.labels" -}}
    35. helm.sh/chart: {{ include "helm-tpl-test-blazor-demo.chart" . }}
    36. {{ include "helm-tpl-test-blazor-demo.selectorLabels" . }}
    37. {{- if .Chart.AppVersion }}
    38. app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
    39. {{- end }}
    40. app.kubernetes.io/managed-by: {{ .Release.Service }}
    41. {{- end }}
    42. {{/*
    43. Selector labels
    44. */}}
    45. {{- define "helm-tpl-test-blazor-demo.selectorLabels" -}}
    46. app.kubernetes.io/name: {{ include "helm-tpl-test-blazor-demo.name" . }}
    47. app.kubernetes.io/instance: {{ .Release.Name }}
    48. {{- end }}
    49. {{/*
    50. Create the name of the service account to use
    51. */}}
    52. {{- define "helm-tpl-test-blazor-demo.serviceAccountName" -}}
    53. {{- if .Values.serviceAccount.create }}
    54. {{- default (include "helm-tpl-test-blazor-demo.fullname" .) .Values.serviceAccount.name }}
    55. {{- else }}
    56. {{- default "default" .Values.serviceAccount.name }}
    57. {{- end }}
    58. {{- end }}

    NOTES.txt

    也不要小瞧这个文件哦,每次我们helm install或者helm upgrade成功之后提示的Notes就来自于它。 

    1. 1. Get the application URL by running these commands:
    2. {{- if .Values.ingress.enabled }}
    3. {{- range $host := .Values.ingress.hosts }}
    4. {{- range .paths }}
    5. http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }}
    6. {{- end }}
    7. {{- end }}
    8. {{- else if contains "NodePort" .Values.service.type }}
    9. export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helm-tpl-test-blazor-demo.fullname" . }})
    10. export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
    11. echo http://$NODE_IP:$NODE_PORT
    12. {{- else if contains "LoadBalancer" .Values.service.type }}
    13. NOTE: It may take a few minutes for the LoadBalancer IP to be available.
    14. You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "helm-tpl-test-blazor-demo.fullname" . }}'
    15. export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helm-tpl-test-blazor-demo.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
    16. echo http://$SERVICE_IP:{{ .Values.service.port }}
    17. {{- else if contains "ClusterIP" .Values.service.type }}
    18. export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "helm-tpl-test-blazor-demo.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
    19. echo "Visit http://127.0.0.1:8080 to use your application"
    20. kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80
    21. {{- end }}

    Helm Template的大体结构与基本应用就是这样啦,是不是非常简单呢。 

  • 相关阅读:
    mysql数据库简介
    Go函数并发情况的错误处理
    【Unity3D】Unity 脚本 ④ ( 游戏物体 GameObject 的坐标 | 修改 游戏物体 GameObject 的本地坐标 )
    JS——手风琴|鼠标悬停图片滚动展示栏 [技术栈:html、css、JavaScript]
    网络GRE,MGRE
    【环境检查】linux环境检查:内存剩余、磁盘读写性能、hosts、innode数量、CPU等
    自动翻译 android/res/values/strings.xml
    LeetCode | 20. 有效的括号
    Revit中如何对项目对象设置透明及“构件元素上色”
    java框架-springmvc
  • 原文地址:https://blog.csdn.net/qq_40404477/article/details/126906877