Jenkins step-by-step build environment (agent)


root@jenkins:~# netstat -antp|grep 50000
tcp6 0 0 :::50000 :: LISTEN 5139/java

1.52 Install Jenkins

root@ubuntu20:~# dpkg -i jenkins_2.414.3_all.deb

The key to configuring various types of Agents is how to start the Agent
? JNLP Agent corresponds to the method of “starting the agent through Java Web”
? Establish a Web connection based on HTTPS collaboration between Master and Slave
? When Slave connects to Master for the first time, it needs to provide the Secret for authentication to complete
Bootstrap
?Brief steps to configure JNLP Agent to create a distributed build environment
? Prepare Agent nodes and deploy Jenkins
? On the Master node, add the definition of the Agent node
? On the Master node, obtain the instructions that need to be executed when connecting the Agent node to the Master.
Order
? On the Agent node, open the command terminal and run the command prompted by the Master
? Verify the added results on the Master node


/home/jenkins/agent

Click on the created

agent 1.52 run command

root@ubuntu20:~# mkdir /home/jenkins/agent -pv
root@ubuntu20:~# chown -R jenkins.jenkins /home/jenkins/agent/

curl -sO http://192.168.1.51:8080/jnlpJars/agent.jar
java -jar agent.jar -jnlpUrl http://192.168.1.51:8080/computer/jenkins01-192.168.1.52/jenkins-agent.jnlp -secret db32e82e85dddf69f96f0f50bd13b5380f5f6de1789f0535c01bb6508 43c844a -workDir “/home/jenkins/agent”

Edit the pipeline to fill in the agent’s label

View pipeline will be deployed in agent1.52

Connect to agent via ssh

root@ubuntu20:/var/lib/jenkins/plugins# passwd jenkins
New password: 123456
Retype new password:
passwd: password updated successfully

Successful connection using ssh

Report an error and authorize

Building remotely on jenkins01-1.52 (linux-agent) in workspace /home/jenkins/agent/workspace/spring-boot-helloword2
The recommended git tool is: NONE
java.lang.ClassNotFoundException: org.jenkinsci.plugins.gitclient.Git$GitAPIMasterToSlaveFileCallable

chown -R jenkins:jenkins /var/lib/jenkins/workspace/

Use docker agent

root@jenkins:~/learning-jenkins-cicd/07-jenkins-agents# cat docker-compose-inbound-agent.yml
version: '3.6'

volumes:
  agent01_data: {<!-- -->}
  agent02_data: {<!-- -->}

networks:
  jenkins_net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.31.9.0/24

services:
  agent01:
    image: jenkins/inbound-agent:alpine-jdk11
    hostname: agent01.magedu.com
    user: root
    environment:
      TZ: Asia/Shanghai
      JENKINS_URL: http://192.168.1.51:8080 #jenkinsmaster address
      JENKINS_AGENT_NAME: agent01.magedu.com #agent name
      JENKINS_AGENT_WORKDIR: /home/jenkins/agent
      JENKINS_SECRET: 7eecbd2daa66612cac412d4fc484faf80fcee0fc0e8502b12abec11605744074 #jenkins key after adding the node, keep it consistent
      # SECRET is automatically generated by the master when adding agent01.
    volumes:
      - agent01_data:/appdata/jenkins/
    networks:
      jenkins_net:
        ipv4_address: 172.31.9.11
        aliases:
          - slave01
          - agent01
    #extra_hosts:
    # - "jenkins.magedu.com:172.29.7.7"
    #restart: always

  agent02:
    image: jenkins/inbound-agent:alpine-jdk11
    hostname: agent02.magedu.com
    user: root
    environment:
      TZ: Asia/Shanghai
      JENKINS_URL: http://192.168.1.51:8080
      JENKINS_AGENT_NAME: agent02.magedu.com
      JENKINS_AGENT_WORKDIR: /home/jenkins/agent
      JENKINS_SECRET: be0e53b93558553ee9694fced68d6443138ee13c5eeaf5d07bfad026d2fd36a6
      # SECRET is automatically generated by the master when adding agent02.
    volumes:
      - agent02_data:/appdata/jenkins/
    networks:
      jenkins_net:
        ipv4_address: 172.31.9.12
        aliases:
          - slave02
          - agent02
    #extra_hosts:
    # - "jenkins.magedu.com:172.29.7.7"
    #restart: always

