Trunk-Based Development with Kubernetes featured image

Team collaboration is critical for ensuring that developers work together to develop and deliver a product on time. Developer teams approach collaboration through version control in two main ways: developing a feature branch and trunk-based development.

The first pattern is creating a development feature branch from the main branch (also known as the trunk) where each developer or a group of developers works in isolation. When work on the feature branch finishes, developers merge it into the main branch. While this pattern is good for agile development, it creates bottlenecks—like complex merging events—and can take several days or even months before integrating into the main branch. It also follows a monolithic approach, which is less scalable and slows down continuous integration and continuous delivery (CI/CD) within the team. 

The second pattern is trunk-based development. In this approach, each developer divides their tasks into smaller batches and collaborates from a separate branch, the feature, into a single branch, called the trunk. These branches/features frequently merge into the trunk, accelerating software release.

Developers work on different features independently from other branches. Working on different branches makes it possible to implement features in any order and merge them into the trunk without affecting the application’s base functionality. Trunk-based development also allows for the implementation of microservice architectures.  

This article will explore how you can power your trunk-based development strategies for microservices with Kubernetes.

Trunk-based development with Kubernetes

Kubernetes increases infrastructure use by efficiently sharing computing resources, making it a popular choice for container orchestration. However, using a Kubernetes microservice architecture can be challenging because each service is isolated and often developed by different team members. 

Therefore, team collaboration and communication are paramount when developing with Kubernetes. You should aim to have each development team and developer rolling back buggy changes and updating the build process to reduce bugs as fast as possible. You can implement trunk-based development to achieve these objectives quickly, consistently, and without team siloing.

To use trunk-based development with Kubernetes, you can implement the following steps:

  • After provisioning and configuring a Kubernetes cluster, you create a new feature branch from the source code main repository. 
  • You should perform adequate testing before merging the changes to the trunk to reduce the introduction of bugs and frequent rollbacks. Each developer on your team is then allocated a temporary isolated test environment in the cluster to test each commit in the feature branch. 
  • Then, you merge the feature branch into the trunk, which automatically deletes the feature branch and cleans it to restore any allocated resources to the Kubernetes cluster. 

The role of trunk-based development in the CI/CD pipeline

The CI/CD pipeline involves integration, testing, delivery, and deployment. Trunk-based development significantly accelerates CI/CD processes in DevOps teams by facilitating and streamlining the merging and integration phases.  

With trunk-based development, you can create small feature branches, build features, and perform swift automated test suites and frequent updates. Changes can be easily reverted and fixed in case of any bugs or issues and code breaks, as the branch structure makes the pushed codebase small. 

Together, trunk-based development, CI/CD, and Kubernetes can expedite software release cadence and allow you to iterate quickly on ideas. Trunk-based development benefits your CI/CD pipeline through the following:

  • You can build and deploy your services independently and implement new features frequently and faster.  
  • Once you switch from the feature branch to the trunk, the new code is ready to be asynchronously deployed to the production environment. This is faster than having separate phases, each of which a quality assurance manager must review. 
  • The quality process at each CI/CD pipeline stage improves the software quality. Improvement means there’s less code breakage, fewer bugs, and no inconsistency within the base code. 
  • With the ability for cross-collaboration among development teams, changes or code versions can live side by side. The non-monolithic structure keeps the code intact without affecting the previously made changes.  
  • There are a few complex merge conflicts as each developer works on their branch. Working on separate branches encourages team ownership of issues with the code, as it’s easy to identify who and what is responsible for it. 

DevOps leads should decide on the container orchestration service to use to achieve this deployment strategy. Cloud vendors often include these services and packaging tools like Helm charts. The orchestration services streamline the management and deployment of Kubernetes microservice architectures. 

Best practices for using trunk-based development

Trunk-based development ensures that DevOps teams achieve release cadence in their Kubernetes environments and CI/CD pipelines. The following is a list of best practices to help you use this branching strategy best. 

Develop in small batches

You should create small batches of tasks to complete when developing a feature. Small groups of tasks make it easier to complete an assigned batch in a short period, and keeping code to a minimum reduces merging and rollback complexities. Developing small tasks also makes it much easier for code reviewers to review the code, as it increases accuracy and prevents confusion resulting from large code bases. 

Use feature flags

Feature flags complement trunk-based development, enabling you to wrap a new feature in an inactive code path and activate it later. You can triage the feature they want to commit first without creating a new feature branch in the same cluster, making room for experimentation and management of new features. 

Additionally, feature flags enable you to control who has access to your features, allowing you to deliver them to new users gradually. 

Execute comprehensive automated testing

You should test each phase of the CI/CD pipeline to avoid deploying code that is not functioning. There are several types of automated testing, including unit testing and integration testing, that you can perform after each change and when integrating a feature into the main branch. 

Performing these automated tests helps you review the code, approving or rejecting it depending on whether it passes or fails the tests. Automated testing enables you to catch any issues that might cause problems later in the pipeline.

You should also perform an end-to-end test later in the production channel to ensure everything works as expected. 

Conduct code reviews

Don’t postpone or reserve code reviews for later. The technical reviewer should check if the automated tests have passed as they act as the first layer of code review. Reviewers should focus on other areas, such as test coverage and code optimization. Focusing on test coverage and code optimization ensures that the deployed code has attained the standards required for merger into the source code.  

Frequent merging with the trunk

When performing trunk-based development, you should merge with the trunk at least once daily. Regular merges keep the flow and rhythm of code tracking. Team leads can then version or tag the main branch when the day ends as a release commit. 

Next steps

Collaboration has become standard in the development lifecycle. A trunk-based development branching strategy is one of the ways you can increase operational efficiency (delivery speed and availability) while maintaining collaboration across development teams. 

Kubernetes microservice architecture offers an excellent way for you to achieve complete CI/CD through trunk-based development. You can split your monolithic applications into microservice clusters, which speed the development process. Additionally, trunk-based development complements the deployment strategy by automating various container environments and ensuring efficient resource allocation. The features created through a trunk-based development strategy are independent of the source code, and the consistency in code makes collaborations easier.

You can use many best practices to aid our trunk-based development, including feature flags. But implementing these practices can be challenging to do alone. LaunchDarkly is a leader in the feature management space, helping you improve your productivity and innovation by giving you peace of mind about managing your software cycles. To build faster, deploy valiantly and release like a pro, check out LaunchDarkly or request a demo.

Related Content

More about Best Practices

November 22, 2022