1. Overview
1.概述
In this tutorial, we’ll go over the fundamentals of Jenkins Architecture. Furthermore, we’ll learn how we can configure Jenkins to improve the performance. Additionally, we’ll discuss the options to restart or shut down Jenkins manually.
在本教程中,我们将介绍Jenkins架构的基本原理。此外,我们将学习如何配置Jenkins以提高性能。此外,我们将讨论手动重启或关闭Jenkins的选项。
2. Jenkins Architecture
2.Jenkins架构
Certain needs could not be met with one Jenkins server. Firstly, we may need multiple distinct environments in which to test our builds. A single Jenkins server will not be able to do this. Secondly, if larger and heavier projects are being produced regularly, a single Jenkins server will be overwhelmed.
某些需求不能用一台Jenkins服务器来满足。首先,我们可能需要多个不同的环境来测试我们的构建。一台Jenkins服务器将无法做到这一点。其次,如果经常产生较大和较重的项目,一个Jenkins服务器就会不堪重负。
Jenkins distributed architecture was created to meet the above requirements. Furthermore, Jenkins manages distributed builds using a Master-Slave architecture. The TCP/IP protocol is used to communicate between the Master and the Slave in this design.
Jenkins的分布式架构是为了满足上述要求而创建的。此外,Jenkins使用主-从架构来管理分布式构建。TCP/IP协议在这种设计中被用来在主站和从站之间进行通信。
2.1. Jenkins Master
2.1 詹金斯大师
The Jenkins master is in charge of scheduling the jobs, assigning slaves, and sending builds to slaves to execute the jobs. It’ll also keep track of the slave state (offline or online) and retrieve the build result responses from slaves and display them on the console output.
Jenkins主站负责调度作业,分配从站,并向从站发送构建,执行作业。它还会跟踪从属状态(离线或在线),从从属中获取构建结果响应,并在控制台输出中显示。
2.2. Jenkins Slave
2.2.Jenkins Slave
It runs on the remote server. The Jenkins server follows the requests of the Jenkins master and is compatible with all operating systems. Building jobs dispatched by the master are executed by the slave. Also, the project can be configured to choose a specific slave machine.
它在远程服务器上运行。Jenkins服务器遵循Jenkins主站的请求,与所有操作系统兼容。由主站调度的构建作业由从站执行。另外,项目可以被配置为选择一个特定的从机。
2.3. Distributed Master-Slave Architecture
2.3.分布式主-从结构
Let’s have a look at the Jenkins architecture using an example. One master and three Jenkins slaves are depicted in the diagram below:
让我们通过一个例子来看看Jenkins的架构。下图中描述了一个主站和三个Jenkins从站。
Let’s look at how we may utilize Jenkins for testing in various systems, such as Ubuntu, Windows, or Mac:
让我们看看我们如何利用Jenkins在不同的系统中进行测试,如Ubuntu、Windows或Mac。
In the diagram, the following items are taken care of:
在图中,以下项目得到了照顾。
- Jenkins will check the GIT repository for any changes in the source code at regular intervals
- Each Jenkins build requires its own testing environment, which can’t be created on a single server. Jenkins accomplishes this by employing various slaves as needed
- Jenkins Master will communicate the request for testing to these slaves, as well as test reports
3. Jenkins CLI
3.Jenkins CLI
Jenkins has a command-line interface that users and administrators can use to access Jenkins from a scripting or shell environment. SSH, the Jenkins CLI client, or a JAR file included with Jenkins can use this command-line interface.
Jenkins有一个命令行界面,用户和管理员可以用它来从脚本或shell环境访问Jenkins。SSH、Jenkins CLI客户端或Jenkins包含的JAR文件可以使用这个命令行界面。
To do so, we must first download jenkins-cli.jar from a Jenkins controller at the URL /jnlpJars/jenkins-cli.jar, in effect JENKINS_URL/jnlpJars/jenkins-cli.jar, and then run it as follows:
为此,我们必须首先从Jenkins控制器中下载jenkins-cli.jar,网址是/jnlpJars/jenkins-cli.jar,实际上是JENKINS_URL/jnlpJars/jenkins-cli.jar,,然后按以下方法运行。
java -jar jenkins-cli.jar -s http://localhost:8080/ -webSocket help
This option is available by selecting “Jenkins CLI” from the Tools and Actions section of the Manage Jenkins page:
从管理Jenkins页面的工具和行动部分选择 “Jenkins CLI”,就可以获得这个选项。
A list of available commands is displayed in this area. We can use the command-line tool to access a variety of functions using these commands.
在这个区域显示了一个可用的命令列表。我们可以使用命令行工具,利用这些命令访问各种功能。
4. Restart Jenkins Manually
4.手动重启Jenkins
If we wish to manually restart or shut down Jenkins, just follow the below-mentioned steps:
如果我们想手动重启或关闭Jenkins,只需按照下面提到的步骤即可。
4.1. Restart
4.1.重新启动
We can execute the restart using the Jenkins Rest API. This will force the procedure to restart without waiting for existing jobs to finish:
我们可以使用Jenkins Rest API执行重启。这将强制程序重启,而不需要等待现有作业的完成。
http://(jenkins_url)/restart
We can execute the safeRestart using the Jenkins Rest API. This allows us to finish any existing tasks:
我们可以使用Jenkins Rest API执行safeRestart。这使我们能够完成任何现有的任务。
http://(jenkins_url)/safeRestart
If we install it as an rpm or deb package, the command below will work:
如果我们把它安装成rpm或deb包,下面的命令就会起作用。
service jenkins restart
4.2. Ubuntu
4.2.Ubuntu
We can also use apt-get/dpkg to install the following:
我们还可以使用apt-get/dpkg来安装以下内容。
sudo /etc/init.d/jenkins restart
Usage: /etc/init.d/jenkins {start|stop|status|restart|force-reload}
4.3. For Safely Shutting Down Jenkins
4.3.为了安全地关闭Jenkins
If we wish to safely shut down Jenkins, we can execute the exit using the Jenkins Rest API:
如果我们希望安全地关闭Jenkins,我们可以使用Jenkins Rest API执行退出。
http://(jenkins_url)/exit
We can execute the kill using the Jenkins Rest API to terminate all of our processes:
我们可以使用Jenkins Rest API执行kill,以终止我们所有的进程。
http://(jenkins_url)/kill
5. Boosting Jenkins Performance
5.提升詹金斯的性能
Lagging or slow responsiveness is a typical complaint among Jenkins users, and there are many reported faults. Slow CI systems are inconvenient since they slow down development and waste time. Using a few simple recommendations, we can increase the performance of those systems.
滞后或缓慢的响应性是Jenkins用户的典型抱怨,而且有许多报告的故障。慢的CI系统是不方便的,因为它们拖慢了开发速度,浪费了时间。使用一些简单的建议,我们可以提高这些系统的性能。
In the following sections, we’ll discuss few suggestions for improving Jenkins and putting a smile on our engineers’ faces.
在下面的章节中,我们将讨论一些改进Jenkins的建议,让我们的工程师脸上露出笑容。
5.1. Minimize Builds on the Master Node
5.1.尽量减少主节点上的构建
The master node is where the application is actually executing; it’s Jenkins’ brain, and unlike a slave, it can’t be replaced. So we want to keep our Jenkins master as “work-free” as possible, using the CPU and RAM for scheduling and triggering builds on slaves. We can achieve this by restricting our jobs to a node label, such as SlaveNode.
主节点是应用程序实际执行的地方;它是Jenkins的大脑,而且与从属节点不同,它不能被替换。因此,我们想让我们的Jenkins主节点尽可能保持 “无工作”,使用CPU和RAM来调度和触发从节点的构建。我们可以通过将我们的作业限制在一个节点标签上来实现,比如SlaveNode。
When allocating the node for pipeline jobs, use the label, for example:
在为管道作业分配节点时,使用标签,例如:。
stage("stage 1"){
node("SlaveNode"){
sh "echo \"Hello ${params.NAME}\" "
}
}
The task and node block will only run on slaves having the SlaveNode label in those cases.
在这些情况下,任务和节点块将只在具有SlaveNode标签的从机上运行。
5.2. Don’t Keep Too Much Build History
5.2.不要保留太多的构建历史
While configuring a job, we can specify how many of its builds to be kept on the filesystem and for how long. When we trigger many builds of a job in a quick time, this feature, known as Discard Old Builds, becomes useful.
在配置一个作业时,我们可以指定在文件系统中保留多少个构建,以及保留多长时间。当我们在短时间内触发一个作业的许多构建时,这个被称为 “丢弃旧构建 “的功能就变得非常有用。
We have seen examples where the history limit was set too high, resulting in excessive builds being saved. Also, Jenkins had to load a lot of old builds in these circumstances.
我们看到过这样的例子:历史限制设置得太高,导致过多的构建被保存。而且,在这种情况下,Jenkins不得不加载大量的旧构建。
5.3. Clear Old Jenkins Data
5.3.清除旧的詹金斯数据
Continuing on from the previous suggestion for build data, another key element to know is the old data management functionality. Jenkins, as we may know, manages jobs and stores data on the filesystem. The data format may change when we undertake actions such as upgrading our core, installing, or updating a plugin.
继续前面关于构建数据的建议,另一个需要了解的关键因素是旧的数据管理功能。正如我们可能知道的那样,Jenkins管理作业并在文件系统上存储数据。当我们进行升级我们的核心、安装或更新一个插件等行动时,数据格式可能会改变。
Jenkins saves the old data format to the file system and loads the new format into memory in this situation. It’s quite beneficial if we need to roll back an upgrade, but there are times when too much data is loaded into RAM. Slow UI responsiveness and even OutOfMemory problems are signs of high memory use. It is recommended to open the previous data management page to avoid such situations:
在这种情况下,Jenkins将旧的数据格式保存到文件系统,并将新的格式加载到内存中。如果我们需要回滚升级,这是相当有益的,但有时会有太多的数据被加载到内存中。UI响应速度慢,甚至OutOfMemory问题都是内存使用量大的表现。建议打开以前的数据管理页面以避免这种情况。
5.4. Define the Right Heap Size
5.4.定义正确的堆大小
The maximum heap size option is used by many current Java applications. There is an essential JVM aspect to be aware of while defining the heap size. UseCompressedOops is the name of this feature, and it only works on 64-bit platforms, which most of us use. It reduces the object’s pointer from 64 to 32 bits, saving a significant amount of memory.
目前许多Java应用程序都使用了最大堆大小的选项。在定义堆的大小时,有一个重要的JVM方面需要注意。UseCompressedOops是这个功能的名称,它只在64位平台上工作,而我们大多数人都使用这种平台。它将对象的指针从64位减少到32位,节省了大量的内存。
This flag is enabled by default on heaps up to 32GB (a little less) in size, and it stops working on heaps larger than that. The heap should be expanded to 48GB to compensate for the lost capacity. As a result, while defining heap size, it’s advisable to keep it under 32GB.
这个标志在默认情况下对大小不超过32GB(稍小)的堆启用,对大于这个大小的堆停止工作。堆应该被扩展到48GB,以补偿损失的容量。因此,在定义堆的大小时,建议将其保持在32GB以下。
We can use the following command (jinfo is included with the JDK) to see if the flag is set:
我们可以使用以下命令(jinfo包含在JDK中)来查看该标志是否被设置。
jinfo -flag UseCompressedOops <pid>
5.5. Tune the Garbage Collector
5.5.调整垃圾收集器
The garbage collector is a memory management system that runs in the background.
垃圾收集器是一个在后台运行的内存管理系统。
Its primary objective is to locate unused objects in the heap and free the memory they contain. The Java application may stall as a result of some GC actions (remember the UI freeze?). This is most likely to happen if our application has a huge heap (more than 4GB). To reduce the delay time in these circumstances, GC optimization is required. After dealing with these challenges in multiple Jenkins setups, We have come up with the following recommendations:
它的主要目的是定位堆中未使用的对象,并释放它们所包含的内存。Java应用程序可能会因为一些GC动作而停滞不前(还记得UI冻结吗?)如果我们的应用程序有一个巨大的堆(超过4GB),这是最有可能发生的。为了减少这些情况下的延迟时间,需要进行GC优化。在处理了多个Jenkins设置中的这些挑战后,我们提出了以下建议。
- Enable G1GC — the most up-to-date GC implementation (default on JDK9)
- Enable GC logging – this will aid in future monitoring and tuning
- If necessary, configure GC with extra flags
- Keep monitoring
6. Conclusion
6.结论
In this quick tutorial, we first discussed the distributed master-slave architecture in Jenkins. After that, we looked at few options to manually start, stop and restart Jenkins. Finally, we explored different configurations in Jenkins to improve the performance.
在这个快速教程中,我们首先讨论了Jenkins的分布式主从架构。之后,我们看了一些手动启动、停止和重启Jenkins的选项。最后,我们探索了Jenkins中的不同配置,以提高性能。
If we devote some time to Jenkins and follow these guidelines, we’ll be able to take advantage of its many useful capabilities while avoiding potential dangers.
如果我们为詹金斯投入一些时间并遵循这些准则,我们将能够利用它的许多有用的能力,同时避免潜在的危险。