2.Helm Template Ingress的使用

演示资源已提交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等。

apiVersion: v2
name: helm-tpl-test-blazor-demo
description: A balzor demo helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.0.1

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
appVersion: 1.0.1

values.yaml 

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

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

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

# Default values for helm-tpl-test-blazor-demo.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 2

image:
  repository: docker.io/phone8848/blazor-demo
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "v1"

imagePullSecrets: 
  - name: phone8848-auth
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""

podAnnotations: {}

podSecurityContext: {}
  # fsGroup: 2000

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "affinity"
    nginx.ingress.kubernetes.io/session-cookie-expires: "14400"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "14400"
  hosts:
    - host: k8s-helm-tpl-test.local
      paths: 
        - /
  tls: 
   - secretName: helm-demo-tls
     hosts:
       - k8s-helm-tpl-test.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

_helpers.tpl

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

{
   
   {/* vim: set filetype=mustache: */}}
{
   
   {/*
Expand the name of the chart.
*/}}
{
   
   {- define "helm-tpl-test-blazor-demo.name" -}}
{
   
   {- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{
   
   {- end }}

{
   
   {/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{
   
   {- define "helm-tpl-test-blazor-demo.fullname" -}}
{
   
   {- if .Values.fullnameOverride }}
{
   
   {- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{
   
   {- else }}
{
   
   {- $name := default .Chart.Name .Values.nameOverride }}
{
   
   {- if contains $name .Release.Name }}
{
   
   {- .Release.Name | trunc 63 | trimSuffix "-" }}
{
   
   {- else }}
{
   
   {- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{
   
   {- end }}
{
   
   {- end }}
{
   
   {- end }}

{
   
   {/*
Create chart name and version as used by the chart label.
*/}}
{
   
   {- define "helm-tpl-test-blazor-demo.chart" -}}
{
   
   {- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{
   
   {- end }}

{
   
   {/*
Common labels
*/}}
{
   
   {- define "helm-tpl-test-blazor-demo.labels" -}}
helm.sh/chart: {
   
   { include "helm-tpl-test-blazor-demo.chart" . }}
{
   
   { include "helm-tpl-test-blazor-demo.selectorLabels" . }}
{
   
   {- if .Chart.AppVersion }}
app.kubernetes.io/version: {
   
   { .Chart.AppVersion | quote }}
{
   
   {- end }}
app.kubernetes.io/managed-by: {
   
   { .Release.Service }}
{
   
   {- end }}

{
   
   {/*
Selector labels
*/}}
{
   
   {- define "helm-tpl-test-blazor-demo.selectorLabels" -}}
app.kubernetes.io/name: {
   
   { include "helm-tpl-test-blazor-demo.name" . }}
app.kubernetes.io/instance: {
   
   { .Release.Name }}
{
   
   {- end }}

{
   
   {/*
Create the name of the service account to use
*/}}
{
   
   {- define "helm-tpl-test-blazor-demo.serviceAccountName" -}}
{
   
   {- if .Values.serviceAccount.create }}
{
   
   {- default (include "helm-tpl-test-blazor-demo.fullname" .) .Values.serviceAccount.name }}
{
   
   {- else }}
{
   
   {- default "default" .Values.serviceAccount.name }}
{
   
   {- end }}
{
   
   {- end }}

NOTES.txt

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

1. Get the application URL by running these commands:
{
   
   {- if .Values.ingress.enabled }}
{
   
   {- range $host := .Values.ingress.hosts }}
  {
   
   {- range .paths }}
  http{
   
   { if $.Values.ingress.tls }}s{
   
   { end }}://{
   
   { $host.host }}{
   
   { . }}
  {
   
   {- end }}
{
   
   {- end }}
{
   
   {- else if contains "NodePort" .Values.service.type }}
  export NODE_PORT=$(kubectl get --namespace {
   
   { .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {
   
   { include "helm-tpl-test-blazor-demo.fullname" . }})
  export NODE_IP=$(kubectl get nodes --namespace {
   
   { .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT
{
   
   {- else if contains "LoadBalancer" .Values.service.type }}
     NOTE: It may take a few minutes for the LoadBalancer IP to be available.
           You can watch the status of by running 'kubectl get --namespace {
   
   { .Release.Namespace }} svc -w {
   
   { include "helm-tpl-test-blazor-demo.fullname" . }}'
  export SERVICE_IP=$(kubectl get svc --namespace {
   
   { .Release.Namespace }} {
   
   { include "helm-tpl-test-blazor-demo.fullname" . }} --template "{
   
   {"{
   
   { range (index .status.loadBalancer.ingress 0) }}{
   
   {.}}{
   
   { end }}"}}")
  echo http://$SERVICE_IP:{
   
   { .Values.service.port }}
{
   
   {- else if contains "ClusterIP" .Values.service.type }}
  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}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace {
   
   { .Release.Namespace }} port-forward $POD_NAME 8080:80
{
   
   {- end }}

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

猜你喜欢

转载自blog.csdn.net/qq_40404477/article/details/126906877