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版本的指令区别

命令Helm2Helm3命令区别命令说明
createcreate a new chart with the given name
deletegiven a release name, delete the release from Kubernetes
dependencymanage a chart’s dependencies
fetchdownload a chart from a repository and (optionally) unpack it in local directory
getdownload a named release
historyfetch release history
homedisplays the location of HELM_HOME
initinitialize Helm on both client and server
inspectinspect a chart
installinstall a chart archive
lintexamines a chart for possible issues
listlist releases
packagepackage a chart directory into a chart archive
pluginadd, list, or remove Helm plugins
repoadd, list, remove, update, and index chart repositories
resetuninstalls Tiller from a cluster
rollbackroll back a release to a previous revision
searchsearch for a keyword in charts
servestart a local http web server
statusdisplays the status of the named release
templatelocally render templates
testtest a release
upgradeupgrade a release
verifyverify that a chart at the given path has been signed and is valid
versionprint the client/server version information
envHelm client environment information
helpHelp about any command
pulldownload a chart from a repository and (optionally) unpack it in local directory
showshow information of a chart
uninstalluninstall a release

v3版本去除的指令

Tiller的去除,导致了reset命令没有存在的意义,同时init存在仅剩客户端需要设定的功能,所以被去除了。
fetch命令,而在Helm 3中提供了pull命令予以替代。
home命令用于显示HELM_HOME环境变量,而在Helm 3中提供env命令可以显示所有的环境变量信息,用增强的功能予以了替换
总之就是做了无用的删除和指令的增强

命令Helm2Helm3命令说明
deletegiven a release name, delete the release from Kubernetes
fetchdownload a chart from a repository and (optionally) unpack it in local directory
homedisplays the location of HELM_HOME
initinitialize Helm on both client and server
inspectinspect a chart
resetuninstalls Tiller from a cluster
servestart a local http web server

v3版本中新增的指令

env是对被删除的命令home的强化
pull是对被删除的命令fetch的替换
show是对被删除的命令inspect的替换
uninstall是功能特性的增强

命令Helm 2Helm 3命令说明
envHelm client environment information
helpHelp about any command
pulldownload a chart from a repository and (optionally) unpack it in local directory
showshow information of a chart
uninstalluninstall a release

同一指令的变化

命令Helm 2Helm 3命令是否有区别Helm 2 命令说明Helm 3命令说明
getdownload a named releasedownload extended information of a named release
installinstall a chart archiveinstall a chart
pluginadd, list, or remove Helm pluginsinstall, list, or uninstall Helm plugins
testtest a releaserun tests for a release
versionprint the client/server version informationprint 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实例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值