My latest and most complete articles are in pumpkin slow talk www.pkslow.com, article update is only in the official website, welcome to tea ~~

1 introduction

Helm is an excellent package manager, which we have covered in the following article:

Kubernetes applications are deployed with Helm, supporting multi-environment deployment and version rollback

Kubernetes installs the Ingress with Helm and stomps on the pit used

The template function at Helm 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 in different files, which can be specified during rendering.

At the beginning of 2 experience

To use Helm’s Template function, you need to create a Chart, which is the Helm’s basic file structure. To create a Nginx resource file, run the following command:

helm create pkslow-nginx
Copy the code

Chart files are automatically created when the command is executed:

Key documents:

  • Directory template: place the template file, want to render any file out, put the corresponding template in this directory;
  • Chart.yaml: The Chart description can be ignored if only the template function of Helm is used;
  • The file values.yaml: contains variable defaults.

Templates /tests won’t help us much, so delete it.

We tried rendering the resulting file without modifying the template or adding variables:

$ 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
Copy the code

Helm directly generates files for three resources based on some variables and judgments. Yaml: service.yaml: service.yaml: service.yaml: service.

3 Add the 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.comCopy the code

After executing the command, the result of rendering 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.comCopy the code

It’s not that different from a template because we don’t use variables, judgment statements, etc., in the template file.

3.1 Using Variables in templates

We modified 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 }}Copy the code

You can see that we are using a lot of double curly braces in the template {{.values.xxx}}. We need to define these variables in the values.yaml file as follows:

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

$helm template pkslow-nginx/ –output-dir./result

---
# 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.comCopy the code

3.2 Setting different variables for different environments

Multienvironment management at Helm Template this is also very simple, we create a values-dev.yaml variable file with the following contents:

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

To specify the variable files for the dev environment, run the following command:

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

The result of this rendering is the configuration for Dev. The same goes for other environments.

3.3 Setting Variables on the CLI

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
Copy the code

conclusion

Please check the code: github.com/LarryDpk/pk…