Day-23 - Jenkins Agent

Day-23 - Jenkins Agent

ยท

5 min read

Step 1: Sign in to AWS Console

Navigate to the AWS Management Console and sign in to your account.

Step 2: Launch EC2 Instance

  1. Click on the EC2 service.

  2. Select "Launch Instance."

  3. Choose an Amazon Machine Image (AMI) based on your requirements.

  4. Select an Instance Type.

  5. Configure Instance Details (network settings, subnet, etc.).

  6. Add storage as needed.

  7. Configure Security Group to allow inbound traffic for Jenkins (port 8080 by default).

  8. Review and Launch the instance.

Step 3: Connect to EC2 Instance

  1. Once the instance is running, note down its Public IP address.

  2. Access the instance via SSH using a terminal or SSH client.

     kotlinCopy codessh -i your_key.pem ec2-user@<your-instance-public-ip>
    

Step 4: Install Jenkins on the New EC2 Instance

  1. Update system packages:

     sqlCopy codesudo yum update -y
    
  2. Install Jenkins:

     arduinoCopy codesudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
     sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
     sudo yum install jenkins -y
    
  3. Start Jenkins service:

     bashCopy codesudo systemctl start jenkins
     sudo systemctl enable jenkins
    

Step 5: Configure Jenkins on the New Instance

  1. Open a web browser and enter the New EC2 Instance's Public IP followed by port 8080 (default Jenkins port).

     vbnetCopy codehttp://<your-instance-public-ip>:8080
    
  2. Follow the setup process to unlock Jenkins using the initial admin password (located at /var/lib/jenkins/secrets/initialAdminPassword).

  3. Customize Jenkins by installing plugins and setting up admin credentials.

Step 6: Connect New Instance to Jenkins Master

  1. Log in to the Jenkins Master.

  2. Go to Manage Jenkins > Manage Nodes and Clouds > New Node.

  3. Enter a Node name, select "Permanent Agent," and click "OK."

  4. Configure the New EC2 Instance settings:

    • Set the Remote Root Directory and Labels.

    • Choose Launch Method as "Launch agent by connecting it to the master."

    • Specify the New EC2 Instance's details (IP, credentials, etc.).

  5. Save the configuration.

Step 7: Verify Connection

  1. Go back to the Jenkins dashboard.

  2. Check if the New Node (EC2 Instance) appears under Manage Nodes and Clouds.

  3. Run a test job or pipeline on the New Node to ensure the connection is successful.

Step 1: Generate SSH Key Pair on Jenkins Master

  1. Access Jenkins Master: Log in to the Jenkins Master server where Jenkins is installed.

  2. Generate SSH Key Pair: Open a terminal or command prompt.

     mathematicaCopy codessh-keygen -t rsa -b 4096 -C "jenkins_agent"
    

    Follow the prompts to create the SSH key pair. Keep the default file location or specify a location as needed.

Step 2: Prepare the SSH Key for Agent

  1. Copy the Public Key: Retrieve the public key generated in the previous step.

     bashCopy codecat ~/.ssh/id_rsa.pub
    

    Copy the entire content of the public key.

  2. Authorize SSH Access on Agent: Connect to the new EC2 instance (Agent) using SSH.

     kotlinCopy codessh ec2-user@<your-agent-public-ip>
    

    Create an .ssh directory if not present.

     bashCopy codemkdir ~/.ssh
     chmod 700 ~/.ssh
    

    Open the authorized_keys file using a text editor.

     javascriptCopy codenano ~/.ssh/authorized_keys
    

    Paste the copied public key into this file and save it.

Step 3: Configure Jenkins Master

  1. Add SSH Credentials to Jenkins: Open Jenkins Dashboard. Go to Credentials > System > Global credentials > Add Credentials. Select SSH Username with private key. Fill in the required fields:

    • Username: (e.g., jenkins_agent)

    • Private Key: Choose Enter directly and paste the contents of the private key (id_rsa).

Step 4: Configure Jenkins Agent Node

  1. Create a Jenkins Node for the Agent: Go to Jenkins Dashboard. Navigate to Manage Jenkins > Manage Nodes and Clouds > New Node. Enter Node Name, select "Permanent Agent," and click "OK."

  2. Configure Node for SSH Connection: Fill in the Node details:

    • Remote Root Directory

    • Labels

    • Launch Method: Select Launch agent via execution of command on the master.

    • Launch command:

        vbnetCopy codessh -i /path/to/private/key jenkins_agent@<your-agent-public-ip> java -jar /path/to/slave.jar
      

      Replace /path/to/private/key with the path to the private key on the Jenkins Master.

  3. Save Configuration.

Step 5: Verify Connection

  1. Start the Jenkins Agent: Go back to Jenkins Dashboard. Check the newly created Node. Click on it and select Launch Agent.

  2. Monitor Jenkins Logs: Check Jenkins logs for the Agent connection status.

    • You should see the Agent connecting and becoming online in the Jenkins dashboard.
  3. Run a Test Job: Create a simple Jenkins job and assign it to the newly connected Node. Execute the job and verify that it runs on the Agent successfully.

๐ŸŒ Verifying Node Status in Jenkins

Step 1: Access Jenkins Dashboard

  1. Log in to Jenkins: Open a web browser and enter the URL for your Jenkins instance. Log in using your credentials.

Step 2: Navigate to Nodes Section

  1. Go to Nodes Page: On the Jenkins Dashboard, locate and click on Manage Jenkins.

  2. Access Nodes and Clouds: From the dropdown, select Manage Nodes and Clouds.

Step 3: Check Node Status

  1. Find the Agent Node: Look for the Node representing your connected EC2 instance (Agent) in the list of Nodes. The name of the Node you created for the Agent should be visible.

  2. Node Status:

    • Online Status: If the Agent successfully connected, it should display an "Online" status indicator.

    • Offline or Disconnected: If the status is "Offline" or "Disconnected," there might be connectivity issues or misconfiguration.

Step 4: View Node Details

  1. Inspect Node Configuration: Click on the Node representing your Agent to view its details.

    • Check the labels, launch method, and other configuration settings.

    • Verify the Remote Root Directory and any other parameters set during configuration.

  2. Check Node Log: Look for any logs or information related to the Node's connectivity status.

    • It might provide insights into any errors or issues encountered during the connection attempt.

Step 5: Verify Connectivity

  1. Test Job Execution: Create or select a Jenkins job and assign it explicitly to the connected Node.

    • Execute the job and observe if it runs on the connected EC2 instance.
  2. Monitor Job Execution: Check the Console Output or Build History of the job to confirm that it's running on the intended Node.

Step 6: Troubleshooting

  1. Addressing Offline Nodes: If the Node appears offline or disconnected:

    • Double-check the SSH key configuration on both Jenkins Master and the Agent.

    • Ensure the correct IP address, credentials, and launch command in Jenkins Node configuration.

    • Review firewall settings and security groups on AWS EC2 for proper port access.

  2. Inspect Logs: Examine Jenkins logs for any error messages related to the Agent connection.

    • Logs might provide clues about connectivity issues or misconfigurations.
ย