Yaml file JENKINS_SECRET and the key added in jenkins must be consistent


run
docker-compose -f docker-compose-inbound-agent.yml up

After running successfully

Adding a pipeline can be used

Connect via docker ssh

ssh-keygen -t rsa -b 2048 -P ’
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): .jenkins-ssh-agent-rsa
Your identification has been saved in .jenkins-ssh-agent-rsa
Your public key has been saved in .jenkins-ssh-agent-rsa.pub

View and replace yanl

root@jenkins:~# cat .jenkins-ssh-agent-rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPIv6Hjrl0r4QroJe + + /xx3wMJesjoKW7Or16ilVTEXLP5PahwJyNlRZIJN5JdmFwu6QV/izQw0JvyceOo5Gebo1LZW6qZmhMJAJB537U38l1Wex7iIpn TlugrLkq2CtaOPKNEYEXBXH0SOaZuTosMoSLI83CBHlWWzAKcd/WRWQOMB48eABGvxC8LZDzrsA/uA3kIRaJS56PeWkBWK/OU/inCfzmSUVbN5uY3eRV/WHZ32R2dk9ELbmBjcxt + w2h + a0aWCu Wj4wSQd8Oy0LfFTuuP + PwCpWasJqhIf3r + nms72oXk1qS9P7peFGybSDGePbW8BD209gJ3UzERif/j root@jenkins
 cat docker-compose-ssh-agent.yml
version: '3.6'

volumes:
  ssh_agent01_data: {<!-- -->}
  ssh_agent02_data: {<!-- -->}

networks:
  jenkins_net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.31.9.0/24

services:
  ssh-agent01:
    image: jenkins/ssh-agent:jdk11
    hostname: ssh-agent01.magedu.com
    #user: jenkins
    environment:
      TZ: Asia/Shanghai
      JENKINS_AGENT_HOME: /home/jenkins
      JENKINS_AGENT_SSH_PUBKEY: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPIv6Hjrl0r4QroJe + + /xx3wMJesjoKW7Or16ilVTEXLP5PahwJyNlRZIJN5JdmFwu6QV/izQw0JvyceOo5Gebo1LZW6qZmhMJAJ B537U38l1Wex7iIpnTlugrLkq2CtaOPKNEYEXBXH0SOaZuTosMoSLI83CBHlWWzAKcd/WRWQOMB48eABGvxC8LZDzrsA/uA3kIRaJS56PeWkBWK/OU/inCfzmSUVbN5uY3eRV/WHZ32R2dk9ELb mBjcxt + w2h + a0aWCuWj4wSQd8Oy0LfFTuuP + PwCpWasJqhIf3r + nms72oXk1qS9P7peFGybSDGePbW8BD209gJ3UzERif/j root@jenkins
      # SSH PRIVATE KEY and PUBLIC KEY is generated by you.
    networks:
      jenkins_net:
        ipv4_address: 172.31.9.21
        aliases:
          -ssh-slave01
          -ssh-agent01
    ports:
      - "22022:22"
    #restart: always

  ssh-agent02:
    image: jenkins/ssh-agent:jdk11
    hostname: ssh-agent02.magedu.com
    #user: jenkins
    environment:
      TZ: Asia/Shanghai
      #JENKINS_AGENT_HOME: /home/jenkins
      JENKINS_AGENT_SSH_PUBKEY: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPIv6Hjrl0r4QroJe + + /xx3wMJesjoKW7Or16ilVTEXLP5PahwJyNlRZIJN5JdmFwu6QV/izQw0JvyceOo5Gebo1LZW6qZmhMJAJ B537U38l1Wex7iIpnTlugrLkq2CtaOPKNEYEXBXH0SOaZuTosMoSLI83CBHlWWzAKcd/WRWQOMB48eABGvxC8LZDzrsA/uA3kIRaJS56PeWkBWK/OU/inCfzmSUVbN5uY3eRV/WHZ32R2dk9ELb mBjcxt + w2h + a0aWCuWj4wSQd8Oy0LfFTuuP + PwCpWasJqhIf3r + nms72oXk1qS9P7peFGybSDGePbW8BD209gJ3UzERif/j root@jenkins
      # SSH PRIVATE KEY and PUBLIC KEY is generated by you.
    networks:
      jenkins_net:
        ipv4_address: 172.31.9.22
        aliases:
          -ssh-slave02
          -ssh-agent02
    ports:
      - "22122:22"
    #restart: always

