Skip to content

A starting point for automating the creation of microservices and all its Ops costs with a single click.

License

Notifications You must be signed in to change notification settings

andreschaffer/one-click-microservice

Repository files navigation

Build Status

One Click Microservice

Presentation at JFokus 2017

image alt text

Motivation

Having a way of rapidly creating and deploying microservices is a prerequisite if you want to do serious microservices. It should be easy, so that we can focus on business, the thing that makes us unique.
This project serves as a starting point for automating all the operational setup cost associated to creating new microservices with a single click.

How it works

The central pieces of this solution are Docker, Jenkins, the Jenkins Job-DSL plugin and Gogs (it should be easy for you to adapt it to work with any other Git Server though, like GitHub or BitBucket).

We use Docker to run Jenkins and Gogs. Jenkins itself has access to Docker in a Docker-outside-of-Docker approach, i.e. we map the host's Docker socket, so that containers started by Jenkins will be siblings.

We have prepared two repositories into Gogs (our Git server):

  • microservice_code_generator, which is responsible for the generation of the microservices' skeleton.
    In our case, we are creating a simple Python Flask application as our microservice but you can simply adjust it to your needs (being the code skeleton relevant for your context and with your programming language of choice).
  • jenkins_seed, where we have our pipelines as code via Jenkins Job-DSL definitions.

One of the pipelines is the create_microservice pipeline that fulfills the promise of creating microservices with one click. What it does:

  • it creates a new repository in Gogs with the microservice's skeleton code generated by the microservice_code_generator.
  • it creates and adds the microservice's pipeline to the jenkins_seed.

The microservice's pipeline anatomy:

  • it runs the tests and builds a docker image for it.
  • it deploys it as a docker container in the host machine.

Both the create_microservice pipeline and the microservice's pipeline again are easy for you to adapt and extend to your needs.
Good examples of extending the create_microservice pipeline are adding steps to create metrics dashboards and monitoring alerts for your new microservice.
An obvious example of adapting the microservice's pipeline is changing how / where it deploys the service.

Let's get hands on next!

Prerequisites

Action

  • Clone this repository

  • Run ./run.sh (the first time it might take a couple of minutes since it needs to build the Jenkins docker image)

  • Now you got Jenkins and Gogs running (the repositories on Gogs can be explored at http://localhost:3000/explore/repos if you'd like)

  • Browse to Jenkins at http://localhost:8080

  • Run the seed job (the first time it might take a few seconds because it downloads libs and runs tests before effectively updating the jobs)
    alt text

  • Go to the all_pipelines view (or create_microservice view, but all_pipelines is especially good because you can see everything that will happen) alt text

  • Run the create_microservice pipeline (provide a service name, i.e. good_service and the port that it shall listen to, e.g. 8000) alt text

  • Wait for the pipeline to finish and for the seed job to run again

  • Voilà! You have a brand new pipeline for your microservice. Trigger it to get your microservice up and running alt text

  • After the deployment is done, you can check your microservice echo Hello World! (e.g. http://localhost:8000) alt text

  • Enjoy and maybe create some more microservices :)

Going forward

Now that you have a solution for easily creating microservices, one thing will happen: you WILL create lots of microservices. And in order to be able to maintain them, you'll need the proper mindset. A few tips:

That's it! We hope you enjoyed it!

Note there is quite some Inception here, so just remember to carry your totem with you and have fun! :)

Contributing

If you would like to help making this project better, see the CONTRIBUTING.md.

Maintainers

Send any other comments, flowers and suggestions to André Schaffer and Tommy Tynjä.

License

This project is distributed under the MIT License.