NashTech Blog

Helm Best Practices

Introduction

Helm, the package manager for Kubernetes, simplifies the deployment and management of applications on Kubernetes clusters. By using Helm charts, you can define, install, and upgrade even the most complex Kubernetes applications. However, to ensure a smooth and efficient workflow, it’s essential to follow best practices. This guide will walk you through the best practices for using Helm, from chart creation to deployment.

1. Chart Structure and Organization

a. Maintain a Clean and Consistent Directory Structure

A well-organized chart makes it easier to manage and understand. The typical structure of a Helm chart includes:

my-chart/
  ├── charts/
  ├── templates/
  ├── Chart.yaml
  ├── values.yaml
  ├── README.md
  • Chart.yaml: Contains metadata about the chart.
  • values.yaml: Defines default configuration values.
  • templates/: Contains Kubernetes manifest templates.
  • charts/: Holds dependent charts.
  • README.md: Provides documentation.

b. Use Meaningful Names and Descriptions

Ensure your chart names, template file names, and variable names are descriptive and meaningful. This practice helps others understand the purpose and functionality of your chart quickly.

2. Versioning and Dependencies

a. Follow Semantic Versioning

Helm charts should adhere to Semantic Versioning (SemVer). This practice helps in managing versions effectively and understanding the impact of updates:

  • MAJOR version for incompatible API changes.
  • MINOR version for adding functionality in a backward-compatible manner.
  • PATCH version for backward-compatible bug fixes.

b. Manage Dependencies

Use the requirements.yaml file to manage chart dependencies. This file should list all the dependent charts, their repositories, and versions. Running helm dependency update will fetch and install the required dependencies.

3. Values and Configuration

a. Use values.yaml for Default Values

The values.yaml file should contain default configuration values for your chart. This file allows users to override specific values without modifying the chart directly.

b. Avoid Hardcoding Values

Avoid hardcoding values in your templates. Instead, reference values from values.yaml. This practice makes your chart more flexible and reusable.

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.configMapName }}
data:
  my-value: {{ .Values.myValue }}

c. Provide a Schema for Values

Using values.schema.json, you can define a schema for the values accepted by your chart. This schema helps validate the values provided by users and ensures they are in the correct format.

4. Templates and Templating

a. Follow Kubernetes Best Practices

Ensure your templates adhere to Kubernetes best practices. For example, use resource limits and requests, define readiness and liveness probes, and follow security best practices.

b. Use Helpers and Functions

Leverage Helm’s helper templates and functions to avoid repetition and simplify complex logic. Define common snippets in _helpers.tpl and use them across your templates.

{{- define "my-chart.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

c. Minimize Template Logic

Keep logic in templates minimal to maintain readability and reduce complexity. Use pipelines and Helm functions to process values efficiently.

env:
  - name: MY_ENV_VAR
    value: {{ required "Environment variable MY_ENV_VAR is required" .Values.myEnvVar | quote }}

5. Documentation

a. Document Your Chart

A well-documented chart is easier to use and maintain. Include a comprehensive README.md that covers:

  • Chart description and purpose.
  • Installation instructions.
  • Configuration options and examples.
  • Dependencies and their versions.

b. Use Annotations

Use Helm annotations to provide additional information about your chart and templates. Annotations can include usage instructions, links to documentation, or any other relevant information.

6. Testing and Validation

a. Write Unit Tests

Use the helm-unittest plugin to write unit tests for your chart. Unit tests help ensure your templates render correctly and behave as expected.

b. Lint Your Chart

Run helm lint to catch common issues in your chart. This command checks for best practices and common errors, helping you maintain high-quality charts.

7. Security

a. Follow Kubernetes Security Best Practices

Ensure your chart follows Kubernetes security best practices. For example:

  • Use RBAC policies to restrict permissions.
  • Define security contexts for your pods.
  • Avoid running containers as root.

b. Manage Sensitive Data

Use Kubernetes Secrets to manage sensitive data like passwords and API keys. Reference these secrets in your templates rather than hardcoding them.

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: {{ .Values.secretUsername | b64enc | quote }}
  password: {{ .Values.secretPassword | b64enc | quote }}

8. CI/CD Integration

a. Automate Chart Testing and Deployment

Integrate your Helm chart into your CI/CD pipeline. Automate testing, linting, and deployment to ensure your chart is always in a deployable state.

b. Use Helm Repositories

Publish your charts to a Helm repository to make them easily accessible to others. Use repositories like Artifact Hub or set up your own private repository.

Conclusion

Following these best practices will help you create robust, maintainable, and reusable Helm charts. A disciplined approach to chart creation, versioning, templating, and documentation ensures that your Kubernetes applications are deployed efficiently and reliably. By adhering to these guidelines, you can harness the full power of Helm and Kubernetes for your deployments.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top