
手里有一堆跑得好好的 K8s YAML 文件,想迁移到 Helm Chart 管理但又不想手写模板,Helmify 就是干这个的——一行命令把 YAML 转成符合 Helm 3 规范的 Chart 结构。
Helmify 是一个 Go 写的命令行工具,解析 K8s YAML 后自动生成 Chart。它主要做三件事:
Chart.yaml、values.yaml 和 templates/ 目录brew install helmify# Snap
sudo snap install helmify
# 或者从 GitHub Releases 下载二进制
wget https://github.com/arttor/helmify/releases/download/v0.4.2/helmify_0.4.2_linux_amd64.tar.gz
tar -xzf helmify_0.4.2_linux_amd64.tar.gz
sudomv helmify /usr/local/bin/从 Helmify Releases 下载对应的 .exe,加到系统 PATH 里。
helmify --version
# 输出类似: helmify version 0.4.2下面用一个 Web 应用来演示整个流程。
假设有一个包含 Deployment 和 Service 的 webapp.yaml:
# webapp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp-container
image: nginx:1.21-alpine
ports:
- containerPort: 80
env:
- name: ENVIRONMENT
value: "production"
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
---
apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP# 管道方式(推荐)
cat webapp.yaml | helmify webapp-chart
# 或者用 -f 指定文件
helmify -f webapp.yaml webapp-chartwebapp-chart/
├── Chart.yaml
├── values.yaml
└── templates/
├── deployment.yaml
└── service.yaml生成的 Chart.yaml:
apiVersion: v2
name: webapp-chart
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.0"生成的 values.yaml(自动从原始 YAML 中提取):
replicaCount: 2
image:
repository: nginx
tag: 1.21-alpine
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
resources:
limits:
cpu: 200m
memory: 256Mi
requests:
cpu: 100m
memory: 128Mi
env:
ENVIRONMENT: production生成的 templates/deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "webapp-chart.fullname" . }}-deployment
labels:
{{- include "webapp-chart.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "webapp-chart.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "webapp-chart.labels" . | nindent 8 }}
spec:
containers:
- name: webapp-container
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 80
env:
- name: ENVIRONMENT
value: {{ .Values.env.ENVIRONMENT | quote }}
resources:
{{- toYaml .Values.resources | nindent 12 }}# k8s 配置都在 manifests/ 目录下
helmify -f ./manifests -r my-microservice
# 查看生成的所有模板文件
ls-la my-microservice/templates/
# 可能包含: deployment.yaml, service.yaml, configmap.yaml, ingress.yaml 等ConfigMap 的数据会被提取到 values.yaml 中。比如原始 ConfigMap:
# config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
app.properties: |
server.port=8080
logging.level=INFO转换后 values.yaml 会新增:
configmap:
app-config:
data:
app.properties: |
server.port=8080
logging.level=INFO模板中会生成对应的 ConfigMap 资源。
# 保留原始命名空间
helmify -f ./manifests --preserve-ns myapp
# CRD 放到独立文件夹
helmify -f ./manifests --crd-dir myapp-with-crds
# CRD 会放在: myapp-with-crds/crds/ 目录下
# 添加 imagePullSecrets 引用
helmify -f ./manifests --image-pull-secrets myapp# 检查 Chart 语法
helm lint webapp-chart/
# 模板渲染测试(查看实际生成的 YAML)
helm template webapp-chart/ --debug
# 用自定义值覆盖测试
helm template webapp-chart/ --setreplicaCount=3--set image.tag=1.22-alpine
# 安装到 K8s 集群
helm install my-webapp webapp-chart/
# 或使用自定义值文件
helm install my-webapp webapp-chart/ -f my-values.yamlvalues.yaml 参数、补全 Chart.yaml 描述、添加 NOTES.txt 等helm lint + helm template,确认没问题再部署“无他,惟手熟尔”!有需要的用起来!
如果你觉得这篇文章有用,欢迎点赞、转发、收藏、留言、推荐❤!
本文分享自 Nicholas与Pypi 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!