Friday, July 1, 2022

How to simply build a Jenkins server and Agent

Having been working on another DevOps Academy it was surprising with the research that the students did on how to build a Jenkins server with Agent, how complicated most people made it.

This example is based on using AWS with 2 EC2 instances, but would work on-prem and other clouds.

Steps to build

1. Create 2 Ubuntu instances both Medium

  • 1 is Controller
  • 1 is the Agent
  • Settings
    • t2.medium
    • 20GB disk
    • Ubuntu image
      • Select or create a security group (e.g. jenkinssg) that has the following inbound ports
        • 8080
        • 22
        • 8200

2. Install Jenkins on the controller

    • ssh onto the Jenkins controller
    • sudo apt update # It's Ubunutu after all
    • sudo apt -y install openjdk-11-jdk # Install Java, Jenkins needs it
    • wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
    • sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
    • sudo apt update
    • sudo apt install jenkins
    • sudo cat /var/lib/jenkins/secrets/initialAdminPassword   # Get the login password

3. Web browser

    • Point your web browser at http://yourPublicIPforInstance:8080
      • Paste in the text from the file
      • Click Continue button
    • Click Install suggested plugins
    • Fill in the form to create the main Jenkins user
      • Username: admin
      • Password: secret123
      • Confirm password: secret123
      • Fullname: Administrator
      • E-mail address: root@nowhere.com
    • Click Save and Continue button
    • Change the IP address to the Private IP address of your Jenkins instance
      • Leave the http:// and the :8080/
    • Click Save and Finish button
    • Click Start using Jenkins button

4. Configuring Jenkins to see the new node to be the agent

    • After doing step 3 you should be logged in as Administrator
      • If not log in using admin and the password you set
    • Click Manage Jenkins in the left menu
    • Click Manage Nodes and Clients
    • Click New Node in the new menu
    • Set the Node name to Worker1
    • Select Permanent Agent
    • Click Create button
    • New screen
      • Number of executors: 2
      • Remote root directory: /home/ubuntu
      • Labels: all
      • Leave all other as default
      • Click Save
    • The worker node will show as not connected
    • Click on Worker1 link
    • You'll notice an error message
      • JNLP agent port is disabled and agents cannot connect this way. Go to security configuration screen and change it.
      • Click the link Go to security configuration screen and change it.
      • Scroll down to Agents
        • Select Fixed
        • Set the Port to 8200 (This is already allowed by jenkinssg)
      • Scroll to the bottom and click the Save button
    • Click Manage Nodes and Clients
    • Click Worker1
    • Right click the blue agent.jar link and Copy link address

5. Now ssh on to your Worker/Agent instance

    • sudo apt update
    • sudo apt -y install openjdk-11-jdk
    • wget http://52.213.211.75:8080/jnlpJars/agent.jar
      • Where the http:// link is pasted from the Copy link address
    • On the Jenkins web page copy the 2nd box echo line
      • Paste this line into the terminal of the Worker/Agent ssh session
    • On the Jenkins web page copy the 2nd box java -jar line
      • In the terminal of the Worker/Agent ssh session
      • Type the word   nohup
        • Then paste the java -jar line after this
        • Type a space and then   &    at the end of the line
      • e.g.
        • nohup java -jar agent.jar -jnlpUrl http://172.31.16.36:8080/computer/Worker1/jenkins-agent.jnlp -secret @secret-file -workDir "/home/ubuntu" &

6. Back to the Jenkins web site

    • Click Back to List if you are in the Agent Worker1 screen
    • or
    • Click Dashboards top left of the page
      • Click Manage Jenkins
      • Click Manage Nodes and Clouds
    • Note your agent is now connected

NOTE:
This configuration doesn't create a service to start the agent on reboot, but is purely an example to get it running.  To make the agent a proper service you would need to create the appropriate file in /etc/systemd/system (call it jenkins-agent.service) or for older systems the service script in /etc/init.d.

No comments:

Post a Comment