news 2026/4/3 0:06:53

今天我们继续学习kubernetes内容Helm

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
今天我们继续学习kubernetes内容Helm

一、Helm概述


helm通过打包的方式,支持发布的版本管理和控制,很大程度上简化了Kubernetes应用的部署和管理。

Helm本质就是让k8s的应用管理(Deployment、Service等)可配置,能动态生成。通过动态生成K8S资源清单文(deployment.yaml、service.yaml)。然后kubectl自动调用K8S资源部署。

对于K8s来说,应用资源配置可以定义为K8s API对象,包括Deployment,Namespace,Service, PV(Persistent Volumes)和PVC(PersistentVolumeClaims)等等。通常一个应用的部署会涉及很多资源的共同协作,用户会定义这些API对象到一系列Yaml文件中,然后通过kubectl来逐一进行部署。

Helm 组件及相关术语

Helm是官方提供类似于YUM的包管理,是部署环境的流程封装,Helm有三个重要的概念:chart、release和Repository

  • Helm:Helm 是一个命令行下的客户端工具。主要用于 Kubernetes 应用程序 Chart 的创建、打包、发布以及创建和管理本地和远程的 Chart 仓库。

  • Tiller:Tiller 是 Helm 的服务端,部署在 Kubernetes 集群中。Tiller 用于接收 Helm 的请求,并根据 Chart 生成 Kubernetes 的部署文件( Helm 称为 Release ),然后提交给 Kubernetes 创建应用。Tiller 还提供了 Release 的升级、删除、回滚等一系列功能。

  • Chart:Helm 的软件包,采用 TAR 格式。类似于 APT 的 DEB 包或者 YUM 的 RPM 包,其包含了一组定义 Kubernetes 资源相关的 YAML 文件。Chart有特定的文件目录结构,如果开发者想自定义一个新的 Chart,只需要使用Helm create命令生成一个目录结构即可进行开发。

  • Repoistory:Helm 的软件仓库,Repository 本质上是一个 Web 服务器,该服务器保存了一系列的 Chart 软件包以供用户下载,并且提供了一个该 Repository 的 Chart 包的清单文件以供查询。Helm 可以同时管理多个不同的 Repository, 官方仓库的地址是https://hub.helm.sh。

  • Release:使用 helm install 命令在 Kubernetes 集群中部署的 Chart 称为 Release。

二、Helm部署


安装

[root@k8s-master01 ~]# mkdir helm [root@k8s-master01 helm]# wget https://get.helm.sh/helm-v3.14.0-linux-amd64.tar.gz [root@k8s-master01 helm]# tar -zxvf helm-v3.14.0-linux-amd64.tar.gz [root@k8s-master01 helm]# cd linux-amd64/ [root@k8s-master01 linux-amd64]# cp helm /usr/local/bin/ [root@k8s-master01 linux-amd64]# echo "source <(helm completion bash)" >> ~/.bashrc [root@k8s-master01 linux-amd64]# source ~/.bashrc

chart库配置

使用helm search搜索官方helm hub chart库

helm search hub nginx

添加第三方Chart库

helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts helm repo add bitnami https://charts.bitnami.com/bitnami

查看Chart库

helm repo list

从仓库中查找指定chart的名字

helm search repo nginx

Helm命令

命令字中文释义作用
completion完成生成特定Shell的自动补全脚本
create创建使用给定的名称创建新图表
dependency依赖管理图表的依赖关系
env环境Helm客户端环境信息
get获取下载已命名发布的扩展信息
help帮助关于任何命令的帮助
history历史获取发布历史记录
install安装安装图表
lint检查检查图表可能存在的问题
list列表列出发布
package打包将图表目录打包成图表存档
plugin插件安装、列出或卸载Helm插件
pull拉取从存储库下载图表,并可选在本地目录中解包
push推送将图表推送到远程存储库
registry注册表登录或注销注册表
repo仓库添加、列出、删除、更新和索引图表存储库
rollback回滚将发布回滚到先前版本
search搜索在图表中搜索关键字
show显示显示图表的信息
status状态显示指定发布的状态
template模板本地渲染模板
test测试运行发布的测试
uninstall卸载卸载发布
upgrade升级升级发布
verify验证验证给定路径的图表已签名并且有效
version版本打印客户端版本信息

三、Helm Chart 详解


chart目录结构

# 通过helm create命令创建一个新的chart包 [root@k8s-master01 helm]# helm create nginx Creating nginx [root@k8s-master01 nginx]# tree . ├── charts ├── Chart.yaml ├── templates │ ├── deployment.yaml │ ├── _helpers.tpl │ ├── hpa.yaml │ ├── ingress.yaml │ ├── NOTES.txt │ ├── serviceaccount.yaml │ ├── service.yaml │ └── tests │ └── test-connection.yaml └── values.yaml 3 directories, 10 files ####目录结构解析#### nginx/ ├── charts #依赖其他包的charts文件 ├── Chart.yaml # 该chart的描述文件,包括ico地址,版本信息等 ├── templates # #存放k8s模板文件目录 │ ├── deployment.yaml # 创建k8s资源的yaml 模板 │ ├── _helpers.tpl # 下划线开头的文件,可以被其他模板引用 │ ├── hpa.yaml # 弹性扩缩容,配置服务资源CPU 内存 │ ├── ingress.yaml # ingress 配合service域名访问的配置 │ ├── NOTES.txt # 说明文件,helm install之后展示给用户看的内容 │ ├── serviceaccount.yaml # 服务账号配置 │ ├── service.yaml # kubernetes Serivce yaml 模板 │ └── tests # 测试模块 │ └── test-connection.yaml └── values.yaml # 给模板文件使用的变量

