如何将 k8s yaml 转换为 helm chart

mch*_*wre 14 yaml kubernetes kubernetes-helm

现在我正在使用 yaml 文件在 k8s 上部署应用程序。

像下面一张:

apiVersion: v1
kind: Service
metadata:
  name: serviceA
  namespace: flow
spec:
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    app: serviceA
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: serviceA-ingress
  namespace: flow
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    kubernetes.io/ingress.class: nginx
    certmanager.k8s.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - serviceA.xyz.com
    secretName: letsencrypt-prod
  rules:
  - host: serviceA.xyz.com
    http:
      paths:
      - path: /
        backend:
          serviceName: serviceA
          servicePort: 8080
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: serviceA-config
  namespace: flow
data:
  application-dev.properties: |
    spring.application.name=serviceA-main
    server.port=8080
    logging.level.org.springframework.jdbc.core=debug
    lead.pg.url=serviceB.flow.svc:8080/lead
    task.pg.url=serviceB.flow.svc:8080/task
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: serviceA-deployment
  namespace: flow
spec:
  selector:
    matchLabels:
      app: serviceA
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: serviceA
    spec:
      containers:
      - name: serviceA
        image: xyzaccount.dkr.ecr.eu-west-1.amazonaws.com/flow/test:serviceA-v1
        command: [ "java", "-jar", "-agentlib:jdwp=transport=dt_socket,address=9098,server=y,suspend=n", "serviceA-service.jar", "--spring.config.additional-location=/config/application-dev.properties" ]
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: serviceA-application-config
          mountPath: "/config"
          readOnly: true
      volumes:
      - name: serviceA-application-config
        configMap:
          name: serviceA-config
          items:
          - key: application-dev.properties
            path: application-dev.properties
      restartPolicy: Always
Run Code Online (Sandbox Code Playgroud)

是否有任何自动方法可以将此 yaml 转换为helm charts.

或者我可以用来实现此目的的任何其他解决方法或示例模板。

即使没有任何通用的方法,那么我也想知道如何将这个特定的 yaml 转换为 helm chart。

还想知道我应该保持可配置的所有内容(我的意思是转换为变量),因为我不能将 yaml 中的这些资源放入单独的模板文件夹中并将其称为 helm chart。

Art*_*tem 10

没有明确的方法将 k8s YAML 映射到 Helm 图表。因为应用程序/图表维护者应该决定:

\n
    \n
  • 图表用户可以修改哪些应用程序参数
  • \n
  • 这些参数中哪些是强制性的
  • \n
  • 默认值是什么,等等...
  • \n
\n

因此,Helm 图表的创建是一个手动过程。但它包含很多常规步骤。例如,大多数图表创建者希望:

\n
    \n
  • 从模板中删除命名空间以设置它helm install -n
  • \n
  • 删除k8s生成的字段
  • \n
  • 将 helm 版本名称添加到资源名称
  • \n
  • 保留模板名称之间的正确链接
  • \n
  • 将一些明显的参数移动为values.yaml:\n
      \n
    • 容器资源
    • \n
    • 服务类型和端口
    • \n
    • 配置映射/秘密值
    • \n
    • 图像存储库:标签
    • \n
    \n
  • \n
\n

我创建了一个名为 helmify 的 CLI来自动执行上面列出的步骤。\n它从 stdin 读取 k8s 对象列表并从中创建一个 helm 图表。

\n

您可以使用brew安装它brew install arttor/tap/helmify。然后使用 YAML 文件生成图表:

\n
cat my-app.yaml | helmify mychart\n
Run Code Online (Sandbox Code Playgroud)\n

或来自<my_directory>包含 yaml 的目录:

\n
awk \'FNR==1 && NR!=1  {print "---"}{print}\' /<my_directory>/*.yaml | helmify mychart\n
Run Code Online (Sandbox Code Playgroud)\n

mychart与命令类似,这两个命令都会从 k8s 对象创建Helm 图表目录helm create

\n

这是 helmify 根据问题中发布的 yaml 生成的图表:

