1. Overview
1.概述
In this tutorial, we’ll go over the concept of distributed builds in Jenkins Architecture. Furthermore, we’ll learn how we can configure Jenkins master-slave architecture. Additionally, we’ll build a pipeline on Jenkins master to run build jobs on the slave nodes.
在本教程中,我们将了解Jenkins架构中分布式构建的概念。此外,我们将学习如何配置Jenkins主从架构。此外,我们将在Jenkins主节点上建立一个管道,以便在从属节点上运行构建作业。
2. Distributed Builds
2.分布式构建
Ideally, the machine where we install standard Jenkins will be our Jenkins master. On the slave node machine, we will install a runtime program called Agent. Installing Agent will not be a standard Jenkins install, but this Agent will run on the JVM. It is capable enough to run the subtask or main task of Jenkins in a dedicated executor:
理想情况下,我们安装标准Jenkins的机器将是我们的Jenkins主站。在从属节点的机器上,我们将安装一个叫做Agent的运行时程序。安装Agent将不是一个标准的Jenkins安装,但这个Agent将在JVM上运行。它有足够的能力在一个专门的执行器中运行Jenkins的子任务或主任务。
We can have any number of Agent nodes or slave nodes. Further, we can configure the master node to decide which task or job should run on which agent and how many executor agents we can have. The communication between master and Jenkins slave nodes is bi-directional and takes place over TCP/IP.
我们可以有任何数量的代理节点或从属节点。此外,我们可以配置主节点来决定哪个任务或工作应该在哪个代理上运行,以及我们可以有多少个执行者代理。主节点和Jenkins从属节点之间的通信是双向的,通过TCP/IP进行。
3. Configuring Jenkins Master and Slave Nodes
3.配置Jenkins主节点和从节点
The standard Jenkins installation includes Jenkins master, and in this setup, the master will be managing all our build system’s tasks. If we’re working on a number of projects, we can run numerous jobs on each one. Some projects require the use of specific nodes, which necessitates the use of slave nodes.
标准的Jenkins安装包括Jenkins master,在这个设置中,master将管理我们构建系统的所有任务。如果我们正在处理许多项目,我们可以在每个项目上运行许多作业。有些项目需要使用特定的节点,这就需要使用从属节点。
The Jenkins master is in charge of scheduling jobs, assigning slave nodes, and sending builds to slave nodes for execution. It will also keep track of the slave node state (offline or online), retrieve build results from slave nodes, and display them on the terminal output. In most installations, multiple slave nodes will be assigned to the task of building jobs.
Jenkins主站负责调度作业,分配从属节点,并将构建发送到从属节点执行。它还将跟踪从属节点的状态(离线或在线),从从属节点检索构建结果,并在终端输出上显示它们。在大多数安装中,多个从属节点将被分配到构建作业的任务。
Before we get started, let’s double-check that we have all of the prerequisites in place for adding a slave node:
在我们开始之前,让我们仔细检查一下,我们是否有添加从属节点的所有先决条件。
- Jenkins Server is up and running and ready to use
- Another server for a slave node configuration
- The Jenkins server and the slave server are both connected to the same network
To configure the Master server, we’ll log in to the Jenkins server and follow the steps below.
为了配置主服务器,我们将登录到Jenkins服务器,并按照以下步骤进行配置。
First, we’ll go to “Manage Jenkins -> Manage Nodes -> New Node” to create a new node:
首先,我们到“管理Jenkins -> 管理节点 -> 新节点 “创建一个新节点:
On the next screen, we enter the “Node Name” (slaveNode1), select “Permanent Agent”, then click “OK”:
在下一个屏幕,我们输入 “节点名称”(slaveNode1),选择 “永久代理”,然后点击 “确定”。
After clicking “OK”, we’ll be taken to a screen with a new form where we need to fill out the slave node’s information. We’re considering the slave node to be running on Linux operating systems, hence the launch method is set to “Launch agents via ssh”.
点击 “确定 “后,我们会被带到一个有新表格的屏幕上,我们需要填写从属节点的信息。W我们认为从属节点是在Linux操作系统上运行的,因此启动方式被设置为 “通过ssh启动代理”。
In the same way, we’ll add relevant details, such as the name, description, and a number of executors.
以同样的方式,我们将添加相关细节,如名称、描述和执行者的数量。
We’ll save our work by pressing the “Save” button. The “Labels” with the name “slaveNode1” will help us to set up jobs on this slave node:
我们将通过按 “保存 “按钮保存我们的工作。名称为 “slaveNode1 “的 “标签 “将帮助我们在这个从属节点上设置作业。
4. Building the Project on Slave Nodes
4.在从属节点上构建项目
Now that our master and slave nodes are ready, we’ll discuss the steps for building the project on the slave node.
现在我们的主节点和从节点已经准备好了,我们将讨论在从节点上构建项目的步骤。
For this, we start by clicking “New Item” in the top left corner of the dashboard.
为此,我们首先点击仪表板左上角的 “新项目”。
Next, we need to enter the name of our project in the “Enter an item name” field and select the “Pipeline project”, and then click the “OK” button.
接下来,我们需要在 “输入项目名称 “栏中输入我们的项目名称,并选择 “管道项目”,然后点击 “确定 “按钮。
On the next screen, we’ll enter a “Description” (optional) and navigate to the “Pipeline” section. Make sure the “Definition” field has the Pipeline script option selected.
在下一个屏幕上,我们将输入一个 “描述”(可选)并导航到 “管道 “部分。确保 “定义 “字段选择了管道脚本选项。
After this, we copy and paste the following declarative Pipeline script into a “script” field:
之后,我们复制并粘贴以下声明性的Pipeline脚本到一个 “脚本 “字段。
node('slaveNode1'){
stage('Build') {
sh '''echo build steps'''
}
stage('Test') {
sh '''echo test steps'''
}
}
Next, we click on the “Save” button. This will redirect to the Pipeline view page.
接下来,我们点击 “保存 “按钮。这将重定向到管道视图页面。
On the left pane, we click the “Build Now” button to execute our Pipeline. After Pipeline execution is completed, we’ll see the Pipeline view:
在左侧窗格中,我们点击 “立即构建 “按钮来执行我们的管道。管道执行完成后,我们会看到管道视图。
We can verify the history of the executed build under the Build History by clicking the build number. As shown above, when we click on the build number and select “Console Output”, we can see that the pipeline ran on our slaveNode1 machine.
我们可以在 “构建历史 “下通过点击构建号来验证已执行的构建历史。如上所示,当我们点击构建号并选择 “控制台输出 “时,我们可以看到管道在我们的slaveNode1机器上运行。
5. Conclusion
5.总结
In this tutorial, we’ve covered what a Distributed Build is in Jenkins, and we saw how the Jenkins master node invokes the slave nodes. Additionally, we learned how to set up a Jenkins master-slave configuration. Finally, we’ve put the slave node configuration to the test.
在本教程中,我们已经介绍了什么是Jenkins中的分布式构建,我们看到了Jenkins主节点是如何调用从节点的。此外,我们还学习了如何设置Jenkins的主从配置。最后,我们对从属节点的配置进行了测试。



