Justin의 개발 로그

퍼온 곳


https://medium.com/konvergen/jenkins-for-node-js-app-on-aws-ec2-part-1-installing-jenkins-on-ec2-24675cc08998

Jenkins를 수동으로 설치하는 방법이 있어 퍼왔습니다.


다만, 저는 Jenkins를 자주 설치해서 사용하기 때문에, 

CloudFormation + Ansible로 IaC (Infrastructure as Code)로 구성해 두었습니다.

What are we going to build?

In this article series, we will build a simple Node.js app that hosted on AWS EC2 instance. Furthermore, we are going to configure automation server and host Jenkins on a separate AWS EC2 instance. Jenkins will help us to automate the CI/CD process. For every code change from our Node.js app repository, Jenkins will get notified and it will pull the changes into our Jenkins server, install dependencies and run the integration test. If all tests pass, Jenkins is going to deploy the app to the node server. If it fails, developer will be notified.

In this article?

  • EC2 Setup
  • Installing Jenkins
  • Configuring Firewall Settings
  • Setting up EC2 Security Groups
  • Complete Jenkins First Setup

Prerequisites:

Before we start there are some assumed preconditioned technological setups:

  • You have an Amazon Web Services account, if not please sign up, and for first time users you will be eligible for the free tier. The AWS Free Tier enables you to gain free, hands-on experience with AWS.
  • Your Amazon EC2 Key Pairs are set up, if not please click here to follow Amazon’s walkthrough on Creating a Key Pair Using Amazon EC2.

EC2 Setup

  • Log into your AWS console search for and select EC2
  • Select Launch Instance
  • Step 1: Choose an Amazon Machine Image (AMI) Select the free tiered Ubuntu Server 16.04 LTS
  • Accept the defaults for Step 2: Choose an Instance Type and choose Next
  • Accept the defaults for Step 3: Configure Instance Details
  • Once completed select Add Storage and accept the defaults for Step 4: Add Storage
  • Accept the defaults for Step 5: Add Tags
  • Step 6: Configure Security Group and choose Create a new security group
  • Confirm that port 22 is configured to allow access to your VM
  • Finally select Review and Launch
  • Step 7: Review Instance Launch

After you select Launch you will be prompted to Select an existing key pair or to create one

I have previously set up a key pair and I will select Choose an existing key pair

After you’ve launched your instance you can scroll to the bottom of the page and view your instance

If you are unfamiliar with how to connect to your instance AWS gives you a brief tutorial on how to do so. In your EC2 console view select Connect

You will be presented with the below guide on how to connect over SSH, please take notice of your IP address

The IP address of your virtual server can also be found in the EC2 console page

Installing Jenkins

Try to connect to your instance over SSH and then follow this steps below

Step 1: Install Java

sudo add-apt-repository ppa:webupd8team/java

Step 2: Download Oracle Java 8 Installer

sudo apt update
sudo apt install oracle-java8-installer

When you run the commands above you will be prompted to access the license terms of the software. Accept and continue

Step 3: Configure Oracle JDK8 As Default

sudo apt install oracle-java8-set-default

you could check your java version by running following command

javac -version

Step 4: Install Jenkins

Run the commands below to add Jenkins repository to your system. First add the repository key

cd /tmp && wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

Then run the commands below to add the repository

echo 'deb https://pkg.jenkins.io/debian-stable binary/' | sudo tee -a /etc/apt/sources.list.d/jenkins.list

After that, run the commands below to install Jenkins

cd
sudo apt update
sudo apt install jenkins

After installing Jenkins, the commands below can be used to stop, start and enable Jenkins to always start up when the server boots

sudo systemctl stop jenkins.service
sudo systemctl start jenkins.service
sudo systemctl enable jenkins.service

Lets verify that Jenkins has started successfully with the below command

sudo systemctl status jenkins

Configuring Firewall Settings

Jenkins runs on port 8080 by default, lets allow access to the port 8080 and also 22 to keep ssh access

sudo ufw allow 8080
sudo ufw allow 22

Now, lets check our firewall status

sudo ufw status

If you receive an inactive status after running the above command, run the below command to fix it

sudo ufw enable

Setting up EC2 Security Groups

Because our Jenkins server is on AWS, we will need to expose port :8080 in our security group

Lets jump back into the EC2 console view, select Security Groups from the left side scroll bar and choose Create Security Group

We will now add two Inbound rules to access our Jenkins service from the web browser (port :8080) and through the command line (SSH)

Return to the EC2 console and change your VM’s security group to the newly created Security Group

Uncheck the old security group (default) and assign the newly created one

Note: there is a possibility that your terminal session will be disconnected while changing security groups, if this occurs please reconnect via ssh

Complete Jenkins First Setup

Visit Jenkins on its default port 8080, using the server IP address http://ec2_instance_ip_address:8080

The IP address of your VM can be found in the EC2 console page.

Once you have successfully navigated to your VM’s public IP address in your web browser you will be prompted to Unlock Jenkins. So, jump back into your terminal session and run the below command for the initial password

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

Copy the password from your terminal and paste the password into the password field in your web browser and then select Continue

Select Install suggested plugins

When the installation is complete, you’ll be prompted to set up the first admin user. Go ahead and create the user

For Instance Configuration, if you don’t want to set the Jenkins URL, you could select Not now

Select Save and Finish

Note: If it’s just a blank page after you click Start using Jenkins. I suggest you accessing Jenkins with /restart to restart your Jenkins

http://JENKINS.SERVER.IP:8080/restart

Jenkins has been successfully installed!!!

What a good step for us. In next article we will start the Node.js app project to develop.


profile

Justin의 개발 로그

@라이프노트

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!