Jenkins Master‐Slave Architecture - cloudeguru/Jenkins GitHub Wiki
In a Jenkins master-slave architecture, distributing the workload across multiple agents efficiently is crucial for optimizing resource utilization and improving build performance. Here's how you can achieve load distribution using pipeline as code:
- Define Agent Labels: Assign labels to your Jenkins agents based on their capabilities, such as hardware specifications, software dependencies, or any other relevant criteria. For example, you could have labels like linux, windows, docker, java, etc.
- Configure Pipeline Jobs with Agent Directives: In your pipeline script (Jenkinsfile), use the agent directive to specify the label(s) that the pipeline should run on. By default, Jenkins will select any available agent with the matching label(s) to execute the pipeline.
pipeline {
agent { label 'linux' }
// Pipeline stages and steps...
}
- Utilize Multiple Agent Labels: To distribute the workload across different agents with varying capabilities, you can specify multiple agent labels in your pipeline script. Jenkins will select an agent with any of the specified labels that is available to execute the pipeline.
pipeline {
agent { label 'linux && docker' }
// Pipeline stages and steps...
}
- Use Dynamic Agent Selection: You can also dynamically select agents based on specific conditions or requirements within your pipeline script using the node block. This allows you to choose agents based on factors such as resource availability, workload, or specific criteria defined in your pipeline logic.
pipeline {
agent none
stages {
stage('Build on Linux') {
agent { label 'linux' }
steps {
// Build steps for Linux
}
}
stage('Build on Windows') {
agent { label 'windows' }
steps {
// Build steps for Windows
}
}
}
}
- Scale Agents Horizontally: To handle increased workload or improve parallelization, you can horizontally scale your Jenkins agents by adding more agent nodes to your Jenkins infrastructure. This allows you to distribute the workload across a larger pool of agents, reducing build queue times and increasing overall throughput.
Use Dynamic Agent Selection:
Dynamic agent selection allows you to choose Jenkins agents based on specific conditions or requirements within your pipeline script using the node block. This provides flexibility and control over which agent should execute specific stages or steps of your pipeline. Let's dive deeper with a real-time scenario example:
Scenario: Suppose you have a Jenkins pipeline that involves building and testing a Java application. You want to execute the build stages on Linux agents and the deployment stage on a Windows agent.
Pipeline Script with Dynamic Agent Selection:
pipeline {
agent none // Disable default agent selection
`stages {`
`stage('Build on Linux') {`
`steps {`
`node('linux') { // Select a Linux agent with 'linux' label`
`sh 'mvn clean install' // Build the Java application using Maven`
`}`
`}`
`}`
`stage('Test on Linux') {`
`steps {`
`node('linux') {`
`sh 'mvn test' // Run tests on the Linux agent`
`}`
`}`
`}`
`stage('Deploy on Windows') {`
`steps {`
`node('windows') { // Select a Windows agent with 'windows' label`
`bat 'deploy.bat' // Deploy the application using a Windows batch script`
`}`
`}`
`}`
`}`
}
Explanation:
In this pipeline script, we disable the default agent selection using agent none because we want to dynamically select agents based on our requirements. We define multiple stages in the pipeline, each representing a different phase of the software delivery process (build, test, deploy). For the build and test stages, we use the node('linux') block to select a Linux agent with the 'linux' label. This ensures that these stages are executed on Linux agents. Similarly, for the deployment stage, we use the node('windows') block to select a Windows agent with the 'windows' label. This ensures that the deployment step is executed on a Windows agent. Real-time Example:
Let's say you have a Jenkins setup with multiple Linux and Windows agents to accommodate different types of builds and deployments. When a developer triggers the pipeline, Jenkins dynamically selects a Linux agent for building and testing the Java application. Once the build and test stages are completed successfully, Jenkins dynamically switches to a Windows agent to deploy the application on a Windows server. This dynamic agent selection ensures that each stage of the pipeline is executed on the appropriate platform, optimizing resource utilization and improving overall pipeline efficiency.