PART 2
In Part 1 of this 5-part blog, we already set up the SSH port in the default security group.
We also created a key pair which will be used for logging into the ec2 instance.
In this blog we will do the following:
- Select the appropriate “ubuntu” ami (Amazon Machine Image). This is the image we will be using for the initial configuration.
- Please select the appropriate ec2 instance type for launching the instance for our initial configuration. For our initial OS configuration, we will be using a “t2.micro”.
STEP 1: INSTALL AND CONFIGURE THE OS (Ubuntu)
On the EC2 page
On the left tab — Images — AMI catalogue
In the AMIs search tab enter — ubuntu (x86_64)
This will give you multiple options, but we will use the below ubuntu 18.04 LTS “ami” as it’s FREE.
Select this AMI. This AMI is Free Tier, which means we will not pay for using this AMI, we will only be paying for the underlying VM that is deployed.
Click continue.
Next click the “Launch Instance with AMI” (Marked in RED)
This opens the “Launch an instance” page. On this page there are some important values that need to be specified which are mentioned below:
Name and tags
- Name–> ubuntu-base-os (You can give any name to the EC2 instances you like)
Application and OS Images (Amazon Machine Image)
- AMI from catalog It should pick up the previously selected Ubuntu image selected earlier.
Instance type
- Instance type –> “t2.micro” ( we will use this for setting up the OS)
- Key pair (login)–> “jenkins” (use the key-pair created in Step 1: Configuring key-pair)
Network settings
- VPC –> use the default settings.
- Subnet –> No preference (Default)
- Auto-assign public IP –> Enable
- Firewall (security groups) –> Select existing security group –> Security groups –> default (Configured with SSH port in Step 2: Configuring security groups)
Configure storage
- 1 x 10 GiB –> io1
Advanced details
- No configurations are required here.
Summary
The summary should look as below. Click the Launch instance once you check your settings.
Go back to the EC2 page and wait for the instance to be in a running state. In the EC2 dashboard, click on the instance and note down the below public IP address:
We will now log in to the EC2 instance using SSH. In macOS you can use the terminal shell or any other application for SSH.
In Windows OS, you can use PUTTY or any other application to SSH into the EC2 instance.
(Important: We are using “jenkins.pem” as this is the key-pair we had specified when creating the EC2 instance)
We use a macOS, so on your local computer, go to the directory where the “jenkins.pem” file was downloaded.
We will be using the default “ubuntu” user to login and perform the initial setup.
$ chmod 400 jenkins.pem
(syntax for login = ssh <user>@<aws_public_ip> -i <pem file name>)
$ ssh ubuntu@52.66.233.40 -i jenkins.pem
- Type “yes” at the prompt
This will log into the EC2 instance as user “ubuntu”.
Check your username when you are logged into the EC2 instance.
$ who am I
ubuntu pts/0 2023-03-15 10:18
We will now create a new user “jenkins” which will be the administrator for the docker and Kubernetes. We don’t use the default “ubuntu” user for below reasons:
- It is a default user and it can be a security risk to use this user. In fact, once the configuration is complete, this use should be disabled.
- In addition, the “ubuntu” user does not have a password.
STEP 2: CREATE A NEW USER FOR ADMINISTRATING DOCKER & KUBERNETES
The user that installs docker should have a userid of 1000 in Ubuntu and also have admin permissions. So we will create a new user “jenkins”, assign that a userid of 1000, move the default user “ubuntu” to another userid and then add admin permissions to “jenkins” user.
In the EC2 instance do the following:
$ sudo su ### Login as root to create a new user
Create a new user “jenkins” with passwd: 12345678 (you can provide a strong password for your user)
# adduser jenkins # create user jenkins
Provide the password “12345678”, and re-enter the password. Provide the necessary user details at the prompt. Or press <enter> for defaults. Run the below commands:
# sudo su – jenkins # login as jenkins user in /home/jenkins
$ cd ~
$ mkdir .ssh
$ chmod 700 .ssh
$ cd .ssh
$ vi authorized_keys ## paste the output of ssh-keygen, done in part1 of the blog.
The ssh-keygen output is below, but it will different for you as your ssh-keygen will give a different output:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCePm7WMuB4ZykXkQkn0fTMSfSlMFWFZ6j5hFhQgk72SgRScKf7twk0A/f5fsaYAZD1a
Q+nREXVGyrozdYyuW2qQDMr+GaUpPsJcr7NcJ/khthcJz1XUqI3VRuDTBNaNxBzXgITiXbkB8PF1JrXOiUY87lQjxqWOVFZ94W2hW
+jimtXcZiIdQpNmE7VtE85BF9Tzi/TmgFQ5Ax6fdCGCX4tlcnamlB7Nm10PhpTJeEkldZpD61vHkaoljucJ4DTm4lXOVVRVmbzpR7Re
BVE/oPUM+qSdKcm9CIw0M5bWvouaSvuBD9qYGRwlCF837W6KTZRmv/hqpjwCrbdDf+eUA0V
$ chmod 600 authorized_keys
Login as user “ubuntu” to add “jenkins” user to below group
$ sudo usermod -aG adm jenkins ### Adds user “jenkins” to the admin group ###
Exit from the ec2 instance. Now “jenkins” user should be able to login to the ec2 instance.
Login as “jenkins” user to modify the “ubuntu” user’s uid & gid from 1000 to 2000
$ sudo usermod -u 2000 ubuntu
$ cat /etc/passwd
Output of /etc/passwd –> ubuntu:x:2000:1000:Ubuntu:/home/ubuntu:/bin/bash
$ sudo groupmod -g 2000 ubuntu
$ cat /etc/passwd
Output of /etc/passwd –> ubuntu:x:2000:2000:Ubuntu:/home/ubuntu:/bin/bash
Exit as “jenkins” user and once again login as “ubuntu” user.
Login as “ubuntu” user.
$ sudo usermod -u 1000 jenkins
$ cat /etc/passwd
Output of /etc/passwd –> jenkins:x:1000:1001:Jenkins:/home/jenkins:/bin/bash
$ sudo groupmod -g 1000 jenkins
$ cat /etc/passwd
Output of /etc/passwd –> jenkins:x:1000:1000:Jenkins:/home/jenkins:/bin/bash
Now the “jenkins” user has been added, with userid/groupid = 1000 and “ubuntu” user has been moved to a userid/groupid = 2000.
IMPORTANT: From here on we will do all the installation & configuration, using the “jenkins” user.
STEP 3: CREATING AN AMI OF THE BASE-OS
You can take an AMI of this instance and use it for any future Ubuntu base OS operations.
How to create an AMI of an EC2 instance.
Go to EC2 page –> click on the instance –> click the “Actions” tab –> Image and template –> Create image
This opens a new window. Update the below parameters:
Image name –> ubuntu-base-image (give any name that is suitable for you)
Image description –> ubuntu-base-image (give any name that is suitable for you)
Click the “Create image” tab
This completes the UBUNTU base-os installation.
<– Part 1: Introduction & Pre-requisites
Part3: Installing docker components –>