部署Nginx应用

[root@k8s-master01 nginx-helm]# helm pull bitnami/nginx --version 15.3.5 [root@k8s-master01 nginx-helm]# ls nginx-15.3.5.tgz [root@k8s-master01 nginx-helm]# tar xf nginx-15.3.5.tgz [root@k8s-master01 nginx-helm]# ls nginx nginx-15.3.5.tgz [root@k8s-master01 nginx-helm]# cd nginx [root@k8s-master01 nginx]# vim values.yaml 532 service: 533 ## @param service.type Service type 534 ## 535 type: ClusterIP 536 ## @param service.ports.http Service HTTP port 537 ## @param service.ports.https Service HTTPS port 538 ## 539 ports: 540 http: 80 541 https: 443 ###安装chart### [root@k8s-master01 nginx]# helm install nginx-server . NAME: nginx-server LAST DEPLOYED: Sat Feb 3 15:57:33 2024 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: CHART NAME: nginx CHART VERSION: 15.3.5 APP VERSION: 1.25.3 ** Please be patient while the chart is being deployed ** NGINX can be accessed through the following DNS name from within your cluster: nginx-server.default.svc.cluster.local (port 80) To access NGINX from outside the cluster, follow the steps below: 1. Get the NGINX URL by running these commands: export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services nginx-server) kubectl port-forward --namespace default svc/nginx-server ${SERVICE_PORT}:${SERVICE_PORT} & echo "http://127.0.0.1:${SERVICE_PORT}" ####查看pod和service### [root@k8s-master01 nginx]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGE nginx-deploy 3/3 3 3 23h nginx-deploy1 3/3 3 3 22h nginx-deploy2 3/3 3 3 22h nginx-server 1/1 1 1 56s [root@k8s-master01 nginx]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deploy-5f87d95c-7ph78 1/1 Running 1 (151m ago) 23h nginx-deploy-5f87d95c-dswvq 1/1 Running 1 (151m ago) 23h nginx-deploy-5f87d95c-vk9vg 1/1 Running 1 (151m ago) 23h nginx-deploy1-c8d58b5c7-7dfrd 1/1 Running 1 (151m ago) 22h nginx-deploy1-c8d58b5c7-d2hd7 1/1 Running 1 (151m ago) 22h nginx-deploy1-c8d58b5c7-pfvhn 1/1 Running 1 (151m ago) 22h nginx-deploy2-db98bd9d9-2jl74 1/1 Running 1 (151m ago) 22h nginx-deploy2-db98bd9d9-h67n6 1/1 Running 1 (151m ago) 22h nginx-deploy2-db98bd9d9-wfcmw 1/1 Running 1 (151m ago) 22h nginx-server-ff5765f8-4wbms 1/1 Running 0 2m5s pod-controller-qk5jl 1/1 Running 1 (151m ago) 19h pod-controller-scsxt 1/1 Running 1 (151m ago) 19h [root@k8s-master01 nginx]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 14d nginx-server ClusterIP 10.10.127.16 <none> 80/TCP 2m32s nginx-svc ClusterIP 10.10.83.76 <none> 80/TCP 23h nginx-svc1 LoadBalancer 10.10.168.131 192.168.115.167 80:31261/TCP 22h nginx-svc2 NodePort 10.10.14.245 <none> 80:31110/TCP 22h ####测试访问### [root@k8s-master01 nginx]# curl 10.10.127.16 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>

四、升级与回滚


修改配置文件

[root@k8s-master01 nginx]# vim values.yaml 123 replicaCount: 3 124 ## @param revisionHistoryLimit The number of old history to retain to allow rollback 125 ##

升级

[root@k8s-master01 nginx]# helm upgrade nginx-server

查看升级结果

[root@k8s-master01 nginx]# kubectl get pod [root@k8s-master01 nginx]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deploy-5f87d95c-7ph78 1/1 Running 1 (166m ago) 23h nginx-deploy-5f87d95c-dswvq 1/1 Running 1 (166m ago) 23h nginx-deploy-5f87d95c-vk9vg 1/1 Running 1 (166m ago) 23h nginx-deploy1-c8d58b5c7-7dfrd 1/1 Running 1 (166m ago) 23h nginx-deploy1-c8d58b5c7-d2hd7 1/1 Running 1 (166m ago) 23h nginx-deploy1-c8d58b5c7-pfvhn 1/1 Running 1 (166m ago) 23h nginx-deploy2-db98bd9d9-2jl74 1/1 Running 1 (166m ago) 22h nginx-deploy2-db98bd9d9-h67n6 1/1 Running 1 (166m ago) 22h nginx-deploy2-db98bd9d9-wfcmw 1/1 Running 1 (166m ago) 22h nginx-server-ff5765f8-4p6sh 1/1 Running 0 31s nginx-server-ff5765f8-4wbms 1/1 Running 0 16m nginx-server-ff5765f8-lnkkg 1/1 Running 0 31s

