To use templating variables in values.yaml
for Helm in
Kubernetes, we need to define our variables clearly. Then we can
reference them in our Helm charts. This helps us change configurations
easily. It also makes our Kubernetes deployments more flexible and
easier to manage. By using templating variables, we can make the
deployment process smoother. We can also make sure our applications are
set up right with less manual work.
In this article, we will look into templating variables in
values.yaml
for Helm in Kubernetes. We will talk about how
to define custom variables. We will also see how to use built-in
functions and apply conditional logic to improve our Helm charts. Plus,
we will learn how to reference other values properly. The following
topics will be discussed:
- Understanding Templating Variables in
values.yaml
for Helm - How to Define Custom Variables in
values.yaml
for Helm - Leveraging Built-in Functions in
values.yaml
for Helm - Using Conditional Logic with Templating Variables in
values.yaml
for Helm - How to Reference Other Values in
values.yaml
for Helm - Frequently Asked Questions about Templating in Helm
For more information on Kubernetes and its parts, you can check articles like What is Kubernetes and How Does it Simplify Container Management? and What are the Key Components of a Kubernetes Cluster?.
Understanding Templating Variables in values.yaml for Helm
In Helm, values.yaml
is the configuration file. It helps
us to customize our charts. Templating variables in this file allow us
to change settings based on user input or default values. We use the Go
template syntax to do this. This lets us evaluate different expressions
when we create Kubernetes manifests.
Basic Structure
A typical values.yaml
looks like this:
replicaCount: 2
image:
repository: myapp
tag: latest
service:
enabled: true
type: ClusterIP
Accessing Values in Templates
To access values from values.yaml
, we use
{{ .Values }}
in our template files. For example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-deployment
spec:
replicas: {{ .Values.replicaCount }}
template:
spec:
containers:
- name: {{ .Values.image.repository }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
Default Values
We should define default values in values.yaml
. This
helps our templates have backup options:
image:
repository: myapp
tag: "1.0"
If a user does not give an image tag, we will use the default “1.0”.
User-Defined Values
Users can change values during installation or updates. They can use
the --set
flag or provide a custom values.yaml
file:
helm install my-release my-chart --set image.tag=2.0
This command will change the image.tag
to “2.0”.
Nested Values
We can create nested structures in values.yaml
for more
detailed settings:
database:
host: localhost
port: 5432
username: user
password: password
To reference nested values in templates, we do it like this:
env:
- name: DB_HOST
value: {{ .Values.database.host }}
- name: DB_PORT
value: {{ .Values.database.port | quote }}
Custom Functions
Helm has built-in functions to change values. For example, we can use
the quote
function to put a string in double quotes:
env:
- name: DB_HOST
value: {{ .Values.database.host | quote }}
Using Conditionals
We can use conditionals to control what gets rendered based on values:
{{- if .Values.service.enabled }}
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-service
spec:
type: {{ .Values.service.type }}
{{- end }}
Loops
We can loop through lists defined in values.yaml
:
ports:
- name: http
port: 80
- name: https
port: 443
In our template, we write:
spec:
ports:
{{- range .Values.ports }}
- name: {{ .name }}
port: {{ .port }}
{{- end }}
By using templating variables in values.yaml
, we can
create flexible and reusable deployment settings in Helm. This helps us
manage Kubernetes applications better. Helm is a strong tool in the
Kubernetes world. For more details on Helm and its features, visit Best
Online Tutorial - What is Helm and How Does it Help with Kubernetes
Deployments?.
How to Define Custom Variables in values.yaml for Helm
In Helm, the values.yaml
file is very important for
defining custom variables. We use these variables in our templates. They
help us change how we deploy our applications without changing the
templates directly. Here is how to define and use them in a good
way.
Defining Custom Variables
To define custom variables, we just add key-value pairs in our
values.yaml
file. Here is an example:
# values.yaml
replicaCount: 3
image:
repository: myapp
tag: latest
service:
type: ClusterIP
port: 80
Accessing Custom Variables in Templates
In our Helm templates, we can access these variables using the
{{ .Values }}
syntax. For example, to set the number of
replicas in a Deployment, we can do it like this:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-app
spec:
replicas: {{ .Values.replicaCount }}
template:
spec:
containers:
- name: {{ .Chart.Name }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
ports:
- containerPort: {{ .Values.service.port }}
Nested Variables
We can also create nested variables for better organization. For example:
# values.yaml
database:
host: db.example.com
port: 5432
user: admin
password: secret
We access them in our templates like this:
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-config
data:
DATABASE_HOST: {{ .Values.database.host }}
DATABASE_PORT: {{ .Values.database.port }}
DATABASE_USER: {{ .Values.database.user }}
DATABASE_PASSWORD: {{ .Values.database.password }}
Overriding Values
When we deploy our Helm chart, we can override these values using the
--set
flag or by giving a custom values.yaml
file:
helm install my-release ./my-chart --set replicaCount=5
Best Practices
- Use clear and easy variable names.
- Group related variables under a common prefix to avoid problems with names.
- Write notes about your variables in the
values.yaml
file for better understanding.
By defining and managing custom variables well in our
values.yaml
, we can make flexible and reusable Helm charts
that fit our application’s needs. For more on Helm chart setups, check
out this
article.
Leveraging Built-in Functions in values.yaml for Helm
In Helm, we can use built-in functions. These functions help us
change data right in our values.yaml
or templates. They are
important for dynamic configuration. They also make our Helm charts more
flexible. Here are some common built-in functions we use in
values.yaml
:
required
: This function makes sure we provide a value. If we do not, it shows an error.myValue: {{ required "A value is required!" .Values.someValue }}
default
: This function gives a default value if we do not set the specified value.myValue: {{ default "defaultValue" .Values.someValue }}
toYaml
: This changes a given value into YAML format.myYaml: {{ toYaml .Values.myMap | nindent 2 }}
quote
: This wraps a string in quotes.myQuotedValue: {{ quote .Values.someString }}
lookup
: This gets a Kubernetes resource by its kind, name, and namespace.mySecret: {{ (lookup "v1" "Secret" "default" "my-secret").data }}
trim
: This removes spaces from the start and end of a string.myTrimmedValue: {{ trim " " .Values.someString }}
join
: This connects elements of a slice into one string with a separator we choose.myJoinedString: {{ join "," .Values.myStringArray }}
split
: This breaks a string into a slice using a delimiter.mySplitArray: {{ split "," .Values.myString }}
hasKey
: This checks if a map has a certain key.myKeyExists: {{ if hasKey .Values.myMap "key" }}true{{ else }}false{{ end }}
upper
andlower
: This changes strings to upper or lower case.yaml myUpper: {{ upper .Values.someString }} myLower: {{ lower .Values.someString }}
We can combine these built-in functions to make more complex setups
in our values.yaml
. For more details on Helm and what it
can do, we can look at this Helm
documentation.
Using Conditional Logic with Templating Variables in values.yaml for Helm
In Helm charts, we can use conditional logic in the
values.yaml
file. This helps us control how resources are
deployed based on specific rules. It gives us flexible setups and helps
us manage resources better.
Syntax for Conditional Logic
Helm uses the Go templating engine. It allows us to use
if
statements, else
statements, and
with
statements. The basic way to write this looks
like:
{{- if .Values.someCondition }}
# Resource configuration when condition is true
{{- else }}
# Resource configuration when condition is false
{{- end }}
Example Usage
Let’s say we want to turn a feature on or off based on a value in
values.yaml
. Here is how to do it:
values.yaml
enableFeature: true
template.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
feature: |
{{- if .Values.enableFeature }}
Feature is enabled
{{- else }}
Feature is disabled {{- end }}
In this case, the ConfigMap
will say “Feature is
enabled” or “Feature is disabled” depending on the value of
enableFeature
.
Nested Conditions
We can put conditions inside other conditions to make more complex logic. For example:
{{- if .Values.enableFeature }}
{{- if .Values.someOtherCondition }}
# Configuration when both conditions are true
{{- else }}
# Configuration when only the first condition is true
{{- end }}
{{- else }}
# Configuration when the feature is disabled
{{- end }}
Leveraging Default Values
We can also set default values for conditions with the
default
function. This helps to make sure a value is set if
it is not there in values.yaml
.
featureEnabled: {{ .Values.featureEnabled | default false }}
Practical Scenario
A real example could be changing resource limits based on an environment variable:
values.yaml
production: false
resource-templates.yaml
resources:
limits:
cpu: {{- if .Values.production }}200m{{- else }}100m{{- end }}
memory: {{- if .Values.production }}512Mi{{- else }}256Mi{{- end }}
This setup makes sure if the production
flag is true,
the resource limits are higher.
Conclusion
Using conditional logic in values.yaml
in Helm charts is
a good way to manage configurations in an easy way. By using conditions,
we can create flexible and reusable Helm charts that work for many
deployment situations. For more details on Helm and what it can do,
check out this
detailed guide.
How to Reference Other Values in values.yaml for Helm
In Helm, we can reference other values in our
values.yaml
file. This helps us make our charts flexible.
We use the {{ .Values }}
object to get these values in our
templates.
Basic Reference
To reference a value from values.yaml
, we use this
syntax:
{{ .Values.variableName }}
For example, if we have this in our values.yaml
:
replicaCount: 3
image:
repository: myapp
tag: latest
We can reference replicaCount
in our deployment template
like this:
spec:
replicas: {{ .Values.replicaCount }}
Nested Values
If our values are nested, we can access them with dot notation. For
example, to reference the tag
of the image
, we
write:
spec:
containers:
- name: {{ .Values.image.repository }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
Default Values
We can also set default values using the default
function. This is helpful when a value may not be set. For example:
spec:
replicas: {{ default 1 .Values.replicaCount }}
This sets the replicas
to 1 if replicaCount
is not defined.
Combining Values
We can combine different values together. For example, if we want to create a full image name, we do it like this:
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
Conditional References
We can use conditional logic to reference values based on certain conditions. For example:
spec:
replicas: {{ if .Values.isProduction }}{{ .Values.prodReplicaCount }}{{ else }}{{ .Values.devReplicaCount }}{{ end }}
Use Cases
- Environment-Specific Configurations: We can reference different values based on the environment like development, testing, or production.
- Versioning: We can manage image versions by
updating just one value in
values.yaml
.
By referencing other values in values.yaml
, we can make
our Helm charts easier to maintain and configure. They can adapt to
different deployment situations. For more details on working with Helm
and Kubernetes, check out this
tutorial on managing Helm releases.
Frequently Asked Questions
1. What are
templating variables in Helm’s values.yaml
?
Templating variables in Helm’s values.yaml
help us to
set dynamic values. We can use these values in different parts of our
Helm charts. This makes it easier to change deployments based on what we
need. By using these variables, we can adjust our Kubernetes apps
without hardcoding values. This helps us to have smoother deployments
and changes.
2.
How can I define custom variables in values.yaml
for
Helm?
To define custom variables in values.yaml
, we just
create key-value pairs in the right section. For example:
myVariable: myValue
Then we can use this variable in our templates like this:
{{ .Values.myVariable }}
. This way makes it easy to manage
configurations and gives us more flexibility in our Kubernetes
deployments with Helm.
3. How do built-in functions work with templating variables in Helm?
Helm has built-in functions to change data in our
values.yaml
file. We can use functions like
upper
, lower
, and quote
to change
strings. For example, {{ .Values.myVariable | upper }}
will
change the value of myVariable
to uppercase. Using these
functions makes our Helm charts more powerful and flexible.
4. Can I use conditional logic with templating variables in Helm?
Yes, we can use conditional logic in Helm with templating variables.
Helm uses the Go templating language. This lets us use things like
if
, else
, and with
. For
example:
{{ if .Values.isProduction }}
replicas: 3
{{ else }}
replicas: 1
{{ end }}
This feature helps us to make decisions in our Kubernetes deployments. We can have different settings based on the environment.
5.
How do I reference other values in values.yaml
within my
Helm templates?
Referencing other values in values.yaml
is easy. We can
use dot notation for nested values. For example, if we have:
database:
name: mydb
We can reference it in our templates like this:
{{ .Values.database.name }}
. This ability is important for
creating complex Helm charts that need many configuration values for our
Kubernetes apps.
For more help on managing Kubernetes deployments, we can check this article on how to use Helm to deploy a complex application on Kubernetes.