今天我们继续学习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部署


安装

bash 复制代码
[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库

bash 复制代码
helm search hub nginx

添加第三方Chart库

bash 复制代码
helm repo add aliyun  https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 
helm repo add bitnami https://charts.bitnami.com/bitnami

查看Chart库

bash 复制代码
helm repo list

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

bash 复制代码
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目录结构

bash 复制代码
# 通过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应用

bash 复制代码
[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>

四、升级与回滚


修改配置文件

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

升级

bash 复制代码
[root@k8s-master01 nginx]# helm upgrade nginx-server

查看升级结果

bash 复制代码
[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

查看记录

bash 复制代码
[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

回滚

bash 复制代码
[root@k8s-master01 nginx]# helm rollback nginx-server 1

验证回滚

bash 复制代码
[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

卸载

bash 复制代码
[root@k8s-master01 nginx]# helm uninstall nginx-server
相关推荐
charlie1145141912 小时前
编写INI Parser 测试完整指南 - 从零开始
开发语言·c++·笔记·学习·算法·单元测试·测试
weixin_521431122 小时前
Docker容器技术
运维·docker·容器
Bigan(安)2 小时前
【奶茶Beta专项】【LVGL9.4源码分析】08-theme主题管理
linux·c语言·mcu·arm·unix
冬夜戏雪2 小时前
【java学习日记】【12.14】【12/60】
学习
小汐睡着了2 小时前
解决虚拟机VMware与宿主机网络不通的问题-error
linux·网络·redhat
xdxghy09212 小时前
mini centos7+k3s部署(镜像拉取解决版)
linux·运维·服务器·阿里云·运维开发
老华带你飞2 小时前
列车售票|基于springboot 列车售票系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
了一梨3 小时前
外设与接口:按键输入 (libgpiod)
linux·c语言
TL滕3 小时前
从0开始学算法——第十六天(双指针算法)
数据结构·笔记·学习·算法