Skip to content

IBM/landmark

Repository files navigation

Chart per service approach with Helm Charts in Openshift

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.

Preparation

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

Helm chart setup for the microservice

Clone the landmark repository from https://github.com/IBM/landmark. Try to build the project locally using Run-Landmark-Microservice.adoc.

Working locally with the Docker image for Landmark Microservice

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

Create the Helm charts for Landmark Microservice

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

Modify values.yaml file

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
Change the Docker image values

Modify the name of the values file with the image values with:

image:
  repository: ammbra/landmark
  tag: "1.0"
  pullPolicy: IfNotPresent
Add the PosgreSQL details

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
Configure the health endpoints

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
Modify the service values

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

Modify the template associated with Deployment

Navigate to templates/deployment.yaml. The keys associated to the values defined in values.yaml need to be employed in corresponding templates.

Modify the Docker image template

Change the following:

image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
  - name: http
  containerPort: {{ .Values.image.containerPort }}
Add the PostgreSQL environment

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 }}
Modify the health endpoints

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}}

Modify the template associated to the Service

Navigate to templates/service.yaml. and change the port and NodePort:

ports:
  - port: {{ .Values.service.ports.port }}
    nodePort: {{ .Values.service.ports.nodePort}}

Work with your modified charts

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!

Replicate the installation in qa project

Step1

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
Step2

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

Solution and more

Looking to validate your charts? Checkout the validation branch: https://github.com/IBM/landmark/tree/validation/chart-per-service-lab

Navigate to global instructions

Navigate to next section


Compatibility

The Java code in the repositories is compatible with Java11.


License

This code is dedicated to the public domain to the maximum extent permitted by applicable law, pursuant to CC0.

About

This is the repository of the landmark microservice

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published