25.B站薪享宏福笔记——第十章(1)k8s HELM

10 k8s HELM && Ingress

                                                                                          ——— Kubernetes 中的类 Yum 工具

10.1 HELM 概念

                                          ——— 基于他,可以快速部署

10.1.1 什么是 HELM

  在没使用 helm 之前,向 kubernetes 部署应用,我们要依此部署 deployment,svc 等,步骤较繁琐。况且随着很多项目微服务化,复杂的应用在容器中部署以及管理显得较为复杂,helm 通过打包的方式,支持发布的版本管理和控制,很大程度上简化了 Kubernetes 应用的部署和管理

  Helm 本质就是让 K8s 的应用管理(Deployment,Service 等)可配置,能 动态生成。通过动态生成 K8s 资源清单文件(deployment.yaml,service.yaml)(将可以修改的内容抽取到变量,基于修改变量的方式动态生成资源清单),然后调用 kubectl 自动执行 K8s 资源部署

10.1.2 HELM 重要组件

Helm 是官方提供的类似于 YUM 的包管理器,是部署环境的流程封装。Helm 有两个重要的概念:Chart 和 Release
  Chart:是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板、参数定义、依赖关系、文档说明等。chart 是应用部署的自包含逻辑单元。可以将 chart 想象成 apt、yum 中的软件安装包(类比镜像)
  Release:是 chart 的运行实例,代表了一个正在运行的应用。当 chart 被安装到 Kubernetes 集群,就生成一个 realease。chart 能够多次安装到同一个集群,每次安装都是一个 release(类比容器)
  Helm cli:helm 客户端组件,负责和 kubernetes apiS 通信
  Repository:用于发布和存储 Chart 的仓库(类比镜像仓库)

10.1.3 组件结构

Helm 诞生之初,RBAC 还没有完整的被应用在各个环境中

Helm v2版本设计理念:
  1.资源创建主要和 Kube-apiServer 进行通信,v2 版本是以 Pod 运行一个 Tiller 组件
  2.在 RBAC 之前,所有的权限都需要绑定在 Kubernetes集群内部拥有的实体上(即需要权限的附着点,v2版本的 Tiller)
  3.Helm-Client 通过 gRPC 协议将数据传输给 tiller,tiller 因为了绑定在了集群内,所以有权限,可以调用 kube-apiServer 接口实现对应资源对象的创建
Helm v3版本设计理念:
  1.当拥有 RBAC 后,所有访问都可以通过 kubeconfig 进行
  2.Helm-Client 通过 kubeconfig 既可发起基于 HTTPS协议 对 Kube-apiServer 的调用

10.1.4 v2历史包袱、v3优势

  Helm v2 是 C/S 架构,主要分为客户端 helm 和服务端 Tiller。而由于 RBAC 等权限控制体系的逐渐完善,多租户和安全的需求日益兴起,Tiller 变得越来越不安全(Tiller需要具备完全的管理员控制权限,被攻击后集群将沦陷),社区在权限控制领域遇到了极大的阻碍。所以在 Helm v3 版本中,直接将 Tiller 这一核心组件移除,Helm 直接和 kubernetes API 进行通信。直接带来如下好处:
  1.Helm 的架构变的更为简单和灵活(不需要了解所有字段,只需要知道抽象出来的字段意思既可)
  2.不再需要创建 ServiceAccount ,直接使用当前环境中 kubeconfig 配置(Helm 需要调用 kubeconfig,可以防止权限溢出,用户没权限,Helm也没权限)
  3.可以直接和 kubenetes API 交互,更为安全(双向认证 HTTPS协议)
  4.不再需要使用 helm init 来进行初始化(Helm v2版本需先部署客户端,再初始化部署集群内部服务端 Tiller 的 Pod)

10.2 HELM 安装及演示

                                                         ——— 安装后演示跑通环节

10.2.1 HELM 安装

(1)安装

