Hands-On CI/CD for Microservices with Jenkins X
Kubernetes is growing in popularity and many companies use it to orchestrate their containerized microservices. Moreover, microservices are increasing the release cycle, because you do not have to build and deploy a huge monolith application every time a small change is made, therefore DevOps teams should be able to deploy multiple times per day. However, the continuous integration and delivery process is becoming a bottleneck, because at the end of the development process, code changes will trigger a pipeline in a CI tool, and this tool is usually shared by all your microservices. So, what can we do? The answer is Jenkins X:
Jenkins X allows you to create a distributed and decoupled CI/CD system.
Setting Up a Cluster
There are a few alternatives to create a Kubernetes cluster with Jenkins X. You can use Minikube, AWS, Azure, Oracle, OpenShift or Google Cloud, but for simplicity, GCP will be our choice in this example.
- Install jx tool
- Create Google Cloud Account
- Create a project on Google Cloud
- Install Git, gcloud and kubectl
Login into your Google Cloud Account:
gcloud auth login
jx create cluster gke --skip-login --default-admin-password=mypassword -n jenkinsx-demo
--default-admin-password sets the password to access Jenkins with the admin user;
-n sets the Kubernetes cluster name.
Follow the prompts on the console: select your Google Cloud project (
my-jenkinsx-demo), Google Cloud zone (in this case,
europe-west2-a), machine type (
n1-standard-2), minimum number of nodes (3 by default), maximum number of nodes (3), install ingress controller, set domain (choose default), set GitHub username, and recreate Jenkins X cloud environment.
After the process is done, two new repositories will be created on your GitHub account, and the Kubernetes context will be switched to the Jenkins X one.
kubectl config get-contexts CURRENT NAME CLUSTER * gke_my-jenkinsx-demo_europe-west2-a_jenkinsx-demo gke_my-jenkinsx-demo_europe-west2-a_jenkinsx-demo AUTHINFO NAMESPACE gke_my-jenkinsx-demo_europe-west2-a_jenkinsx-demo jx
Now you are ready to create your microservices and deploy them on your new Kubernetes cluster!
There are three alternatives to create your microservice and deploy it:
- Create your microservice manually and then import it using jx import.
- Use the jx create Spring command which provides an easy way to create a microservice with Spring Boot from your terminal. Dependencies can be specified by including the
-dargument followed by the dependency you want to add, e.g.
jx create spring -d web -d actuator.
- Use jx create quickstart which allows you to select one of the templates provided by Jenkins X.
What happens when you use jx tool?
- Creates an application in a directory.
- Initializes the repository and pushes code to GitHub.
- Adds Dockerfile (to create container) and Jenkinsfile (to create pipeline).
- Creates a Helm chart to deploy on Kubernetes
- Registers webhook on your git repository for your teams jenkins
- Triggers the pipeline for the first time
For this example, Golang will be used with the quickstart tool provided by Jenkins X.
jx create quickstart -l Go
-l argument allows us to filter by language.
Note: The only available template for Golang at the moment is golang-http.
After following all the steps and the pipeline runs, the Go application will be deployed on the staging namespace (
Kubernetes specifications include an ingress for the microservice, so you will be able to hit it from outside the cluster.
The URL to hit the service can be found by running the following command:
jx get app APPLICATION STAGING PODS URL golang-http 0.0.1 1/1 http://golang-http.jx-staging.your-google-cloud-ip.nip.io PRODUCTION PODS URL
The application was only deployed on the staging environment and now it is time to promote it to production.
jx promote golang-http --version 0.0.1 --env production
Note: Make sure that the version match with the one in staging, otherwise the promotion pipeline will fail.
The previous command will open a pull request on the production repository, and only when the changes are merged, the production pipeline on Jenkins will be triggered. If everything goes well, the application should be deployed on your
jx-production namespace, and the production URL will be available.
APPLICATION STAGING PODS URL golang-http 0.0.1 1/1 http://golang-http.jx-staging.your-google-cloud-ip.nip.io PRODUCTION PODS URL 0.0.1 1/1 http://golang-http.jx-production.your-google-cloud-ip.nip.io
Congratulations! You have just deployed your first application on Kubernetes with Jenkins X!
Depending on your jx version you might come across a Helm error like this:
'Error: UPGRADE FAILED: incompatible versions client[v2.10.0-rc.1] server[v2.9.1]': exit status 1
This happens because Helm is not back compatible and Jenkins X might have installed an old version. This can be solved by installing the latest Helm version on your local machine.
helm init --upgrade
As you can see, Jenkins X will take care of your microservices deployment pipeline and will save you the time of creating pipelines for each microservice.