
Follow the steps below to generate the deploy manifest files:
- Install Helm on your local
# MacOS
brew install helm
# Linux
sudo apt-get install helmCreate Helm chart for rails application using helm create [name] in your application home folder:
helm create deployEdit the values.yaml file in the chart directory with the desired configuration for the deployment. Configurations can be image name, tag, database connection settings etc. You can also create values-[ENV].yaml for environment specific files where ENV can be test, staging, dev or uat.
The templates directory containes the Kubernetes deployment manifests files with .yml or .yaml extensions. These files use the Go language syntax for the resource like deployments, services or ingress rules. {{ .Values }} object can be used to access the values in the values.yaml file.
Run helm install to deploy the rails application to Kubernetes cluster:
helm install my-release mychart --kube-context=my-contextwhere my-release is the name of the release, mychart is the name of the chart to deploy and my-context is the name of the kubernetes context to use.
Below is the directory structure of a Helm chart in this case deploy for a rails application:
1deploy/
2 Chart.yaml
3 values.yaml
4 templates/
5 deployment.yaml
6 service.yaml
7 ingress.yamlThe deployment.yaml file defines the kubernetes resources using the Go template language. It specifies the name of the container, image, tag, ports, environment variables etc. Example of the file is shown below:
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: {{ .Values.appName }}
5spec:
6 replicas: {{ .Values.replicaCount }}
7 selector:
8 matchLabels:
9 app: {{ .Values.appName }}
10 template:
11 metadata:
12 labels:
13 app: {{ .Values.appName }}
14 spec:
15 containers:
16 - name: {{ .Values.appName }}
17 image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
18 ports:
19 - containerPort: {{ .Values.containerPort }}
20 env:
21 - name: DATABASE_URL
22 value: {{ .Values.databaseUrl }}
23 # Add any other environment variables hereThe service.yaml file defines the kubernetes services details to expose the rails application to the outer network. The service file contains information like the apiVersion, kind of resource eg Service, metadata like name, labels, annotations and spec describing the desired state of the service. Example of the file is shown below:
1apiVersion: v1
2kind: Service
3metadata:
4 name: {{ .Values.appName }}
5spec:
6 selector:
7 app: {{ .Values.appName }}
8 ports:
9 - name: http
10 port: {{ .Values.servicePort }}
11 targetPort: {{ .Values.containerPort }}
12 type: {{ .Values.serviceType }}The ingress.yaml file defines the ingress resource to route traffic to the application based on the host header. Example of the file is shown below:
1apiVersion: networking.k8s.io/v1
2kind: Ingress
3metadata:
4 name: {{ .Values.appName }}
5spec:
6 rules:
7 - host: {{ .Values.hostname }}
8 http:
9 paths:
10 - path: /
11 pathType: Prefix
12 backend:
13 service:
14 name: {{ .Values.appName }}
15 port:
16 name: httpThanks to Helm for providing an awesome package manager for Kubernetes. It made life easier