# 上传压缩包
[root@k8s-master01 10]# rz -E
rz waiting to receive.
[root@k8s-master01 10]# ls -ltr
total 15656
-rw-r--r-- 1 root root 16028423 Aug 21  2024 helm-v3.12.3-linux-amd64.tar.gz
# 解压和配置环境变量
[root@k8s-master01 10]# tar -xvf helm-v3.12.3-linux-amd64.tar.gz 
linux-amd64/
linux-amd64/LICENSE
linux-amd64/README.md
linux-amd64/helm
[root@k8s-master01 10]# ls -ltr
total 15656
drwxr-xr-x 2 1001 docker       50 Aug 11  2023 linux-amd64
-rw-r--r-- 1 root root   16028423 Aug 21  2024 helm-v3.12.3-linux-amd64.tar.gz
[root@k8s-master01 10]# cp linux-amd64/helm /usr/local/bin/
[root@k8s-master01 10]# chmod +x /usr/local/bin/helm 
# 确定安装完成 [root@k8s
-master01 10]# helm version version.BuildInfo{Version:"v3.12.3", GitCommit:"3a31588ad33fe3b89af5a2a54ee1d25bfe6eaa5e", GitTreeState:"clean", GoVersion:"go1.20.7"} # Helm 默认复用当前用户的 .kube/config RBAC认证权限,所以如果 config 没有权限,helm 也没有对应操作权限 [root@k8s-master01 10]# ll /root/.kube/config -rw------- 1 root root 5657 May 28 02:36 /root/.kube/config

(2)初始化

# 初始化,添加 helm 仓库(v3版本默认没有添加仓库),类似 yum 仓库,安装命令时,获取 yum 仓库带的 rpm 包地址
[root@k8s-master01 10]# helm repo ls
Error: no repositories to show
# 添加仓库,仓库名,仓库地址
[root@k8s-master01 10]# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
[root@k8s-master01 10]# helm repo ls
NAME       URL                               
bitnami    https://charts.bitnami.com/bitnami
# 查询 名为 bitnami 仓库可用的 charts,NAME:chart 名、CHART VERSION:helm封装的chart版本、APP VERSION:基于的应用app版本、DESCRIPTION:描述信息
[root@k8s-master01 10]# helm search repo bitnami
NAME                                            CHART VERSION    APP VERSION      DESCRIPTION                                       
bitnami/airflow                                 24.2.0           3.0.2            Apache Airflow is a tool to express and execute...
bitnami/apache                                  11.3.18          2.4.63           Apache HTTP Server is an open-source HTTP serve...
bitnami/apisix                                  5.0.4            3.13.0           Apache APISIX is high-performance, real-time AP...
bitnami/appsmith                                6.0.12           1.78.0           Appsmith is an open source platform for buildin...
..........
# Helm 会添加缓存文件到 .cache 目录下,bitnami-charts.txt 记录了 chart 名称,bitnami-index.yaml 是 chart 元数据信息(包括版本、依赖、描述等)
[root@k8s-master01 10]# du -sh /root/.cache/helm/repository/*
4.0K    /root/.cache/helm/repository/bitnami-charts.txt
24M    /root/.cache/helm/repository/bitnami-index.yaml

(3)常用命令

# 更新仓库,类似 yum makecache(从官方更新最新的chart包版本)
helm repo update
# 显示仓库 bitnami 名下 apache 服务的 chart 变量,可以通过修改变量,部署时修改变量,按照修改变量的值进行部署生效
helm show values bitnami/apache
# 安装 bitnami 仓库下 apache chart ,自动生成实例名(同一名称空间下实例名不能重复)
helm install bitnami/apache --generate-name
# chart 的基本信息
helm show chart bitnami/apache
# chart 的所有信息
helm show all bitnami/apache

10.2.2 Helm 演示

bitnami 中的 apache 的 helm 包 ,导入后先把后缀 .zip 去掉再解压:apache-11.3.13.tgz.zip

