A Step-by-Step Guide to Managing EBS Volumes and Migrating Jenkins Across AWS Regions

A Step-by-Step Guide to Managing EBS Volumes and Migrating Jenkins Across AWS Regions

Project Overview:

This project demonstrates how to manage Amazon Elastic Block Store (EBS) volumes in AWS. You’ll learn key tasks like creating, attaching, and formatting EBS volumes, setting up data storage, and migrating data using EBS snapshots across different Availability Zones (AZs). The project uses essential AWS tools like EC2, EBS, and AWS Snapshots, along with the Linux command line for configuration and management.

What you'll learn:

  • Create and Attach EBS Volumes: Set up EBS volumes and attach them to EC2 instances for data storage.

  • Format and Mount Volumes: Format and mount EBS volumes for use on EC2 instances.

  • Manage Data Storage: Configure EBS volumes for persistent data storage on your EC2 instances.

  • Use EBS Snapshots for Backup and Migration: Create snapshots to back up data and migrate it between AWS regions or Availability Zones.

  • Migrate Volumes Across AZs: Copy EBS snapshots between AZs to maintain high availability and system resilience.

Project Implementation

Launch EC2 Instances in Two Different Availability Zones for Jenkins High Availability

In this step, we will launch two EC2 instances in two different Availability Zones (AZs) within two different AWS regionsMumbai (ap-south-1) and Singapore (ap-southeast-1). This setup is crucial for demonstrating cross-AZ migration and ensuring high availability for the Jenkins application. By deploying Jenkins in different regions and Availability Zones, we ensure fault tolerance and improve application resilience in case of failures.

AWS automatically creates and attaches a root EBS volume to each EC2 instance, which holds the operating system and system files. This root volume can be found in the Volumes section of the EC2 dashboard.

Install Jenkins Without Starting the Service

  • Install Jenkins without starting the service immediately.

  • Starting Jenkins right away will store application data on the root volume, which should only hold system files.

  • Storing Jenkins data on the root volume can cause performance issues and storage problems.

  • Delay starting Jenkins until it's configured to use a separate volume for its data, ensuring better performance and storage management.

To install Jenkins using the official documentation, follow these steps: https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos

Verify No Files in /var/lib/jenkins Directory

Since you've just installed Jenkins, you can verify that the /var/lib/jenkins directory is empty:

sudo ls -lrth /var/lib/jenkins

If the directory is empty, that's expected since Jenkins hasn't started storing data yet.

Verify the Current Disks with lsblk

You can use lsblk to list all available block devices and identify the new disk (EBS volume) that will be attached to the Jenkins instance:

lsblk

As of now, no external volumes are attached, as confirmed by the lsblk command.

Create a new EBS volume:

  • Go to the EC2 dashboard in the AWS Management Console.

  • Navigate to Volumes under Elastic Block Store (EBS).

Click on Create Volume.

  • Choose the desired size and volume type.

  • Select the same Availability Zone (AZ) where your EC2 instance is located

Click Create Volume.

After the external volume is created in the EBS section, it will be available and visible

Attach the EBS volume to your EC2 instance:

  • After the volume is created, right-click on it in the Volumes section.

  • Select Attach Volume.

  • In the Attach Volume dialog, select your Jenkins EC2 instance from the dropdown.

  • Choose an appropriate device name (e.g., /dev/xvdf or /dev/sdf).

  • Click Attach.

The volume will be attached to the EC2 instance in the same region and same AZ.

After the external volume is created and attached, you can find it listed under the 'Storage' section in the EC2 instance details.

Mount the New Volume to /var/lib/jenkins

After the volume is attached, you need to mount it and configure it to be used by Jenkins.

  1. Check if the disk is available: Run the following to confirm that the newly attached volume is visible/
 lsblk

You should see the new volume listed (e.g., xvdf).

  1. Check if the volume is formatted
    Before formatting, you can check if the volume is already formatted using the following command:
sudo file -s /dev/xvdf

This will tell you if the volume has a file system.

  1. Format the volume (if unformatted):
    If the volume is unformatted, you can format it with the mkfs command:
