Setting Up Jenkins Master Node on AWS

Setting Up Jenkins Master Node on AWS

Table of contents

No heading

No headings in the article.

Introduction

Jenkins, an open-source automation server, is widely used for automating the build, deployment, and testing of software applications. By setting up a Jenkins master node on AWS (Amazon Web Services), you can leverage the power of cloud infrastructure to efficiently manage your software development pipelines. In this guide, we will walk through the process of setting up a Jenkins master node on AWS, complete with examples and best practices.

Prerequisites

  1. AWS Account: You need an active AWS account to create and manage instances.

  2. Basic Knowledge of Jenkins: Familiarity with Jenkins concepts such as jobs, pipelines, and plugins will be helpful.

Step 1: Launch an EC2 Instance for Jenkins Master

1.1. Log in to AWS Console:

  • Go to the AWS Management Console and log in to your AWS account.

1.2. Navigate to EC2:

  • Open the EC2 Dashboard.

1.3. Launch an Instance:

  • Click on "Launch Instance" to start the instance creation process.

  • As we are creating a master and node on jenkins so take 2 ubuntu machine of t2.micro

  • Here I am using 2 instance naming

    1.jenkins-server

    2.jenkins-agent

    Step 2: Connect to Jenkins Instance

    2.1. SSH into Instance:

    • Use the private key associated with your instance's key pair to SSH into the instances or click on connect and use ec2 instance connect to connect or ssh client to connect(There are many ways to connect to an instance u can use any one of them)

Step 3: Install Jenkins on jenkins-server

3.1. Update Packages:

    sudo apt update -y

3.2. To install Jenkins java is required

go to link and follow steps :-https://www.jenkins.io/doc/book/installing/linux/

  • 3.3. Install Java Development Kit (JDK):

      sudo apt update
      sudo apt install openjdk-17-jre
      java -version
    

  • 3.3.Install Jenkins:

  • Go to weekly release section of https://www.jenkins.io/doc/book/installing/linux/

      curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
        /usr/share/keyrings/jenkins-keyring.asc > /dev/null
      echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
        https://pkg.jenkins.io/debian binary/ | sudo tee \
        /etc/apt/sources.list.d/jenkins.list > /dev/null
      sudo apt-get update
      sudo apt-get install jenkins
    

    3.4. Start Jenkins:

      sudo systemctl start jenkins
      sudo systemctl enable jenkins
      sudo systemctl status jenkins
    

  • Step 4: Access Jenkins Web UI

  • 4.1. Access Jenkins Dashboard:

    In aws provide access to 8080 at ur ip under security group

    click on edit inbound rules open port 8080 and save rules

    Open a web browser and enter: http://<your-instance-public-ip>:8080

  • 4.2. Unlock Jenkins:

    • Retrieve the initial admin password from the Jenkins instance:

      jenkins provide initial pwd so copy the path shown at ur terminal and run below command

        sudo cat /var/lib/jenkins/secrets/initialAdminPassword
      

    • Copy the password and paste it in the Jenkins setup wizard.

4.3. Install Plugins:

  • Choose the "Install Suggested Plugins" option during setup.

4.4. Create Admin User:

  • Create an admin user with your desired credentials.

    4.5. Jenkins Ready:

  • Jenkins is now set up and ready to use!

  • To Setup connection with node we will be using SSH

    connecting jenkins-server to jenkins node

    1.Go to jenkins-server machine and execute below command to create

    ssh public and private key

      ssh-keygen -t rsa
    

  • Now to connect jenkins-server to jenkins-agent machine we need to copy the id_pub key created in jenkins-master and paste it in jenkins-agent at path shown

    below .

    1.In jenkins master run below cmd and copy key contents

      cd .ssh
      cat id_rsa.pub
    

    2.Go to jenkins-agent run below cmd and paste the code copied from jenkins-server

      cd .ssh
      vi authorized_keys
    

    3.To verify connection is established or not go to jenkins-master and use below cmd

      sytax :- ssh user@ip
      ssh Ubuntu@35.154.5.78
    

    Now jenkins-master is connected to jenkins-agent using ssh.

  • At Agent side <jenkins-sgent>

    Install java and it should be on same version as jenkins master

      sudo apt update
      sudo apt install openjdk-17-jre
      java -version
    

    4.6. Add Slave Node to Master:

    • On the Jenkins master dashboard, navigate to "Manage Jenkins" > "Manage Nodes and Clouds" > "New Node".

    • Provide a node name and select "Permanent Agent" and click on create.

    • Configure the remote root directory and usage as per your requirements.

      Description :- Give any meaningful sentence to describe about node

      Number of executers:- Maximum number of concurrent builds that Jenkins may perform on this node.

      Remote root directory:- An agent needs to have a directory dedicated to Jenkins here i am using home path of jenkins-agent server

      Labels:-one can provide label name so you can tag different jobs with label so that will work on that node only

      Usage:-Controls how Jenkins schedules builds on this node.here i am using Using this node as much as possible as option

      Availability:-Controls when Jenkins starts and stops this agent.

      • Choose "Launch agent via SSH" and enter the slave instance's SSH details.

        Launch method:-To launch the agent one can use either of the two options here i am using Launch agents via SSH option

        Host :- Provide ip of ur agent ie jenkins-agent

        Credentials:-Select the credentials to be used for logging in to the remote host. as we are using ssh so we will create a key , click on add to generate a key

        A new page will pop up as below and select options as seen in below screenshot

        Id :- can be any name

        Description :- any

        Username :- please provide agent user in our case Ubuntu

Now under Private key section we need to copy private key of jenkins-master click on add and paste it under private key tab and again click on add.

Now on credential select key which u have created

Host Key Verification Strategy:- Controls how Jenkins verifies the SSH key presented by the remote host. choose option as below and click on save

Now the node is up

  • Note :- Many a times it creates issue with ssh rsa key so you use below

      ssh-keygen -t ed25519
    

    https://stackoverflow.com/questions/73472577/connecting-to-node-jenkins-ssh-error-server-rejected-the-1-private

  • Conclusion

    Setting up a Jenkins master node on AWS provides a scalable and efficient solution for managing your software development pipelines. By following this step-by-step guide, you've successfully launched an EC2 instance, installed Jenkins, and accessed the Jenkins dashboard. From here, you can explore Jenkins' extensive capabilities, configure build jobs, and create pipelines to automate various aspects of your software development lifecycle. Happy automating!