# 由于上面添加了 bitnami 仓库但是安装网络不好,所以又添加了 aliyun 的 helm 仓库(没卵用,k8s 的 1.29版本里面很多接口不适配最新的阿里云 helm)
[root@k8s-master01 10]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" already exists with the same configuration, skipping
[root@k8s-master01 10]# helm repo ls
NAME       URL                                                   
bitnami    https://charts.bitnami.com/bitnami                    
aliyun     https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@k8s-master01 10]# helm search repo aliyun
NAME                              CHART VERSION    APP VERSION      DESCRIPTION                                       
..........
aliyun/prometheus                 5.4.0                             Prometheus is a monitoring system and time seri...
aliyun/nginx-ingress              0.9.5            0.10.2           An nginx Ingress controller that uses ConfigMap...
.........
[root@k8s-master01 10]# rz

[root@k8s-master01 10]# ls -ltr
total 48
-rw-r--r-- 1 root root 46077 Jul  7 02:35 apache-11.3.13.tgz  
[root@k8s-master01 10]# tar -xvf apache-11.3.13.tgz 
# 使用本地化安装,与演示 helm install bitnami/apache --generate-name 一样,只是远程网址下不下来
[root@k8s-master01 10]# helm install ./apache --generate-name
NAME: apache-1751860970
LAST DEPLOYED: Mon Jul  7 12:02:50 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: apache
CHART VERSION: 11.3.13
APP VERSION: 2.4.63

Did you know there are enterprise versions of the Bitnami catalog? For enhanced secure software supply chain features, unlimited pulls from Docker, LTS support, or application customization, see Bitnami Premium or Tanzu Application Catalog. See https://www.arrow.com/globalecs/na/vendors/bitnami for more information.

** Please be patient while the chart is being deployed **

1. Get the Apache URL by running:

** Please ensure an external IP is associated to the apache-1751860970 service before proceeding **
** Watch the status using: kubectl get svc --namespace default -w apache-1751860970 **

  export SERVICE_IP=$(kubectl get svc --namespace default apache-1751860970 --template "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}")
  echo URL            : http://$SERVICE_IP/


WARNING: You did not provide a custom web application. Apache will be deployed with a default page. Check the README section "Deploying your custom web application" in https://github.com/bitnami/charts/blob/main/bitnami/apache/README.md#deploying-a-custom-web-application.



WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
  - resources
+info https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
# 有 REVISION 可以支持版本回滚
[root@k8s-master01 10]# helm list -n default
NAME                 NAMESPACE    REVISION    UPDATED                                    STATUS      CHART             APP VERSION
apache-1751860970    default      1           2025-07-07 12:02:50.716013636 +0800 CST    deployed    apache-11.3.13    2.4.63 
[root@k8s-master01 10]# kubectl get svc
NAME                TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)                      AGE
apache-1751860970   LoadBalancer   10.6.214.88   <pending>     80:32209/TCP,443:30530/TCP   4h8m
kubernetes          ClusterIP      10.0.0.1      <none>        443/TCP                      40d
[root@k8s-master01 10]# kubectl get pod
NAME                                 READY   STATUS    RESTARTS   AGE
apache-1751860970-698d74966c-mdzg4   1/1     Running   0          4h9m
# helm 卸载,将所有资源一并卸载
[root@k8s-master01 10]# helm uninstall apache-1751860970
release "apache-1751860970" uninstalled
[root@k8s-master01 10]# helm list --all
NAME    NAMESPACE    REVISION    UPDATED    STATUS    CHART    APP VERSION
[root@k8s-master01 10]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   40d
# helm status 会将 install 安装的日志再次输出一遍
[root@k8s-master01 10]# helm install ./apache --generate-name
NAME: apache-1751876634
LAST DEPLOYED: Mon Jul  7 16:23:54 2025
.........
[root@k8s-master01 10]# helm list
NAME                 NAMESPACE    REVISION    UPDATED                                    STATUS      CHART             APP VERSION
apache-1751876634    default      1           2025-07-07 16:23:54.737805867 +0800 CST    deployed    apache-11.3.13    2.4.63     
[root@k8s-master01 10]# helm status apache-1751876634
NAME: apache-1751876634
LAST DEPLOYED: Mon Jul  7 16:23:54 2025
.........

