How to install Jenkins in AWS EC2 instance
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In this series, we will explore one of the most popular CI/CD tool Jenkins. We will try to cover each aspect of Jenkins with a demo.
What is Jenkins?
Jenkins is a self-contained, open-source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.
Components of Jenkins:
- Master Node: The system where you install and run Jenkins.
- Managed Node: Target systems managed by Jenkins.
- Repository: Version controlled system where you keep your code.
- UI: User interface to manage and configure Jenkins.
- Pipeline: Means of continuous deployment of your code to target systems from the version control system.
- Plugins: Means of enhancing the functionality of a Jenkins.
Next, we are going to learn how to install Jenkins.
Jenkins Installation:
Prerequisite:
- One EC2 Amazon Linux 2 instance with internet access
- Java 8 runtime environments
1 2 3 4 5 6 7 8 9 10 11 12 |
##################################### ## Jenkins installation on AWS EC2 ## ##################################### ## Create an EC2 instance with Amazon Linux 2 AMI ## Amazon Linux 2 AMI (HVM), SSD Volume Type ## Connect to your EC2 instance ## Update all packages sudo yum update -y ## Install Java sudo yum install java-1.8.0-openjdk-devel |
There are several ways you can install Jenkins in your master node (EC2 instance in our case).
- Using Docker
- Using Yum
- Using a War file
I would prefer the 1st method, using docker which comes with blue ocean plugin preinstalled and this will be used in rest of the Jenkins blog series.
Using docker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
############################################################# ## Jenkins installation on AWS EC2 using docker blue ocean ## ############################################################# ## Install Docker. sudo yum install docker ## Add the ec2-user to the docker group so you can execute Docker commands without using sudo. ## Exit the terminal and re-login to make the change effective sudo usermod -a -G docker ec2-user exit ## Enable docker service sudo systemctl enable docker ## Start docker service sudo systemctl start docker ## Check the Docker service. sudo systemctl status docker ## Run docker jenkins blueocean container docker run \ -u root \ --rm \ -d \ -p 8080:8080 \ -p 50000:50000 \ --name myjenkin \ -v jenkins-data:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ jenkinsci/blueocean ## get the administrator password docker exec -it myjenkin bash cat /var/jenkins_home/secrets/initialAdminPassword exit #or from docker logs docker logs myjenkin |
Using Yum:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
############################################### ## Jenkins installation on AWS EC2 using YUM ## ############################################### ## Add Jenkins repo to your yum repository sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo ## Import a key file from Jenkins-CI to enable installation from the package sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key ## For Amazon Linux 2 sudo amazon-linux-extras install epel ## Install Jenkins sudo yum install jenkins -y ## Start and enable Jenkins service sudo systemctl start jenkins sudo systemctl enable jenkins sudo systemctl status jenkins ## Get the initial administrative password sudo cat /var/lib/jenkins/secrets/initialAdminPassword |
Using a WAR file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
################################################### ## Jenkins installation on AWS EC2 using war file## ################################################### ## Get the latest jenkins war file wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war ## Install jenkins using war file java -jar jenkins.war ## Get the initial administartive password from the output of the above command # Jenkins initial setup is required. An admin user has been created and a password generated. # Please use the following password to proceed to installation: # 2871112a4e704e3bbc9c2e0b2963c8c0 # This may also be found at: /home/ec2-user/.jenkins/secrets/initialAdminPassword |
Jenkins Configuration:
Step 1: Open your EC2 instance public DNS or public IP (http://<PUBLIC_DNS/PUBLIC_IP>:8080/) along with port 8080 in your favorite browser. And provide the administrative password obtained during the installation.
Step 2: Click ‘Install suggested plugins’.
Step 3: Create an administrative user.
Step 4: Our Jenkins configuration completed, click ‘Start using Jenkins’ to open the Jenkins UI.
Hope you have enjoyed this article. To install Jenkins on another platform, you can refer below Jenkins documentation
https://jenkins.io/doc/book/installing/
To get more details on Jenkins, please refer to Jenkins documentation.
How can I update the jenkins.war file on the Jenkins remote server? By clicking on “download” the new version of Jenkins, I get a .war file on my local computer but I need to update the remote server that is hosted on ec2 aws
ssh into your ec2 instance and update from there