当前位置: 首页 > news >正文

今天我们继续学习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
http://www.cnnetsun.cn/news/53645.html

相关文章:

  • 音视频学习(七十二):视频压缩:分块与预处理
  • AMD Ryzen性能调优:快速掌握处理器调试工具的使用技巧
  • 深蓝词库转换:轻松打通全平台输入法数据壁垒
  • (新卷,200分)- 最小传输时延Ⅱ(Java JS Python)
  • OpenHarmony AI人脸识别与手势控制系统开发指南
  • 新一代空间感知驱动的军工仓库与硐室透明化管控技术研究
  • Sketch MeaXure插件:设计师必备的智能标注工具
  • 强化学习Q-learning求最优策略
  • 你对电脑上的【Fn】熟悉多少
  • 计及N-k安全约束的含光热电站电力系统优化调度模型【IEEE14节点、118节点】附Matlab代码
  • 计及需求响应的粒子群算法求解风能、光伏、柴油机、储能容量优化配置附Matlab代码
  • conda使用详细指南
  • 豆包与DeepSeek底层大模型的深度解析:技术架构、设计理念与生态分野
  • Linux系统中的socket激活:先创建监听端口,后启动程序
  • 从零解决pyproject.toml构建失败的实战指南
  • Redis Lua脚本入门:从零写出你的第一个原子操作
  • 旧机转手不再慌!电子产品信息清除新国标落地,核心技术逻辑全解析
  • 安全体验馆好用供应商
  • 第二章——数据分析场景之Python数据可视化:用Matplotlib与Seaborn绘制洞察之图
  • 【Java毕设全套源码+文档】基于springboot的高校毕业生离校管理系统小程序设计与实现(丰富项目+远程调试+讲解+定制)
  • 如何用AI工具jstat优化Java应用性能分析
  • 【Java毕设全套源码+文档】基于springboot的高校毕业生信息管理系统的设计与实现(丰富项目+远程调试+讲解+定制)
  • Day 38 GPU训练及类的call方法
  • 【Python实战】火爆全网的“隔空手势画板”是如何实现的?教你用OpenCV+MediaPipe复刻钢铁侠黑科技!
  • 【学习笔记】如果打造可复现、可评测、可迭代的AI技术体系
  • 【论文自动阅读】See Once, Then Act: Vision-Language-Action Model with Task Learning from One-Shot Video Demo
  • 利用齐次坐标系证明各种几何定理【射影几何】
  • 小程序基于springboot的乡镇普法知识科普宣传系统 律师预约系统设计与实现_qf4cwws6(java毕业设计项目源码)
  • 面向对象编程三大特性:封装、继承、多态的核心要义
  • leetcode 2147. 分隔长廊的方案数 困难