Use a Helm chart per microservice approach if you seek to achieve flexible, simple versioning and low complexity charts to package your application. Be careful, this technique might bring:
-
huge amount of duplication
-
difficulty to keep consistent many templates
-
hardship to introduce global changes
Goal of this section: deploy the landmark microservice using Helm charts.
All the below commands will be executed in a terminal window.
To login to the OpenShift cluster from the Terminal run:
$ oc login -u kubeadmin -p YOUR_SECRET_PASSWORD https://api.crc.testing:6443
Check if you already have the dev project:
$ oc get project $ oc new-project dev
The landmark microservice will persist its data into a database, in this case a PostgreSQL database instance.
In order to install the database, the PostgreSQL Helm Charts will be used. Check if the Helm repo https://charts.bitnami.com/bitnami is added to your existing list:
$ helm repo list
If the repo is not there, please run the following set of commands:
$ helm repo add bitnami https://charts.bitnami.com/bitnami
Setup the database instance with Helm, using the following command:
$ helm install landmark-db \
--set postgresqlUsername=landmark-default,postgresqlPassword=postgres,postgresqlDatabase=landmark,persistence.enabled=false \
stable/postgresql
$ helm status landmark-db
Validate the installation via:
$ helm list
$ helm get all landmark-db
Clone the landmark repository from https://github.com/IBM/landmark. Try to build the project locally using Run-Landmark-Microservice.adoc.
Make sure that you are under root folder of the clone repository (landmark) and run:
$ mvn package
Then, build the image with:
$ docker run -d -p 5000:5000 --restart=always --name registry registry:2
$ docker build -f src/main/docker/Dockerfile.jvm -t quarkus/landmark:1.0 .
$ docker tag quarkus/landmark:1.0 localhost:5000/quarkus/landmark:1.0
$ docker push localhost:5000/quarkus/landmark:1.0
Make sure that you are under root folder of the clone repository (landmark) and run:
$ cd chart
$ helm create landmark
The above command generates the following structure:
landmark
├── Chart.yaml
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── deployment.yaml
│ ├── ingress.yaml
│ ├── service.yaml
│ ├── serviceaccount.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
Go to the newly created charts and find the values.yaml file. You can use vim or your favorite IDE to edit its values.
$ cd landmark
$ vim values.yaml
Modify the name of the values file with the image values with:
image:
repository: ammbra/landmark
tag: "1.0"
pullPolicy: IfNotPresent
Using the details from above you can parameterize the connection to the database:
postgresql:
server: landmark-db-postgresql
postgresqlUsername: landmark-default
secretName: landmark-db-postgresql
secretKey: postgresql-password
Every well designed application needs health checks and this why you should add the following:
readinessProbe:
path: /health/ready
initialDelaySeconds: 5
timeoutSeconds: 3
periodSeconds: 3
failureThreshold: 3
livenessProbe:
path: /health/live
initialDelaySeconds: 30
timeoutSeconds: 2
periodSeconds: 8
failureThreshold: 3
You deployed application should be accessible from inside and outside the Openshift cluster. A Kubernetes Service of type NodePort will be used for this installation.
Please change the service values that will expose your microservice:
service:
ports:
port: 8080
nodePort: 31125
type: NodePort
Navigate to templates/deployment.yaml. The keys associated to the values defined in values.yaml need to be employed in corresponding templates.
Change the following:
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.image.containerPort }}
Add the following environment variables, defined in values.yaml
env:
- name: POSTGRES_SERVER
value: {{ .Values.postgresql.server | default (printf "%s-postgresql" ( .Release.Name )) | quote }}
- name: POSTGRES_USERNAME
value: {{ default "postgres" .Values.postgresql.postgresUsername | quote }}
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.postgresql.secretName | default (printf "%s-postgresql" ( .Release.Name )) | quote }}
key: {{ .Values.postgresql.secretKey }}
The health endpoints need to adapted in order to use the keys defined in values.yaml. Change the health and readiness using the following:
readinessProbe:
httpGet:
path: {{ .Values.readinessProbe.path}}
port: {{ .Values.service.ports.port }}
initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds}}
timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds}}
periodSeconds: {{ .Values.readinessProbe.periodSeconds}}
failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
livenessProbe:
httpGet:
path: {{ .Values.livenessProbe.path}}
port: {{ .Values.service.ports.port }}
initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds}}
timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds}}
periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
failureThreshold: {{ .Values.livenessProbe.failureThreshold}}
Install your charts in default namespace
$ helm install simple ./chart/landmark
$ helm status simple
$ kubectl get ns
$ kubectl get svc
$ kubectl port-forward --namespace dev svc/simple-landmark 8080
Go in a browser window and copy-paste http://localhost:8080/api/museum/muse
Congratulations, you found a landmark!
According to Openshift documentation a project is a Kubernetes namespace with additional annotations, and is the central vehicle by which access to resources for regular users is managed. This means that a project offers you the ability to deploy Helm charts without specifying the namespace.
Create qa project and install a PostgreSQL instance:
$ oc new-project qa
$ helm install landmark-db \
--set postgresqlUsername=landmark-default,postgresqlPassword=postgres,postgresqlDatabase=landmark,persistence.enabled=false \
stable/postgresql
$ helm status landmark-db
Duplicate the values.yaml and rename it to values.qa.yaml. Change the NodePort value in values.qa.yaml to 31126. Install the charts:
$ helm install simple ./chart/landmark --values ./chart/landmark/values.qa.yaml
$ helm status simple
$ kubectl get ns
$ kubectl get svc
$ kubectl port-forward --namespace qa svc/simple-landmark 8080
Go in a browser window and copy-paste http://localhost:8080/api/museum/muse
Congratulations, you found a landmark and replicated deployment process accros environments!
In order to switch back to project dev, use the below command:
$ oc project dev
Blocked? Checkout the solution branch : https://github.com/IBM/landmark/tree/solution/chart-per-service-lab
Looking to validate your charts? Checkout the validation branch: https://github.com/IBM/landmark/tree/validation/chart-per-service-lab
This code is dedicated to the public domain to the maximum extent permitted by applicable law, pursuant to CC0.