查看记录

[root@k8s-master01 nginx]# helm history nginx-server REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION 1 Sat Feb 3 15:57:33 2024 superseded nginx-15.3.5 1.25.3 Install complete 2 Sat Feb 3 16:13:44 2024 deployed nginx-15.3.5 1.25.3 Upgrade complete

回滚

[root@k8s-master01 nginx]# helm rollback nginx-server 1

验证回滚

[root@k8s-master01 nginx]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deploy-5f87d95c-7ph78 1/1 Running 1 (170m ago) 23h nginx-deploy-5f87d95c-dswvq 1/1 Running 1 (170m ago) 23h nginx-deploy-5f87d95c-vk9vg 1/1 Running 1 (170m ago) 23h nginx-deploy1-c8d58b5c7-7dfrd 1/1 Running 1 (170m ago) 23h nginx-deploy1-c8d58b5c7-d2hd7 1/1 Running 1 (170m ago) 23h nginx-deploy1-c8d58b5c7-pfvhn 1/1 Running 1 (170m ago) 23h nginx-deploy2-db98bd9d9-2jl74 1/1 Running 1 (170m ago) 22h nginx-deploy2-db98bd9d9-h67n6 1/1 Running 1 (170m ago) 22h nginx-deploy2-db98bd9d9-wfcmw 1/1 Running 1 (170m ago) 22h nginx-server-ff5765f8-lnkkg 1/1 Running 0 4m44s

卸载

[root@k8s-master01 nginx]# helm uninstall nginx-server
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/1 8:56:26

GUID为什么不会重复?

GUID为什么不会重复? GUID/UUID &#xff08;全局唯一标识符&#xff09;之所以被认为“几乎不会重复”&#xff0c;是因为其庞大的组合空间和精心设计的生成算法&#xff0c;使得在现实世界中重复的概率低到可以忽略不计。 以下是 GUID 不会重复的核心原因&#xff1a; 1. 庞…

作者头像 李华
网站建设 2026/4/1 12:53:04

E-Hentai批量下载工具:高效管理数字收藏资源的最佳方案

在数字资源日益丰富的今天&#xff0c;如何高效管理和保存有价值的在线内容成为了许多用户的共同痛点。面对心仪的图库资源&#xff0c;传统的手动保存方式不仅效率低下&#xff0c;还容易导致文件混乱。针对这一需求&#xff0c;E-Hentai-Downloader提供了一个简单而强大的解决…

作者头像 李华
网站建设 2026/3/21 11:57:52

布隆过滤器

一、布隆过滤器 1. 什么是布隆过滤器&#xff1f; 布隆过滤器是一种空间效率极高的概率型数据结构&#xff0c;核心作用是快速判断「一个元素是否存在于集合中」。它的特点可以总结为&#xff1a; 说「元素不在」→ 100%准确&#xff08;绝对没在集合里&#xff09;&#xff1b…

作者头像 李华
网站建设 2026/3/24 11:39:12

【JESD22-B109C】倒装芯片拉伸测试

B109C 测试方法&#xff1a;Flip Chip Tensile Pull 倒装芯片拉伸测试1 范围本测试方法适用于芯片与基板焊点形成后、未涂覆底部填充胶或其他会提高表观结合强度的材料前的倒装芯片。其用途包括&#xff1a;评估特定倒装芯片的芯片接合工艺一致性与质量&#xff1b;评估特定倒装…

作者头像 李华
网站建设 2026/4/3 3:16:08

2025年应届生闭坑指南:如何挑选低费用、高认可度的AI技能证书?

随着人工智能技术席卷各行各业&#xff0c;手握相关技能证书已成为应届毕业生提升就业竞争力的重要筹码。然而&#xff0c;面对市场上琳琅满目、价格不一的认证项目&#xff0c;许多同学不禁感到迷茫&#xff1a;如何避开“高价低能”的坑&#xff0c;选择一款既具高含金量又不…

作者头像 李华
网站建设 2026/4/2 18:40:09

基于YOLOv12农作物检测系统1:农作物检测数据集说明(含下载链接)

一. 前言 本篇博客是《基于YOLOv12农作物检测系统》系列文章之《农作物检测数据集说明(含下载链接)》&#xff0c;网上有很多农作物检测数据集的数据&#xff0c;百度一下&#xff0c;一搜一大堆&#xff0c;但质量参差不齐&#xff0c;很多不能用&#xff0c;即使一个一个的看…

作者头像 李华