Hand-in-hand teaching you to write a generic helm chart

Keywords: Linux Nginx Kubernetes Docker github

[TOC]

1. Template introduction

First, put the template link:

https://github.com/ygqygq2/charts/tree/master/mod-chart

This chart can be used as a general template for POD single image. All you need to do is replace the chart name with sed and modify README.md and NOTES.txt. Below, I will illustrate by copying this chart into an example-chart.

[root@master1 mod-chart]# tree
.
├── Chart.yaml
├── README.md
├── templates
│   ├── configmap.yaml
│   ├── deployment-statefulset.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── pvc.yaml
│   ├── secret.yaml
│   ├── service-headless.yaml
│   └── service.yaml
└── values.yaml

1 directory, 12 files
[root@master1 mod-chart]# helm3 lint --strict .
1 chart(s) linted, 0 chart(s) failed

2. New chart Making

Note:
I reserve the contents of the document below, with only comments.
Where the comments need to be modified [*] is marked as required and [-] is marked as optional.

2.1 Catalog Preparation

Copy the template mod-chart into example-chart and replace it with content.

rsync -avz mod-chart/ example-chart/
cd example-chart/
sed -i 's@mod-chart@example-chart@g' *.*
sed -i 's@mod-chart@example-chart@g' templates/*.*

2.2 Modify Chart.yaml

vim Chart.yaml

apiVersion: v1  # The current version of helm api does not require modification
appVersion: 1.14.2  # Here is the version number of your application [*]
description: Chart for the nginx server  # Introduce what this chart is for and modify it as needed.
engine: gotpl  # go template engine, no modification [-]
name: example-chart  # Template name, corresponding directory name [*]
version: 1.0.0  # This chart version number [*]
home: http://www.nginx.org Application Official Network [*]
icon: https://bitnami.com/assets/stacks/nginx/img/nginx-stack-220x234.png# application logo address [*]
keywords:  # Keyword list [*]
- nginx
- http
- web
- www
- reverse proxy
maintainers:  # Maintenance personnel list [*]
- email: 29ygq@sina.com
  name: Chinge Yang
sources:  # Application source [-]
- https://github.com/bitnami/bitnami-docker-nginx

2.3 Modify values.yaml

Because the values.yaml settings involve the yaml format, the yaml file format description can be seen in this article:

http://www.ruanyifeng.com/blog/2016/07/yaml.html

Here are some common points:

  1. Use 2 spaces for indentation.
  2. Double quotation marks are used to identify numbers as character types.
  3. In order to cater to the specification of helm3, the null definition is best complemented by the relevant symbols:
    string: ""
    list: []
    map: {}

There's no special requirement. There are image, service, health Check, persistent Volume. mountPaths that need to be modified.

# Default values for mod-chart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

## Global Docker image parameters
## Please, note that this will override the image parameters, including dependencies, configured to use the global value
## Current available global Docker image parameters: imageRegistry and imagePullSecrets
##
global:  # Override the default mirror warehouse after setting
  imageRegistry: ""
  imagePullSecrets: []
#     - myRegistryKeySecretName

statefulset:
  enabled: false

## String to partially override fullname template (will maintain the release name)
##
nameOverride: ""

## String to fully override fullname template
##
fullnameOverride: ""

## By default deploymentStrategy is set to rollingUpdate with maxSurge of 25% and maxUnavailable of 25% .
## You can change type to `Recreate` or can uncomment `rollingUpdate` specification and adjust them to your usage.
deploymentStrategy: {}
  # rollingUpdate:
  #   maxSurge: 25%
  #   maxUnavailable: 25%
  # type: RollingUpdate

# Number of copies
replicaCount: 1

# Container image and tag
image:
  registry: docker.io
  repository: bitnami/nginx
  tag: latest
  pullPolicy: IfNotPresent  # IfNotPresent: Yes, no pull (reduce traffic and operation steps), Always: regardless of the total tag pull (suitable for tag update when unchanged)
  pullSecrets: []
  #  - private-registry-key

service:
  type: ClusterIP  # Normally no modification is required.
  ingressPort: 8080
  ports:
    web:  # When multiple ports are exposed, copy a section
      port: 8080  # Service port number for client-a port.
      protocol: TCP  # Service port protocol for client-a port.

## env set
## ref: https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
env: []
#  - name: DEMO_GREETING
#    value: "Hello from the environment"
#  - name: DEMO_FAREWELL
#    value: "Such a sweet sorrow"

## command set
startCommand: []
#  - "java -Xdebug -Xnoagent -Djava.compiler=NONE"
#  - "-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n"
#  - "-Djava.security.egd=file:/dev/urandom"
#  - "-jar /test.jar"
#  - "-Duser.timezone=GMT+08"

## Enable configmap and add data in configmap
config:
  enabled: false
  subPath: ""
  mountPath: /conf
  data: {}
## For the following example, mount the file to / conf/app.conf
#  enabled: true
#  mountPath: /conf  
#  subPath: app.conf
#  data:
#    app.conf: |-
#      appname = example-chart

## To use an additional secret, set enable to true and add data
secret:
  enabled: false
  mountPath: /etc/secret-volume
  subPath: ""
  readOnly: true
  data: {} 
## For the following example, mount the file to / etc/secret-volume
#  enabled: true
#  mountPath: /conf  
#  data:
#    app.conf: |-
#      appname = example-chart

## liveness and readiness 
## ref: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
healthCheck:
  enabled: true
  type: tcp  # http/tcp
  port: http  # The port name or port above
  httpPath: '/'  # http must be set
  livenessInitialDelaySeconds: 10  # Initial delay seconds
  livenessPeriodSeconds: 10  # Detection period, default value 10, minimum 1
  readinessInitialDelaySeconds: 10  # Initial delay seconds
  readinessPeriodSeconds: 10   # Detection period, default value 10, minimum 1

resources: {}
  # Container resource settings
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

## Node labels and tolerations for pod assignment
### ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector
### ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#taints-and-tolerations-beta-feature
labels: {}
podAnnotations: {}
nodeSelector: {}
tolerations: []
affinity: {}
annotations: {}

## Enable persistence using Persistent Volume Claims
## ref: http://kubernetes.io/docs/user-guide/persistent-volumes/
##
persistentVolume:   # Storage persistence or not
  enabled: false
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, azure-disk on
  ##   Azure, standard on GKE, AWS & OpenStack)
  ##
  storageClass: "-"
  accessMode: ReadWriteOnce
  annotations: {}
  #   helm.sh/resource-policy: keep
  size: 1Gi  # Size
  existingClaim: {}  # Using existing pvc
  mountPaths: []
  #  - name: data-storage
  #    mountPath: /config
  #    subPath: config
  #  - name: data-storage
  #    mountPath: /data
  #    subPath: data

ingress:  # Whether to use nginx to expose domain names or ports
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

## Add init containers. e.g. to be used to give specific permissions for data
## Add your own init container or uncomment and modify the given example.
initContainers: []

## Prometheus Exporter / Metrics
##
metrics:
  enabled: false
  image:
    registry: docker.io
    repository: nginx/nginx-prometheus-exporter
    tag: 0.1.0
    pullPolicy: IfNotPresent
    ## Optionally specify an array of imagePullSecrets.
    ## Secrets must be manually created in the namespace.
    ## ref: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
    ##
    pullSecrets: []
    #   - myRegistrKeySecretName
  ## Metrics exporter pod Annotation and Labels
  podAnnotations:
    # prometheus.io/scrape: "true"
    # prometheus.io/port: "9113"
    ## Metrics exporter resource requests and limits
    ## ref: http://kubernetes.io/docs/user-guide/compute-resources/
    ##
  resources: {}

## Uncomment and modify this to run a command after starting the core container.
## ref: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/
lifecycle: {}
  # preStop:
  #   exec:
  #     command: ["/bin/bash","/pre-stop.sh"]
  # postStart:
  #   exec:
  #     command: ["/bin/bash","/post-start.sh"]

## Deployment additional volumes.
deployment:
  additionalVolumes: []

## init containers
## ref: https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
## Add init containers. e.g. to be used to give specific permissions for data
## Add your own init container or uncomment and modify the given example.
initContainers: {}
#  - name: fmp-volume-permission
#    image: busybox
#    imagePullPolicy: IfNotPresent
#    command: ['chown','-R', '200', '/extra-data']
#    volumeMounts:
#      - name: extra-data
#        mountPath: /extra-data

## Additional containers to be added to the core pod.
additionalContainers: {}
#  - name: my-sidecar
#    image: nginx:latest
#  - name: lemonldap-ng-controller
#    image: lemonldapng/lemonldap-ng-controller:0.2.0
#    args:
#      - /lemonldap-ng-controller
#      - --alsologtostderr
#      - --configmap=$(POD_NAMESPACE)/lemonldap-ng-configuration
#    env:
#      - name: POD_NAME
#        valueFrom:
#          fieldRef:
#            fieldPath: metadata.name
#      - name: POD_NAMESPACE
#        valueFrom:
#          fieldRef:
#            fieldPath: metadata.namespace
#    volumeMounts:
#    - name: copy-portal-skins
#      mountPath: /srv/var/lib/lemonldap-ng/portal/skins

Pending completion

Posted by tecmeister on Sun, 13 Oct 2019 12:54:12 -0700