简体   繁体   English

如何从现有的 Kubernetes 清单构建 Helm 图表

[英]How to build Helm chart from existing Kubernetes manifest

i'm a newbie to k8s.我是k8s的新手。 I have a homework and this is my situation:我有一个作业,这是我的情况:
There is an app with microservice-oriented, build with amount ten containers.有一个面向微服务的应用程序,用十个容器构建。 It had a docker-compose file for easily set up.它有一个docker-compose文件,可以轻松设置。 Now my mission is deploy it into Kubernetes.现在我的任务是将其部署到 Kubernetes 中。 My idea: convert docker-compose file to k8s manifest with kompose , and create helm chart for each services.我的想法:使用 kompose 将docker-compose文件转换为kompose清单,并为每个服务创建 helm chart。
My question is: I have to modify each chart one-by-one, isn't it?我的问题是:我必须一个一个地修改每个图表,不是吗? Is there any way to generate values.yaml base on existing k8s manifest?有什么方法可以根据现有的 k8s 清单生成values.yaml吗? example, from this:例如,从此:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.22.0 (955b78124)
  creationTimestamp: null
  labels:
    io.kompose.service: bookstore-account-service
  name: bookstore-account-service
...

to this, automatically:为此,自动:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: {{ .Values.cmd }}
    kompose.version: {{ .Values.ver }}
  creationTimestamp: null
  labels:
    io.kompose.service: {{ .Values.name }}
  name: {{ .Values.name }}
...
# values.yaml
cmd: kompose convert
ver: 1.22.0 (955b78124)
name: bookstore-account-service

p/s: sorry for my bad English, it's not my first language:D p/s:对不起我的英语不好,这不是我的第一语言:D

The Helm values.yaml file is the main point where you can configure the chart at deployment time. Helm values.yaml文件是您可以在部署时配置图表的主要点。 On the one hand, you can't configure anything that's not referenced in .Values ;一方面,您不能配置.Values中未引用的任何内容; on the other hand, you usually don't want every individual line of the YAML file to be configurable.另一方面,您通常不希望 YAML 文件的每一行都是可配置的。

If I was going to approach this, I'd start by helm create a new chart.如果我要解决这个问题,我会先helm create一个新图表。 I'd then switch to the templates directory, move aside most of the boilerplate there (but leave the generated _helpers.tpl file), and run kompose convert .然后我会切换到templates目录,将大部分样板移到那里(但保留生成的_helpers.tpl文件),然后运行kompose convert This will generate a set of YAML files, though with no Helm templating.这将生成一组 YAML 文件,但没有 Helm 模板。

From here I'd edit the files to make them match typical Helm usage.从这里我将编辑文件以使它们与典型的 Helm 用法相匹配。 Look at the original files from helm create (or in the Helm source ) for examples.查看helm create (或Helm 源代码)中的原始文件以获取示例。 I would expect the edited deployment.yaml to look like:我希望编辑后的deployment.yaml看起来像:

