Sub topic 4
Defining and overriding values
This chapter assumes you know basic Kubernetes objects, can read YAML, have Helm 3 installed, and have seen the chart layout (Chart.yaml, values.yaml, templates/). Here you will learn how a chart defines defaults and how users override them at install time.
Defaults live in values.yaml
The values.yaml file at the chart root holds the defaults. Templates read it through .Values. A good file is a readable list of the settings a user may change.
replicaCount: 2
image:
repository: nginx
tag: ""
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
env:
LOG_LEVEL: info
resources: {}Nested keys become nested paths in templates. service.port is read as .Values.service.port. An empty tag lets the template fall back to .Chart.AppVersion with default.
Two ways to override
You can override defaults when you install or upgrade.
-f(or--values) loads a YAML file. Use it for any set of changes you want to keep in version control.--setchanges one value on the command line. Use it for quick tests.
helm template my-release ./webapp -f prod-values.yaml
helm template my-release ./webapp --set replicaCount=3
helm template my-release ./webapp --set image.tag=1.27.1 --set service.port=8080An override file only needs the keys you change:
replicaCount: 4
env:
LOG_LEVEL: debugMerge order
Helm merges maps key by key. Later sources win:
- The chart's
values.yaml. - Each
-ffile, in the order given on the command line. - Each
--setflag, in the order given.--setbeats every file.
In the example above, env.LOG_LEVEL becomes debug, and replicaCount becomes 4. Keys you did not mention, such as image.repository, keep their defaults.
Common errors
Lists are replaced, not merged. If the default has a list of three items and your file supplies one, the result has one item.
--set guesses types. --set tag=1.20 is parsed as a number, so a value like 1.20 may lose its trailing zero. Use --set-string image.tag=1.20 to keep it as text.
Dots in --set mean nesting. To set a key that contains a dot, escape it: --set 'podAnnotations.example\.com/owner=web'.
A misspelled key is silently accepted. --set replicacount=3 creates a new unused key and the Deployment keeps replicaCount: 2. Check the result with helm template or helm get values.
Reusing values on upgrade
helm upgrade resets to the chart defaults unless you pass your overrides again. Use --reuse-values to keep the previous release's values, or pass the same -f file each time. Passing the file is safer because it makes the result repeatable.
helm upgrade --install my-release ./webapp -f prod-values.yamlExercise
A chart's values.yaml sets replicaCount to 2. You run helm template r ./webapp -f a.yaml -f b.yaml --set replicaCount=5. File a.yaml sets replicaCount to 3 and b.yaml sets it to 4. What replica count is rendered, and why?
Show answer
5. Helm applies values.yaml first, then a.yaml, then b.yaml, and finally --set. The last source to set the key wins, and --set always comes after the files.