sudo mkfs.ext4 /dev/xvdf

This ensures the volume is ready for mounting and use.

  1. Mount the EBS volume:
    Mount the volume to the /var/lib/jenkins directory:
sudo mount /dev/xvdf /var/lib/jenkins

  1. Check if the volume is mounted:
    Use the lsblk command to verify that the volume is successfully mounted:
lsblk

You should see the volume listed with /var/lib/jenkins under the MOUNTPOINT column.

Steps to Permanently Mount the Volume Using /etc/fstab:

sudo cp /etc/fstab /etc/fstab.bak

sudo blkid   # Get UUID of the new volume

Update /etc/fstab to ensure the volume gets mounted automatically after reboot:

For example, you would add the following line:

sudo vi /etc/fstab    # Add the UUID entry for auto-mount

UUID=<your-uuid> /var/lib/jenkins ext4 defaults,nofail 0 2

Replace <your-uuid> with the actual UUID from the blkid command.

Step-by-Step Confirmation Process:

The Step-by-Step Confirmation Process is for verifying that the EBS volume is correctly mounted to /var/lib/jenkins on the Jenkins server and that Jenkins is functioning properly after the volume has been mounted.

  1. Unmount the volume (if already mounted): If the volume is already mounted, unmount it to start fresh:

     sudo umount /var/lib/jenkins
    

  2. Apply /etc/fstab changes: Run the following command to mount all file systems as defined in /etc/fstab, ensuring the new volume is mounted automatically:

     sudo mount -a
    
  3. Reload the Docker daemon (if necessary): If Docker is in use and needs to be reloaded for the changes to take effect, run:

     sudo systemctl daemon-reload
    

  4. Verify the mount: Check that the new EBS volume is mounted correctly by running:

     lsblk
    

    Ensure that /var/lib/jenkins is listed with the new volume mounted.

  5. Check the /var/lib/jenkins directory: Verify that the mount was successful by checking the contents of /var/lib/jenkins:

     sudo ls -lrth /var/lib/jenkins
    

    This should show the contents of the directory, now linked to the newly mounted volume.

  6. Change the ownership to the Jenkins user: After confirming the mount, change the ownership of the /var/lib/jenkins directory to the Jenkins user to ensure proper permissions:

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

  7. Start the Jenkins service: Start the Jenkins service now that everything is set up:

     sudo systemctl start jenkins
    
  8. Verify Jenkins is running: Finally, verify Jenkins is running properly with the following command:

     sudo systemctl status jenkins
    

    Access Jenkins in Mumbai Region

    After setting up Jenkins on the EC2 instance in Mumbai and attaching an external EBS volume for Jenkins data, you accessed Jenkins and created a

    sample job.

    To confirm that Jenkins is using the external volume for its data, you can check the disk usage and mount details by running this command:

     df -Th
    

    This will display all the mounted volumes and their file systems. You should see that /var/lib/jenkins is now mounted on the external volume you attached earlier, not the root volume. This confirms that Jenkins is using the external EBS volume for its data storage.

Step-by-Step Guide: Migrating Jenkins Data from Mumbai to Singapore


1. Mount the External Volume on the Mumbai Jenkins Instance

  • You've already mounted the external volume at /var/lib/jenkins on the Mumbai EC2 instance, and verified using df -Th that it's being used for Jenkins data.

2. Create a Snapshot of the External Volume in Mumbai

After confirming that Jenkins is writing data to the external EBS volume, go to the AWS Console:

  • Navigate to the EBS section under EC2 in the Mumbai region.

  • Select the volume attached to the Jenkins instance.

  • Create a snapshot of the volume.

  • This snapshot will capture the state of your Jenkins data and is stored in S3.

3. Copy the Snapshot to the Singapore Region

  • Once the snapshot is created, go to the Snapshots section in the AWS Console.

  • Select the snapshot you just created.

  • Click on the Copy option and choose Singapore as the destination region.

  • The snapshot will be copied and made available in Singapore.

