Getting Started With Helm
Helm is a tool for managing Kubernetes packages which allows to create, scale and maintain complex applications easily. To learn more about Helm and its architecture, click here.
Installing Helm
To install helm, kubernetes must be installed and minikube should be running.
Open up the terminal type the following commands.
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3chmod 700 get_helm.sh./get_helm.sh
To verify the Helm installation, type
helm version
and press enter, you should see the helm version.
Creating PostgreSQL Chart
Type the following command.
$ mkdir helm-tutorial$ cd helm-tutorial$ helm create postgres
Creating postgres
You should see a directory named postgres which the following structure.
$ cd postgres$ ls
charts Chart.yaml templates values.yaml
Now lets start defining our deployment, but first delete the deployment.yml file and create and new one.
$ rm templates/deployment.yaml
$ vi templates/deployment.yaml
Paste the following code.
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.postgres.name }}
labels:
app: {{ .Values.postgres.name }}
group: {{ .Values.postgres.group }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.postgres.name }}
template:
metadata:
labels:
app: {{ .Values.postgres.name }}
group: {{ .Values.postgres.group }}
spec:
volumes:
- name: {{ .Values.postgres.volume.name }}
persistentVolumeClaim:
claimName: {{ .Values.postgres.volume.pvc.name }}
containers:
- name: {{ .Values.postgres.name }}
image: {{ .Values.postgres.container.image }}
ports:
- containerPort: {{ .Values.postgres.container.port }}
envFrom:
- configMapRef:
name: {{ .Values.postgres.config.name }}
volumeMounts:
- name: {{ .Values.postgres.volume.name }}
mountPath: {{ .Values.postgres.volume.mountPath }}
To create the persistent volume claim, create a file called pvc.yml.
$ vi templates/pvc.yaml
Copy the following content.
---
apiVersion: v1
kind: {{ .Values.postgres.volume.kind }}
metadata:
name: {{ .Values.postgres.volume.pvc.name }}
spec:
accessModes:
- {{ .Values.postgres.volume.pvc.accessMode }}
resources:
requests:
storage: {{ .Values.postgres.volume.pvc.storage }}
To expose postgres we have to define a service, by first deleting the service.yml file and create and new one.
$ rm templates/service.yaml
$ vi templates/service.yaml
Copy the following content.
---
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.postgres.name }}
labels:
group: {{ .Values.postgres.group }}
spec:
type: {{ .Values.postgres.service.type }}
selector:
app: {{ .Values.postgres.name }}
ports:
- port: {{ .Values.postgres.service.port }}
targetPort: {{ .Values.postgres.container.port }}
To provide configurations through a file, create a file called config.yml.
$ vi templates/config.yaml
Copy the following content.
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.postgres.config.name }}
labels:
group: {{ .Values.postgres.group }}
data:
{{- range .Values.postgres.config.data }}
{{ .key }}: {{ .value }}
{{- end}}
lets provide default values, by first deleting the values.yml file and create and new one.
$ rm values.yaml
$ vi values.yaml
Copy the following content.
---
replicaCount: 1
postgres:
name: postgres
group: db
container:
image: postgres:9.6-alpine
port: 5432
service:
type: ClusterIP
port: 5432
volume:
name: postgres-storage
kind: PersistentVolumeClaim
mountPath: /var/lib/postgresql/data
pvc:
name: postgres-persistent-volume-claim
accessMode: ReadWriteOnce
storage: 4Gi
config:
name: postgres-config
data:
- key: POSTGRES_DB
value: RevolutionArmy
- key: POSTGRES_USER
value: Revolutioner
- key: POSTGRES_PASSWORD
value: Revolutioner
Go outside the postgres folder, i.e. helm-tutorial and create a file to override basic configurations.
$ cd ..
$ vi override-config.yaml
Copy the following content.
postgres:
config:
data:
- key: POSTGRES_DB
value: postgres
- key: POSTGRES_USER
value: postgres
- key: POSTGRES_PASSWORD
value: postgres
Now everything is ready, we can now deploy postgres but lets clean up the unused files.
$ cd postgres/templates$ rm _helpers.tpl$ rm hpa.yaml$ rm ingress.yaml$ rm NOTES.txt$ rm serviceaccount.yaml$ rm -rf tests
Deploying PostgreSQL Chart
When a chart is deployed to a cluster using Helm, it is called a release.
$ helm install -f override-config.yaml postgres ./postgres
You should an output something like this.
NAME: postgres
LAST DEPLOYED: Thu Jul 30 09:31:54 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
To view all the charts, type the following
$ helm listNAME NAMESPACE REVISION STATUS CHART APP VERSION
postgres default 1 deployed postgres-0.1.0 1.16.0
To view all the releases, type the following ( note this will also list other k8s deployments)
$ kubectl get deploymentsNAME READY UP-TO-DATE AVAILABLE AGE
postgres 1/1 1 1 2m45s