Helm Chart开发入门

本文详述了如何开发一个简单的Helm Chart,包括创建Chart、配置Deployment和Service模板,以及安装和测试过程。通过示例代码和步骤,展示了如何修改values.yaml以配置镜像和Service类型,并在Kubernetes上部署和验证服务的可达性。

本文我们展示如何开发一个简单的Chart,其中包含了一个Deployment和Service简单的Template,最后安装该Chart。整个Chart的代码已经放到https://github.com/twingao/httpbin

先使用helm create命令创建一个Chart。

helm create httpbin
Creating httpbin

查看httpbin的目录结构。

tree httpbin -a
httpbin
├── charts
├── Chart.yaml
├── .helmignore
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 10 files

Helm规范了Chart的目录和文件结构,这些目录或者文件都有确定的用途。

  • charts/,包含其它Chart,称之为Sub Chart,或者依赖Chart。

  • Chart.yaml,包含Chart的说明,可在从模板中访问Chart定义的值。

  • .helmignore,定义了在helm package时哪些文件不会打包到Chart包tgz中。

  • ci/,缺省没有该目录,持续集成的一些脚本。

  • templates/,用于放置模板文件,主要定义提交给Kubernetes的资源yaml文件。安装Chart时,Helm会根据chart.yaml、values.yam以及命令行提供的值对Templates进行渲染,最后会将渲染的资源提交给Kubernetes。

  • _helpers.tpl,定义了一些可重用的模板片断,此文件中的定义在任何资源定义模板中可用。

  • NOTES.txt,提供了安装后的使用说明,在Chart安装和升级等操作后,

  • tests/,包含了测试用例。测试用例是pod资源,指定一个的命令来运行容器。容器应该成功退出(exit 0),测试被认为是成功的。该pod定义必须包含helm测试hook注释之一:helm.sh/hook: test-success或helm.sh/hook: test-failure。

  • values.yaml,values文件对模板很重要,该文件包含Chart默认值。Helm渲染template时使用这些值。

查看values.yaml文件,需要做一些配置修改:

  • 将镜像改为image.repository=docker.io/kennethreitz/httpbin。
  • 不创建serviceAccount,serviceAccount.create=false
  • 为了Kubernetes集群外能访问Service,将type改为NodePort。并增加一个参数,为nodePort配置一个固定端口。

values.yaml修改如下:

cd httpbin/

vi values.yaml
# Default values for httpbin.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: docker.io/kennethreitz/httpbin
  tag: latest
  pullPolicy: IfNotPresent

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  # Specifies whether a service account should be created
  create: false
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name:

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: NodePort
  port: 80
  nodePort: 30080

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: ngi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值