0
0
Kubernetesdevops~5 mins

Chart templates and values.yaml in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to deploy apps on Kubernetes, you often need to repeat similar settings with small changes. Chart templates and values.yaml help you write these settings once and reuse them easily with different values.
When you want to deploy the same app multiple times with different configurations like ports or replicas.
When you want to share your app deployment setup with others without hardcoding details.
When you want to update app settings quickly by changing values without editing many files.
When you want to keep your Kubernetes files clean and easy to understand by separating data from templates.
When you want to automate app deployments with tools like Helm using reusable templates.
Config File - deployment.yaml
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.app.name }}
  labels:
    app: {{ .Values.app.name }}
spec:
  replicas: {{ .Values.app.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.app.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.app.name }}
    spec:
      containers:
      - name: {{ .Values.app.name }}-container
        image: {{ .Values.app.image }}
        ports:
        - containerPort: {{ .Values.app.port }}

This is a Kubernetes Deployment template file using Helm syntax.

  • {{ .Values.app.name }} inserts the app name from values.yaml.
  • replicas sets how many copies of the app run.
  • image is the container image to use.
  • containerPort is the port the app listens on.

This template lets you change these values easily without editing this file.

Commands
Creates a new Helm chart named 'my-app' with default templates and a values.yaml file.
Terminal
helm create my-app
Expected OutputExpected
Creating new chart 'my-app'
Shows the default values.yaml file where you can set app-specific values used in templates.
Terminal
cat my-app/values.yaml
Expected OutputExpected
replicaCount: 1 image: repository: nginx pullPolicy: IfNotPresent tag: "" service: type: ClusterIP port: 80
Installs the Helm chart with custom values overriding defaults to deploy 3 replicas of the app on port 8080.
Terminal
helm install example-release my-app --set replicaCount=3,image.repository=myapp/image,image.tag=v1.0,service.port=8080
Expected OutputExpected
NAME: example-release LAST DEPLOYED: Thu Apr 27 12:00:00 2024 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None
--set - Override values.yaml settings directly from the command line
Displays the values used for the installed release to verify the applied configuration.
Terminal
helm get values example-release
Expected OutputExpected
replicaCount: 3 image: repository: myapp/image pullPolicy: IfNotPresent tag: v1.0 service: type: ClusterIP port: 8080
Key Concept

If you remember nothing else from this pattern, remember: templates define the structure and values.yaml provides the flexible data to customize deployments easily.

Common Mistakes
Editing the template files directly for each deployment instead of using values.yaml.
This causes duplication and makes updates hard because you must change many files.
Keep templates generic and change settings only in values.yaml or with --set flags.
Using incorrect Helm template syntax like missing curly braces or wrong variable names.
Helm will fail to render templates and the deployment will not work.
Use {{ .Values.key }} syntax exactly and test templates with 'helm template' command.
Not overriding values when installing the chart, so the app runs with default settings.
The deployment might not match your needs like wrong ports or image versions.
Use a custom values.yaml file or --set flags to provide correct values.
Summary
Helm chart templates use placeholders to define Kubernetes resources generically.
The values.yaml file holds the data that fills these placeholders for each deployment.
You can override values.yaml settings using the --set flag during helm install for quick changes.