chart文件结构

在 Helm 中,Chart 是一个用于定义、安装和升级 Kubernetes 应用程序的包。Chart 文件结构遵循一定的目录和文件组织方式,以下是典型的 Helm Chart 文件结构:

1. Chart 文件结构示例

复制代码
mychart/
├── Chart.yaml          # 描述 Chart 的基本信息
├── values.yaml         # 默认配置值
├── charts/             # 依赖的其他 Chart
├── templates/          # Kubernetes 资源模板
│   ├── deployment.yaml # Deployment 模板
│   ├── service.yaml    # Service 模板
│   ├── ingress.yaml    # Ingress 模板
│   ├── _helpers.tpl    # 模板助手函数
│   └── NOTES.txt       # 安装后的提示信息
└── templates/tests/    # 测试文件
    └── test-connection.yaml

2. 主要文件和目录详解

2.1 Chart.yaml

Chart.yaml 文件包含了 Chart 的基本信息,如名称、版本、描述等。

yaml 复制代码
apiVersion: v2
name: mychart
version: 0.1.0
description: A Helm chart for Kubernetes
type: application
keywords:
  - example
home: https://example.com
sources:
  - https://github.com/example/mychart
maintainers:
  - name: John Doe
    email: john.doe@example.com
dependencies:
  - name: postgresql
    version: "10.9.4"
    repository: "https://charts.helm.sh/stable"
2.2 values.yaml

values.yaml 文件包含了 Chart 的默认配置值,这些值可以在安装或升级 Chart 时被覆盖。

yaml 复制代码
replicaCount: 1
image:
  repository: nginx
  tag: "1.16.0"
  pullPolicy: IfNotPresent
service:
  type: ClusterIP
  port: 80
ingress:
  enabled: false
  annotations: {}
  hosts:
    - host: chart-example.local
      paths:
        - /
  tls: []
2.3 charts/

charts/ 目录包含了当前 Chart 依赖的其他 Chart。Helm 会自动解析这些依赖并在安装时一并处理。

复制代码
charts/
├── postgresql/
│   ├── Chart.yaml
│   ├── values.yaml
│   ├── templates/
│   └── ...
└── redis/
    ├── Chart.yaml
    ├── values.yaml
    ├── templates/
    └── ...
2.4 templates/

templates/ 目录包含了 Kubernetes 资源模板文件。这些模板文件使用 Go 模板语言编写,并根据 values.yaml 中的值动态生成 Kubernetes 资源配置。

  • deployment.yaml: 定义 Deployment 资源。
  • service.yaml: 定义 Service 资源。
  • ingress.yaml: 定义 Ingress 资源。
  • _helpers.tpl: 包含模板助手函数,用于在多个模板文件中复用代码。
  • NOTES.txt: 安装后显示的提示信息。
2.5 templates/tests/

templates/tests/ 目录包含测试文件,用于验证 Chart 的安装是否成功。

yaml 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: "{{ .Release.Name }}-test-connection"
  labels:
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app.kubernetes.io/name: "{{ .Values.name }}"
    app.kubernetes.io/instance: "{{ .Release.Name }}"
  annotations:
    "helm.sh/hook": test
spec:
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args: ['{{ .Release.Name }}:{{ .Values.service.port }}']
  restartPolicy: Never

3. 常用命令

  • helm create <chart-name>: 创建一个新的 Chart。
  • helm install <release-name> <chart-name>: 安装一个 Chart。
  • helm upgrade <release-name> <chart-name>: 升级一个已安装的 Chart。
  • helm template <chart-name>: 渲染 Chart 模板并输出 Kubernetes 资源配置。
  • helm lint <chart-name>: 检查 Chart 的语法和结构是否正确。

4. 模板语法示例

templates/ 目录中的文件可以使用 Go 模板语言来动态生成 Kubernetes 资源配置。以下是一个简单的 deployment.yaml 示例:

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-deployment
  labels:
    app: {{ .Values.name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
    spec:
      containers:
        - name: {{ .Values.name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.service.port }}
相关推荐
hey you~28 分钟前
400电话选型技术测评:一个开发视角的线路质量评估方案
运维·网络·数据库·400电话·企业通信
筱羽_筱羽37 分钟前
跟着“Ubuntu18.04/20.04编译Linux5.10.76+Xenomai-3.2.1+IgH”教程遇到的问题
linux·运维·ubuntu
无忧.芙桃2 小时前
线程同步与线程互斥(上)
linux·运维·操作系统·运维开发
xywww1687 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
梦想的颜色10 小时前
【Docker原理】Docker 容器一文打尽:生命周期、环境管控、迁移备份、日志进程排查、垃圾清理
运维·docker·容器·容器生命周期·容器日志排查·容器迁移备份·容器进程管理
donoot12 小时前
Linux系统下图书馆级电子书全自动标准化分类整理完整实施方案
大数据·linux·运维·电子书管理
ITKEY_12 小时前
macOS brew 安装的nginx 文件在哪里?
运维·nginx·macos
Elastic 中国社区官方博客13 小时前
如何比较两个 Elasticsearch 索引并找出缺失的文档
大数据·运维·数据库·elasticsearch·搜索引擎
Tim_Van14 小时前
在 CentOS 7 中安装 Chromium 的完整步骤
linux·运维·chrome·centos
孫治AllenSun14 小时前
【Nginx】配置参数和使用案例
运维·nginx