Hands-On CI/CD for Microservices With Jenkins X
DevOps and microservices come together in this tutorial on using Jenkins X to create a distributed, decoupled CI/CD system.
Join the DZone community and get the full member experience.
Join For FreeKubernetes 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
To create a Kubernetes cluster with Jenkins X, there are a few alternatives. You can use Minikube, AWS, Azure, Oracle, OpenShift or Google Cloud, but for simplicity, GCP will be chosen for this example.
Prerequisites
- Install jx tool
- Create Google Cloud Account
- Create a project on Google Cloud
- Install Git, gcloud and kubectl
Getting Started
Login into your Google Cloud Account:
gcloud auth login
Create a cluster:
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 AUTHINFO NAMESPACE
* gke_my-jenkinsx-demo_europe-west2-a_jenkinsx-demo gke_my-jenkinsx-demo_europe-west2-a_jenkinsx-demo 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!
Deploying Microservices
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 -d argument 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
The -l argument allows us to filter by language.
*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 (jx-staging).
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 PRODUCTION PODS URL
golang-http 0.0.1 1/1 http://golang-http.jx-staging.your-google-cloud-ip.nip.io
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
*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 PRODUCTION PODS URL
golang-http 0.0.1 1/1 http://golang-http.jx-staging.your-google-cloud-ip.nip.io 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!
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.
Troubleshooting
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
Published at DZone with permission of Sergio Martin. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments