Introduction
Jenkins is a robust CI/CD tool that helps automate development workflows, testing, and deployments. In this guide, we’ll walk you through:
Installing Jenkins on an AWS EC2 instance.
Understanding Jenkins' real-time architecture.
Exposing Jenkins to the outside world.
Using Docker as an agent instead of worker nodes.
Advantages of using Docker as an agent.
Step 1: Installing Jenkins on an EC2 Instance
Prerequisites:
AWS account.
Basic knowledge of AWS EC2 setup.
A running EC2 instance (Ubuntu preferred).
Instructions:
Launch EC2 Instance:
Log into the AWS Management Console.
Create an EC2 instance with Ubuntu 22.04 (or similar).
Ensure the instance has a security group allowing traffic on ports
22
(SSH) and8080
(Jenkins).
Update System Packages:
sudo apt update && sudo apt upgrade -y
Install Java (Jenkins Prerequisite):
sudo apt install openjdk-11-jdk -y java -version
Add Jenkins Repository and Install Jenkins:
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.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-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt update sudo apt install jenkins -y
Start Jenkins Service:
sudo systemctl start jenkins sudo systemctl enable jenkins
Access Jenkins:
Visit
http://<Your-EC2-Public-IP>:8080
.Retrieve the admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Complete the setup by following the on-screen instructions and installing suggested plugins.
Step 2: Exposing Jenkins to the Outside World
To make Jenkins accessible externally:
Edit the Security Group Rules:
- Allow inbound traffic on port
8080
in the EC2 instance's security group.
- Allow inbound traffic on port
Ensure Jenkins Listens on All Interfaces:
Modify the Jenkins configuration file:
sudo nano /etc/default/jenkins
- Set
HTTP_HOST=0.0.0.0
.
- Set
Restart Jenkins:
sudo systemctl restart jenkins
Now, you can access Jenkins using the public IP of the EC2 instance.
Step 3: Jenkins Architecture in Real-Time
Jenkins employs a master-agent architecture:
Master Node: Manages configurations, schedules jobs, and monitors agents.
Agent Nodes: Execute tasks such as builds and tests.
In traditional setups, agents run on separate VMs or physical machines. Jenkins communicates with these nodes over SSH or via Jenkins agents.
Step 4: Using Docker as Jenkins Agents
Instead of relying on VMs as agents, Docker containers can dynamically serve as Jenkins agents.
Benefits of Docker Agents Over VMs:
Dynamic Provisioning: Containers spin up and down on-demand.
Lightweight: Use fewer resources than VMs.
Consistency: Standardized environments with container images.
Simplicity: Easier to configure and manage dependencies.
Configuring Docker Agents:
Install Docker on Jenkins Master:
sudo apt install docker.io -y sudo usermod -aG docker jenkins sudo systemctl restart jenkins
Install Docker Pipeline Plugin in Jenkins:
- Go to Manage Jenkins > Plugins Manager > Available Plugins and install the Docker Pipeline Plugin.
Configure Docker Host in Jenkins:
Navigate to Manage Jenkins > Manage Nodes and Clouds > Configure Clouds.
Add a new Docker Cloud and specify Docker host settings (
unix:///var/run/docker.sock
).
Define Docker Agents in Jenkins Pipelines:
Example Pipeline:pipeline { agent { docker { image 'maven:3.8.6-openjdk-11' } } stages { stage('Build') { steps { sh 'mvn --version' } } } }
Step 5: Advantages of Using Docker as Agents
Cost Efficiency:
Containers consume fewer resources, reducing the overall cost compared to VM-based agents.Scalability:
Docker containers can be spun up and torn down on-demand, ensuring resource optimization during peak workloads.Isolation:
Each container has its environment, eliminating dependency conflicts across builds.Flexibility:
Developers can customize images to include specific tools and configurations, enabling repeatable and consistent builds.Ease of Maintenance:
Upgrading or modifying agents is simple—just rebuild the container with the updated image.
Conclusion
By combining Jenkins with Docker as agents, you can achieve a scalable, resource-efficient, and flexible CI/CD infrastructure. This approach not only simplifies agent management but also integrates seamlessly with modern containerized environments like Kubernetes, paving the way for advanced DevOps workflows.
Dive into Jenkins with Docker today, and elevate your CI/CD game! 🚀