10.2.3 helm 三大概念

Chart 代表着 Helm 包,它包含在 Kubernetes 集群内部运行应用程序、工具或服务所需的所有资源定义。可以看作 小米应用商店、yum rpm 在 Kubernetes 中的等价物

(1)helm search:查找 Charts

# 可以本地仓库搜索,也可以通过官方仓库(https://hub.helm.sh),或 URL 地址搜索
[root@k8s-master01 10]# helm search repo wordpress
NAME                       CHART VERSION    APP VERSION    DESCRIPTION                                       
aliyun/wordpress           0.8.8            4.9.4          Web publishing platform for building blogs and ...
bitnami/wordpress          25.0.0           6.8.1          WordPress is the world's most popular blogging ...
bitnami/wordpress-intel    2.1.31           6.1.1          DEPRECATED WordPress for Intel is the most popu...
[root@k8s-master01 10]# helm search hub wordpress
URL                                                   CHART VERSION    APP VERSION            DESCRIPTION                                       
https://artifacthub.io/packages/helm/kube-wordp...    0.1.0            1.1                    this is my wordpress package                      
https://artifacthub.io/packages/helm/wordpress-...    1.0.2            1.0.0                  A Helm chart for deploying Wordpress+Mariadb st...
.........
# helm 仓库的本地地址
[root@k8s-master01 10]# cat ~/.config/helm/repositories.yaml 
apiVersion: ""
generated: "0001-01-01T00:00:00Z"
repositories:
- caFile: ""
  certFile: ""
  insecure_skip_tls_verify: false
  keyFile: ""
  name: bitnami
  pass_credentials_all: false
  password: ""
  url: https://charts.bitnami.com/bitnami
  username: ""
- caFile: ""
  certFile: ""
  insecure_skip_tls_verify: false
  keyFile: ""
  name: aliyun
  pass_credentials_all: false
  password: ""
  url: https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
  username: ""

(2)变量修改抽象values

# 查看当前 apache 所有可以更改的 values 选项 值
[root@k8s-master01 10]# helm show values apache/ |grep -A 10 '## Apache service parameters' 
## Apache service parameters
##
service:
  ## @param service.type Apache Service type
  ##
  type: LoadBalancer
  ## @param service.ports.http Apache service HTTP port
  ## @param service.ports.https Apache service HTTPS port
  ##
  ports:
    http: 80
[root@k8s-master01 10]# kubectl get svc
NAME                TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
apache-1751876634   LoadBalancer   10.11.22.166   <pending>     80:31459/TCP,443:31580/TCP   29m
kubernetes          ClusterIP      10.0.0.1       <none>        443/TCP                      40d
[root@k8s-master01 10]# helm uninstall apache-1751876634
release "apache-1751876634" uninstalled
[root@k8s-master01 10]# helm list
NAME    NAMESPACE    REVISION    UPDATED    STATUS    CHART    APP VERSION
[root@k8s-master01 10]# cat 1.values.yaml 
service:
  type: NodePort
[root@k8s-master01 10]# helm install -f 1.values.yaml apache --generate-name
NAME: apache-1751878571
LAST DEPLOYED: Mon Jul  7 16:56:12 2025
.........
[root@k8s-master01 10]# helm ls
NAME                 NAMESPACE    REVISION    UPDATED                                    STATUS      CHART             APP VERSION
apache-1751878571    default      1           2025-07-07 16:56:12.027241088 +0800 CST    deployed    apache-11.3.13    2.4.63     
[root@k8s-master01 10]# kubectl get svc
NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                      AGE
apache-1751878571   NodePort    10.1.187.15   <none>        80:31768/TCP,443:31167/TCP   18s
kubernetes          ClusterIP   10.0.0.1      <none>        443/TCP                      40d

通过服务器 IP + 端口可以访问:

(3)传参方式

安装过程中有两种方式传递配置数据

-- values (或 -f ):使用 YAML 文件覆盖配置,可以指定多次,优先使用最右边的文件

-- set :通过命令行的方式对指定项进行覆盖