apiVersion: apps/v1
kind: Deployment
metadata:
  {{-/* delete the Kompose annotations: and empty creationTimestamp: */}}
  labels:
    {{-/* get a standard set of labels from _helpers.tpl }}
    {{- include "bookstore.labels" . | nindent 4 }}
  {{-/* get a standard name from _helpers.tpl }}
  name: {{ include "bookstore.fullname" . }}

What should go in the values.yaml file, then?那么values.yaml文件中的 go 应该是什么? These are things you'd need to configure at deploy time .这些是您需要在部署时配置的东西。 If you need to override the container command: or args: , these would usually be fixed, but if you needed to supply some sort of credentials or host name, those could vary per-deployment.如果您需要覆盖容器command:args: ,这些通常是固定的,但如果您需要提供某种凭据或主机名,这些可能会因部署而异。 (If you helm install ed the chart twice, what would be different between the installs?) The helm create template makes resource limits configurable, since these can vary heavily based on the actual workload: (如果您helm install两次安装图表,安装之间会有什么不同?) helm create模板使资源限制可配置,因为这些可能会根据实际工作负载而有很大差异:

# deployment.yaml (from helm/create.go linked above)
resources:
  {{- toYaml .Values.resources | nindent 12 }}
# values.yaml (also from helm/create.go)
resources: {}

You could deploy this with a specific set of values here:您可以在此处使用一组特定的值来部署它:

# values.dev.yaml
resources:
  requests:
    memory: 256Mi
  limits:
    memory: 1Gi
# values.prod.yaml
resources:
  requests:
    memory: 2Gi
  limits:
    memory: 4Gi
helm install bookstore . -f values.dev.yaml

If you had preserved, for example, the "what version of Kompose generated this file" annotation, there'd be no reason to change that between environments, and so you could just leave that as a fixed string.例如,如果您保留了“Kompose 的哪个版本生成了此文件”注释,则没有理由在环境之间更改它,因此您可以将其保留为固定字符串。

the thing is, how do you decide which one will be customizable (go into values file).问题是,你如何决定哪一个是可定制的(进入值文件)。 if you do have that list, then it's easier.如果您确实有该列表,那就更容易了。 you can use a yaml parser, replace teh value for those fields you want to customize and place those values to values.yaml file.您可以使用 yaml 解析器,替换您想要自定义的那些字段的值并将这些值放置到values.yaml文件中。

You can run the docker-compose file to deploy the services and from the K8s YAML manifest you can write down the helm chart with the fields you want.您可以运行 docker-compose 文件来部署服务,并从 K8s YAML 清单中写下带有所需字段的掌舵图。

In values.yaml you can add necessary based on your requirement which one you want to keep configurable.values.yaml ,您可以根据您想要保持可配置的要求添加必要的。

You can also run the command for the basic helm template and edit it as per requirement您还可以运行基本 helm 模板的命令并根据需要对其进行编辑

helm create mychartname

Read more at: https://opensource.com/article/20/5/helm-charts阅读更多: https://opensource.com/article/20/5/helm-charts

I was trying to find one open-source project to do this, but none of them fits my requirement.我试图找到一个开源项目来做到这一点,但它们都不符合我的要求。 I decided to write one myself, so https://github.com/yeahdongcn/kustohelmize was created for this.我决定自己写一个,所以为此创建了https://github.com/yeahdongcn/kustohelmize

It could be simply integrated with any of Golang's projects, especially for the projects generated by Operator SDK.它可以简单地与 Golang 的任何项目集成,尤其是对于 Operator SDK 生成的项目。 Say your project has a Makefile, insert the following lines:假设您的项目有一个 Makefile,插入以下行:

.PHONY: helm
helm: kustohelmize
    $(KUSTOHELMIZE) create --from=<<path-to-your-all-in-one-yaml-file>> <<path-to-your-target-dir>>
    helm lint <<path-to-your-target-dir>>

KUBERNETES-SPLIT-YAML ?= $(LOCALBIN)/kubernetes-split-yaml
KUSTOHELMIZE ?= $(LOCALBIN)/kustohelmize

.PHONY: kubernetes-split-yaml
kubernetes-split-yaml: $(KUBERNETES-SPLIT-YAML) ## Download kubernetes-split-yaml locally if necessary.
$(KUBERNETES-SPLIT-YAML): $(LOCALBIN)
    GOBIN=$(LOCALBIN) go install github.com/yeahdongcn/kubernetes-split-yaml@v0.4.0

.PHONY: kustohelmize
kustohelmize: $(KUSTOHELMIZE) ## Download kustohelmize locally if necessary.
$(KUSTOHELMIZE): $(LOCALBIN) kubernetes-split-yaml
    GOBIN=$(LOCALBIN) go install github.com/yeahdongcn/kustohelmize@latest

Try make helm and a helm chart is created with the default configurations.尝试make helm并使用默认配置创建一个 helm 图表。 One can update the <>.config with your own config, please ref to https://github.com/yeahdongcn/kustohelmize/blob/main/examples/README.md您可以使用自己的配置更新 <>.config,请参考https://github.com/yeahdongcn/kustohelmize/blob/main/examples/README.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM