ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

kubernetes之helm详解

2021-09-18 15:04:16  阅读:439  来源: 互联网

标签:kubernetes chart 详解 helm world k8s root hello


Helm 官方文档:https://helm.sh/zh/docs/

本文使用的 Helm 的版本为 3.4.1,Kubernets版本为1.19

一、概述

在没使用 helm 之前,向 kubernetes 部署应用,我们要依次部署 deployment、svc 等,步骤较繁琐。况且随着很多项目微服务化,复杂的应用在容器中部署以及管理显得较为复杂,helm 通过打包的方式,支持发布的版本管理和控制,很大程度上简化了 Kubernetes 应用的部署和管理

Helm 本质就是让 K8s 的应用管理(Deployment,Service 等 ) 可配置,能动态生成。通过动态生成 K8s 资源清单文件(deployment.yaml,service.yaml)。然后调用 Kubectl 自动执行 K8s 资源部署

Helm 是官方提供的类似于 YUM 的包管理器,是部署环境的流程封装。Helm 有两个重要的概念:chart 和 release

  • chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板、参数定义、依赖关系、文档说明等。chart 是应用部署的自包含逻辑单元。可以将 chart 想象成 apt、yum 中的软件安装包
  • release 是 chart 的运行实例,代表了一个正在运行的应用。当 chart 被安装到 Kubernetes 集群,就生成一个 release。chart 能够多次安装到同一个集群,每次安装都是一个 release

Helm 包含两个组件:Helm 客户端和 Tiller 服务器,如下图所示

 

 

 

二、Helm 部署

越来越多的公司和团队开始使用 Helm 这个 Kubernetes 的包管理器,我们也将使用 Helm 安装 Kubernetes 的常用组件。 Helm 的安装十分简单。 下载 helm 命令行工具到 master 节点的 /usr/bin 下,我使用的是 Centos7

这里下载的是 3.4.1 版本:

[root@k8s-master helm]# pwd
/root/k8s_practice/helm
[root@k8s-master helm]# wget https://get.helm.sh/helm-v3.4.1-linux-amd64.tar.gz
[root@k8s-master helm]# tar -zxvf helm-v3.4.1-linux-amd64.tar.gz
[root@k8s-master helm]# cd linux-amd64/
[root@k8s-master linux-amd64]# cp helm /usr/bin
[root@k8s-master linux-amd64]# chmod a+x /usr/bin/helm

其它版本请或者其它系统请移步官网:https://helm.sh/zh/docs/intro/install/

 

三、Helm 自定义模板

1. Helm 目录结构

chart 是一个组织在文件目录中的集合。

在这个目录中,Helm 期望可以匹配以下结构:

Chart.yaml          # 包含了chart信息的YAML文件
LICENSE             # 可选: 包含chart许可证的纯文本文件
README.md           # 可选: 可读的README文件
values.yaml         # chart 默认的配置值
values.schema.json  # 可选: 一个使用JSON结构的values.yaml文件
charts/             # 包含chart依赖的其他chart
crds/               # 自定义资源的定义
templates/          # 模板目录, 当和values 结合时,可生成有效的Kubernetes manifest文件
templates/NOTES.txt # 可选: 包含简要使用说明的纯文本文件

2. Chart.yaml 结构

Chart.yaml 文件是 chart 必需的。包含了以下字段:

apiVersion: chart API 版本 (必需)
name: chart名称 (必需)
version: 版本(必需)
kubeVersion: 兼容Kubernetes版本的语义化版本(可选)
description: 一句话对这个项目的描述(可选)
type: chart类型 (可选)
keywords:
  - 关于项目的一组关键字(可选)
home: 项目home页面的URL (可选)
sources:
  - 项目源码的URL列表(可选)
dependencies: # chart 必要条件列表 (可选)
  - name: chart名称 (nginx)
    version: chart版本 ("1.2.3")
    repository: 仓库URL ("https://example.com/charts") 或别名 ("@repo-name")
    condition: (可选) 解析为布尔值的yaml路径,用于启用/禁用chart (e.g. subchart1.enabled )
    tags: # (可选)
      - 用于一次启用/禁用 一组chart的tag
    enabled: (可选) 决定是否加载chart的布尔值
    import-values: # (可选)
      - ImportValue 保存源值到导入父键的映射。每项可以是字符串或者一对子/父列表项
    alias: (可选) chart中使用的别名。当你要多次添加相同的chart时会很有用
maintainers: # (可选)
  - name: 维护者名字 (每个维护者都需要)
    email: 维护者邮箱 (每个维护者可选)
    url: 维护者URL (每个维护者可选)
icon: 用做icon的SVG或PNG图片URL (可选)
appVersion: 包含的应用版本(可选)。不需要是语义化的
deprecated: 不被推荐的chart (可选,布尔值)
annotations:
  example: 按名称输入的批注列表 (可选).

apiVersion:在 Helm3 中,apiVersion=v2;在 Helm3 之前的版本,apiVersion=v1

 

3. 内置对象

1)values

Values 对象是为 Chart 模板提供值,这个对象的值有4个来源,后面的可以覆盖前面的:

  • chart 包中的 values.yaml 文件
  • 父 chart 包的 values.yaml 文件
  • 通过 helm install 或者 helm upgrade 的 -f 或者 --values 参数传入的自定义的 yaml 文件
  • 通过 --set 参数传入的值

