🛡️
CTHFM: Kubernetes
  • Welcome
  • Kubernetes Fundamentals
    • Kubernetes Components
      • Kubernetes Master Node
      • Worker Nodes
      • Pods
      • Service
      • ConfigMaps and Secrets
      • Namespaces
      • Deployments
      • ReplicaSets
      • Jobs and CronJobs
      • Horizontal Pod Autoscaler (HPA)
      • Kubernetes Ports and Protocols
    • Kubectl
      • Installation and Setup
      • Basic Kubectl
      • Working With Pods
      • Deployments and ReplicaSets
      • Services and Networking
      • ConfigMaps and Secrets
      • YAML Manifest Management
      • Debugging and Troubleshooting
      • Kubectl Scripting: Security
      • Customizing Kubectl
      • Security Best Practices
      • Common Issues
      • Reading YAML Files
    • MiniKube
      • Intro
      • Prerequisites
      • Installation MiniKube
      • Starting MiniKube
      • Deploy a Sample Application
      • Managing Kubernetes Resources
      • Configuring MiniKube
      • Persistent Storage in Minikube
      • Using Minikube for Local Development
      • Common Pitfalls
      • Best Practices
  • Kubernetes Logging
    • Kubernetes Logging Overview
    • Audit Logs
    • Node Logs
    • Pod Logs
    • Application Logs
    • Importance of Logging
    • Types of Logs
    • Collecting and Aggregating Logs
    • Monitoring and Alerting
    • Log Parsing and Enrichment
    • Security Considerations in Logging
    • Best Practices
    • Kubernetes Logging Architecture
  • Threat Hunting
    • Threat Hunting Introduction
    • What Makes Kubernetes Threat Hunting Unique
    • Threat Hunting Process
      • Hypothesis Generation
      • Investigation
      • Identification
      • Resolution & Follow Up
    • Pyramid of Pain
    • Threat Frameworks
      • MITRE Containers Matrix
        • MITRE Att&ck Concepts
        • MITRE Att&ck Data Sources
        • MITRE ATT&CK Mitigations
        • MITRE Att&ck Containers Matrix
      • Microsoft Threat for Kubernetes
    • Kubernetes Behavioral Analysis and Anomaly Detection
    • Threat Hunting Ideas
    • Threat Hunting Labs
  • Security Tools
    • Falco
      • Falco Overview
      • Falco's Architecture
      • Runtime Security Explained
      • Installation and Setup
      • Falco Rules
      • Tuning Falco Rules
      • Integrating Falco with Kubernetes
      • Detecting Common Threats with Falco
      • Integrating Falco with Other Security Tools
      • Automating Incident Response with Falco
      • Managing Falco Performance and Scalability
      • Updating and Maintaining Falco
      • Real-World Case Studies and Lessons Learned
      • Labs
        • Deploying Falco on a Kubernetes Cluster
        • Writing and Testing Custom Falco Rules
        • Integrating Falco with a SIEM System
        • Automating Responses to Falco Alerts
    • Open Policy Agent (OPA)
      • Introduction to Open Policy Agent (OPA)
      • Getting Started with OPA
      • Rego
      • Advanced Rego Concepts
      • Integrating OPA with Kubernetes
      • OPA Gatekeeper
      • Policy Enforcement in Microservices
      • OPA API Gateways
      • Introduction to CI/CD Pipelines and Policy Enforcement
      • External Data in OPA
      • Introduction to Decision Logging
      • OPA Performance Monitoring
      • OPA Implementation Best Practices
      • OPA Case Studies
      • OPA Ecosystem
    • Kube-Bench
    • Kube-Hunter
    • Trivy
    • Security Best Practices and Documentation
      • RBAC Good Practices
      • Official CVE Feed
      • Kubernetes Security Checklist
      • Securing a Cluster
      • OWASP
  • Open Source Tools
    • Cloud Native Computing Foundation (CNCF)
      • Security Projects
  • Infrastructure as Code
    • Kubernetes and Terraform
      • Key Focus Areas for Threat Hunters
      • Infastructure As Code: Kubernetes
      • Infrastructure as Code (IaC) Basics
      • Infastructure As Code Essential Commands
      • Terraform for Container Orchestration
      • Network and Load Balancing
      • Secrets Management
      • State Management
      • CI/CD
      • Security Considerations
      • Monitoring and Logging
      • Scaling and High Availability
      • Backup and Disaster Recovery
    • Helm
      • What is Helm?
      • Helm Architecture
      • Write Helm Charts
      • Using Helm Charts
      • Customizing Helm Charts
      • Customizing Helm Charts
      • Building Your Own Helm Chart
      • Advanced Helm Chart Customization
      • Helm Repositories
      • Helm Best Practices
      • Helmfile and Continuous Integration
      • Managing Secrets with Helm and Helm Secrets
      • Troubleshooting and Debugging Helm
      • Production Deployments
      • Helm Case Studies
