How to deploy a service?
Abstract
To deploy a service you will need to create an Helm chart with the corresponding definition and values. The following steps will guide you through the process of deploying a new service.
Dockerize it
The services in order to be deployed in the Kubernetes infrastructure need to be dockerized. The process of dockerizing an application is extensively described in the Docker documentation. However in this paragraph some good practices are described.
Basic setup
First of all create a Dockerfile
in the root of the service folder. The Dockerfile
is a text document that contains
all the commands a user could call on the command line to assemble an image. In order to generate a consistent
Dockerfile we use the docker cli. Supposing that you are in service folder you can run the following command
By following the instructions you will be able to create a Dockerfile
a .dockerignore
file and eventually provide you
a skeleton for the docker file. To be noted the following best practices:
- Create a non-privileged user that the app will run under.
- Cache dependencies (e.g. pip install) in a separate layer.
Build the image
Build your docker image locally to test if the image is built correctly. The following command will build the image and tag it with the name of the service.
Run locally and test
Once the image is built you can run the image locally to test if the service is working correctly. You will generally run the image by using the run command as follows (Note that you may pass parameters depending on the application specific parameters).
Setup a CI/CD pipeline
The project has been configured to automatically build Docker images for services. The process is triggered by pushing changes to the service repository. The Action will build the Docker image assign a unique tag and push it to the Harbor registry. Moreover the action automatically changes the tag value in the helm chart value of the relative service in order to have the latest version of the service.
flowchart TD
Start[Local service code] -->|Push to GitHub| B{Changes?};
B -->|Yes| C[Triggers Github Action];
B -->|No| End[Online service code];
C --> |Push Docker Image|D[(Harbor)];
C --> |Update values.yaml| Values[Updated Service];
Values[Service chart]--> |Auto commit + push| End[Online service code];
Create helm chart
The entire repository is structured by following the classic helm chart repository structure ( see helm documentation for further details). A chart is organized as a collection of files inside of a directory and the directory name is the name of the chart.
In the specific case, the main chart is called bos
and it contains all the subcharts that are needed to deploy the
services in the chart
folder. The structure is the following:
bos/
Chart.yaml # A YAML file containing information about the chart
values.yaml # The default configuration values for this chart
charts/ # A directory containing services charts
templates/ # A directory of templates that, when combined with values, will generate valid Kubernetes manifest files.
LICENSE # OPTIONAL: A plain text file containing the license for the chart
README.md # OPTIONAL: A human-readable README file
In order to deploy a new service, you will need to create a new helm chart in order to create the relative service in the Kubernetes infrastructure. Depending on the typo of service you are deploying the content of the chart will change but the overall folders structure will be the same.
Supposing that you are in the root directory of the project, navigate to the sub-charts folder of the bos main chart.
Create a new chart folder with the name of the service you are deploying. By convention, the service folder name should be in lowercase and separated by a hyphen. For further details see the helm CLI documentation.
Remember to add the new chart as a nes dependency into the bos/Chart.yaml
file. You should add something like this:
dependencies:
- name: <service-name>
version: <service-version>
repository: file://charts/<service-name>
In order to be sure that the service has been added properly to the main chart and the system resolves the dependency in
a proper way.
Execute the following commands in the bos
folder:
Push the changes
Once you locally tested the service, the helm chart was created it is time to put the service online. You can simple apply the changes on the kubernetes cluster by one command. Supposing you are in the root directory of the project you can run the following command:
This command will run under the hood helm upgrade
command and applies the changes to the kubernetes cluster.