Step-by-Step Guide: Connecting Jenkins Master and Agent via SSH and Deploying Nginx

 


 Launch EC2 Instances:

      1. Start by launching two EC2 instances on AWS. One will serve as the master node, and the other as the agent node.
  1. Install Jenkins and Java on Master:

    • SSH into the master instance and install Jenkins and Java. You can follow the official Jenkins documentation for installation instructions.



  2. Install Java on Agent:

    • Similarly, SSH into the agent instance and install Java. Ensure that the same version of Java is installed on both the master and agent nodes.
  3. Generate SSH Key Pair:

    • On the master node, generate an SSH key pair using the ssh-keygen command. This will create a public and private key pair.
  4. Authorize Agent:

    • Copy the public key generated on the master node (~/.ssh/id_rsa.pub) and paste it into the authorized_keys file on the agent node. This allows the master node to authenticate with the agent node via SSH.


  5. Open Port 8080:

    • In the security group settings for both EC2 instances, open port 8080 to allow inbound traffic for Jenkins.
  6. Configure Jenkins Master:

    • Access the Jenkins web interface on the master node by navigating to http://<master-instance-public-ip>:8080. Follow the on-screen instructions to complete the setup process.
  7. Add Agent in Jenkins:

    • Once Jenkins is set up, navigate to the Jenkins dashboard and click on "Manage Jenkins" > "Manage Nodes and Clouds" > "New Node". Enter a name for the agent node and select "Permanent Agent". Then, click "OK".
  8. Configure Agent:

    • On the agent configuration page, enter the connection details, such as the agent's hostname and credentials. Select "Launch agent via execution of command on the master" and specify the command to launch the agent. Ensure that the SSH private key of the master node is provided.






  9. Save and Connect Agent:

    • Save the agent configuration and Jenkins will attempt to connect to the agent using SSH. If successful, the agent node will be listed as online in the Jenkins dashboard.



  10. Deploy Nginx on Agent:

    • Finally, you can deploy Nginx on the agent node using a Jenkins pipeline job. Write a Jenkinsfile using Groovy syntax to define the steps of the deployment process. This can include tasks such as downloading Nginx, configuring it, and starting the service.


By following these steps, you should have successfully connected a Jenkins agent via SSH and deployed Nginx on the agent node using a pipeline job.