How CI/CD pipeline works with Kubernetes?
Kubernetes is a portable, extensible open source platform for managing containerized workloads and services, that provides both configuration and automation. It was originally designed by Google and now maintained by the Cloud Native Computing Foundation(CNCF), written in Go language.
Docker and Kubernetes concept
Docker is a container hosting and container runtime platform while Kubernetes is a container orchestration platform. In other terms, containers run on Docker while they can be managed by Kubernetes (However, Docker have their own native clustering tool, known as Docker Swarm but Kubernetes is more extensive than that of Docker tool).
CI/CD with Kubernetes deploys code faster
Continuous integration and deployment (CI/CD) is a development strategy that gets application updates to the customers in a fast, automated way. By using Kubernetes and Kubernetes Engine, we can solve the following problems.
- Long release cycles: Manual testing and deployment processes can cause delays in getting our code to production. This makes code-merge collisions more likely, and increases the time customers have to wait for patches and updates.
- Outages: When we manage our infrastructure manually, someone has to carry a pager. Whether it’s lightning hit on a data center or a traffic spike that exceeds capacity, someone on the team is getting woken up at 3am. While your app is down, we are losing money as well as customers.
- Inefficient server utilization: If we are not managing our apps to ensure they’re packed efficiently onto servers, we are probably paying too much for capacity, whether it’s in the cloud or on-premise.
Here are some of the solutions that Kubernetes offers, to reduce these common problems:
- Ability to Containerize the Code: With the platform, we can run our apps in containers. This ensures they have the resources and libraries necessary, while also preventing common issues that arise between library versions and application components. Containerizing the code makes the app portable between environments, while also making them easy to replicate and scale.
- Orchestrate Deployment with the Platform: Kubernetes makes the deployment process easier in a number of ways. Running apps on containers doesn’t solve every problem in the CI/CD pipeline as you still need to manage these apps. The platform can do everything from deploying them to monitoring their health and scaling them to meet customer demand.
Above solutions help programmers, to reduce the amount of time and effort developing and deploying their apps throughout CI/CD pipeline.
Below are the components that make up a Continuous Delivery (CD) pipeline:
- Version Control System (VCS): A source code repository where changes and updates are pushed.
- CI system: An integration and test system that may also build the Docker image.
- Docker Registry: The image registry that stores your Docker images
- Kubernetes Cluster: Set up with a few clicks in GKE or using one of the available installers.
To coordinate and automate these pieces, increase velocity and make Kubernetes deployments seamless.
We will need a failure or rollback strategy in case the microservice deployments or canary verifications fail. We can easily do this with Kubernetes, which can keep a few old deployment controllers active with zero pods for each environment. Then, we can automatically resize the controllers back again when you need them.
We can also roll back environment variables and services configurations with the controller rollback. This makes easy to keep older service versions passive or active in clusters, which you can resize instantly for rollback. The platform also has rollout history and undo functions to rollback to a previous deployment manually.
Kubernetes has plenty of benefits in the CI/CD pipeline that can save our time and money. The platform can reduce manual input and automate the deployment process. It can easily reduce the time between release cycles, solve outages instantly and patch outages without any manual inputs.
Additionally, the platform has an intuitive user interface that anyone can use without a hassle to improve the speed and return of their deployment pipeline.
Kubernetes is compatible with the majority of CI/CD tools which allows developers run tests, deploy builds in Kubernetes and update applications with no downtime. One of the most popular CI/CD tools is Jenkins.
One of the best CI/CD tool: Jenkins
Jenkins is very easy to configure, modify and extend. It deploys code instantly, generate test reports. We can configure it according to the requirements for CI and CD.
It is available for all platforms and different OS (OS X, Windows or Linux). The extensive pool of plugins makes Jenkins flexible and allows building, deploying and automating across various platforms. There is no shortage of support from large online communities of agile teams as it is open source. Finally, most of the integration work is automated (Hence fewer integration issues). This saves both time and money over the lifespan of a project.
Jenkins: Build & Test Automation
In recent years, Jenkins has focused on handling the end-to-end development pipeline including the CD phases beyond the CI.
Running Jenkins on Kubernetes brings many benefits. To start, Jenkins can take advantage of the scalability and high availability of Kubernetes. With the numerous worker nodes in Jenkins and handling infrastructure to run Jenkins can become a nightmare. Kubernetes makes this easier with its automatic pod management features.
Further, Kubernetes enables zero-downtime updates with Jenkins. This is made possible by the rolling updates feature of Kubernetes where it gradually phases out pods with an older version of the application and replaces them with new ones. In this way, Kubernetes brings the ability to do canary releases and blue-green deployments to Jenkins.
Spinnaker: Multi-Cloud Deployment
Jenkins is perfect for the build stages of the pipeline but more complex problem to solve is deployments, especially when it involves multiple cloud platforms and mature deployment practices. Kubernetes has a deployment API and has a support for rollout, rollback, and other core deployment functionality. However, another open source tool, Spinnaker (created by Netflix), has been in the spotlight for its advanced deployment controls.
Spinnaker focuses on the last mile of the delivery pipeline i.e. deployment in the cloud. It automate deployment processes and cloud resources and acts as a bridge between the source code, Github, and the deployment target like a cloud platform. It supports multiple cloud platforms, and enables a multi-cloud approach to infrastructure, which is the best part of Spinnaker. It is one of the original promises of Kubernetes that is being fulfilled by Spinnaker.
Helm: Package Management
It is a package manager for Kubernetes which makes installation of applications easier in Kubernetes. It is essential to be able to quickly package, share and install application code and its dependencies along with the automation for successful CI/CD pipeline. Helm has a collection of ‘charts’ and with each chart being a package that we can install in Kubernetes. Helm places an agent called Tiller within the Kubernetes cluster which interacts with the Kubernetes API and handles installing and managing of packages.
It brings predictability and repeatability to the CI/CD pipeline, which is the biggest advantage of Helm. It lets you define and add extensive configurations and metadata for every deployment. Further, it gives us complete control over rollbacks and brings deep visibility into every stage of a deployment.
The diagram shows the CI/CD pipeline and the various tools involved. The application used as an example is a simple web service written in Java using Spring Boot framework with Maven being used as a build tool.
The various stages in the pipeline are shown in the figure below:
- Code changes are committed to version control system – GitHub
- Each commit to GitHub automatically triggers Jenkins build. Jenkins uses Maven to compile the code, run unit test and perform additional checks – code coverage, code quality etc.
- Once the code has been successfully compiled and all the tests have been passed. Jenkins builds a new docker image and push it to the docker registry.
- Jenkins notifies Kubernetes of the new image available for deployment.
- Kubernetes pulls the new docker image from the docker registry.
- Kubernetes deploys and manages the docker instance/container.