1. Overview
In theJenkins Deployment Architecture OverviewThe Jenkins deployment architecture is explained in this blog post. For distributed architecture, Jenkins includes both solid and dynamic Agent solutions.
- Fixed Agent (commonly used in virtual machines): Agent container has been running, the task will not be destroyed after the completion of the construction, after the creation of the completion of the cluster will always occupy the resources, the configuration process is simpler.
- Dynamic Agent (commonly used in K8s): dynamically create Agent containers when building tasks, and destroy the containers after the completion of the task build, which can achieve dynamic allocation of resources, high resource utilization, but the configuration process is more complex.
In this article, we will add a fixed Agent to Jenkins to explain in detail.
2, Jenkins add a fixed Agent
(1) Log in to the Jenkins Dashboard, click "Manage Jenkins" on the left side, select "Security → Proxy Configuration Page (TCP port for JNLP)", as shown in the figure. As shown in the figure. We can choose to open a fixed port or randomly open a port of Jenkins Master to provide JNLP services.
The proxy on the newly installed Jenkins Master node is disabled by default. If you don't enable the proxy here, the following error will be reported when you add a fixed node with JNLP connection mode later.
: http://10.20.31.153:8080/jenkins/tcpSlaveAgentListener/ is invalid: 404 null at (:222) at (:809) at (:563)
(2) Click "Manage Jenkins" on the left and select "Nodes".
(3) Click "New Node" in the upper right corner of the page, enter the node name as node203 (the name can be customized), and select the type of fixed node.
Configure the following node information:
- Name: name of the agent.
- Number of executors (maximum number of concurrent builds): the executor, the unit that actually executes the project, defaults to 1. An executor can be interpreted as a separate process (in fact, a thread). Multiple executors can be run on a single node.
- Remote root directory: The working directory on the agent machine, using an absolute path.
- Labels: labels of the agents. When the number of agents becomes large, how do you know which ones support JDK 8 and which ones support the environment? We can determine this by labeling the agent (sometimes called tag). The same agent can have multiple tags.
- Usage: the usage policy of the agent, there are two kinds:
- Use this node as much as possible,Use this whenever possibleagent。
- Only build jobs with label expressions matching this node, use this agent only if the build jobs match the label of this agent.
-
Launch method, there are two types of launch methods:
- Launch agent by connecting it to the controller, through the Java Web launch agent (JNLP, cross-platform, but must be installed and configured in advance in the fixed node JRE environment, the most commonly used way).
- Launch agent via SSH, Jenkins Master connects to the fixed Agent via SSH (this method is relatively simple, but not cross-platform, used less).
The rest of the parameters can be left as default and need not be filled in, and click the"Save".
(4) InClicking on the name of the added node in the "Node List", you can see that the Agent status is not connected and provides a way for the node to connect to Jenkins.
(5) On the fixed Agent node, open a command terminal and run the command prompted by Master.
Note 1: Agent nodes need to be installed in advance to match the Jenkins version of the JRE, otherwise reported has been compiled by a more recent version of the Java Runtime (class file version 5x.0) error. Where 5x: 51=Jdk7, 52=Jdk8, 53=Jdk9, 54=Jdk10, 55=Jdk11, and so on.
Note 2: The main core difference between the 2 startup methods, SSH and JNLP, is:
- SSH is the Master active connection Slave, when you in the Master with a machine account password, then the Master can through the account password SSH to this machine to execute commands, so the
SSH is the Master's active connection to the Slave.
。- JNLP is where the Slave actively connects to the Master, and the Slave receives and executes the Job passed by the Master, and then feeds the results back to the Master.
(6) In Jenkins MasterClick the new node name in the "node list", you can see the Agent status is connected, you can use the fixed node to build the pipeline tasks.
3. Use a fixed Agent to execute pipelined tasks
(1) Create a new pipeline with the following Pipeline.
pipeline { agent { node { label 'node203' } } stages { stage('test node203') { steps { echo 'Execution pipeline using node203' sh 'sleep 1h' } } } }
(2) Run the pipeline and check the node203 node status again, you can see that the pipeline task assigned (by label matching) to the current node by the pipeline is being executed.
Note 1: The above example is just a Hello World, if you need to execute mvn commands inside the pipeline, you need to install and configure the specified version of mvn in the fixed node, if you need to execute npm commands in the pipeline, you need to install and configure the specified version of nodejs ..... in the fixed node.
4, Jenkins using JNLP to start Agent principle explained in detail
4.1 General
The Java Network Launching Protocol (JNLP) is a protocol that allows clients to launch applications hosted on remote Web servers to establish secure and efficient communication between Java applications.
JNLP is just the technical approach at its core:
- The Slave node actively runs a
Agent program
Establishes a connection with the Master, receives and executes the Job passed by the Master, and then sends the results back to the Master. - The Master communicates with the slave nodes through the JNLP protocol and distributes the build tasks to the Slave nodes for execution.
4.2 Why use the JNLP protocol
- automatization: The JNLP protocol allows the connection and configuration process between the Jenkins Master and Agent to be automated without the need for manual intervention. connection and authentication with the Master is handled automatically when the Agent starts, simplifying the configuration process.
- safety: The authentication mechanism in the JNLP protocol ensures that only authorized Agents can connect to Jenkins Master, thus increasing the security of the system.
- dynamic update (Internet): JNLP files can contain up-to-date configuration information, which allows the Agent to dynamically obtain and use this information at startup.
4.3 Communication process between Master and Slave nodes
(1) The slave node establishes a TCP/IP long connection with the master node via the JNLP protocol, a mechanism that allows continuous communication between the Jenkins Master and the Agent for real-time task scheduling, execution, and state updates.
java -jar -url http://10.20.31.153:8080/jenkins/ -secret 800b585576416c0041ed6ee9783f895118193e443309a2d557cfb319b057a8a9 -name node203 -workDir "/opt/jenkins"
Here are more details about long connections:
- Long connection establishment:
-
- Agent startup: When the Jenkins Agent starts, it connects to the Jenkins Master and establishes a persistent TCP connection. This connection uses the JNLP protocol.
- communications hold: This connection is used to transfer task configurations, execution steps, log messages, and so on. It ensures that communication between the Jenkins Master and the Agent takes place in real time.
-
Communication process:
- real time interaction: During task execution, the Agent periodically sends task progress, build logs, and other status information to the Jenkins Master, which receives these updates and displays real-time progress in a web interface.
- Send command: The Jenkins Master sends task instructions and configuration updates to the Agent over this long connection.
-
Stability of the connection:
- TCP connection: Jenkins uses a TCP connection to maintain a long connection to the Agent.The TCP protocol provides reliable data transfer, ensuring packet order and integrity.
- Heartbeat mechanism: In order to keep the connection active, Jenkins Masters and Agents typically use a heartbeat mechanism (i.e., periodically sending idle network packets) to check if the connection is still active. If the connection drops, the Master will either try to reconnect or mark the Agent as offline.
(2) The master node schedules the build task, the slave nodes receive the build task according to the label and execute the build job according to the instructions of the master node and send the build result and log back to the master node.
(3) The master node publishes build results and logs to the Jenkins interface for users to view and monitor.
5. Summary
This article explains in detail how to add a fixed Agent in Jenkins and use fixed nodes to run pipelined tasks, and also explains the principle of Jenkins JNLP connection method. Through this article, you can better understand and optimize Jenkins node configuration and task management.
Reference:Jenkins Installs JNLP Nodes
Reference:jenkins publish multiple java jenkins concurrent builds