在模板文件中,通过 {{.Values}} 对象来访问设置的值。

  • 例如:定义一个 values.yaml
image:
  repository: wangyanglinux/myapp
  tag: 'v1'
  • 在模板文件中就可以通过 {{.Values}} 对象访问到:
apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  template:
    spec:
      containers:
        - name: nginx
          image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
          ports:
            - containerPort: 80
              protocol: TCP

(2)其它内置对象

对象.属性 说明
Release.Name  版本名称(非chart的)
Release.Namespace 发布的chart版本的命名空间
Release.Service 组织版本的服务
Release.IsUpgrade  如果当前操作是升级或回滚,设置为true
Release.IsInstall 如果当前操作是安装,设置为true
Chart  Chart.yaml的内容。因此,chart的版本可以从 Chart.Version 获得, 并且维护者在Chart.Maintainers里。

 

4. Helm部署nginx

(1)创建文件夹

[root@k8s-master helm]# pwd
/root/k8s_practice/helm
[root@k8s-master helm]# mkdir ./hello-world
[root@k8s-master helm]# cd ./hello-world

(2)创建 Chart.yaml

创建一个 hello-world 的 Chart.yaml,这里只写了必需的字段:

[root@k8s-master hello-world]# pwd
/root/k8s_practice/helm/hello-world
[root@k8s-master hello-world]# cat Chart.yaml 
apiVersion: v2
name: hello-world
version: 1.0.0

(3)创建 values.yaml

文件名如果不是 values.yaml,则需要在 helm install 时,通过 -f 指定 yaml 文件的位置

[root@k8s-master hello-world]# cat values.yaml 
image:
  repository: wangyanglinux/myapp
  tag: 'v1'

(4)创建模版文件(templates)

模板文件, 用于生成 Kubernetes 资源清单(manifests)

注意:模板文件所有的目录必须是 templates

[root@k8s-master templates]# pwd
/root/k8s_practice/helm/hello-world/templates
[root@k8s-master templates]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
        - name: hello-world
          image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
          ports:
            - containerPort: 80
              protocol: TCP
[root@k8s-master templates]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: hello-world

使用命令 helm install RELATIVE_PATH_TO_CHART 创建一次 Release

[root@k8s-master hello-world]# helm install hello-world .
NAME: hello-world
LAST DEPLOYED: Sat Sep 18 14:21:13 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s-master hello-world]# helm list 
NAME           NAMESPACE    REVISION    UPDATED                                    STATUS      CHART                APP VERSION
hello-world    default      1           2021-09-18 14:21:13.508764988 +0800 CST    deployed    hello-world-1.0.0               
[root@k8s-master hello-world]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
hello-world-7b6fdbcfd6-4lb8m              1/1     Running   0          12m
[root@k8s-master hello-world]# kubectl get svc
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
hello-world   NodePort    10.106.95.64     <none>        80:32332/TCP   12m

 

 

5. Helm常用操作

拉取Chart

  • 命令格式:helm pull [chart URL | repo/chartname] [...] [flags]

(1)安装

  • 命令格式:helm install [NAME] [CHART] [flags]

常用参数:

  • -f, --values:指定 values.yaml 文件
  • --set:在命令行中直接设置 values 的值
  • --dry-run:模拟执行,测试能不能创建,但不创建
  • --debug:允许冗长的输出(输出多余信息)

(2)查看信息

[root@k8s-master hello-world]# helm ls
NAME           NAMESPACE    REVISION    UPDATED                                    STATUS      CHART                APP VERSION
hello-world    default      1           2021-09-18 14:21:13.508764988 +0800 CST    deployed    hello-world-1.0.0               
[root@k8s-master hello-world]# helm status hello-world
NAME: hello-world
LAST DEPLOYED: Sat Sep 18 14:21:13 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

(3)更新

#更新,格式:helm upgrade [RELEASE] [CHART] [flags]
[root@k8s-master hello-world]# helm upgrade hello-world .
Release "hello-world" has been upgraded. Happy Helming!
NAME: hello-world
LAST DEPLOYED: Sat Sep 18 14:44:06 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None

(4)历史版本/回滚

# 查看 releases 历史信息,格式:helm history RELEASE_NAME [flags]
[root@k8s-master hello-world]# helm history hello-world
REVISION    UPDATED                     STATUS        CHART                APP VERSION    DESCRIPTION     
1           Sat Sep 18 14:21:13 2021    superseded    hello-world-1.0.0                   Install complete
2           Sat Sep 18 14:44:06 2021    deployed      hello-world-1.0.0                   Upgrade complete

# 回滚到之前版本,格式:helm rollback <RELEASE> [REVISION] [flags]
[root@k8s-master hello-world]# helm rollback hello-world 1
Rollback was a success! Happy Helming!

(5)删除

# 删除,格式:helm delete RELEASE_NAME [...] [flags]
[root@k8s-master hello-world]# helm delete hello-world
release "hello-world" uninstalled

 

标签:kubernetes,chart,详解,helm,world,k8s,root,hello
来源: https://www.cnblogs.com/wuxinchun/p/15308687.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有