如果同时使用两种方式,则 -- set 中的值会被合并到 -- values 中,但是 -- set 中的值优先级更高。在 --set 中覆盖的内容会被保存在 ConfigMap 中,可以通过 helm get values <release-name> 来查看指定 release 中 --set 设置的值,也可以通过运行 helm upgrade 并指定 --reset-values 字段来清除 --set 中设置的值(改动多,使用 values 文件,改动少可以使用命令行 -- set)

-- set 的格式和限制

-- set 选项使用 0 或 多个 name/value 对,最简单的用法类似于:--set name=value ,等价于 name: value 格式

(4)helm upgrade 和 helm rollback:升级 release 和失败时恢复

当想升级到 chart 的新版本或是修改 release 的配置,可以使用 helm upgrade 命令,helm 会尝试执行最小浸入式升级,即只会更新自上次发布以来发生了更改的内容
[root@k8s-master01 10]# kubectl get svc
NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                      AGE
apache-1751878571   NodePort    10.1.187.15   <none>        80:31768/TCP,443:31167/TCP   4h16m
kubernetes          ClusterIP   10.0.0.1      <none>        443/TCP                      40d
[root@k8s-master01 10]# cat 2.values.yaml 
service:
  type: ClusterIP
[root@k8s-master01 10]# helm upgrade -f 2.values.yaml apache-1751878571 apache
Release "apache-1751878571" has been upgraded. Happy Helming!
NAME: apache-1751878571
LAST DEPLOYED: Mon Jul  7 21:13:26 2025
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
NOTES:
CHART NAME: apache
CHART VERSION: 11.3.13
APP VERSION: 2.4.63

Did you know there are enterprise versions of the Bitnami catalog? For enhanced secure software supply chain features, unlimited pulls from Docker, LTS support, or application customization, see Bitnami Premium or Tanzu Application Catalog. See https://www.arrow.com/globalecs/na/vendors/bitnami for more information.

** Please be patient while the chart is being deployed **

1. Get the Apache URL by running:
  kubectl port-forward --namespace default svc/apache-1751878571 8080:80
  echo URL            : http://127.0.0.1:8080/


WARNING: You did not provide a custom web application. Apache will be deployed with a default page. Check the README section "Deploying your custom web application" in https://github.com/bitnami/charts/blob/main/bitnami/apache/README.md#deploying-a-custom-web-application.



WARNING: There are "resources" sections in the chart not set. Using "resourcesPreset" is not recommended for production. For production installations, please set the following values according to your workload needs:
  - resources
+info https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
[root@k8s-master01 10]# kubectl get svc
NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
apache-1751878571   ClusterIP   10.1.187.15   <none>        80/TCP,443/TCP   4h17m
kubernetes          ClusterIP   10.0.0.1      <none>        443/TCP          40d
[root@k8s-master01 10]# helm get values apache-1751878571
USER-SUPPLIED VALUES:
service:
  type: ClusterIP

(5)升级与回滚

假如在一次发布过程中,发生了不符合预期的事情,也很容易通过 helm rollback [RELEASE] [REVISAION] 命令回滚到之前的发布版本
[root@k8s-master01 10]# helm list
NAME                 NAMESPACE    REVISION    UPDATED                                    STATUS      CHART             APP VERSION
apache-1751878571    default      2           2025-07-07 21:13:26.514528945 +0800 CST    deployed    apache-11.3.13    2.4.63     
[root@k8s-master01 10]# helm history apache-1751878571
REVISION    UPDATED                     STATUS        CHART             APP VERSION    DESCRIPTION     
1           Mon Jul  7 16:56:12 2025    superseded    apache-11.3.13    2.4.63         Install complete
2           Mon Jul  7 21:13:26 2025    deployed      apache-11.3.13    2.4.63         Upgrade complete
[root@k8s-master01 10]# helm rollback apache-1751878571 1
Rollback was a success! Happy Helming!
[root@k8s-master01 10]# kubectl get svc
NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                      AGE
apache-1751878571   NodePort    10.1.187.15   <none>        80:31654/TCP,443:31454/TCP   4h48m
kubernetes          ClusterIP   10.0.0.1      <none>        443/TCP                      40d

