Generation of Kubernetes Deploy Manifests For a Rails Application
A very simple and easy way to generate kubernetes deployment manifests for a rails application is through the Helm chart. Helm is simply a package manager for Kubernetes. Helm Chart is a collection of kubernetes resource files like deployments, services and ingress rules also the values that is used to configure these resources.
Follow the steps below to generate the deploy manifest files:
- Install Helm on your local
# MacOS
brew install helm# Linux
sudo apt-get install helm- Create Helm chart for rails application using
helm create [name]in your application home folder
helm create deploy- Edit the
values.yamlfile in the chart directory with the desired configuration for the deployment. Configurations can be image name, tag, database connection settings etc. You can also createvalues-[ENV].yamlfor environment specific files where ENV can betest,staging,devoruat. - The templates directory containes the Kubernetes deployment manifests files with
.ymlor.yamlextensions. 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 thevalues.yamlfile. - Run
helm installto 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
deploy/
Chart.yaml
values.yaml
templates/
deployment.yaml
service.yaml
ingress.yamlThe deployment.yaml file defines the kubernetes resources using the Go template languaage. It specifies the name of the container, image, tag , ports , environment variables etc. Example of the file is shown below
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.appName }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.appName }}
template:
metadata:
labels:
app: {{ .Values.appName }}
spec:
containers:
- name: {{ .Values.appName }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
ports:
- containerPort: {{ .Values.containerPort }}
env:
- name: DATABASE_URL
value: {{ .Values.databaseUrl }}
# 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 descring the desired state of the service. Example of the file is shown below
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.appName }}
spec:
selector:
app: {{ .Values.appName }}
ports:
- name: http
port: {{ .Values.servicePort }}
targetPort: {{ .Values.containerPort }}
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
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Values.appName }}
spec:
rules:
- host: {{ .Values.hostname }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ .Values.appName }}
port:
name: httpThanks to Helm for providing an awesome package manager for Kubernetes. It made life easier