4. Create a New EBS Volume in Singapore from the Snapshot

  • In the Singapore region, navigate to the Snapshots section under the Elastic Block Store (EBS) in the AWS Console.

  • Find the snapshot you copied from the Mumbai region.

  • Select the snapshot and click on Create Volume.

  • Availability Zone: Choose an availability zone in Singapore where you want the new volume to be created. Make sure it’s in the same availability zone as the EC2 instance where you will attach and mount the volume.

  • During the creation process, ensure the size, type, and availability zone match your requirements. The new volume will be created based on the snapshot data.

  • Once the volume is created, it will appear in the Volumes section.

5. Launch a New EC2 Instance in Singapore for Jenkins

Once you've created the EBS volume from the snapshot in Singapore, launch a new EC2 instance in Singapore for Jenkins.

  • Important: When launching the instance, make sure to select the same Availability Zone where the new EBS volume is located.

  • The EBS volume and the EC2 instance need to be in the same Availability Zone for them to work together. Don’t forget to attach the EBS volume to the instance during the launch process.

After the instance is running, you'll be able to attach and mount the EBS volume just like you did in Mumbai.

6. Attach and Mount the EBS Volume in Singapore

  • Once the new instance is running, attach the newly created volume (from the snapshot) to your EC2 instance in Singapore.

  • After attaching the volume, verify it by running the following command to check the attached volumes:

lsblk

No Need to Format: Since the volume already contains the Jenkins data (from the snapshot), you do not need to format the volume. Simply mount it to the /var/lib/jenkins directory.

7. Mount the volume

  • Run the following command to mount the volume to /var/lib/jenkins:
sudo mount /dev/xvdf /var/lib/jenkins

8. Update /etc/fstab for Automatic Mounting

  • To ensure the volume mounts automatically after reboot, follow these steps:

  • First, find the UUID of the volume:

sudo blkid

  • Runthe following command to open the file with the vi editor:
sudo vi /etc/fstab
  • Edit /etc/fstab to add the following line (replace <your-uuid> with the actual UUID of the volume):

UUID=<your-uuid> /var/lib/jenkins ext4 defaults,nofail 0 2

9.Verify the Mount of the External EBS Volume for Jenkins

To ensure that the external EBS volume is successfully mounted to /var/lib/jenkins, use the following command:

ls -lrth /var/lib/jenkins

This will display the files stored on the external volume, which should now include Jenkins data (e.g., job configurations and other files) from the Mumbai region. If everything is set up correctly, you'll see the same Jenkins data that was originally created on the Jenkins instance in Mumbai, confirming that the volume is mounted properly and the data migration was successful.

10. Change Permissions for Jenkins

  • Set the correct ownership for the /var/lib/jenkins directory:
sudo chown -R jenkins:jenkins /var/lib/jenkins
sudo systemctl daemon-reload

11. Start Jenkins on the Singapore Instance

  • Start the Jenkins service on the Singapore EC2 instance:

  • Check the status of Jenkins to ensure it’s running properly:

      sudo systemctl start jenkins
      sudo systemctl status jenkins
    

12. Access Jenkins in Singapore

  • Open your browser and go to the Jenkins URL in the Singapore region.

  • You should be able to see your old Jenkins jobs that were created in Mumbai, now running on the new instance in Singapore.

Conclusion and Cleanup Reminder

In this project, we successfully migrated Jenkins data across AWS regions from Mumbai to Singapore using EBS volumes and snapshots. Here’s a brief summary of the steps:

  1. Jenkins Setup in Mumbai: Jenkins was installed on an EC2 instance in Mumbai, with external EBS volumes configured to store application data.

  2. Snapshot and Migration: A snapshot of the EBS volume in Mumbai was created and copied to the Singapore region, where a new EBS volume was created from it.

  3. Jenkins Setup in Singapore: A new EC2 instance in Singapore was launched, the volume was attached and mounted, and Jenkins was restored successfully.

  4. Access Jenkins in Singapore: We confirmed the migration was successful by accessing Jenkins in the Singapore region and finding all jobs intact.

Cleanup to Avoid Unnecessary AWS Charges After Practice

After completing the practice, it's important to clean up all AWS resources to avoid incurring unnecessary charges.