Helm Template for the first time, easy to manage multiple environments

My latest and complete articles are in Pumpkin speak slowly www.pkslow.com com , the article update is only on the official website. Welcome to tea~~

1 Introduction

Helm is an excellent package manager. We have introduced this part before. The article is as follows:

Deploy Kubernetes application with Helm to support multi environment deployment and version rollback

Kubernetes installed Ingress with Helm and stepped on the pit used

Helm's template function is also very powerful. It is very convenient to define various Kubernetes resource templates, such as Deployment, Service, Ingress, ConfigMap, etc. The variables of different environments are placed on different files, and the environment variable file can be specified during rendering.

2 initial experience

To use Helm's Template function, you need to create a Chart, which is the basic file composition architecture of Helm. Let's create a resource file related to Nginx. The command is as follows:

helm create pkslow-nginx

After the command is executed, the related files of Chart will be automatically created:

Key documents:

  • Directory template: place the template file. If you want to render any file, place the corresponding template in this directory;
  • File Chart Yaml: the description of the Chart. If you only use Helm's template function, you can ignore it;
  • File values Yaml: contains the default value of the variable.

templates/tests doesn't work for us. Delete them.

We try to render the result file directly without modifying the template or adding variables, as follows:

$ helm template pkslow-nginx/ --output-dir ./result
wrote ./result/pkslow-nginx/templates/serviceaccount.yaml
wrote ./result/pkslow-nginx/templates/service.yaml
wrote ./result/pkslow-nginx/templates/deployment.yaml

According to some variables and judgments, helm directly helps us generate files of three resources. View one of the files service Yaml is still very complete. It can basically meet the needs. Just change it according to your own needs.

3 add template file

Try adding a template file configmap Yaml to the templates directory, as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: pkslow-file
  namespace: default
data:
  application.yaml: |-
    server:
      port: 8080
    pkslow:
      name: Larry
      age: 18
      webSite: www.pkslow.com

The result of rendering after executing the command is as follows:

---
# Source: pkslow-nginx/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: pkslow-file
  namespace: default
data:
  application.yaml: |-
    server:
      port: 8080
    pkslow:
      name: Larry
      age: 18
      webSite: www.pkslow.com

It's no different from the template because we don't use variables and judgment statements in the template file.

3.1 variables used in templates

We modify the template as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: pkslow-config-{{ .Values.environment }}
  namespace: default
data:
  application.yaml: |-
    server:
      port: {{ .Values.server.port }}
    pkslow:
      name: {{ .Values.pkslow.name }}
      age: {{ .Values.pkslow.age }}
    {{- if .Values.pkslow.webSite }}
      webSite: {{ .Values.pkslow.webSite }}
    {{- end }}

You can see that we use many double brace variables {{. Values.xxx}} in the template. We need to These variables are defined in the yaml file as follows:

environment: dev
server:
  port: 80
pkslow:
  name: Larry Deng
  age: 28
  webSite: https://www.pkslow.com

Re execute the command $helm template pkslow nginx / -- output dir/ Result, the rendered result is as follows:

---
# Source: pkslow-nginx/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: pkslow-config-dev
  namespace: default
data:
  application.yaml: |-
    server:
      port: 80
    pkslow:
      name: Larry Deng
      age: 28
      webSite: https://www.pkslow.com

3.2 setting different variables for different environments

Multi environment management is also very simple in Helm Template. We create a variable file of values-dev.yaml, which is as follows:

environment: dev
server:
  port: 8080
pkslow:
  name: Larry Deng
  age: 1

Specify the variable file of the dev environment with the following command:

$ helm template pkslow-nginx/ --output-dir ./result -f pkslow-nginx/values-dev.yaml

The result of this rendering is the related configuration of dev. The same is true for other environments.

3.3 setting variables from the command line

Use -- set or -- set string as follows:

$ helm template pkslow-nginx/ --output-dir ./result -f pkslow-nginx/values-dev.yaml --set pkslow.webSite=www.pkslow.com

summary

Please check the code: https://github.com/LarryDpk/pkslow-samples

Added by uktrips007 on Sun, 16 Jan 2022 19:57:47 +0200