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

man*_*r76 8 docker kubernetes kubernetes-helm

我是 k8s 的新手。我有一个作业,这就是我的情况:
有一个面向微服务的应用程序,由十个容器构建。它有一个docker-compose易于设置的文件。现在我的任务是将其部署到 Kubernetes 中。我的想法:docker-compose使用 将文件转换为 k8s 清单kompose,并为每个服务创建 helm 图表。
我的问题是:我必须一一修改每个图表,不是吗?有什么方法可以values.yaml根据现有的 k8s 清单生成吗?例如,从此:

# 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
...
Run Code Online (Sandbox Code Playgroud)

对此,自动:

# 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 }}
...
Run Code Online (Sandbox Code Playgroud)
# values.yaml
cmd: kompose convert
ver: 1.22.0 (955b78124)
name: bookstore-account-service
Run Code Online (Sandbox Code Playgroud)

p/s:抱歉我的英语不好,这不是我的母语:D

Dav*_*aze 5

Helmvalues.yaml文件是您可以在部署时配置图表的要点。一方面,您无法配置任何未在 中引用的内容.Values;另一方面,您通常不希望 YAML 文件的每一行都是可配置的。

如果我要解决这个问题,我会从helm create一张新图表开始。然后,我切换到该templates目录,将大部分样板移到一边(但保留生成的_helpers.tpl文件),然后运行kompose convert​​. 这将生成一组 YAML 文件,但没有 Helm 模板。

从这里我将编辑这些文件,使它们符合典型的 Helm 用法。查看来自helm create(或Helm 源)的原始文件作为示例。我希望编辑后的内容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" . }}
Run Code Online (Sandbox Code Playgroud)

values.yaml那么文件中应该包含什么内容呢?这些是您需要在部署时配置的内容。如果您需要覆盖容器command:args:,这些通常是固定的,但如果您需要提供某种凭据或主机名,这些可能会因部署而异。(如果您helm install对图表进行了两次编辑,两次安装之间会有什么不同helm create?)该模板使资源限制可配置,因为这些限制可能会根据实际工作负载而有很大差异:

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

您可以在此处使用一组特定的值来部署它:

# values.dev.yaml
resources:
  requests:
    memory: 256Mi
  limits:
    memory: 1Gi
Run Code Online (Sandbox Code Playgroud)
# values.prod.yaml
resources:
  requests:
    memory: 2Gi
  limits:
    memory: 4Gi
Run Code Online (Sandbox Code Playgroud)
helm install bookstore . -f values.dev.yaml
Run Code Online (Sandbox Code Playgroud)

例如,如果您保留了“哪个版本的 Kompose 生成了此文件”注释,则没有理由在环境之间更改该注释,因此您可以将其保留为固定字符串。