Powered by GitBook
On this page
  • Customizing Helm Charts
  • The Role of values.yaml in Customization
  • Default Values in values.yaml
  • Overriding Values from the Command Line
  • Using a Custom Values File
  • Customizing Templates with Go Templating
  • Understanding Go Templating in Helm
  • Adding Conditionals
  • Using Loops
  • Managing Dependencies
  • Defining Dependencies in Chart.yaml
  • Using the requirements.yaml File (Deprecated)
  • Managing Dependencies with helm dependency
  • Hands-on Example: Customizing an Nginx Chart
  • Summary
  1. Infrastructure as Code
  2. Helm

Customizing Helm Charts

Customizing Helm Charts

Helm charts offer a powerful way to deploy applications in Kubernetes, but the real strength of Helm lies in its ability to customize these deployments to meet specific needs. Whether you’re deploying a chart to different environments (e.g., development, staging, production) or tailoring an application to your unique requirements, customizing Helm charts is a crucial skill. In this lesson, we will explore how to modify Helm charts using various techniques, such as overriding values, utilizing the values.yaml file, and creating custom templates. By the end of this lesson, you'll have the knowledge and tools to tailor Helm charts to your exact specifications.

The Role of values.yaml in Customization

The values.yaml file is the cornerstone of Helm chart customization. It contains default configuration values that define how the chart will be deployed. By modifying this file or overriding its values during installation, you can easily customize the behavior of the chart.

Default Values in values.yaml

Every Helm chart comes with a values.yaml file that specifies default values for various configuration options. These options typically include settings for the application’s image, number of replicas, service type, resource limits, and more.

Example values.yaml:

replicaCount: 2
image:
  repository: nginx
  tag: 1.16.0
  pullPolicy: IfNotPresent
service:
  type: ClusterIP
  port: 80
  • replicaCount: Specifies the number of replicas for the deployment.

  • image: Defines the Docker image repository, tag, and pull policy.

  • service.type: Determines the type of Kubernetes service (e.g., ClusterIP, NodePort).

Overriding Values from the Command Line

One of the simplest ways to customize a Helm chart is to override values in the values.yaml file using the --set flag during installation or upgrade.

Example of Overriding Values:

helm install my-app bitnami/nginx --set replicaCount=3 --set service.type=NodePort
  • replicaCount=3: Overrides the default replica count to 3.

  • service.type=NodePort: Changes the service type to NodePort.

This approach is particularly useful for making quick adjustments without modifying the chart’s files directly.

Using a Custom Values File

For more complex customizations, you can create your own values.yaml file and pass it to Helm during installation or upgrade.

Creating a Custom values.yaml:

replicaCount: 4
image:
  repository: custom-nginx
  tag: 1.17.0
service:
  type: LoadBalancer
  port: 8080

Applying the Custom Values File:

bashCopy codehelm install my-app bitnami/nginx -f my-values.yaml

Using a custom values file allows for greater flexibility and makes it easier to manage configurations across different environments.

Customizing Templates with Go Templating

Helm uses Go templating to render Kubernetes manifests, which means you can customize the templates themselves to achieve more dynamic behavior.

Understanding Go Templating in Helm

Go templating allows you to introduce logic into your Kubernetes manifests. You can use variables, conditionals, loops, and more to create highly customizable templates.

