Example Helm Chart for Nginx - unix1998/technical_notes GitHub Wiki

Helm is a package manager for Kubernetes that allows you to define, install, and upgrade complex Kubernetes applications using "charts." Nginx is a popular web server that can also be used as a reverse proxy, load balancer, and HTTP cache. Below, I'll provide an example of a Helm chart for deploying Nginx in a Kubernetes cluster.

Example Helm Chart for Nginx

Here's a simple example of a Helm chart structure for Nginx:

nginx-chart/
├── Chart.yaml
├── values.yaml
├── templates/
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   └── _helpers.tpl
└── README.md

Chart.yaml

This file contains metadata about the chart:

apiVersion: v2
name: nginx
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.19.6"

values.yaml

This file contains the default values for your chart's variables:

replicaCount: 2

image:
  repository: nginx
  tag: "1.19.6"
  pullPolicy: IfNotPresent

service:
  type: LoadBalancer
  port: 80

ingress:
  enabled: true
  annotations: {}
  hosts:
    - host: example.local
      paths:
        - /
  tls: []

resources: {}

nodeSelector: {}

tolerations: []

affinity: {}

templates/deployment.yaml

This file defines the Kubernetes Deployment resource for Nginx:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "nginx.fullname" . }}
  labels:
    app: {{ include "nginx.name" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ include "nginx.name" . }}
  template:
    metadata:
      labels:
        app: {{ include "nginx.name" . }}
    spec:
      containers:
        - name: nginx
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: 80
          resources:
            {{- toYaml .Values.resources | nindent 12 }}

templates/service.yaml

This file defines the Kubernetes Service resource for Nginx:

apiVersion: v1
kind: Service
metadata:
  name: {{ include "nginx.fullname" . }}
  labels:
    app: {{ include "nginx.name" . }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: 80
      protocol: TCP
      name: http
  selector:
    app: {{ include "nginx.name" . }}

templates/ingress.yaml

This file defines the Kubernetes Ingress resource for Nginx:

{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "nginx.fullname" . }}
  labels:
    app: {{ include "nginx.name" . }}
  annotations:
    {{- range $key, $value := .Values.ingress.annotations }}
      {{ $key }}: {{ $value | quote }}
    {{- end }}
spec:
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .paths }}
          - path: {{ . }}
            pathType: ImplementationSpecific
            backend:
              service:
                name: {{ include "nginx.fullname" $. }}
                port:
                  number: 80
          {{- end }}
    {{- end }}
  {{- if .Values.ingress.tls }}
  tls:
    {{- range .Values.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName | quote }}
    {{- end }}
  {{- end }}
{{- end }}

templates/_helpers.tpl

This file can contain helper templates that can be reused across other templates:

{{- define "nginx.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "nginx.fullname" -}}
{{- default (include "nginx.name" .) .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

README.md

This file provides an overview and usage instructions for the chart:

# Nginx Helm Chart

This is a simple Helm chart for deploying Nginx to a Kubernetes cluster.

## Installation

```sh
helm install my-nginx ./nginx-chart

Configuration

You can configure this chart using the values.yaml file or by providing overrides on the command line:

helm install my-nginx ./nginx-chart --set replicaCount=3

### Installing the Chart

To install the chart, run the following command:

```sh
helm install my-nginx ./nginx-chart

Customizing the Chart

You can customize the chart by modifying the values.yaml file or by passing custom values via the --set flag. For example:

helm install my-nginx ./nginx-chart --set replicaCount=3

This is a basic example to get you started with Helm and Nginx. You can further customize and expand this chart to fit more specific requirements. Let me know if you need more details or specific configurations!