本文我们展示如何开发一个简单的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

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

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



