• helm使用


    前言

    类似于 Linux 的 YUM、APT,Helm 是 K8S 的包管理工具。

    Helm, 一个二进制工具,用来安装、升级、卸载 K8S 中的应用程序。

    Helm Chart,一个 tgz 包,类似安卓的 APK。

    K8S 应用打包成 Chart,通过 Helm 安装到 K8S 集群中。

    1.环境准备

    需要准备一套k8s的集群,helm主要是k8s集群的包管理器,主要用来管理helm中的各种chart包。

    2.传统服务部署到k8s集群的流程

    拉取代码->打包编译->构建镜像->准备一堆相关部署的yaml文件(如:deploymet、service、ingress等)->kubectl apply 部署到k8s集群。

    3.传统方式部署引发的问题

    1)随着引用的增多,需要维护大量的yaml文件

    2)不能根据一套yaml文件来创建多个环境,需要手动进行修改。

    例如:一般环境都分为dev、预生产、生产环境,部署完了dev这套环境,后面再部署预生产和生产环境,还需要复制出两套,并手动修改才行

    4.什么是helm

    Helm是Kubernetes 的包管理工具,可以方便地发现、共享和构建 Kubernetes 应用

    helm是k8s的包管理器,相当于centos系统中的yum工具,可以将一个服务相关的所有资源信息整合到一个chart包中,并且可以使用一套资源发布到多个环境中, 可以将应用程序的所有资源和部署信息组合到单个部署包中。
    就像Linux下的rpm包管理器,如yum/apt等,可以很方便的将之前打包好的yaml文件部署到kubernetes上。

    5.helm的组件

    1)Chart:

    就是helm的一个整合后的chart包,包含一个应用所有的kubermetes声明模版,类似于yum的rpm包或者apt的dpkg文件

    理解:

    helm将打包的应用程序部署到k8s,并将它们构建成Chart。这些Chart将所有预配置的应用程序资源以及所有版本都包含在一个易于管理的包中。

    Helm把kubernetes资源(如deployments、 services或ingres等)打包到一个chart中,chart被保存到chart仓库。通过chart仓库可用来存储和分享chart。

    2)Helm客户端: 

    helm的客户端组件,负责和k8s apiserver通信

    3)Repository:

    用于发布和存储chart包的仓库,类似yum仓库或docker仓库

    4)Release:

    用chart包部署的一个实例。通过chart在k8s中部署的应用都会产生一个唯一的Release,同chart部署多次就会产生多个Release。

    理解:

    将这些yaml部署完成后,他也会记录部署时候的一个版本,维护了一个release版本状态,通过Release这个实例,他会具体帮我们创建pod.deployment等资源

    6.helm3和helm2的区别

    1)helm3移除了Tiller组件

    helm2中helm客户端和k8s通信是通过Tiler组件和k8s通信,helm3移除了Tiler组件,直接使用kubeconfig文件和k8s的apiserver通信

    2)删除 release 命令变更

    1. #helm delete release-name --purge
    2. ------------>> helm uninstall release-name

    3)查看 charts 信息命令变更

    #helm inspect release-name ------------>>helm show release-name

    4)拉取 charts 包命令变更

    1. #helm fetch chart-name -------
    2. ---->> helm pull chart-name

    5)helm3中必须指定 release 名称,如果需要生成一个随机名称,需要加选项-generate-name,helm2中如果不指定release名称,可以自动生成一个随机名称

    #helm install ./mychart --generate-name

    一、安装Helm和目录结构

    1、用二进制版本安装

    1. [root@k8s-master-136 ~]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 261d v1.21.0
    4. k8s-node-135 Ready <none> 261d v1.21.0
    5. k8s-node-137 Ready <none> 261d v1.21.0

    每个Helm 版本都提供了各种操作系统的二进制版本,这些版本可以手动下载和安装。

    下载 需要的版本

    wget https://repo.huaweicloud.com/helm/v3.5.4/helm-v3.5.4-linux-amd64.tar.gz

    解压

    tar -zxvf helm-v3.5.4-linux-amd64.tar.gz

    在解压目录中找到helm程序,移动到需要的目录中

    mv linux-amd64/helm /usr/local/bin/helm

    然后就可以执行客户端程序并添加稳定仓库: helm help

    2.chart的目录结构

    1. # helm create mychart
    2. [root@k8s-master-136 samve]# tree mychart
    3. mychart #chart包的名称
    4. ├── charts #存放子chart的目录,目录里存放这个chart依赖的所有子chart
    5. ├── Chart.yaml #保存chat的基本信息,包括名字、描述信息及版本等,这个变量文件都可以被templates目录下文件所引用
    6. ├── templates #模板文件目录,目录里面存放所有yaml模板文件,包含了所有部署应用的yaml文件
    7. │   ├── deployment.yaml #创建deployment对象的模板文件
    8. │   ├── _helpers.tpl #放置模板助手的文件,可以在整个chat中重复使用,是放一些templates目录下这些vaml都有可能会用的一些模板
    9. │   ├── hpa.yaml
    10. │   ├── ingress.yaml
    11. │   ├── NOTES.txt #存放提示信息的文件,介绍chat帮助信息,helm instl部署后展示给用户,如何使用chat等,是部署chat后给用户的提示信息
    12. │   ├── serviceaccount.yaml
    13. │   ├── service.yaml
    14. │   └── tests #用于测试的文件,测试完部署完chat后,如web,做一个链接,看看你是否部署正常
    15. │   └── test-connection.yaml
    16. └── values.yaml #用于染模板的文件(变量文件,定义变量的值) 定义templates目录下的yaml文件可能引用到的变量
    17. #values.yaml用于存储 templates 目录中模板文件中用到变量的值,这些变量定义都是为了让templates目录下yaml引用

    3.Chart.yaml 文件字段说明

    1. [root@k8s-master-136 mychart]# cat Chart.yaml
    2. apiVersion: v2
    3. name: mychart
    4. description: A Helm chart for Kubernetes
    5. # A chart can be either an 'application' or a 'library' chart.
    6. #
    7. # Application charts are a collection of templates that can be packaged into versioned archives
    8. # to be deployed.
    9. #
    10. # Library charts provide useful utilities or functions for the chart developer. They're included as
    11. # a dependency of application charts to inject those utilities and functions into the rendering
    12. # pipeline. Library charts do not define any templates and therefore cannot be deployed.
    13. type: application
    14. # This is the chart version. This version number should be incremented each time you make changes
    15. # to the chart and its templates, including the app version.
    16. # Versions are expected to follow Semantic Versioning (https://semver.org/)
    17. version: 0.1.0
    18. # This is the version number of the application being deployed. This version number should be
    19. # incremented each time you make changes to the application. Versions are not expected to
    20. # follow Semantic Versioning. They should reflect the version the application is using.
    21. # It is recommended to use it with quotes.
    22. appVersion: "1.16.0"

    详细字段说明:

    • apiVersion: # chat API 版本信息,通常是"v2"(必须) appVersion字段与version 字段并没有直接的联系。这是指定应用版本的一种方式;#对于部分仅支持helm3的chat,apiVersion应该指定为V2。对于可以同时支持helm3和helm2版本的chat,可以将其设置为V1
    • name: # chart 的名称(必须)
    • version: # chat 包的版本(必须),version 字段用于指定 chat 包的版本号,后续更新 chat 包时候也需要同步更新这个字段
    • kubeVersion: # 指定kubernetes 版本(可选) 用于指定受支持的kubernetes版本,helm在安装时候会验证版本信息。
    • type: # chat类型(可选)V2版新增了type 字段,用于区分chat类型,取值为application(默认)和library,如"应用类型chart"和"库类型chart”
    • description: # 对项目的描述(可选)

    二、helm3创建和编写一个chart 

    1.创建一个chat

    使用helm create 命令即可创建一个chart,其中包含完整的目录结构:

    1. [root@k8s-master-136 samve]# helm create mychart
    2. [root@k8s-master-136 samve]# tree mychart
    3. mychart
    4. ├── charts
    5. ├── Chart.yaml
    6. ├── templates
    7. │   ├── deployment.yaml
    8. │   ├── _helpers.tpl
    9. │   ├── hpa.yaml
    10. │   ├── ingress.yaml
    11. │   ├── NOTES.txt
    12. │   ├── serviceaccount.yaml
    13. │   ├── service.yaml
    14. │   └── tests
    15. │   └── test-connection.yaml
    16. └── values.yaml

    2.编写一个chat,不引用内置对象的变量值(用helm3发布创建一个confgmap,创建的k8s集群中,发布其他应用也一样)

    1. [root@k8s-master-136 samve]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 261d v1.21.0
    4. k8s-node-135 Ready <none> 261d v1.21.0
    5. k8s-node-137 Ready <none> 261d v1.21.0

    1)创建一个chart包:

    1. [root@k8s-master-136 samve]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 261d v1.21.0
    4. k8s-node-135 Ready <none> 261d v1.21.0
    5. k8s-node-137 Ready <none> 261d v1.21.0
    6. [root@k8s-master-136 samve]# helm create mychart
    7. Creating mychart
    8. [root@k8s-master-136 samve]# cd mychart/
    9. [root@k8s-master-136 mychart]# ls
    10. charts Chart.yaml templates values.yaml
    11. [root@k8s-master-136 mychart]# cd templates/
    12. [root@k8s-master-136 templates]# ls
    13. deployment.yaml _helpers.tpl hpa.yaml ingress.yaml NOTES.txt serviceaccount.yaml service.yaml tests
    14. [root@k8s-master-136 templates]# rm -rf *
    15. [root@k8s-master-136 templates]# vim configmap.yaml

    configmap.yaml: 

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: mychart-configmap
    5. data:
    6. myvalue: "hello world"

     2)创建一个release实例:

    1. [root@k8s-master-136 samve]# helm install myconfigmap ./mychart/
    2. NAME: myconfigmap
    3. LAST DEPLOYED: Sun Oct 15 16:25:07 2023
    4. NAMESPACE: default
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None

    3)查看创建后的相关信息和验证是否已经在k8s集群中创建了configmap

    1. [root@k8s-master-136 samve]# helm get manifest myconfigmap
    2. ---
    3. # Source: mychart/templates/configmap.yaml
    4. apiVersion: v1
    5. kind: ConfigMap
    6. metadata:
    7. name: mychart-configmap
    8. data:
    9. value: "hello world"

    安装成功后,用helm get manifest release名 命令可以查看已经发布到k8s中的release信息。

    1. [root@k8s-master-136 samve]# helm list |grep myconfigmap #list列出创建的release实例名
    2. myconfigmap default 1 2023-10-15 16:25:07.264394056 +0800 CST deployed mychart-0.1.0 1.16.0
    3. [root@k8s-master-136 samve]# kubectl get configmap |grep mychart-configmap #查看使用helm发布后创建的configmap
    4. mychart-configmap 1 4m13s

     4)删除release实例

    使用 helm uninstal release实例名 的命今来删除这个release,删除的时候直接指定release 名称即可。

    1. [root@k8s-master-136 samve]# helm uninstall myconfigmap #删除release实例,指定release的实例名: myconfigmap
    2. release "myconfigmap" uninstalled
    3. [root@k8s-master-136 samve]# helm list|grep myconfigmap
    4. [root@k8s-master-136 samve]# kubectl get configmap | grep mychart-configmap

    3.编写一个chart,引用内置对象的变量值(用helm3发布创建一个configmap,创建的k8s集群中)

    1. [root@k8s-master-136 samve]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 261d v1.21.0
    4. k8s-node-135 Ready <none> 261d v1.21.0
    5. k8s-node-137 Ready <none> 261d v1.21.0

    1)创建一个chart包:

    1. [root@k8s-master-136 samve]# helm create mychart
    2. Creating mychart
    3. [root@k8s-master-136 samve]# cd mychart/
    4. [root@k8s-master-136 mychart]# ls
    5. charts Chart.yaml templates values.yaml
    6. [root@k8s-master-136 mychart]# cd templates/
    7. [root@k8s-master-136 templates]# ls
    8. deployment.yaml _helpers.tpl hpa.yaml ingress.yaml NOTES.txt serviceaccount.yaml service.yaml tests
    9. [root@k8s-master-136 templates]# rm -rf *
    10. [root@k8s-master-136 templates]# vim configmap.yaml

    configmap.yaml: 

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: {{.Release.Name}}-configmap #最前面的,从作用域最项层命名空间开始,即: 在顶层命名空间中开始查找Release对象,再查找Name对象
    5. #上面就是通过内置对象获取获取内置对象的变量值(Release的名称)作为拼接成onfigmap的名字
    6. data:
    7. myvalue: {{.Values.MY_VALUE}}
    1. > /home/samve/mychart/values.yaml
    2. vim /home/samve/mychart/values.yaml

    values.yaml

    MY_VALUE: "Hello World"

    引用内置对象变量或其他变量 (如values.yaml) 的好处:
    如果metadata.name 中设置的值是一个固定值,这样的模板是无法在k8s中多次部署的,所以我们可以试着在每次安装chart时,都自动metadata.name的值设置为release的名称,因为每次部署时候release实例名是不一样的,这样部署时候,里面的资源名也就可以作为一个区分,而可以进行重复部署。

     2)创建一个release实例: 

    使用helm安装一个release的实例,指定release实例名: myconfigmap,指定chart目录,/mychart

    1. [root@k8s-master-136 samve]# helm install myconfigmap2 ./mychart
    2. NAME: myconfigmap2
    3. LAST DEPLOYED: Sun Oct 15 17:03:40 2023
    4. NAMESPACE: default
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None

    3)查看创建后的相关信息和验证是否已经在k8s集群中创建了configmap

    1. [root@k8s-master-136 samve]# helm list |grep myconfigmap2
    2. myconfigmap2 default 1 2023-10-15 17:03:40.877607951 +0800 CST deployed mychart-0.1.0 1.16.0
    3. [root@k8s-master-136 samve]# kubectl get configmap | grep myconfigmap2
    4. myconfigmap2-configmap 1 71s

    4)删除release实例

    使用 helm uninstall release实例名 的命令来删除这个release,删除的时候直接指定release 名称即可。

    1. [root@k8s-master-136 samve]# helm uninstall myconfigmap2
    2. release "myconfigmap2" uninstalled
    3. [root@k8s-master-136 samve]# helm list | grep myconfigmap2
    4. [root@k8s-master-136 samve]# kubectl get configmap | grep myconfigmap2

    4.helm的测试渲染命不真正执行,只是试运行看是否能运行

    helm提供了一个用来渲染模板的命令,该命今可以将模板内容渲染出来,但是不会进行任何安装的操作。可以用该命今来测试模板渲染的内容是否正确。 

    用法: helm install release实例名 chart目录 --debug --dry-run
    例:# helm install myconfigmap3 ./mychart/ --debug --dry-run

    5.helm通过各种类型chart包安装一个release实例名来部署k8s相关的资源(如: pod、deployment、svc、ingress等,根据模板文件定义)

    1)从加入到本地的chart官方仓库(从官方仓库安装)安装release实例
    2)将从chart仓库拉下来的压缩包进行安装release实例(下载好的压缩包本地离线安装release)
    3)将从chart仓库拉下来的压缩包解压后,从解压目录安装release实例(解压下载好的压缩包,从解压目录离线安装release实例)
    4)从一个网络地址仓库压缩包直接安装release实例

    1. #helm install db stable/mysql #从加入到本地的chart官方仓库(从官方仓库安装)安装release实例,db为release实例名
    2. #helm install my-tomcat test-repo/tomcat #从加入到本地的chart社区仓库(从官方仓库安装)安装release实例,my-tomcat 为release实例名
    3. #helm install db mysql-1.6.9.tgz #从chart仓库拉下来的压缩包进行安装release实例(从本地存档文件离线安装),db为release实例名
    4. #helm install db mysql #从chart仓库拉下来的压缩包解压后,从解压目录安装release实例(从解压目录离线安装).db为release实例名
    5. #helm instal db http:/ur ../mysa-1.6.9.taz #从一个网络地址仓库压缩包直接安装release实例(从下载服务器安装), db为release实例名

    卸载release实例:

    #helm uninstall release实例名

    三、helm内置对象详解

    1.helm3常用的内置对象

    Release 对象
    Values 对象
    Chart 对象
    Capabilities 对象
    Template 对象

    2.各内置对象详解:描述了版本发布自身的一些信息。它包含了以下对象:

    1)Release对象
    Release.Name:release 的名称
    Release.Namespace:release 的命名空间
    Release.IsUpgrade:如果当前操作是升级或回滚的话,该值为 true
    Release.IsInstall:如果当前操作是安装的话,该值为 true
    Release.Revision:获取此次修订的版本号。初次安装时为 1,每次升级或回滚都会递增
    Release.Service:获取渲染当前模板的服务名称。一般都是 Helm

    2)Values 对象:描述的是value.yaml 文件(定义变量的文件)中的内容,默认为空。使用Value 对象可以获取到value.yaml文件中已定义的任何变量数值

    Value 键值对     获取方式
    name1: test1    .Values.name1
    info:
     name2: test2   .Values.info.name2

    3)Chart 对象:用于获取Chart.yaml文件中的内容

    .Chart,Name:获取Chart 的名称
    .Chart.Version:获取Chart 的版本

    4)Capabilities对象:提供了关于kubernetes 集群相关的信息。

    该对象有如下方法:

    .Capabilities.APIVersions:返回kubernetes集群 API 版本信息集合
    .Capabilities.APIVersions.Has sversion:用于检测指定的版本或资源在k8s集群中是否可用,例如: apps/v1/Deployment
    .Capabilities.KubeVersion和.Capabilities.KubeVersion.Version:都用于获取kubernetes 的版本号
    .Capabilities.KubeVersion.Major:获取kubernetes 的主版本号
    .Capabilities.KubeVersion.Minor:获取kubernetes 的小版本号

    5)Template对象:用于获取当前模板的信息,它包含如下两个对象

    .Template.Name:用于获取当前模板的名称和路径(例如: mchart/templates/mytemplate.yaml)
    .Template.BasePath:用于获取当前模板的路径(例如: mychart/templates)

    3.各内置对象调用的演示

    1)环境准备 k8s集群

    1. [root@k8s-master-136 samve]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 261d v1.21.0
    4. k8s-node-135 Ready <none> 261d v1.21.0
    5. k8s-node-137 Ready <none> 261d v1.21.0

    2)创建一个chart包(用helm3发布创建一个configmap,创建的k8s集群中,发布其他应用也一样)

    1. [root@k8s-master-136 samve]# helm create mychart
    2. Creating mychart
    3. [root@k8s-master-136 samve]# cd mychart/
    4. [root@k8s-master-136 mychart]# cd templates/
    5. [root@k8s-master-136 templates]# rm -rf *

     3)编写自己需要的yaml文件,调用上面各自内置对象获取相关变量的值

    (1)调用Release对象:描述了版本发布自身的一些信息。

    1. [root@k8s-master-136 templates]# > /home/samve/mychart/templates/configmap.yaml
    2. [root@k8s-master-136 templates]# vim /home/samve/mychart/templates/configmap.yaml

    configmap.yaml 

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: {{.Release.Name}} -configmap
    5. namespace: {{.Release.Namespace}}
    6. data:
    7. value1: "{{.Release.IsUpgrade}}" #如果当前操作是升级或回滚的话,该值为true
    8. value2: "{{ .Release.IsInstall}}" #如果当前操作是安装的话,该值为true
    9. value3: "{{.Release.Revision}}" #获取此次修订的版本号
    10. value4: "{{.Release.Service}}" #获取当前模板的服务名
    1. [root@k8s-master-136 samve]# helm install myconfigmap1 ./mychart/ --debug --dry-run
    2. install.go:173: [debug] Original chart version: ""
    3. install.go:190: [debug] CHART PATH: /home/samve/mychart
    4. NAME: myconfigmap1
    5. LAST DEPLOYED: Sun Oct 15 17:47:17 2023
    6. NAMESPACE: default
    7. STATUS: pending-install
    8. REVISION: 1
    9. TEST SUITE: None
    10. USER-SUPPLIED VALUES:
    11. {}
    12. COMPUTED VALUES:
    13. affinity: {}
    14. autoscaling:
    15. enabled: false
    16. maxReplicas: 100
    17. minReplicas: 1
    18. targetCPUUtilizationPercentage: 80
    19. fullnameOverride: ""
    20. image:
    21. pullPolicy: IfNotPresent
    22. repository: nginx
    23. tag: ""
    24. imagePullSecrets: []
    25. ingress:
    26. annotations: {}
    27. enabled: false
    28. hosts:
    29. - host: chart-example.local
    30. paths:
    31. - backend:
    32. serviceName: chart-example.local
    33. servicePort: 80
    34. path: /
    35. tls: []
    36. nameOverride: ""
    37. nodeSelector: {}
    38. podAnnotations: {}
    39. podSecurityContext: {}
    40. replicaCount: 1
    41. resources: {}
    42. securityContext: {}
    43. service:
    44. port: 80
    45. type: ClusterIP
    46. serviceAccount:
    47. annotations: {}
    48. create: true
    49. name: ""
    50. tolerations: []
    51. HOOKS:
    52. MANIFEST:
    53. ---
    54. # Source: mychart/templates/configmap.yaml
    55. apiVersion: v1
    56. kind: ConfigMap
    57. metadata:
    58. name: -configmap
    59. namespace: .Release.Namespace
    60. data:
    61. value1: "false" #如果当前操作是升级或回滚的话,该值为true
    62. value2: "true" #如果当前操作是安装的话,该值为true
    63. value3: "1" #获取此次修订的版本号
    64. value4: "Helm" #获取当前模板的服务名

    2)调用Values对象:描述的是value.yaml 文件 (定义变量的文件)中的内容 

    清空里面的初始化信息,设置成我们需要的(变量名和赋值) (里面默认的信息都是初始化信息,仅供参考

    1. [root@k8s-master-136 samve]# > /home/samve/mychart/values.yaml
    2. [root@k8s-master-136 samve]# vim /home/samve/mychart/values.yaml
    3. name1: test1
    4. info:
    5. name2: test2
    1. [root@k8s-master-136 samve]# > /home/samve/mychart/templates/configmap.yaml
    2. [root@k8s-master-136 samve]# vim /home/samve/mychart/templates/configmap.yaml

     configmap.yaml

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: {{.Release.Name}}-configmap
    5. namespace: {{.Release.Namespace}}
    6. data:
    7. value1: "{{.Values.name1}}" #获取values.yaml文件中定义的变量的值
    8. value2: "{{.Values.info.name2}}" #获取values.yaml文件中定义的层级变量的值
    1. [root@k8s-master-136 samve]# helm install myconfigmap2 ./mychart/ --debug --dry-run #不真正执行,只是试运行看是否能运行
    2. install.go:173: [debug] Original chart version: ""
    3. install.go:190: [debug] CHART PATH: /home/samve/mychart
    4. NAME: myconfigmap2
    5. LAST DEPLOYED: Sun Oct 15 18:35:36 2023
    6. NAMESPACE: default
    7. STATUS: pending-install
    8. REVISION: 1
    9. TEST SUITE: None
    10. USER-SUPPLIED VALUES:
    11. {}
    12. COMPUTED VALUES:
    13. info:
    14. name2: test2
    15. name1: test1
    16. HOOKS:
    17. MANIFEST:
    18. ---
    19. # Source: mychart/templates/configmap.yaml
    20. apiVersion: v1
    21. kind: ConfigMap
    22. metadata:
    23. name: myconfigmap2-configmap
    24. namespace: default
    25. data:
    26. value1: "test1" #获取values.yaml文件中定义的变量的值
    27. value2: "test2" #获取values.yaml文件中定义的层级变量的值

    (3)调用Chart 对象:用于获取Chart.yaml文件中的内容 

    1. [root@k8s-master-136 samve]# cat /home/samve/mychart/Chart.yaml | grep -vE "#|^s" #先查看下Chart.yaml文件中内容中定义的变量
    2. apiVersion: v2
    3. name: mychart
    4. description: A Helm chart for Kubernetes
    5. type: application
    6. version: 0.1.0
    7. appVersion: "1.16.0"
    1. [root@k8s-master-136 samve]# vim /home/samve/mychart/templates/configmap.yaml #编写一个自己需要的模板文件
    2. apiVersion: v1
    3. kind: ConfigMap
    4. metadata:
    5. name: {{.Release.Name}}-configmap
    6. namespace: {{.Release.Namespace}}
    7. data:
    8. value1: "{{.Chart.Name}}" #获取Chat的名称,获取Chart.yaml文件中定义的变量的值
    9. value2: "{{.Chart.Version}}" #获取Chart的版本,获取Chart.yaml文件中定义的变量的值
    1. helm install myconfigmap3 ./mychart/ --debug --dry-run
    2. install.go:173: [debug] Original chart version: ""
    3. install.go:190: [debug] CHART PATH: /home/samve/mychart
    4. NAME: myconfigmap3
    5. LAST DEPLOYED: Sun Oct 15 18:23:30 2023
    6. NAMESPACE: default
    7. STATUS: pending-install
    8. REVISION: 1
    9. TEST SUITE: None
    10. USER-SUPPLIED VALUES:
    11. {}
    12. COMPUTED VALUES:
    13. affinity: {}
    14. autoscaling:
    15. enabled: false
    16. maxReplicas: 100
    17. minReplicas: 1
    18. targetCPUUtilizationPercentage: 80
    19. fullnameOverride: ""
    20. image:
    21. pullPolicy: IfNotPresent
    22. repository: nginx
    23. tag: ""
    24. imagePullSecrets: []
    25. ingress:
    26. annotations: {}
    27. enabled: false
    28. hosts:
    29. - host: chart-example.local
    30. paths:
    31. - backend:
    32. serviceName: chart-example.local
    33. servicePort: 80
    34. path: /
    35. tls: []
    36. nameOverride: ""
    37. nodeSelector: {}
    38. podAnnotations: {}
    39. podSecurityContext: {}
    40. replicaCount: 1
    41. resources: {}
    42. securityContext: {}
    43. service:
    44. port: 80
    45. type: ClusterIP
    46. serviceAccount:
    47. annotations: {}
    48. create: true
    49. name: ""
    50. tolerations: []
    51. HOOKS:
    52. MANIFEST:
    53. ---
    54. # Source: mychart/templates/configmap.yaml
    55. apiVersion: v1
    56. kind: ConfigMap
    57. metadata:
    58. name: -configmap
    59. namespace: default
    60. data:
    61. value1: "mychart" #获取Chat的名称,获取Chart.yaml文件中定义的变量的值
    62. value2: "0.1.0" #获取Chart的版本,获取Chart.yaml文件中定义的变量的值

    (4)调用Capabilities对象 提供了关于kubernetes 集群相关的信息。该对象有如下方法

    [root@k8s-master-136 samve]# vim /home/samve/mychart/templates/configmap.yaml

    configmap.yaml

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: {{.Release.Name}} -configmap
    5. namespace: {{.Release.Namespace}}
    6. data:
    7. value1: "{{.Capabilities.APIVersions}}" #返回kubernetes集群 API 版本信息集合
    8. value2: "{{.Capabilities.KubeVersion.Version}}" #用于获取kubernetes的版本号
    9. value3: "{{.Capabilities.KubeVersion.Major}}" #获取kubernetes 的主版本号
    10. value4: "{{.Capabilities.KubeVersion.Minor}}" #获取kubernetes 的小版本号
    1. [root@k8s-master-136 samve]# helm install myconfigmap4 ./mychart/ --debug --dry-run
    2. install.go:173: [debug] Original chart version: ""
    3. install.go:190: [debug] CHART PATH: /home/samve/mychart
    4. NAME: myconfigmap4
    5. LAST DEPLOYED: Sun Oct 15 18:01:27 2023
    6. NAMESPACE: default
    7. STATUS: pending-install
    8. REVISION: 1
    9. TEST SUITE: None
    10. USER-SUPPLIED VALUES:
    11. {}
    12. COMPUTED VALUES:
    13. affinity: {}
    14. autoscaling:
    15. enabled: false
    16. maxReplicas: 100
    17. minReplicas: 1
    18. targetCPUUtilizationPercentage: 80
    19. fullnameOverride: ""
    20. image:
    21. pullPolicy: IfNotPresent
    22. repository: nginx
    23. tag: ""
    24. imagePullSecrets: []
    25. ingress:
    26. annotations: {}
    27. enabled: false
    28. hosts:
    29. - host: chart-example.local
    30. paths:
    31. - backend:
    32. serviceName: chart-example.local
    33. servicePort: 80
    34. path: /
    35. tls: []
    36. nameOverride: ""
    37. nodeSelector: {}
    38. podAnnotations: {}
    39. podSecurityContext: {}
    40. replicaCount: 1
    41. resources: {}
    42. securityContext: {}
    43. service:
    44. port: 80
    45. type: ClusterIP
    46. serviceAccount:
    47. annotations: {}
    48. create: true
    49. name: ""
    50. tolerations: []
    51. HOOKS:
    52. MANIFEST:
    53. ---
    54. # Source: mychart/templates/configmap.yaml
    55. apiVersion: v1
    56. kind: ConfigMap
    57. metadata:
    58. name: -configmap
    59. namespace: .Release.Namespace
    60. data:
    61. value1: "[discovery.k8s.io/v1beta1 extensions/v1beta1/Ingress storage.k8s.io/v1beta1/VolumeAttachment admissionregistration.k8s.io/v1beta1/ValidatingWebhookConfiguration authorization.k8s.io/v1beta1/SelfSubjectAccessReview crd.projectcalico.org/v1/IPAMHandle apiregistration.k8s.io/v1 autoscaling/v2beta2 certificates.k8s.io/v1beta1 v1/Endpoints v1/PersistentVolume certificates.k8s.io/v1 storage.k8s.io/v1/CSIDriver crd.projectcalico.org/v1/BGPPeer crd.projectcalico.org/v1/NetworkSet crd.projectcalico.org/v1/IPAMConfig rbac.authorization.k8s.io/v1/ClusterRole rbac.authorization.k8s.io/v1/Role admissionregistration.k8s.io/v1beta1/MutatingWebhookConfiguration coordination.k8s.io/v1beta1/Lease authorization.k8s.io/v1/SelfSubjectAccessReview storage.k8s.io/v1/CSINode storage.k8s.io/v1beta1/CSIStorageCapacity autoscaling/v2beta1 v1/Event v1/PodProxyOptions apps/v1/Scale networking.k8s.io/v1/NetworkPolicy crd.projectcalico.org/v1/NetworkPolicy events.k8s.io/v1/Event events.k8s.io/v1beta1/Event authorization.k8s.io/v1beta1/LocalSubjectAccessReview scheduling.k8s.io/v1/PriorityClass node.k8s.io/v1beta1/RuntimeClass v1/Service policy/v1beta1/PodSecurityPolicy crd.projectcalico.org/v1/IPReservation v1/ServiceAccount rbac.authorization.k8s.io/v1beta1/ClusterRole crd.projectcalico.org/v1/IPPool coordination.k8s.io/v1beta1 authorization.k8s.io/v1/SelfSubjectRulesReview networking.k8s.io/v1/IngressClass rbac.authorization.k8s.io/v1beta1/Role rbac.authorization.k8s.io/v1 v1/PodAttachOptions authorization.k8s.io/v1beta1/SelfSubjectRulesReview policy/v1/PodDisruptionBudget crd.projectcalico.org/v1/HostEndpoint apiextensions.k8s.io/v1beta1 apps/v1/ControllerRevision authorization.k8s.io/v1/SubjectAccessReview crd.projectcalico.org/v1/GlobalNetworkSet v1/Scale v1/ServiceProxyOptions apiregistration.k8s.io/v1/APIService apps/v1 batch/v1beta1 scheduling.k8s.io/v1beta1 v1/Node v1/PersistentVolumeClaim networking.k8s.io/v1beta1/IngressClass rbac.authorization.k8s.io/v1/RoleBinding discovery.k8s.io/v1/EndpointSlice discovery.k8s.io/v1beta1/EndpointSlice crd.projectcalico.org/v1/FelixConfiguration extensions/v1beta1 storage.k8s.io/v1/VolumeAttachment coordination.k8s.io/v1/Lease node.k8s.io/v1/RuntimeClass crd.projectcalico.org/v1/BlockAffinity apiextensions.k8s.io/v1 node.k8s.io/v1beta1 certificates.k8s.io/v1/CertificateSigningRequest authentication.k8s.io/v1 storage.k8s.io/v1beta1/StorageClass scheduling.k8s.io/v1beta1/PriorityClass crd.projectcalico.org/v1/BGPConfiguration v1 storage.k8s.io/v1beta1 v1/ConfigMap v1/ReplicationController batch/v1beta1/CronJob v1/Namespace authentication.k8s.io/v1/TokenReview crd.projectcalico.org/v1/CalicoNodeStatus batch/v1 networking.k8s.io/v1 policy/v1beta1 storage.k8s.io/v1beta1/CSIDriver apiextensions.k8s.io/v1beta1/CustomResourceDefinition authentication.k8s.io/v1beta1 v1/Secret storage.k8s.io/v1beta1/CSINode rbac.authorization.k8s.io/v1beta1/RoleBinding admissionregistration.k8s.io/v1/ValidatingWebhookConfiguration events.k8s.io/v1beta1 v1/Eviction v1/PodExecOptions authentication.k8s.io/v1beta1/TokenReview rbac.authorization.k8s.io/v1beta1/ClusterRoleBinding storage.k8s.io/v1 admissionregistration.k8s.io/v1beta1 v1/PodTemplate scheduling.k8s.io/v1 v1/ResourceQuota apiregistration.k8s.io/v1beta1/APIService apps/v1/ReplicaSet flowcontrol.apiserver.k8s.io/v1beta1 batch/v1/Job policy/v1beta1/PodDisruptionBudget apps/v1/StatefulSet crd.projectcalico.org/v1/ClusterInformation crd.projectcalico.org/v1 v1/PodPortForwardOptions v1/TokenRequest apps/v1/DaemonSet apps/v1/Deployment networking.k8s.io/v1/Ingress rbac.authorization.k8s.io/v1/ClusterRoleBinding events.k8s.io/v1 coordination.k8s.io/v1 authorization.k8s.io/v1beta1/SubjectAccessReview autoscaling/v1/HorizontalPodAutoscaler batch/v1/CronJob crd.projectcalico.org/v1/GlobalNetworkPolicy authorization.k8s.io/v1beta1 v1/Binding v1/NodeProxyOptions autoscaling/v2beta1/HorizontalPodAutoscaler networking.k8s.io/v1beta1/Ingress crd.projectcalico.org/v1/IPAMBlock crd.projectcalico.org/v1/KubeControllersConfiguration apiregistration.k8s.io/v1beta1 v1/ComponentStatus authorization.k8s.io/v1/LocalSubjectAccessReview apiextensions.k8s.io/v1/CustomResourceDefinition flowcontrol.apiserver.k8s.io/v1beta1/PriorityLevelConfiguration admissionregistration.k8s.io/v1/MutatingWebhookConfiguration flowcontrol.apiserver.k8s.io/v1beta1/FlowSchema node.k8s.io/v1 discovery.k8s.io/v1 v1/Pod authorization.k8s.io/v1 autoscaling/v1 networking.k8s.io/v1beta1 policy/v1 rbac.authorization.k8s.io/v1beta1 autoscaling/v2beta2/HorizontalPodAutoscaler certificates.k8s.io/v1beta1/CertificateSigningRequest storage.k8s.io/v1/StorageClass admissionregistration.k8s.io/v1 v1/LimitRange]" #返回kubernetes集群 API 版本信息集合
    62. value2: "v1.21.0" #用于获取kubernetes的版本号
    63. value3: "1" #获取kubernetes 的主版本号
    64. value4: "21" #获取kubernetes 的小版本号

    (5).调用Template对象:用于获取当前模板的信息,它包含如下两个对象

    1. [root@k8s-master-136 samve]# > /home/samve/mychart/templates/configmap.yaml
    2. [root@k8s-master-136 samve]# vim /home/samve/mychart/templates/configmap.yaml
    3. apiVersion: v1
    4. kind: ConfigMap
    5. metadata:
    6. name: {{.Release.Name}}-configmap
    7. namespace: {{.Release.Namespace}}
    8. data:
    9. value1: "{{.Template.Name}}" #用于获取当前模板的名称和路径(例如: mychart/templates/configmap.yaml)
    10. value2: "{{.Template.BasePath}}" #用于获取当前模板的路径(例如: mychart/templates)
    1. [root@k8s-master-136 samve]# helm install myconfigmap5 ./mychart/ --debug --dry-run #不真正执行,只是试运行看是否能运行
    2. install.go:173: [debug] Original chart version: ""
    3. install.go:190: [debug] CHART PATH: /home/samve/mychart
    4. NAME: myconfigmap5
    5. LAST DEPLOYED: Sun Oct 15 18:41:35 2023
    6. NAMESPACE: default
    7. STATUS: pending-install
    8. REVISION: 1
    9. TEST SUITE: None
    10. USER-SUPPLIED VALUES:
    11. {}
    12. COMPUTED VALUES:
    13. info:
    14. name2: test2
    15. name1: test1
    16. HOOKS:
    17. MANIFEST:
    18. ---
    19. # Source: mychart/templates/configmap.yaml
    20. apiVersion: v1
    21. kind: ConfigMap
    22. metadata:
    23. name: myconfigmap5-configmap
    24. namespace: default
    25. data:
    26. value1: "mychart/templates/configmap.yaml" #用于获取当前模板的名称和路径(例如: mychart/templates/configmap.yaml)
    27. value2: "mychart/templates" #用于获取当前模板的路径(例如: mychart/templates)

    四、helm3的常用命令

    1.helm的常用命令使用

    查看当前使用的Helm版本:helm version 

    Chart相关的操作:

    1. 根据关键字搜索Chart包:helm search
    2. 在Artifact Hub或自己的hub实例中搜索chart:helm search hub [KEYWORD] [flags]
    3. 用Chart中关键字搜索仓库:helm search repo [keyword] [flags]
    4. 创建新的Chart:helm create [CHARTNAME]
    5. 显示Chart的所有信息:helm show all [CHARTNAME]
    6. 检查Chart(目录、文件或URL)并显示Chart.yaml文件的内容:helm show chart [CHARTNAME] [flags]
    7. 检查Chart(目录、文件或URL)并显示values.yaml文件的内容:helm show values [CHARTNAME] [flags]
    8. 从仓库下载并(可选)在本地目录解压:helm pull [chart URL | repo/chartname] [...] [flags]
    9. 将Chart推送到远程:helm push
    10. 将Chart目录打包到Chart归档中:helm package [CHART]
    11. 验证打包好的Chart是否符合规范:helm lint [CHART]
    1. # 不带参数,列出所有
    2. helm search hub
    3. # 指定chart
    4. helm search hub mysql
    5. #用chart中关键字搜索仓库,搜索会读取系统上配置的所有仓库,并查找匹配。搜索这些仓库会使用存储在系统中的元数据。它会展示找到最新稳定版本的chart。如果指定了--devel参数,输出会包括预发布版本。
    6. helm search repo nginx
    7. helm search repo nginx --devel
    8. helm search repo nginx --version 13.2.13
    9. helm create mychart
    10. helm show all mychart
    11. helm show chart mychart
    12. helm show values mychart
    1. [root@k8s-master-144 samve]# helm search repo nginx
    2. apphub/nginx 15.10.2 1.25.3 NGINX Open Source is a web server that can be a...
    3. # --untar是解压, 不加就是压缩包
    4. [root@k8s-master-144 samve]# helm pull apphub/nginx --version 13.2.13 --untar

    Release相关操作: 

    1. 安装release实例: helm install [CHART] [FLAGS]
    2. 卸载一个release实例:helm uninstall RELEASE_NAME [...] [flags]
    3. 查看已安装的release实例:helm list
    4. 更新release实例:helm upgrade [RELEASE] [CHART] [FLAGS]

    5. 删除release实例:helm delete [RELEASE] [FLAGS]

    6. 查看指定已安装的release实例的详细信息:helm get [RELEASE] [FLAGS]
    7. 下载命名版本的所有信息:helm get all RELEASE_NAME [flags]
    8. 下载命名版本的values文件:helm get values RELEASE_NAME [flags]
    9. 获取指定已安装的release实例的资源信息:helm get manifest [RELEASE]
    10. 获取指定已安装的release实例的日志信息:helm get logs [RELEASE]
    11. 查看指定已安装的release实例的历史版本信息:helm history RELEASE_NAME [flags]
    12. 查看指定已安装的release实例的资源状态信息:helm status RELEASE_NAME [flags]
    13. 从之前版本回滚release实例,也可指定要回滚的版本号:helm rollback [REVISION] [flags]
    1. helm upgrade 实例名 chart名 --set imageTag=1.19
    2. helm upgrade 实例名 chart名 -f values.yaml文件
    helm rollback 实例名 版本号

    Repo相关操作: 

    1. 查看已下载的Chart仓库:helm repo list
    2. 添加新的Chart仓库:helm repo add [REPO_NAME] [REPO_URL]
    3. 删除已下载的Chart仓库:helm repo remove [REPO_NAME]
    4. 更新仓库(能更新添加的所有仓库):helm repo update
    5. 读取当前目录,并根据找到的 chart 生成索引文件。:helm repo index [DIR]

    2.helm添加chat仓库和查看仓库:类似yum仓库或docker仓库

    添加仓库:

    可以添加多个仓库,添加仓库时候,记得起个仓库名,如: stable,aliyun,或其他,一般起个稳定版的stable会优先使用

    1. # helm repo add stable http://mirrorazure.cn/kubernetes/charts #添加微软的,强烈推荐
    2. # helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts #添加阿里云的
    3. # helm repo add test-repo http://mirror.kaiyuanshe.cn/kubernetes/charts #添加开源社区的
    4. # helm repo list #列出仓库

    微软仓库(http://mirror.azure.cn/kubernetes/charts/)

    阿里云仓库(https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts )

    推荐仓库(https://charts.bitnami.com/bitnami  )

    添加仓库  :helm repo add 自定义仓库名 仓库地址  (仓库地址就是上面几个网站地址)

    [root@k8s-master-136 samve]# helm repo add apphub https://charts.bitnami.com/bitnami

    删除仓库 : helm repo remove 仓库名    

    查看仓库 : helm repo list

    搜索仓库的chart: helm  search repo chart名称

    更新仓库:

    # helm repo update #更新仓库,能更新添加的所有仓库

    3.创建chart包:

    # helm create mychart #创建一个chart包,chart包名为: mychart

    4.搜索远程仓库的chart包、查看chart包信息、拉取远程仓库chart包到本地,类似docker search 搜索harbor仓库的镜像

    (1)搜索chart包格式: helm search repo chart包名

    示例:

    # helm search repo tomcat

    (2).查看chart包格式: 

    helm show chart chart包名或 helm show values chart包名 (查看详细信息)

    示例:

    # helm show chart stable/tomcat #查看chart包基础信息

    # helm show values stable/tomcat #查看chart包详细信息

    (3).拉取chart包格式:

    # helm pull 远程仓库cart包名 --version 0.4.3 --untar #从远程仓库拉取指定版本的chart包到本地并解压,-untar是解压,不加就是压缩包
    # helm pull 远程仓库chart包名 --untar #从远程仓库拉取最新版本的chart包到本地并解压,-untar是解压,不加就是压缩包

    5.helm通过各种类型chart包安装一个release实例名来部署k8s相关的资源(如: pod,deployment,svc,ingress等,根据模板文件定义)

    1).从加入到本地的chart官方仓库(从官方仓库在线安装)安装release实例
    2).将从chart仓库拉下来的压缩包进行安装release实例 (下载好的压缩包本地离线安装release)
    3),将从chat仓库拉下来的压缩包解压后,从解压目录安装release实例(解压下载好的压缩包,从解压目录离线安装release实例)
    4).从一个网络地址(如http服务器) 仓库压缩包直接安装release实例
    5).在本地创建一个chat包,通过自定义编辑自己的yaml文件后,通过本地chart包进行安装release

    1. #helm search repo tomcat
    2. #helm install tomcat1 stable/tomcat #从加入到本地的chart社区仓库(从官方仓库在线安装)安装release实例,tomcat1release实例名
    3. #helm install tomcat2 tomcat-0.4.3.tgz #从chart仓库拉下来的压缩包进行安装release实例(从本地存档文件离线安装),
    4. #helm install tomcat3 tomcat #从chart仓库拉下来的压缩包解压后,从解压目录安装release实例(从解压目录离线安装),
    5. #helm install dhb htp://ur.../mysa-1.6.9.toz #从一个网络地址库乐缩包直接安装reease实例(从下载服务器安装),db为release实例名

    卸载release实例:

    #helm uninstall release实例名

    6.helm基本命令使用(主要三个命令)

    install:安装release实例 (实际就是k8s应用的安装)
    upgrade:升级release实例 (实际就是k8s应用的升级)
    rollback:回滚release实例 (实际就是k8s应用的回滚)

    五、helm3的部署应用实战案例(发布、升级、回滚、卸载)

    1、helm基本命令使用(主要三个命令)

    install:安装release实例 (实际就是k8s应用的安装)
    upgrade:升级release实例 (实际就是k8s应用的升级)
    rollback:回滚release实例 (实际就是k8s应用的回滚)

    安装release实例:(上面已经介绍了从多种角度进行安装release实例,下面我们以其中一种进行安装演示)

    • # helm create chart名 #指定chart名,创建一个chart包,通过对chat里template的yaml文件自定义编写成自己需要的后再进行安装
    • # helm install release实例名 chart目录路径 #指定release实例名和chart包用录路径进行安装

    release实例

    示例:

    • # helm create mychart #创建一个chart包,chart包名为: mychart
    • # helm install test-release ./mychart #指定release实例名和chart包目录路径进行安装release实例

    升级release实例:

    • # helm upgrade release实例名 chart名 --set imageTag=1.19 #指定release名和chart名进行相关set设置的升级
    • # helm upgrade release实例名 chart名 -f /.../mychart/values.yaml #指定release示例名和chart名和values.yaml文件升级

    示例:

    • # helm upgrade test-release-nginx mychart --set imageTag=1.19 #指定release实例名和chart名set升级
    • # helm upgrade test-release-nginx mychat -f /root/hem//mychart/values,yaml #指定release示例名和chart名和values.yaml文件升级

    回滚release实例:

    • # helm rollback release实例名 #指定release实例名,回滚到上一个版本
    • # helm rollback release实例名 #指定release实例名,回滚到指定版本,注意版本号是release的版本号,不是镜像版本号

    示例:

    1. # helm rollback web-nginx
    2. # helm rollback web-nginx 1.17.10

    获取release实例历史:

    # helm history release实例名

    示例:

    # helm history test #testrelease实例名

    卸载release实例:

    #helm uninstall release实例名

    示例:

    #helm uninstall test-release-nginx #uninstall 直接跟release名,卸载release实例

    2.helm3部署自定义的应用实战案例 (发布、升级、回滚、卸载)

    以部署nginx服务为例,其他应用都类似

    1).准备环境 k8s集群

    1. [root@k8s-master-136 ~]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 261d v1.21.0
    4. k8s-node-135 Ready <none> 261d v1.21.0
    5. k8s-node-137 Ready <none> 261d v1.21.0

    2).创建一个模板的chart包,删除原来的内容,自定义成我们自己需要的内容,后面我们自定义部署的yaml文件

    1. [root@k8s-master-136 ~]# helm create nginx-chart
    2. Creating nginx-chart

    自定义部署的模板yaml文件:

    1. rm -rf templates/*
    2. vim templates/nginx-deploy-service.yaml #自定义需要的yaml模板文件,deployment和svc,通过nodeport暴露

    nginx-deploy-service.yaml 

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: {{.Values.deployment_name}}
    5. spec:
    6. replicas: {{.Values.replicas}}
    7. selector:
    8. matchLabels:
    9. app: {{.Values.pod_label}}
    10. template:
    11. metadata:
    12. labels:
    13. app: {{.Values.pod_label}}
    14. spec:
    15. containers:
    16. - image: {{.Values.image}}:{{.Values.imageTag}}
    17. name: {{.Values.container_name}}
    18. ports:
    19. - containerPort: {{.Values.containerport}}
    20. ---
    21. apiVersion: v1
    22. kind: Service
    23. metadata:
    24. name: {{.Values.service_name}}
    25. namespace: {{.Values.namespace}}
    26. spec:
    27. type: NodePort
    28. ports:
    29. - port: {{.Values.port}}
    30. targetPort: {{.Values.targetport}}
    31. nodePort: {{.Values.nodeport}}
    32. protocol: TCP
    33. selector:
    34. app: {{.Values.pod_label}}
    1. [root@k8s-master-136 nginx-chart]# vim values.yaml
    2. deployment_name: nginx-deployment
    3. replicas: 2
    4. pod_label: nginx-pod-label
    5. image: nginx
    6. imageTag: 1.17
    7. container_name: nginx-container
    8. service_name: nginx-service
    9. namespace: default
    10. port: 80
    11. targetport: 80
    12. containerport: 80
    13. nodeport: 30001

    3)通过chart包安装一个release实例 (即部署一个k8s的nginx应用,版本为:1.17) 

    1. [root@k8s-master-136 samve]# helm install nginx-rerease-6 ./nginx-chart/
    2. NAME: nginx-rerease-6
    3. LAST DEPLOYED: Tue Oct 17 22:27:36 2023
    4. NAMESPACE: default
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None
    1. [root@k8s-master-136 samve]# helm list
    2. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
    3. my-nginx default 1 2023-10-12 22:16:31.191334159 +0800 CST deployed nginx-15.3.3 1.25.2
    4. myconfigriap2 default 1 2023-10-15 16:55:28.122607196 +0800 CST deployed mychart-0.1.0 1.16.0
    5. ng-ingress default 1 2023-10-13 21:45:55.931561498 +0800 CST deployed nginx-ingress-controller-9.9.0 1.9.0
    6. nginx default 1 2023-10-12 21:52:49.36634682 +0800 CST deployed nginx-intel-2.1.15 0.4.9
    7. nginx-rerease default 1 2023-10-15 22:19:44.65435695 +0800 CST failed nginx-chart-0.1.0 1.16.0
    8. nginx-rerease-0 default 1 2023-10-15 22:43:00.564071169 +0800 CST failed nginx-chart-0.1.0 1.16.0
    9. nginx-rerease-3 default 1 2023-10-15 22:42:18.473364862 +0800 CST failed nginx-chart-0.1.0 1.16.0
    10. nginx-rerease-6 default 1 2023-10-17 22:27:36.335540732 +0800 CST deployed nginx-chart-0.1.0 1.16.0
    11. nginx-rerease2 default 1 2023-10-15 22:39:03.4086732 +0800 CST failed nginx-chart-0.1.0 1.16.0
    12. nginx-rerease3 default 1 2023-10-15 22:41:08.782652045 +0800 CST failed nginx-chart-0.1.0 1.16.0
    13. web default 1 2023-10-12 22:55:28.525512768 +0800 CST deployed mychart-0.1.0 1.16.0
    14. [root@k8s-master-136 samve]# kubectl get pod,svc,ep
    15. NAME READY STATUS RESTARTS AGE
    16. pod/my-nginx-8568bb4694-gn5b7 1/1 Running 4 5d
    17. pod/ng-ingress-nginx-ingress-controller-65b5bc8846-6rtfc 1/1 Running 4 4d
    18. pod/ng-ingress-nginx-ingress-controller-default-backend-6d5bc97bt5v 1/1 Running 3 4d
    19. pod/nginx-2-69947fd9df-plct2 1/1 Running 6 7d
    20. pod/nginx-6799fc88d8-2jh5z 1/1 Running 6 7d
    21. pod/nginx-deployment-5c8469b67f-24pq9 1/1 Running 0 4m50s
    22. pod/nginx-deployment-5c8469b67f-2j9s7 1/1 Running 0 4m50s
    23. pod/nginx-nginx-intel-668b58fb4b-8cm6z 0/1 ImagePullBackOff 0 5d
    24. pod/nginx1-b7fb675cb-rhtvn 0/1 CrashLoopBackOff 157 7d
    25. pod/nginx2-74ff6c9fbc-2gb7r 0/1 CrashLoopBackOff 156 7d
    26. pod/web-mychart-5f94885968-8hmz5 1/1 Running 4 4d23h
    27. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    28. service/kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 263d
    29. service/my-nginx LoadBalancer 10.10.200.7 <pending> 80:32374/TCP 5d
    30. service/ng-ingress-nginx-ingress-controller LoadBalancer 10.10.129.100 <pending> 80:31660/TCP,443:30713/TCP 4d
    31. service/ng-ingress-nginx-ingress-controller-default-backend ClusterIP 10.10.184.181 <none> 80/TCP 4d
    32. service/nginx NodePort 10.10.183.194 <none> 80:30111/TCP 263d
    33. service/nginx-nginx-intel LoadBalancer 10.10.72.168 <pending> 80:32756/TCP,443:30799/TCP 5d
    34. service/nginx-service NodePort 10.10.78.52 <none> 80:30001/TCP 4m50s
    35. service/web-mychart ClusterIP 10.10.155.71 <none> 80/TCP 4d23h
    36. NAME ENDPOINTS AGE
    37. endpoints/kubernetes 192.168.85.136:6443 263d
    38. endpoints/my-nginx 10.18.112.63:8080 5d
    39. endpoints/ng-ingress-nginx-ingress-controller 10.18.185.90:443,10.18.185.90:80 4d
    40. endpoints/ng-ingress-nginx-ingress-controller-default-backend 10.18.112.22:8080 4d
    41. endpoints/nginx 10.18.185.102:80 263d
    42. endpoints/nginx-nginx-intel 5d
    43. endpoints/nginx-service 10.18.112.23:80,10.18.185.88:80 4m50s
    44. endpoints/web-mychart 10.18.185.112:80 4d23h
    45. [root@k8s-master-136 samve]# kubectl get pod nginx-deployment-5c8469b67f-24pq9 -o yaml | grep image: #查看部署pod的镜像版本
    46. - image: nginx:1.17
    47. image: nginx:1.17

    4)升级release实例版本 (将nginx版本1.17升级为1.20.0)

    1. [root@k8s-master-136 samve]# vim nginx-chart/values.yaml
    2. deployment_name: nginx-deployment
    3. replicas: 2
    4. pod_label: nginx-pod-label
    5. image: nginx
    6. imageTag: 1.20
    7. container_name: nginx-container
    8. service_name: nginx-service
    9. namespace: default
    10. port: 80
    11. targetport: 80
    12. containerport: 80
    13. nodeport: 30001
    14. [root@k8s-master-136 samve]# helm upgrade nginx-rerease-6 nginx-chart -f /home/samve/nginx-chart/values.yaml #指定release实例名和chart名和values.yaml文件升级
    15. Release "nginx-rerease-6" has been upgraded. Happy Helming!
    16. NAME: nginx-rerease-6
    17. LAST DEPLOYED: Tue Oct 17 22:40:59 2023
    18. NAMESPACE: default
    19. STATUS: deployed
    20. REVISION: 2
    21. TEST SUITE: None
    22. [root@k8s-master-136 samve]# helm list
    23. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
    24. my-nginx default 1 2023-10-12 22:16:31.191334159 +0800 CST deployed nginx-15.3.3 1.25.2
    25. myconfigriap2 default 1 2023-10-15 16:55:28.122607196 +0800 CST deployed mychart-0.1.0 1.16.0
    26. ng-ingress default 1 2023-10-13 21:45:55.931561498 +0800 CST deployed nginx-ingress-controller-9.9.0 1.9.0
    27. nginx default 1 2023-10-12 21:52:49.36634682 +0800 CST deployed nginx-intel-2.1.15 0.4.9
    28. nginx-rerease default 1 2023-10-15 22:19:44.65435695 +0800 CST failed nginx-chart-0.1.0 1.16.0
    29. nginx-rerease-0 default 1 2023-10-15 22:43:00.564071169 +0800 CST failed nginx-chart-0.1.0 1.16.0
    30. nginx-rerease-3 default 1 2023-10-15 22:42:18.473364862 +0800 CST failed nginx-chart-0.1.0 1.16.0
    31. nginx-rerease-6 default 2 2023-10-17 22:40:59.155113485 +0800 CST deployed nginx-chart-0.1.0 1.16.0
    32. nginx-rerease2 default 1 2023-10-15 22:39:03.4086732 +0800 CST failed nginx-chart-0.1.0 1.16.0
    33. nginx-rerease3 default 1 2023-10-15 22:41:08.782652045 +0800 CST failed nginx-chart-0.1.0 1.16.0
    34. web default 1 2023-10-12 22:55:28.525512768 +0800 CST deployed mychart-0.1.0 1.16.0
    35. [root@k8s-master-136 samve]# kubectl get pod,svc,ep
    36. NAME READY STATUS RESTARTS AGE
    37. pod/my-nginx-8568bb4694-gn5b7 1/1 Running 4 5d
    38. pod/ng-ingress-nginx-ingress-controller-65b5bc8846-6rtfc 1/1 Running 4 4d
    39. pod/ng-ingress-nginx-ingress-controller-default-backend-6d5bc97bt5v 1/1 Running 3 4d
    40. pod/nginx-2-69947fd9df-plct2 1/1 Running 6 7d
    41. pod/nginx-6799fc88d8-2jh5z 1/1 Running 6 7d
    42. pod/nginx-deployment-5c8469b67f-24pq9 1/1 Running 0 18m
    43. pod/nginx-deployment-5c8469b67f-2j9s7 1/1 Running 0 18m
    44. pod/nginx-deployment-7fb7865bcf-fpr6q 0/1 ImagePullBackOff 0 4m52s
    45. pod/nginx-nginx-intel-668b58fb4b-8cm6z 0/1 ImagePullBackOff 0 5d
    46. pod/nginx1-b7fb675cb-rhtvn 0/1 CrashLoopBackOff 159 7d
    47. pod/nginx2-74ff6c9fbc-2gb7r 0/1 CrashLoopBackOff 158 7d
    48. pod/web-mychart-5f94885968-8hmz5 1/1 Running 4 4d23h
    49. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    50. service/kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 264d
    51. service/my-nginx LoadBalancer 10.10.200.7 <pending> 80:32374/TCP 5d
    52. service/ng-ingress-nginx-ingress-controller LoadBalancer 10.10.129.100 <pending> 80:31660/TCP,443:30713/TCP 4d
    53. service/ng-ingress-nginx-ingress-controller-default-backend ClusterIP 10.10.184.181 <none> 80/TCP 4d
    54. service/nginx NodePort 10.10.183.194 <none> 80:30111/TCP 264d
    55. service/nginx-nginx-intel LoadBalancer 10.10.72.168 <pending> 80:32756/TCP,443:30799/TCP 5d
    56. service/nginx-service NodePort 10.10.78.52 <none> 80:30001/TCP 18m
    57. service/web-mychart ClusterIP 10.10.155.71 <none> 80/TCP 4d23h
    58. NAME ENDPOINTS AGE
    59. endpoints/kubernetes 192.168.85.136:6443 264d
    60. endpoints/my-nginx 10.18.112.63:8080 5d
    61. endpoints/ng-ingress-nginx-ingress-controller 10.18.185.90:443,10.18.185.90:80 4d
    62. endpoints/ng-ingress-nginx-ingress-controller-default-backend 10.18.112.22:8080 4d
    63. endpoints/nginx 10.18.185.102:80 264d
    64. endpoints/nginx-nginx-intel 5d
    65. endpoints/nginx-service 10.18.112.23:80,10.18.185.88:80 18m
    66. endpoints/web-mychart 10.18.185.112:80 4d23h
    67. [root@k8s-master-136 samve]# kubectl get pod nginx-deployment-5c8469b67f-24pq9 -o yaml | grep image:
    68. - image: nginx:1.17
    69. image: nginx:1.17

    5)回滚release实例版本 (将nginx版本1千0.回滚为1.17) 

    a)回滚到上一个版本:

    1. [root@k8s-master-136 samve]# helm rollback nginx-rerease-6
    2. Rollback was a success! Happy Helming!
    3. [root@k8s-master-136 samve]# helm list
    4. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
    5. my-nginx default 1 2023-10-12 22:16:31.191334159 +0800 CST deployed nginx-15.3.3 1.25.2
    6. myconfigriap2 default 1 2023-10-15 16:55:28.122607196 +0800 CST deployed mychart-0.1.0 1.16.0
    7. ng-ingress default 1 2023-10-13 21:45:55.931561498 +0800 CST deployed nginx-ingress-controller-9.9.0 1.9.0
    8. nginx default 1 2023-10-12 21:52:49.36634682 +0800 CST deployed nginx-intel-2.1.15 0.4.9
    9. nginx-rerease default 1 2023-10-15 22:19:44.65435695 +0800 CST failed nginx-chart-0.1.0 1.16.0
    10. nginx-rerease-0 default 1 2023-10-15 22:43:00.564071169 +0800 CST failed nginx-chart-0.1.0 1.16.0
    11. nginx-rerease-3 default 1 2023-10-15 22:42:18.473364862 +0800 CST failed nginx-chart-0.1.0 1.16.0
    12. nginx-rerease-6 default 4 2023-10-17 22:51:47.58210453 +0800 CST deployed nginx-chart-0.1.0 1.16.0
    13. nginx-rerease2 default 1 2023-10-15 22:39:03.4086732 +0800 CST failed nginx-chart-0.1.0 1.16.0
    14. nginx-rerease3 default 1 2023-10-15 22:41:08.782652045 +0800 CST failed nginx-chart-0.1.0 1.16.0
    15. web default 1 2023-10-12 22:55:28.525512768 +0800 CST deployed mychart-0.1.0 1.16.0
    16. [root@k8s-master-136 samve]# kubectl get pod,svc,ep #回滚后查看
    17. NAME READY STATUS RESTARTS AGE
    18. pod/my-nginx-8568bb4694-gn5b7 1/1 Running 4 5d
    19. pod/ng-ingress-nginx-ingress-controller-65b5bc8846-6rtfc 1/1 Running 4 4d1h
    20. pod/ng-ingress-nginx-ingress-controller-default-backend-6d5bc97bt5v 1/1 Running 3 4d1h
    21. pod/nginx-2-69947fd9df-plct2 1/1 Running 6 7d
    22. pod/nginx-6799fc88d8-2jh5z 1/1 Running 6 7d
    23. pod/nginx-deployment-5c8469b67f-24pq9 1/1 Running 0 24m
    24. pod/nginx-deployment-5c8469b67f-2j9s7 1/1 Running 0 24m
    25. pod/nginx-deployment-7fb7865bcf-fpr6q 0/1 ImagePullBackOff 0 11m
    26. pod/nginx-nginx-intel-668b58fb4b-8cm6z 0/1 ImagePullBackOff 0 5d
    27. pod/nginx1-b7fb675cb-rhtvn 0/1 CrashLoopBackOff 160 7d
    28. pod/nginx2-74ff6c9fbc-2gb7r 0/1 CrashLoopBackOff 159 7d
    29. pod/web-mychart-5f94885968-8hmz5 1/1 Running 4 4d23h
    30. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    31. service/kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 264d
    32. service/my-nginx LoadBalancer 10.10.200.7 <pending> 80:32374/TCP 5d
    33. service/ng-ingress-nginx-ingress-controller LoadBalancer 10.10.129.100 <pending> 80:31660/TCP,443:30713/TCP 4d1h
    34. service/ng-ingress-nginx-ingress-controller-default-backend ClusterIP 10.10.184.181 <none> 80/TCP 4d1h
    35. service/nginx NodePort 10.10.183.194 <none> 80:30111/TCP 264d
    36. service/nginx-nginx-intel LoadBalancer 10.10.72.168 <pending> 80:32756/TCP,443:30799/TCP 5d
    37. service/nginx-service NodePort 10.10.78.52 <none> 80:30001/TCP 24m
    38. service/web-mychart ClusterIP 10.10.155.71 <none> 80/TCP 4d23h
    39. NAME ENDPOINTS AGE
    40. endpoints/kubernetes 192.168.85.136:6443 264d
    41. endpoints/my-nginx 10.18.112.63:8080 5d
    42. endpoints/ng-ingress-nginx-ingress-controller 10.18.185.90:443,10.18.185.90:80 4d1h
    43. endpoints/ng-ingress-nginx-ingress-controller-default-backend 10.18.112.22:8080 4d1h
    44. endpoints/nginx 10.18.185.102:80 264d
    45. endpoints/nginx-nginx-intel 5d
    46. endpoints/nginx-service 10.18.112.23:80,10.18.185.88:80 24m
    47. endpoints/web-mychart 10.18.185.112:80 4d23h

    b)回滚到指定版本 

    1. [root@k8s-master-136 samve]# helm rollback nginx-rerease-6 2
    2. Rollback was a success! Happy Helming!
    3. [root@k8s-master-136 samve]# kubectl get pod,svc,ep
    4. NAME READY STATUS RESTARTS AGE
    5. pod/my-nginx-8568bb4694-gn5b7 1/1 Running 4 5d
    6. pod/ng-ingress-nginx-ingress-controller-65b5bc8846-6rtfc 1/1 Running 4 4d1h
    7. pod/ng-ingress-nginx-ingress-controller-default-backend-6d5bc97bt5v 1/1 Running 3 4d1h
    8. pod/nginx-2-69947fd9df-plct2 1/1 Running 6 7d
    9. pod/nginx-6799fc88d8-2jh5z 1/1 Running 6 7d
    10. pod/nginx-deployment-5c8469b67f-24pq9 1/1 Running 0 27m
    11. pod/nginx-deployment-5c8469b67f-2j9s7 1/1 Running 0 27m
    12. pod/nginx-deployment-7fb7865bcf-fpr6q 0/1 ImagePullBackOff 0 14m
    13. pod/nginx-nginx-intel-668b58fb4b-8cm6z 0/1 ImagePullBackOff 0 5d1h
    14. pod/nginx1-b7fb675cb-rhtvn 0/1 CrashLoopBackOff 161 7d
    15. pod/nginx2-74ff6c9fbc-2gb7r 0/1 CrashLoopBackOff 160 7d
    16. pod/web-mychart-5f94885968-8hmz5 1/1 Running 4 4d23h
    17. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    18. service/kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 264d
    19. service/my-nginx LoadBalancer 10.10.200.7 <pending> 80:32374/TCP 5d
    20. service/ng-ingress-nginx-ingress-controller LoadBalancer 10.10.129.100 <pending> 80:31660/TCP,443:30713/TCP 4d1h
    21. service/ng-ingress-nginx-ingress-controller-default-backend ClusterIP 10.10.184.181 <none> 80/TCP 4d1h
    22. service/nginx NodePort 10.10.183.194 <none> 80:30111/TCP 264d
    23. service/nginx-nginx-intel LoadBalancer 10.10.72.168 <pending> 80:32756/TCP,443:30799/TCP 5d1h
    24. service/nginx-service NodePort 10.10.78.52 <none> 80:30001/TCP 27m
    25. service/web-mychart ClusterIP 10.10.155.71 <none> 80/TCP 4d23h
    26. NAME ENDPOINTS AGE
    27. endpoints/kubernetes 192.168.85.136:6443 264d
    28. endpoints/my-nginx 10.18.112.63:8080 5d
    29. endpoints/ng-ingress-nginx-ingress-controller 10.18.185.90:443,10.18.185.90:80 4d1h
    30. endpoints/ng-ingress-nginx-ingress-controller-default-backend 10.18.112.22:8080 4d1h
    31. endpoints/nginx 10.18.185.102:80 264d
    32. endpoints/nginx-nginx-intel 5d1h
    33. endpoints/nginx-service 10.18.112.23:80,10.18.185.88:80 27m
    34. endpoints/web-mychart 10.18.185.112:80 4d23h
    35. [root@k8s-master-136 samve]# kubectl get pod nginx-deployment-5c8469b67f-24pq9 -o yaml | grep image:
    36. - image: nginx:1.17
    37. image: nginx:1.17

    6)卸载删除release实例

    1. [root@k8s-master-136 samve]# helm uninstall nginx-rerease-6
    2. release "nginx-rerease-6" uninstalled
    3. [root@k8s-master-136 samve]# helm list
    4. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
    5. my-nginx default 1 2023-10-12 22:16:31.191334159 +0800 CST deployed nginx-15.3.3 1.25.2
    6. myconfigriap2 default 1 2023-10-15 16:55:28.122607196 +0800 CST deployed mychart-0.1.0 1.16.0
    7. ng-ingress default 1 2023-10-13 21:45:55.931561498 +0800 CST deployed nginx-ingress-controller-9.9.0 1.9.0
    8. nginx default 1 2023-10-12 21:52:49.36634682 +0800 CST deployed nginx-intel-2.1.15 0.4.9
    9. nginx-rerease default 1 2023-10-15 22:19:44.65435695 +0800 CST failed nginx-chart-0.1.0 1.16.0
    10. nginx-rerease-0 default 1 2023-10-15 22:43:00.564071169 +0800 CST failed nginx-chart-0.1.0 1.16.0
    11. nginx-rerease-3 default 1 2023-10-15 22:42:18.473364862 +0800 CST failed nginx-chart-0.1.0 1.16.0
    12. nginx-rerease2 default 1 2023-10-15 22:39:03.4086732 +0800 CST failed nginx-chart-0.1.0 1.16.0
    13. nginx-rerease3 default 1 2023-10-15 22:41:08.782652045 +0800 CST failed nginx-chart-0.1.0 1.16.0
    14. web default 1 2023-10-12 22:55:28.525512768 +0800 CST deployed mychart-0.1.0 1.16.0

    六、使用helm快速部署一个应用

    使用搜索命令搜索应用

    helm search repo 应用名称

    1. [root@k8s-master-136 samve]# helm search repo nginx
    2. NAME CHART VERSION APP VERSION DESCRIPTION
    3. apphub/nginx 15.3.3 1.25.2 NGINX Open Source is a web server that can be a...
    4. apphub/nginx-ingress-controller 9.9.0 1.9.0 NGINX Ingress Controller is an Ingress controll...
    5. apphub/nginx-intel 2.1.15 0.4.9 DEPRECATED NGINX Open Source for Intel is a lig...

    根据搜索内容选择安装

    helm install 安装后应用的名称 搜索之后应用的名称

    1. [root@k8s-master-136 samve]# helm install my-nginx apphub/nginx
    2. NAME: my-nginx
    3. LAST DEPLOYED: Thu Oct 12 22:16:31 2023
    4. NAMESPACE: default
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None
    8. NOTES:
    9. CHART NAME: nginx
    10. CHART VERSION: 15.3.3
    11. APP VERSION: 1.25.2
    12. ** Please be patient while the chart is being deployed **
    13. NGINX can be accessed through the following DNS name from within your cluster:
    14. my-nginx.default.svc.cluster.local (port 80)
    15. To access NGINX from outside the cluster, follow the steps below:
    16. 1. Get the NGINX URL by running these commands:
    17. NOTE: It may take a few minutes for the LoadBalancer IP to be available.
    18. Watch the status with: 'kubectl get svc --namespace default -w my-nginx'
    19. export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services my-nginx)
    20. export SERVICE_IP=$(kubectl get svc --namespace default my-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    21. echo "http://${SERVICE_IP}:${SERVICE_PORT}"

    查看安装之后的状态

    helm list

    1. [root@k8s-master-136 samve]# helm list
    2. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
    3. my-nginx default 1 2023-10-12 22:16:31.191334159 +0800 CST deployed nginx-15.3.3 1.25.2
    4. nginx default 1 2023-10-12 21:52:49.36634682 +0800 CST deployed nginx-intel-2.1.15 0.4.9

    helm status 安装之后应用的名称

    1. [root@k8s-master-136 samve]# helm status my-nginx
    2. NAME: my-nginx
    3. LAST DEPLOYED: Thu Oct 12 22:16:31 2023
    4. NAMESPACE: default
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None
    8. NOTES:
    9. CHART NAME: nginx
    10. CHART VERSION: 15.3.3
    11. APP VERSION: 1.25.2
    12. ** Please be patient while the chart is being deployed **
    13. NGINX can be accessed through the following DNS name from within your cluster:
    14. my-nginx.default.svc.cluster.local (port 80)
    15. To access NGINX from outside the cluster, follow the steps below:
    16. 1. Get the NGINX URL by running these commands:
    17. NOTE: It may take a few minutes for the LoadBalancer IP to be available.
    18. Watch the status with: 'kubectl get svc --namespace default -w my-nginx'
    19. export SERVICE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].port}" services my-nginx)
    20. export SERVICE_IP=$(kubectl get svc --namespace default my-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    21. echo "http://${SERVICE_IP}:${SERVICE_PORT}"

    当然我们也可以通过kubectl命令查看相关的pod是否创建成功

    1. [root@k8s-master-136 samve]# kubectl get svc,pod
    2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    3. service/kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 258d
    4. service/my-nginx LoadBalancer 10.10.200.7 <pending> 80:32374/TCP 9m37s
    5. service/nginx NodePort 10.10.183.194 <none> 80:30111/TCP 258d
    6. service/nginx-nginx-intel LoadBalancer 10.10.72.168 <pending> 80:32756/TCP,443:30799/TCP 33m
    7. NAME READY STATUS RESTARTS AGE
    8. pod/my-nginx-8568bb4694-gn5b7 1/1 Running 0 9m37s
    9. pod/nginx-2-69947fd9df-plct2 1/1 Running 2 47h
    10. pod/nginx-6799fc88d8-2jh5z 1/1 Running 2 47h
    11. pod/nginx-nginx-intel-668b58fb4b-8cm6z 0/1 ImagePullBackOff 0 33m
    12. pod/nginx1-b7fb675cb-rhtvn 0/1 CrashLoopBackOff 50 47h
    13. pod/nginx2-74ff6c9fbc-2gb7r 0/1 CrashLoopBackOff 50 47h

    七、实例:使用helm部署一个web应用

    1、准备环境 k8s集群

    1. [root@k8s-master-136 ~]# kubectl get node
    2. NAME STATUS ROLES AGE VERSION
    3. k8s-master-136 Ready control-plane,master 296d v1.21.0
    4. k8s-node-135 Ready <none> 296d v1.21.0
    5. k8s-node-137 Ready <none> 296d v1.21.0

    2、创建一个模板的chart包,删除原来的内容,自定义成我们自己需要的内容,后面我们自定义部署的yaml文件

    1. [root@k8s-master-136 ~]# helm create nginx-chart
    2. Creating nginx-chart

    自定义部署的模板yaml文件:

    vim templates/nginx-deploy-service.yaml

    nginx-deploy-service.yaml

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: {{.Values.namespace}}
    5. ---
    6. apiVersion: apps/v1
    7. kind: Deployment
    8. metadata:
    9. name: {{.Values.deployment_name}}
    10. namespace: {{.Values.namespace}}
    11. spec:
    12. replicas: {{.Values.replicas}}
    13. selector:
    14. matchLabels:
    15. app: {{.Values.pod_label}}
    16. template:
    17. metadata:
    18. labels:
    19. app: {{.Values.pod_label}}
    20. spec:
    21. containers:
    22. - name: {{.Values.pod_label}}
    23. image: registry.cn-hangzhou.aliyuncs.com/samve/k8s:2.0
    24. ports:
    25. - name: http
    26. containerPort: {{.Values.containerport}}
    27. ---
    28. apiVersion: v1
    29. kind: Service
    30. metadata:
    31. name: {{.Values.service_name}}
    32. namespace: {{.Values.namespace}}
    33. spec:
    34. selector:
    35. app: {{.Values.pod_label}}
    36. ports:
    37. - name: http
    38. protocol: TCP
    39. port: {{.Values.port}}
    40. targetPort: {{.Values.targetport}}
    41. nodePort: {{.Values.nodeport}}
    42. type: NodePort
    [root@k8s-master-136 nginx-chart]# vim values.yaml 

    values.yaml 

    1. deployment_name: manager-web
    2. service_name: manager-web-service
    3. namespace: shop-web
    4. pod_label: manager-web
    5. replicas: 2
    6. port: 80
    7. targetport: 80
    8. containerport: 80
    9. nodeport: 30002

    3、通过chart包安装一个release实例

    1. [root@k8s-master-136 ~]# helm install nginx-service ./nginx-chart
    2. NAME: nginx-service
    3. LAST DEPLOYED: Sun Nov 19 21:50:28 2023
    4. NAMESPACE: default
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None
    1. [root@k8s-master-136 nginx-chart]# vim values.yaml
    2. [root@k8s-master-136 nginx-chart]# helm list
    3. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
    4. nginx-service default 1 2023-11-19 21:50:28.744590789 +0800 CST deployed nginx-chart-0.1.0 1.16.0

    参考:

    Helm

  • 相关阅读:
    2022官方发布辐轮王土拨鼠全球十大顶级公路自行车品牌排行榜
    Servlet的使用手把手教学(一)
    C++笔记 - - list的模拟实现和使用
    Java开发实习生面试—附简历以及面试题
    C++中的双端队列(deque)
    谷粒商城-基础篇(项目简介&项目搭建)
    Django序列化器中is_valid和validate
    CFdiv2-Common Number-(奇偶数二分+规律)
    Canny算子与霍夫变换检测圆与直线
    Speech | openSMILE语音特征提取工具
  • 原文地址:https://blog.csdn.net/duansamve/article/details/133800141