Getting Started With Kubernetes-Native CI/CD Pipelines
Setting up CI/CD for Kubernetes is challenging. In this blog, the author walks through the process of creating k8s-native CI/CD pipelines using Devtron.
Join the DZone community and get the full member experience.Join For Free
Setting up the CI/CD pipelines for Kubernetes is complex. Post setup, managing the CI/CD can become quite challenging when your team and infrastructure grow quickly. An efficient and fully automated CI/CD plays an important role in ensuring you ship your features faster. By leveraging CI/CD, you can guarantee that the application teams ship features faster while ensuring best practices for the code base.
In this micro-blog, we will see how easily one can create and manage the CI/CD pipelines with Devtron, but before doing that, let's do a quick refresher on CI/CD.
What Is CI/CD?
CI stands for “Continuous integration,” which is the stage where we validate and test the changes pushed by developers. It is where you run all your test cases, i.e., unit and integration tests. After we have validated and tested the codebase, we can build the software artifacts in this step; for e.g., we can build a container image, or it can be a binary executable.
CD stands for “Continuous delivery,” which is when you push your software artifacts to the production/staging environment in an automated process. In this context, after building a container image and pushing it to the container registry, we deploy the same image in the Kubernetes cluster with the help of Devtron.
What Is Kubernetes Native CI/CD?
The Kubernetes native CI/CD leverages the software delivery workflows by being part of the Kubernetes cluster. The benefit of using Kubernetes native CI/CD is that your build step will happen inside a pod, and you don’t need to provision or connect any other compute resources externally for your CI. From a security perspective, this method is recommended as all of our secrets & external credentials are within the cluster.
Kubernetes Native CI/CD With Devtron
When you're managing your pipelines with YAML, you need to write the workflow and update it repeatedly. Understanding YAML, examining the build process, learning different Linux commands, deploying code manually, and learning Kubernetes is hard for a developer. They need something intuitive, simple and doesn’t require domain expertise. Even if you don’t know much about Kubernetes, you should be able to develop, execute & trigger your builds & deployments.
This is what we are trying to solve using Devtron. It gives you an intuitive dashboard to help you quickly set up and manage your CI/CD pipelines on Kubernetes. Let's install Devtron and set up some CI/CD pipelines for our applications.
For installing Devtron, refer to the installation documentation. After installing Devtron, we will port-forward the Devtron service to get the dashboard URL since we are installing it on a local machine.
kubectl -n devtroncd port-forward service/devtron-service 8000:80 &
After this, you should be able to access the Devtron dashboard on
localhost port 8080.
For demonstrating the CI/CD pipeline, let's use a nodejs demo application provided by Devtron on GitHub repository.
After getting this, you need to set the global configurations for your application. For setting global configuration refer to the documentation.
Step 1: Enabling GitOps
To enable GitOps, go to global configuration and then use any Git provider for authentication. In our case, we have used GitHub. It is recommended to create a new GitHub organization of all your GitOps configs.
Step 2: Creating an Application
Now let’s move ahead and create our first application on Devtron. Provide the App Name i.e
demo-application, assign it a project i.e.
devtron-demo and then click on
Step 3: Adding GitHub Repository and Build Configuration
The next step is to add the Git Repo URL of your source code from where it is being hosted. If you want to deploy an application that is in a private repo, explore the Git Accounts Docs.
Now once we have set up our repository, we need to set the build configuration. The build configuration is required to pull and push your container images. You can change the registry to use docker, ECR, GCR, etc, which can be configured from container registries under Global Configurations.
We have now integrated
Buildpacks with which you can build OCI-compliant container images directly from your code.
If you want to build a platform-specific image, then you can configure that in the advanced options in
Step 4: Configuring Base Deployment Template
Base Deployment Template is where all magic happens. Devtron displays a fined-grained configuration, abstracting out all the Kubernetes config complexities and packages the template in a helm chart. The deployment template consists of almost all major configurations that would be required for a production-grade microservice, and it comes with two display options — Basic (GUI Mode) & Advanced (YAML Configs).
You can configure
Requests & Limits for your deployment, provide
Environment Variables, etc. There’s also an advanced option from which you can get the detailed YAML configurations that Devtron will use during deployment and overwrite the same. Here’s a glimpse of how the deployment template looks.
You can configure
Horizontal Pod Autoscaling,
Volume Mountsand many more Kubernetes configurations just with a few tweaks in the predefined template. With Devtron’s deployment template, you don’t have to worry about writings tons of YAML file for your Kubernetes configurations.
Step 5: Creating and Triggering a CI/CD Pipeline
Once you have configured the deployment template, you are ready to build your CI/CD Workflow. Let us go ahead and configure continuous integration. To know more about different ways of creating pipelines, feel free to read workflow editor documentation. In this example, we will build & deploy from Devtron Dashb
Now, let's create our
CI Build Pipeline and configure the pipeline details. In CI pipeline, you can configure
pre/ post build stages, give the
source type i.e, Branch Fixed, Regex, Pull Request or Tag Creation for building images, and a lot more.
Now after the creating build pipeline, let’s create the
Deployment Pipeline. Use the
+ icon on the same page to add your deployment pipeline. With Devtron, you can create as many pipelines as you want, be it sequential pipelines or deploy to multiple environments from the same build.
Deployment Pipeline you can configure
pre/ post deployment stages, out-of-the-box integrations of
deployment strategies i.e., Rolling, Recreate, Blue-Green, and Canary. It also allows you to choose different deployment strategies for different environments in the same application. The default deployment strategy is
Rolling as you can see in the below image.
In this blog, we will proceed with
Blue-Green deployment strategy. You can check out the deployment strategy configurations and make changes depending on your use case. This configuration change is an application for all other deployment strategies.
After configuring the CI & CD pipelines, here’s what the entire pipeline looks like. We can create multiple workflows with different configs for different environments and build types in the same application.
Isn't it interesting!!
Now that pipelines are created, we need to trigger our pipelines and for that we have to go to the
Build & Deploy section.
To build an image, click on
Select Material ->
Select the Commit for which you want to build -> Click on
Once the build is initiated, we can check the logs of the build process and when the build is successful then we can verify the image built in the artifacts section. It also allows you to check the previous builds, and all necessary audit info required in the dashboard as you can see in the image below.
Let’s trigger the deployment pipeline. After the build pipeline is successful, there will be an option to
select image in
Build & Deploy page. Select the image and then click on Deploy. We can also check the commit and security details against which deployment is being initiated. Once deployment is started, Devtron gives you details of all stages along with K8s Configurations against each deployment. In CD pipelines as well, we get all the audit info regarding the deployment as you can see in the image below.
Step 6: App Details
Once your application is deployed, you can view detailed information about your application in the
App Details. All the health-related information about your workloads and application metrics can be easily monitored from
App Details tab.
After the application is successfully deployed, it gives you a 360-degree view of all info regarding the application and allows you to perform different pod operations such as
exec into terminal,
events and also comes with
Logs Analyzer that allows grep on multi-container logs. It also shows all the Kubernetes resources associated with particular applications in a grouped manner that comes very handy for debugging and observability.
In this blog, we have learned about Kubernetes CI/CD and how easily it can be set up using Devtron in just 3 minutes with all the best practices and GitOps. One of the main benefits of a quick Kubernetes CI/CD setup is that it can speed up the whole release process by automating your build and continuously testing the codebase. Developers don’t need to build and test manually. A fast, reliable, and efficient Kubernetes CI/CD will lead to better products and happier users.
Published at DZone with permission of Abhinav Dubey. See the original article here.
Opinions expressed by DZone contributors are their own.