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.
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!
- Make sure you have Git (to be able to clone this repo), Docker & Docker Compose installed.
-
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)
-
Go to the all_pipelines view (or create_microservice view, but all_pipelines is especially good because you can see everything that will happen)
-
Run the create_microservice pipeline (provide a service name, i.e. good_service and the port that it shall listen to, e.g. 8000)
-
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
-
After the deployment is done, you can check your microservice echo Hello World! (e.g. http://localhost:8000)
-
Enjoy and maybe create some more microservices :)
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:
- Grow your system with convention in mind (e.g. for builds, deployments, monitoring, etc)
- Adopt a test strategy that scales (there is one here that you can follow: https://github.com/andreschaffer/microservices-testing-examples)
- Put effort into identifying the right microservices boundaries (strategic Domain-Driven Design can take you a long way)
- Design for failure (because shit will happen)
Note there is quite some Inception here, so just remember to carry your totem with you and have fun! :)
If you would like to help making this project better, see the CONTRIBUTING.md.
Send any other comments, flowers and suggestions to André Schaffer and Tommy Tynjä.
This project is distributed under the MIT License.