上面命令将 apache-1751878571 回滚到了最初版本。release 版本其实是一个增量修订(revision)。每当发生了一次安装、升级、或回滚操作,revision 的值就会加1,第一次 revision 的值永远是1,可以使用 helm history [RELEASE] 命令来查看一个特定的 release 的修订版本号

安装、升级、回滚时选项:
  --timeout:一个 Go duration(go语言模块) 类型的值, 用来表示等待 Kubernetes 命令完成的超时时间,默认值为 5m0s。such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"。
   --wait:表示必须要等到所有的 Pods 都处于 ready 状态,PVC 都被绑定,Deployments 都至少拥有最小 ready 状态 Pods 个数(Desired减去 maxUnavailable),并且 Services 都具有 IP 地址 (如果是LoadBalancer, 则为 Ingress),才会标记该 release 为成功(卡住终端输入端口)。最长等待时间由 -- timeout 值指定。如果达到超时时间,release 将被标记为 FAILED。注意:当 Deployment 的 replicas 被设置为1,但其滚动升级策略中的 maxUnavailable 没有被设置为0时,--wait 将返回就绪,因为已经满足了最小 ready Pod 数
  --no-hooks :不运行当前命令的钩子,即为安装此 chart 时的已定义的安装前或者安装后的动作
  --recreate-pods:(仅适用于 upgrade 和 rollback ):这个参数会导致重建所有的 Pod(deployment中的Pod 除外)。(在 Helm 3 中已被废弃)

(6)helm uninstall:卸载 release

# 使用 helm uninstall 命令从集群中卸载一个 release
$ helm uninstall apache-23213213

Helm v2 版本中,当一个 release 被删除,会保留一条删除记录。而在 Helm v3 中,删除也会移除 release 的记录。 如果想保留删除记录,使用 helm uninstall --keep-history 。使用 helm list --uninstalled 只会展示使用了 --keep-history 删除的 release, helm list --all 会展示 Helm 保留的所有 release 记录,包括失败或删除的条目(指定了 --keephistory )

(7)helm repo:使用仓库

$ helm repo list      # 查看配置的仓库,v3 版本已经不再默认添加一个仓库
$ helm repo add dev https://example.com/dev-charts # 添加新的仓库
$ helm repo update    # 可以通过执行 helm repo update 命令来确保你的 Helm 客户端是最新的
$ helm repo remove    # 移除仓库

10.2.4 创建 helm 仓库

# 创建 helm ,出现 myapp 的目录
[root@k8s-master01 10]# helm create myapp
Creating myapp
[root@k8s-master01 10]# ls
1.values.yaml  2.values.yaml  apache  myapp
[root@k8s-master01 10]# cd myapp/
# Chart.yaml 是描述信息等、charts 是依赖的其他charts 包、templates 是所有资源清单文件、values.yaml 是能够抽象出来用户可以改变的选项变量
[root@k8s-master01 myapp]# ls
Chart.yaml  charts  templates  values.yaml
[root@k8s-master01 myapp]# rm -rf values.yaml templates/*
[root@k8s-master01 myapp]# ls
Chart.yaml  charts  templates
[root@k8s-master01 myapp]# cat templates/nodePort.yaml 
apiVersion: v1
kind: Service
metadata:
 labels:
   app: myapp-test
 name: myapp-test-202401110926-svc
spec:
 ports:
 - name: 80-80
   port: 80
   protocol: TCP
   targetPort: 80
   nodePort: 30001
 selector:
   app: myapp-test
 type: NodePort
[root@k8s-master01 myapp]# cat templates/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
 labels:
   app: myapp-test
 name: myapp-test-202401110926-deploy
spec:
 replicas: 5
 selector:
   matchLabels:
     app: myapp-test
 template:
   metadata:
     labels:
       app: myapp-test
   spec:
     containers:
     - image: myapp:v1.0
       name: myapp