\n
mychart\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Chart.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _helpers.tpl\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 deployment.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ingress.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 serviceA.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 values.yaml\n\n#values.yaml\nconfig:\n  applicationDevProperties:\n    lead:\n      pg:\n        url: serviceB.flow.svc:8080/lead\n    logging:\n      level:\n        org:\n          springframework:\n            jdbc:\n              core: debug\n    server:\n      port: "8080"\n    spring:\n      application:\n        name: serviceA-main\n    task:\n      pg:\n        url: serviceB.flow.svc:8080/task\ndeployment:\n  replicas: 1\nimage:\n  serviceA:\n    repository: xyzaccount.dkr.ecr.eu-west-1.amazonaws.com/flow/test\n    tag: serviceA-v1\nserviceA:\n  ports:\n    - port: 8080\n      targetPort: 8080\n  type: ClusterIP\n---\n# templates/config.yaml\napiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: {{ include "mychart.fullname" . }}-config\n  labels:\n  {{- include "mychart.labels" . | nindent 4 }}\ndata:\n  application-dev.properties: |\n    spring.application.name={{ .Values.config.applicationDevProperties.spring.application.name | quote }}\n    server.port={{ .Values.config.applicationDevProperties.server.port | quote }}\n    logging.level.org.springframework.jdbc.core={{ .Values.config.applicationDevProperties.logging.level.org.springframework.jdbc.core | quote }}\n    lead.pg.url={{ .Values.config.applicationDevProperties.lead.pg.url | quote }}\n    task.pg.url={{ .Values.config.applicationDevProperties.task.pg.url | quote }}\n---\n# templates/deployment.yaml\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: {{ include "mychart.fullname" . }}-deployment\n  labels:\n  {{- include "mychart.labels" . | nindent 4 }}\nspec:\n  replicas: {{ .Values.deployment.replicas }}\n  selector:\n    matchLabels:\n      app: serviceA\n    {{- include "mychart.selectorLabels" . | nindent 6 }}\n  template:\n    metadata:\n      labels:\n        app: serviceA\n      {{- include "mychart.selectorLabels" . | nindent 8 }}\n    spec:\n      containers:\n        - command:\n            - java\n            - -jar\n            - -agentlib:jdwp=transport=dt_socket,address=9098,server=y,suspend=n\n            - serviceA-service.jar\n            - --spring.config.additional-location=/config/application-dev.properties\n          image: {{ .Values.image.serviceA.repository }}:{{ .Values.image.serviceA.tag |\n                   default .Chart.AppVersion }}\n          name: serviceA\n          ports:\n            - containerPort: 8080\n          resources: {}\n          volumeMounts:\n            - mountPath: /config\n              name: serviceA-application-config\n              readOnly: true\n      restartPolicy: Always\n      volumes:\n        - configMap:\n            items:\n              - key: application-dev.properties\n                path: application-dev.properties\n            name: {{ include "mychart.fullname" . }}-config\n          name: serviceA-application-config\n---\n# templates/ingress.yaml\napiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\n  name: {{ include "mychart.fullname" . }}-ingress\n  labels:\n  {{- include "mychart.labels" . | nindent 4 }}\n  annotations:\n    certmanager.k8s.io/cluster-issuer: letsencrypt-prod\n    kubernetes.io/ingress.class: nginx\n    nginx.ingress.kubernetes.io/rewrite-target: /\n    nginx.ingress.kubernetes.io/use-regex: "true"\nspec:\n  rules:\n    - host: serviceA.xyz.com\n      http:\n        paths:\n          - backend:\n              serviceName: serviceA\n              servicePort: 8080\n            path: /\n  tls:\n    - hosts:\n        - serviceA.xyz.com\n      secretName: letsencrypt-prod\n---\n# templates/serviceA.yaml\napiVersion: v1\nkind: Service\nmetadata:\n  name: {{ include "mychart.fullname" . }}-serviceA\n  labels:\n  {{- include "mychart.labels" . | nindent 4 }}\nspec:\n  type: {{ .Values.serviceA.type }}\n  selector:\n    app: serviceA\n  {{- include "mychart.selectorLabels" . | nindent 4 }}\n  ports:\n  {{- .Values.serviceA.ports | toYaml | nindent 2 -}}\n\n
Run Code Online (Sandbox Code Playgroud)\n


cod*_*ger 6

本质上,Helm 图表仍然只是 YAML,因此要制作图表,只需将该文件放在 templates/ 下并添加 Chart.yml。

  • 您的回答似乎与我的问题不太一致,您能否详细说明一下。 (4认同)
  • 我想他可能是在问如何将清单隐藏到模板中,就像将可配置项分离到values.yaml一样 (3认同)
  • 也许您认为舵图比它更复杂?无需转换,您可以将其用作图表的模板数据,只要您愿意。 (2认同)