Run container
root@jenkins:~/learning-jenkins-cicd/07-jenkins-agents# docker-compose -f docker-compose-ssh-agent.yml up

Created in jenkins, the private key is saved as an authentication certificate

cat .jenkins-ssh-agent-rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEAzyL + h465dK + EK6CXvvv8cd8DCXrI6Cluzq9eopVUxFyz + T2ocCcj
ZUWSCTeSXZhcLukFf4s0MNCb8nHjqORnm6NS2VuqmZoTCQCQed + 1N/JdVnse4iKZ05boKy
5KtgrWjjyjRGBFwVx9Ejmmbk6LDKEiyPNwgR5VlswCnHf1kVkDjAePHgARr8QvC2Q867AP
7gN5CEWiU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AgQDoMGkkM4XNIo2bmLTx0o3JntkrxpeKm4igX52/cgpnUVLvgXRBv2tM
Mif7baxWwtecW9p0y0w2WkqWsZZGRVQvUBf0NWx21xw1KWayCWXWCJgznjePJc7KzzxTod
eCW1pS0WbGqeCDx100xxxuCJCe4FB/YMpO5zyClAZUCRjqqwAAAIEA5GDj7zXXwNb1viJO
fMLCcK28Qhho2ueIOZIZF2TqcgElQaPUHVrBHQUtr/wEPqFYGLRP2qAiBNEQCL0QmuA94P
FxaKbQThtW/MZxSweitRxl5brua8jVofx2 + 6w/CNTco4DdmSceV0mZEKm + LPPaZsdM9g/b
huHbDn6nuAsFP6kAAAAMcm9vdEBqZW5raW5zAQIDBAUGBw==
-----END OPENSSH PRIVATE KEY-----

jenkins adds slave node

192.168.1.51 is the host where the jenkins container is running.
Host Key Verification Strategy Select non
/opt/java/openjdk/bin/java is the java path in the container
View normal

Dynamic Agent based on Docker container

#192.168.1.52
docker service add
vim /usr/lib/systemd/system/docker.service
-H tcp://0.0.0.0:2376

root@ubuntu20:~/.ssh# systemctl daemon-reload
root@ubuntu20:~/.ssh# systemctl restart docker.service

jenkins server test

root@jenkins:~# curl http://192.168.1.52:2376/version
{<!-- -->"Platform":{<!-- -->"Name":"Docker Engine - Community"},"Components":[{<!-- -->"Name":"Engine","Version":"24.0.7","Details":{<!-- -->"ApiVersion":\ "1.43","Arch":"amd64","BuildTime":"2023-10-26T09:08:01.000000000 + 00:00","Experimental":"false ","GitCommit":"311b9ff","GoVersion":"go1.20.10","KernelVersion":"5.4.0-81-generic","MinAPIVersion ":"1.12","Os":"linux"}},{<!-- -->"Name":"containerd","Version":\ "1.6.24","Details":{<!-- -->"GitCommit":"61f9fd88f79f081d64d6fa3bb1a0dc71ec870523"}},{<!-- -->"Name":\ "runc","Version":"1.1.9","Details":{<!-- -->"GitCommit":"v1.1.9-0-gccaecfc" }},{<!-- -->"Name":"docker-init","Version":"0.19.0","Details":{<!-- -->"GitCommit":"de40ad0"}}],"Version":"24.0.7","ApiVersion":"1.43","MinAPIVersion": "1.12","GitCommit":"311b9ff","GoVersion":"go1.20.10","Os":"linux","Arch": "amd64","KernelVersion":"5.4.0-81-generic","BuildTime":"2023-10-26T09:08:01.000000000 + 00:00"}

Install the plug-in docker-plugin

tcp://192.168.1.52:2376 #docker address


docker-agent2 docker-dyn-agent-2 #label, the pipeline must be configured with labels before it can be used
jenkins/inbound-agent:jdk11 #mirror

The build prompt failed and succeeded after restarting Jenkins:

Add ssh Agent template on Docker Cloud

System management-clounds


Edit and modify pipeline

Check the used