Example of a Simple Go Template:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-deployment
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
      - name: {{ .Release.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        ports:
        - containerPort: 80
  • {{ .Release.Name }}: Refers to the name of the Helm release.

  • {{ .Values.replicaCount }}: Inserts the value for replicaCount from values.yaml.

Adding Conditionals

You can add conditionals to templates to include or exclude certain resources or configuration options based on specific conditions.

Example of Using a Conditional:

spec:
  containers:
    - name: {{ .Release.Name }}
      image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
{{- if .Values.resources }}
      resources:
        limits:
          cpu: {{ .Values.resources.limits.cpu }}
          memory: {{ .Values.resources.limits.memory }}
{{- end }}
  • {{- if .Values.resources }}: The resources section is only included if resource limits are defined in values.yaml.

Using Loops

Loops can be used to dynamically generate multiple resources or configuration entries based on a list of values.

Example of a Loop in a Template:

env:
{{- range .Values.env }}
  - name: {{ .name }}
    value: {{ .value }}
{{- end }}
  • {{- range .Values.env }}: Loops over each item in the env list and generates an environment variable entry.

Managing Dependencies

Helm charts can depend on other charts, known as sub-charts. Managing dependencies allows you to build more complex applications from smaller, reusable components.

Defining Dependencies in Chart.yaml

You can specify chart dependencies in the Chart.yaml file. Dependencies are other Helm charts that your chart relies on, and they are stored in the charts/ directory.

Example of Dependencies in Chart.yaml:

dependencies:
  - name: redis
    version: 6.0.1
    repository: https://charts.bitnami.com/bitnami
  • name: redis: Specifies the name of the dependent chart.

  • version: 6.0.1: The version of the redis chart to use.

  • repository: The repository from which to download the chart.

Using the requirements.yaml File (Deprecated)

In older versions of Helm (v2), dependencies were managed in a requirements.yaml file. While Helm v3 no longer uses this file, it’s important to be aware of it if you’re working with legacy charts.

Example requirements.yaml:

dependencies:
  - name: redis
    version: 6.0.1
    repository: https://charts.bitnami.com/bitnami

Dependencies defined in Chart.yaml are automatically downloaded and placed in the charts/ directory when you run helm dependency update.

Managing Dependencies with helm dependency

Helm provides commands to manage dependencies, such as updating, listing, and adding them.

Updating Dependencies:

helm dependency update

Listing Dependencies:

helm dependency list

These commands ensure that your chart has all the necessary components to function correctly.

Hands-on Example: Customizing an Nginx Chart

To apply what you’ve learned, let’s customize an Nginx Helm chart:

Steps:

  1. Download the Chart:

    helm pull bitnami/nginx --untar
  2. Edit the values.yaml File: Modify the values.yaml to change the number of replicas and the service type.

    replicaCount: 4
    service:
      type: NodePort
  3. Edit the deployment.yaml Template: Add a conditional to include resource limits only if specified.

    resources:
      {{- if .Values.resources }}
      limits:
        cpu: {{ .Values.resources.limits.cpu }}
        memory: {{ .Values.resources.limits.memory }}
      {{- end }}
  4. Install the Chart with Custom Values:

    helm install my-custom-nginx ./nginx -f custom-values.yaml
  5. Verify the Deployment: Check that the customizations were applied correctly.

    helm status my-custom-nginx

This example walks you through customizing a Helm chart by modifying values.yaml and templates, allowing you to tailor the deployment to specific requirements.

Summary

Customizing Helm charts is a powerful way to adapt Kubernetes deployments to meet your specific needs. By understanding how to modify the values.yaml file, use Go templating, and manage dependencies, you can create highly tailored and dynamic Kubernetes applications. Mastering these techniques will allow you to deploy complex applications with precision and flexibility.

In the next lesson, we’ll explore best practices for creating and managing your own Helm charts, ensuring that your deployments are maintainable, scalable, and secure.

PreviousCustomizing Helm ChartsNextBuilding Your Own Helm Chart

Last updated 9 months ago