#作者:程宏斌
Helm3技术风暴:解锁Kubernetes应用部署的隐藏技能与实战案例
Helm 是 Kubernetes 的包管理器。包管理器类似于我们在 Ubuntu 中使用的apt、Centos中使用的yum 、在Python中使用 pip,能快速查找、下载和安装软件包。
Helm能够将一组K8S资源打包统一管理, 是查找、共享和使用为Kubernetes构建的软件的最佳方式。helm可以理解成k8s的资源整合和再封装。
Helm 包含两个组件,分别是 helm 客户端 和 Tiller 服务器:
helm :一个命令行工具,Helm的客户端,用于本地开发及管理chart,chart仓库管理等
Tiller :Helm 的服务端。Tiller 负责接收 Helm 的请求,与 k8s 的 apiserver 交互,根据chart 来生成一个 release 并管理 release
chart:Helm的打包格式叫做chart,chart就是一系列k8s文件, 它描述了一组相关的 k8s 集群资源.类似yum的rpm文件
release :chart的运行实例,你可以用不同的release name多次安装同一个chart。有点像镜像和容器的关系。
Repoistory:Helm chart 的仓库,存chart包的
config: chart实例化安装运行时的配置信息
常用指令
指令汇总:
helm completion - 为指定的shell生成自动补全脚本
helm create - 使用给定的名称创建chart
helm dependency - 管理chart依赖
helm env - helm客户端环境信息
helm get - 下载命名版本的扩展信息
helm history - 检索发布历史
helm install - 安装chart
helm lint - 验证chart是否存在问题
helm list - 列举发布版本
helm package - 将chart目录打包
helm plugin - 安装、列举或卸载Helm插件
helm pull - 从仓库下载chart并(可选)在本地目录中打开
helm push - 推送chart到远程
helm registry - 从注册表登录或登出
helm repo - 添加、列出、删除、更新和索引chart仓库
helm rollback - 回滚发布到上一个版本
helm search - helm中搜索关键字
helm show - 显示chart信息
helm status - 显示命名版本的状态
helm template - 本地渲染模板
helm test - 执行发布的测试
helm uninstall - 卸载版本
helm upgrade - 升级版本
helm verify - 验证给定路径的chart已经被签名且是合法的
helm version - 打印客户端版本信息
1、仓库相关 helm repo
添加一个chart仓库
helm repo add 起名 仓库地址
$ helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
$ helm repo add --username admin --password password myharbor https://harbor.csdn.cn/chartrepo/charts
查看已添加的chart仓库列表
helm repo list
chart仓库内容更新频繁时,update确保本地仓库的数据最新
helm repo update
从本地删除一个仓库
helm repo remove 仓库名
$ helm repo remove stable
2、chart相关
创建一个chart模板
helm create chartName
$ helm create test
Creating test
检查chart中可能存在的问题(依赖和模板配置是否正确等)
helm lint chartDir
$ helm lint mychart/
==> Linting .
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, no failures
打包chart模板
helm package chartDir
$ helm package . # 此时当前目录就是chart的目录
$ helm package test --debug
$ helm package test
Successfully packaged chart and saved it to: /root/test-0.1.0.tgz
在仓库种查找可用的chart模板
helm search chartName
$ helm search nginx
下载chart到本地目录
# v2用fetch
# v3用pull
helm fetch chartURL
helm fetch repoName/chartName
查看chart的详细信息
helm inspect 仓库/chartName
$ helm inspect stable/redis
查看chart的可配置选项values
helm inspect values 仓库/chartName
$ helm inspect values stable/redis
可以直接在 YAML 格式的文件中来覆盖上面的任何配置,在安装的时候直接使用该配置文件即可:(config.yaml)
mysqlUser: root
mysqlDatabase: code9527
service:
type: NodePort
# 安装chart时指定配置文件config.yaml,这样就可以覆盖原有配置
# 这里我定义了用户名和数据库名
# 并把service类型改为NodePort类型
渲染template目录下的文件,即values.yaml文件填充,看到k8s资源yaml文件的最终样子
helm template chartDir
$ helm template .
$ helm template mychart
安装chart,将chart包发布到k8s集群中安装部署
helm install chartName
$ helm install demo-0.1.0.tgz
$ helm install stable/mysql
# 这里的chartName可以是chart包名,
# chart可以是打包的chart,也可以是没打包的chart目录
# 也可以是仓库中的chart
# 指定配置文件安装,覆盖配置
helm install -f configName.yaml
3、release相关
查看发布到k8s中的chart对应的releas
# 默认返回部署的或者失败的发布release
helm list
helm ls
参数:
# --namespace
# --max设置返回的数据条数
# --offset显示翻页
升级release版本
helm upgrade releaseName chart
# chart参数可以是:
# 1.chart引用('repo/mariadb')
# 2.chart目录路径,打包的chart
# 3.完整URL
# --version指定版本,否则默认更新到最新
在chart中重写value,需要使用’–values’参数并传一个文件或者从命令行使用’–set’参数传个配置
helm upgrade myRelease repo/myRelease --version=1.0.0-feature-15379 --set depService.mq.mqLookupPort=4161 --set depService.mq.connectorType=nsq
查看release的历史修订版本
helm history releaseName [flags]
# --max设置返回数据的最大条数
$ helm history angry-bird
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Mon Oct 3 10:15:13 2016 superseded alpine-0.1.0 1.0 Initial install
2 Mon Oct 3 10:15:13 2016 superseded alpine-0.1.0 1.0 Upgraded successfully
3 Mon Oct 3 10:15:13 2016 superseded alpine-0.1.0 1.0 Rolled back to 2
4 Mon Oct 3 10:15:13 2016 deployed alpine-0.1.0 1.0 Upgraded successfully
将release回退到以前的版本(当然k8s整体资源跟着回退)
helm rollback releaseName [REVISION] [flags]
# REVISION即history指令中的第一列
删除release
helm delete releaseName
# v3版本中已弃用
查看某个release的状态
helm status releaseName
# 返回结果包括:
# 1、最后部署时间
# 2、发布版本所在的k8s命名空间,default
# 3、发布状态(可以是: unknown, deployed, uninstalled, superseded, failed, uninstalling, pending-install, pending-upgrade 或 pending-rollback)
# 4、这个release所包含的K8s资源,如ConfigMap、Deployment、Service等
查看release指定内容
helm get releaseName
helm get notes releaseName #查看release的说明信息(相当于chart中的NOTES.TXT)
helm get manifest releaseName #查看release在k8s中创建出来的资源
helm get hooks releaseName #查看release的回调创建资源
helm get values releaseName #查看release的values配置
helm get all releaseName #查看上述所有内容
4、其他指令
查看helm版本
helm version
helm插件管理
helm plugin list #查看本地安装好的插件 (插件管理)
helm plugin install pluginURL #安装插件
例:helm plugin install https://github.com/chartmuseum/helm-push
#最后的url地址为插件的下载地址,可参考 https://github.com/chartmuseum/helm-push
helm plugin uninstall pluginName #卸载插件
helm plugin update pluginName #更新插件,将插件升级到最新版本
#在下载插件的时候会保存插件及其下载地址,更新的时候使用原本的下载地址直接下载最新版本替换
v2和v3的差别
1、移除tiller
v2版本: helm通过tiller组件与apiserver去交互的,tiller是需要部署在k8s中的
v3版本: helm直接通过kubeconfig去和apiserver通信了,精简了tiller,稳定性和性能貌似更好了
2、删除一个release的命令改变
v3版本中不需要指定–purge了,默认就自带这个参数了
v2版本: helm delete test_release --purge
v3版本: helm uninstall test_release
4、拉取chart包的命令改变
只是改了一个单词,变得和docker类似
v2版本: helm fetch chart-name
v3版本: helm pull chart-name
5、可命名相同名称的release
v2版本: release名称具有唯一性,不能存在相同名称的release
v3版本: release名称可以相同,用不同的namespace区分
6、部署release时候需要指定名称
v2版本: 不需要指定
v3版本: helm install test_release ./mychart
7、release信息存储的命名空间改变
v2版本: 存储在tiller命名空间下,所以release名称不能相同
v3版本: 没有tiller,存储在release实例对应的命名空间下,所以在不同命名空间,可能存在相同名称的release
v2和v3版本的指令区别
| 命令 | Helm2 | Helm3 | 命令区别 | 命令说明 |
|---|---|---|---|---|
| create | 有 | 有 | 无 | create a new chart with the given name |
| delete | 有 | 无 | – | given a release name, delete the release from Kubernetes |
| dependency | 有 | 有 | 无 | manage a chart’s dependencies |
| fetch | 有 | 无 | – | download a chart from a repository and (optionally) unpack it in local directory |
| get | 有 | 有 | 有 | download a named release |
| history | 有 | 有 | 无 | fetch release history |
| home | 有 | 无 | – | displays the location of HELM_HOME |
| init | 有 | 无 | – | initialize Helm on both client and server |
| inspect | 有 | 无 | – | inspect a chart |
| install | 有 | 有 | 有 | install a chart archive |
| lint | 有 | 有 | 无 | examines a chart for possible issues |
| list | 有 | 有 | 无 | list releases |
| package | 有 | 有 | 无 | package a chart directory into a chart archive |
| plugin | 有 | 有 | 有 | add, list, or remove Helm plugins |
| repo | 有 | 有 | 无 | add, list, remove, update, and index chart repositories |
| reset | 有 | 无 | – | uninstalls Tiller from a cluster |
| rollback | 有 | 有 | 无 | roll back a release to a previous revision |
| search | 有 | 有 | 无 | search for a keyword in charts |
| serve | 有 | 无 | – | start a local http web server |
| status | 有 | 有 | 无 | displays the status of the named release |
| template | 有 | 有 | 无 | locally render templates |
| test | 有 | 有 | 有 | test a release |
| upgrade | 有 | 有 | 无 | upgrade a release |
| verify | 有 | 有 | 无 | verify that a chart at the given path has been signed and is valid |
| version | 有 | 有 | 有 | print the client/server version information |
| env | 无 | 有 | – | Helm client environment information |
| help | 无 | 有 | – | Help about any command |
| pull | 无 | 有 | – | download a chart from a repository and (optionally) unpack it in local directory |
| show | 无 | 有 | – | show information of a chart |
| uninstall | 无 | 有 | – | uninstall a release |
v3版本去除的指令
Tiller的去除,导致了reset命令没有存在的意义,同时init存在仅剩客户端需要设定的功能,所以被去除了。
fetch命令,而在Helm 3中提供了pull命令予以替代。
home命令用于显示HELM_HOME环境变量,而在Helm 3中提供env命令可以显示所有的环境变量信息,用增强的功能予以了替换
总之就是做了无用的删除和指令的增强
| 命令 | Helm2 | Helm3 | 命令说明 |
|---|---|---|---|
| delete | 有 | 无 | given a release name, delete the release from Kubernetes |
| fetch | 有 | 无 | download a chart from a repository and (optionally) unpack it in local directory |
| home | 有 | 无 | displays the location of HELM_HOME |
| init | 有 | 无 | initialize Helm on both client and server |
| inspect | 有 | 无 | inspect a chart |
| reset | 有 | 无 | uninstalls Tiller from a cluster |
| serve | 有 | 无 | start a local http web server |
v3版本中新增的指令
env是对被删除的命令home的强化
pull是对被删除的命令fetch的替换
show是对被删除的命令inspect的替换
uninstall是功能特性的增强
| 命令 | Helm 2 | Helm 3 | 命令说明 |
|---|---|---|---|
| env | 无 | 有 | Helm client environment information |
| help | 无 | 有 | Help about any command |
| pull | 无 | 有 | download a chart from a repository and (optionally) unpack it in local directory |
| show | 无 | 有 | show information of a chart |
| uninstall | 无 | 有 | uninstall a release |
同一指令的变化
| 命令 | Helm 2 | Helm 3 | 命令是否有区别 | Helm 2 命令说明 | Helm 3命令说明 |
|---|---|---|---|---|---|
| get | 有 | 有 | 有 | download a named release | download extended information of a named release |
| install | 有 | 有 | 有 | install a chart archive | install a chart |
| plugin | 有 | 有 | 有 | add, list, or remove Helm plugins | install, list, or uninstall Helm plugins |
| test | 有 | 有 | 有 | test a release | run tests for a release |
| version | 有 | 有 | 有 | print the client/server version information | print the client version information |
helm3部署应用的实战案例(发布、升级、回滚、卸载)
创建一个模板的chart包
# helm create mychart
# tree mychart
mychart/
|-- charts
|-- Chart.yaml
|-- mychart-0.1.0.tgz
|-- templates
| -- nginx-deploy-service.yaml -- values.yaml
创建release实例
可以直接使用mychart创建也可以修改mychart下的yaml再创建
删除原来的内容,自定义成我们自己需要的内容。
[root@m1 mychart]# > values.yaml #清空所有默认定义的变量,后面我们根据需要自定义
[root@m1 mychart]# rm -rf templates/* #删除原来所有默认的部署yaml文件, 后面我们根据需要自定义
自定义模版:
[root@m1 mychart]# vim templates/nginx-deploy-service.yaml #自定义需要的yaml模板文件, deployment和svc, 通过nodeport暴露
apiVersion:apps/v1
kind: Deployment
metadata:
name: {{ .Values.deployment_name }}
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
app: {{ .Values.pod_label }}
template:
metadata:
labels:
app: {{ .Values.pod_label }}
spec:
containers:
- image: {{ .Values.image }}:{{ .Values.imageTag }}
name: {{ .Values.container_name }}
ports:
- containerPort: {{ .Values.containerport }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.service_name }}
namespace: {{ .Values.namespace }}
spec:
type: NodePort
ports:
- port: {{ .Values.port }}
targetPort: {{ .Values.targetport }}
nodePort: {{ .Values.nodeport }}
protocol: TCP
selector:
app: {{ .Values.pod_label }}
[root@m1 mychart]# vim values.yaml #自定义需要的变量文件
deployment_name: nginx-deployment
replicas: 2
pod_label: nginx-pod-label
image: nginx
imageTag: 1.17
container_name: nginx-container
service_name: nginx-service
namespace: default
port: 80
targetport: 80
containerport: 80
nodeport: 30001
# helm install nginx-release mychart/ #安装一个release实例, 实例名:nginx-release
mychart是模版的目录
列出release实例
# helm list
获取release实例历史
# helm history nginx-release
升级release实例版本
# helm upgrade nginx-release mychart --set imageTag=1.20
# helm upgrade nginx-release mychart -f mychart/values.yaml
回滚release实例版本
# helm rollback nginx-release #回滚到上一个版本, 指的是release的版本, 不是镜像版本
回滚到指定版本
# helm rollback nginx-release 2 #回滚到指定的版本, 指的是release的版本, 不是镜像版本
卸载删除release实例
# helm uninstall nginx-release #卸载删除release实例

479

被折叠的 条评论
为什么被折叠?



