Intializing a microservice from scratch transforms a microservice source code project into a kubernetes deployment in minutes. It builds the necessary Dockerfile and the Kubernetes artifacts based on the microservice profile.

Since the Docker builds happen inside a Kubernetes cluster, the initialization process requires one time setup of creating a Cluster or registering a pre-existing cluster in the gopaddle subscription. We also need a Docker registry to push the newly built Docker Image after the build process. We need a buildImage which is used to create a build environment within the Kubernetes cluster. Microservice is built inside a container launched from the buildImage.

The automated build process generates the Dockerfile, builds the container and pushes the image to the registry. The automated deployment process generates the kubernetes artifacts and deploys the application to the Kubernetes cluster. gpctl supports only nodeport based deployments. However the template created in the init process can be extended and reused from the gopaddle UI interface to deploy applications with routing policies and loadbalancer.

Pre-requisite

1. Register a Cloud Account which can be used to provision a Kubernetes Cluster and to register a Docker registry.

2. From the gopaddle console, Create or Register a Kubernetes Cluster. This cluster will be used to build the Docker container and to deploy the microservice. Follow these steps to create an EKS, GKE or AKS cluster or register an external cluster.

3. From the gopaddle console, Register a Docker Registry. This registry will be used for both pulling the base image used to build the microservice and also to push the final docker image generated after the build process. A baseImage is a Docker Image used to launch a Docker container-based build environment, inside which the microservice buildscripts will be executed.

4. Choose a few ports in the range 30000 - 32767 and open the firewall ports in the Cloud Account. When the service is launched using gpctl, you can choose one of these ports to access the service via Kubernetes node port.

5. Download and configure gopaddle command line utility by following the steps mentioned here;

6. Install the dependencies that are required to initialize the project.

apt-get install strace iproute2 -y

7. Cleanup the /tmp/gp directory, if any.

rm -rf /tmp/gp

8. As a best practice, ensure that you are initializing your application as a non-root user. If not, create a user and a group, before initiatizing the project.

groupadd --gid 1000 demo
useradd -m -b /home --home-dir /home/demo --uid 1000 --gid 1000 demo
su demo

Initialize the microservice

1. Clone the project from the source control repository

git clone <cloneURL>

2. Create a build script to build the project. Say if it is a nodeJS application, you can create a buildScript.sh as below.

#!/bin/bash
npm install
npm run build

3. Create a run script to run the project. Say, if it is a nodeJS application that has a dependency on an external Mongodb database, you can create a runScript.sh as below:

#!/bin/bash
cd dist
MONGODB_URI=mongodb://35.232.13.226:30000 npm start

NOTE: Since the buildScript.sh and startScript.sh will be run on two different environments, environment variables required to build the container and to run the container should be defined in these scripts appropriately.

4. Change the mode of the buildScript.sh and startScript.sh and check if you can execute them locally.

chmod +x buildScript.sh
chmod +x startScript.sh

5. Initialize the project by executing the gpctl init command

gpctl init --startScript=startScript.sh --buildScript=buildScript.sh --buildImage=<buildImage> --configMap=<folder1>:<folder2> --startAfter=<initialWaitTimeInSecs>

startScript - Script that starts the microservice

buildScript - Script that builds the microservice

buildImage - Base image to bring up a container that acts as the build environment to build the microservice. Build script will be executed within this container to build the microservice.

configMap - ':' separated list of folders in the current project that needs to be added as configMap in Kubernetes. All regular files within this folder will be added to a configMap and use for microservice deployment.

startAfter - Initial wait time before the initialization process starts. Defaults to 20 seconds if not provided. This is the time required for the microservice to move to running state when the startScript is executed locally.

6. Once the init is executed, you will be asked to choose the cluster where the build needs to be executed, the docker registry where the docker image has to be pushed, the allocation policy to use for building and deploying the container, the alternate ports to use to map the microservice port to kubernetes node ports. When the init completes, you will get the application URL to access the microservice running in the Kubernetes environment.

Did this answer your question?