[root@k8s-master01 myapp]# helm install myapp ../myapp/
NAME: myapp
LAST DEPLOYED: Mon Jul  7 22:49:24 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s-master01 myapp]# helm list
NAME     NAMESPACE    REVISION    UPDATED                                    STATUS      CHART          APP VERSION
myapp    default      1           2025-07-07 22:49:24.957330824 +0800 CST    deployed    myapp-0.1.0    1.16.0     
[root@k8s-master01 myapp]# kubectl get pod
NAME                                             READY   STATUS    RESTARTS   AGE
myapp-test-202401110926-deploy-745fdf4d8-55b9w   1/1     Running   0          87s
myapp-test-202401110926-deploy-745fdf4d8-9phh6   1/1     Running   0          87s
myapp-test-202401110926-deploy-745fdf4d8-hz4l4   1/1     Running   0          87s
myapp-test-202401110926-deploy-745fdf4d8-kpl8f   1/1     Running   0          87s
myapp-test-202401110926-deploy-745fdf4d8-vgs9l   1/1     Running   0          87s
[root@k8s-master01 myapp]# kubectl get svc
NAME                          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes                    ClusterIP   10.0.0.1        <none>        443/TCP        40d
myapp-test-202401110926-svc   NodePort    10.14.193.213   <none>        80:30001/TCP   99s

10.2.5 注入 HELM 灵魂

helm 函数查询网站:https://helm.sh/zh/docs/chart_template_guide/function_list/ 
# 安装完成后的通知模板,时间是 go语言 显示
[root@k8s-master01 myapp]# cat templates/NOTES.txt 
1、这是一个测试的 myapp chart
2、myapp release 名字:myapp-test-{{ now | date "20060102030405" }}-deploy
3、service 名字:myapp-test-{{ now | date "20060102030405" }}-svc
# 将 副本数、镜像名、镜像版本通过函数定义
[root@k8s-master01 myapp]# cat templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app: myapp-test name: myapp-test-{{ now | date "20060102030405" }}-deploy spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app: myapp-test template: metadata: labels: app: myapp-test spec: containers: - image: {{ .Values.image.repository }}:{{ .Values.image.tag }} name: myapp
# quote 将英文首字母大写,假如判断,如果 svc 是 NodePort 类型,什么也不做,加入了判断
[root@k8s-master01 myapp]# cat templates/service.yaml apiVersion: v1 kind: Service metadata: labels: app: myapp-test name: myapp-test-{{ now | date "20060102030405" }}-svc spec: ports: - name: 80-80 port: 80 protocol: TCP targetPort: 80 {{- if eq .Values.service.type "NodePort" }} nodePort: {{.Values.service.nodeport }} {{- end }} selector: app: myapp-test type: {{ .Values.service.type | quote }}
# 将其他 yaml 文件中的 变量,通过 values.yaml 文件中内容赋值
[root@k8s-master01 myapp]# cat values.yaml 
# Default values for myapp.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 5
image:
 repository: myapp
 tag: "v1.0"
service:
 type: NodePort
 nodeport: 32321
[root@k8s-master01 myapp]# helm install myapp ../myapp/ -f values.yaml 
NAME: myapp
LAST DEPLOYED: Mon Jul  7 23:33:54 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
1、这是一个测试的 myapp chart
2、myapp release 名字:myapp-test-20250707113354-deploy
3、service 名字:myapp-test-20250707113354-svc
[root@k8s-master01 myapp]# helm list
NAME     NAMESPACE    REVISION    UPDATED                                    STATUS      CHART          APP VERSION
myapp    default      1           2025-07-07 23:33:54.089629314 +0800 CST    deployed    myapp-0.1.0    1.16.0     
[root@k8s-master01 myapp]# kubectl get svc
NAME                            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes                      ClusterIP   10.0.0.1       <none>        443/TCP        40d
myapp-test-20250707113354-svc   NodePort    10.14.213.42   <none>        80:32321/TCP   29s

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2025-06-04 22:47  马俊南  阅读(115)  评论(8)    收藏  举报