How to Deploy a Kubernetes Cluster & Create 3 Replica of Nginx Deployment — Kubernetes Assignment 1

Visal Tyagi
DevOps-Guides
Published in
10 min readJul 9, 2024

You have been asked to:

● Deploy a Kubernetes Cluster for 3 nodes

● Create a nginx deployment of 3 replicas

Check the Git Hub Repository for this Assignment to Copy the Commands:

How to Deploy a Kubernetes Cluster & Create 3 Replica of Nginx Deployment — Kubernetes Assignment 1
How to Deploy a Kubernetes Cluster & Create 3 Replica of Nginx Deployment — Kubernetes Assignment 1

Problem (1) Solution: Deploy a Kubernetes Cluster for 3 nodes

Follow these steps to deploy the Kubernetes Cluster on Three Nodes:

A. Create Three Instances (Master, Slave1 & Slave2)

Step 1: Go to the “Services” section & search “EC2” there. Put cursor over “EC2” & click on “Instances”.

EC2 Search
Search EC2

Step 2: Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 3: Choose “Name” as “EC2” as of now. After creating the three instances, we will change the name.

Write instance name
Write instance name

Step 4: Choose “AMI” as “Ubuntu”. While “AMI Version” as “Ubuntu Server 22.04 LTS (HVM), SSD Volume Type”.

Ubuntu AMI
Ubuntu AMI

Step 5: Choose “Instance type” as “t2.medium”. While select “key pair (login)” as “Docker”.

Choose Instance Type & Key Pair
Choose Instance Type & Key Pair

Step 6: Click on “Edit” in “Network Settings”.

Edit Network Settings
Edit Network Settings

Step 7: Choose “Create security groups” in “Firewall (security groups)”. Put “Security group name — required” as “Assignment-Security Group”.

While in “Description — required”, write description as “Security Group for Kubernetes Assignment”.

Click on “Add security group rule”.

Add Security Group Rule
Add Security Group Rule

Choose “Type” as “All traffic” & “Source type” as “Anywhere”.

All Traffic
All Traffic

Step 8: In “Summary”, choose “Number of Instances” as “3”. Click on
“Launch Instance”.

Launch the Instance
Launch the Instance

Step 9: “Three new instances” will be successfully launched. Click on “Any Instance Hyperlink”.

Click Hyperlink to View the Instances
Click Hyperlink to View the Instances

Step 10: The “Instance” section will be opened. Click on “Cross Sign”.

Click on “Cross Sign” to View All Instances
Click on “Cross Sign” to View All Instances

Step 11: All three “EC2” instances will be shown.

Instances Running
Instances Running

Step 12: Now, we will rename all these instances. First one as “Master-EC2”, Second one as “Slave1-EC2” & third one as “Slave-2”.

Rename the EC2 Instances
Rename the EC2 Instances

B. Run the Given Kubernetes Installation Script to Master & Slaves Node

Step 1: Select the “Master-EC2” & click on “Connect”.

Select the Master Instance
Select the Master Instance

Step 2: Click on “Connect” again.

Connect to Master Instance
Connect to Master Instance

Step 3: Run the below-given command to update the machine. The machine will be successfully updated.

sudo apt-get update
Update the Master Machine
Update the Master Machine

Step 4: Create a file “install.sh” using the below-given command:

nano install.sh
Create an install.sh file
Create an install.sh file

Note: We are not using the sudo command because we are not working as a root user, only work as a normal user. Take care if you are working as a root user then use sudo otherwise start command without sudo. Otherwise, in the deployment creation, you will face the issue.

Press “Enter” from keyboard to open the “install.sh” file.

Step 7: Paste the below-given script in the “install.sh”. It will automatically install Docker & Kubernetes components.

sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl enable --now docker
sudo swapoff -a
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
sudo mkdir -p -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo systemctl enable --now kubelet
Paste the Docker & Kubernetes Installation Script
Paste the Docker & Kubernetes Installation Script

Do “CTRL+X” & Type “Yes” from the Keyboard. Press “Enter” from keyboard & “install.sh” file will be saved.

Step 8: Run the below-given command to start the installation:

bash install.sh

It will start the installation.

Run the bash script
Run the bash script
Script Running Complete
Script Running Complete

The script will be successfully installed Docker, Kubelet, Kubeadm & Kubectl.

Step 9: Go to the Instances & connect to the Slave1 Machine. First, select the Slave1-EC2 instance & click on “Connect”.

Select the Slave-1 Instance
Select the Slave-1 Instance

Step 10: Again, click on “Connect”.

Connect to the Slave1 Instance
Connect to the Slave1 Instance

Step 11: Update the machine using the below-given command:

sudo apt-get update
Update the Slave1 Machine
Update the Slave1 Machine

Step 12: Create a file “install.sh” using the below-given command & press “Enter” from keyboard.

nano install.sh
Create an install.sh file
Create the install.sh file

Step 13: Paste the below given script in the “install.sh” file. It will automatically install docker & kubernetes components.

sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl enable --now docker
sudo swapoff -a
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
sudo mkdir -p -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo systemctl enable --now kubelet
Paste the Docker & Kubernetes Installation Script
Paste the Docker & Kubernetes Installation Script

Do “CTRL+X” & Type “Yes” from the Keyboard. Press “Enter” from keyboard & “install.sh” file will be saved.

Step 14: Run the below-given command to start the installation:

bash install.sh

It will start the installation.

Run the Installation Script
Run the Installation Script
Docker & Kubernetes Successfully Installed
Docker & Kubernetes Successfully Installed

The script will be successfully installed Docker, Kubelet, Kubeadm & Kubectl.

Step 15: Go to the Instances & connect to the Slave2 Machine. First, select the Slave2-EC2 instance & click on “Connect”.

Select the Slave2 Instance
Select the Slave2

Step 16: Again, click on “Connect”.

Connect to Slave2 Instance
Connect to Slave2 Instance

Step 17: Update the machine using the below-given command:

sudo apt-get update
Update the Slave2-Machine
Update the Slave2-Machine

Step 18: Create a file “install.sh” using the below-given command:

nano install.sh
Create an install.sh file
Create the install.sh file

Press “Enter” from keyboard.

Step 19: Paste the below given script in the “install.sh” file. It will automatically install Docker & Kubernetes components.

sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl enable --now docker
sudo swapoff -a
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
sudo mkdir -p -m 755 /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo systemctl enable --now kubelet
Paste the Script in the Install.sh file
Paste the Script in the Install.sh file

Do “CTRL+X” & Type “Yes” from the Keyboard. Press “Enter” from keyboard & “install.sh” file will be saved.

Step 20: Run the below-given command to start the installation:

bash install.sh

It will start the installation.

Run the Bash Command
Run the Bash Command
Docker & Kubernetes Successfully Installed
Installation Complete on Slave2

The script will be successfully installed Docker, Kubelet, Kubeadm & Kubectl.

C. Join the Slave Nodes with Master Node to Form a Cluster

Step 1: Run the below-given command to generate the token for slave 1 & slave2:

sudo kubeadm init

It will provide you the token & commands.

Initialize the kubeadm
Initialize the kubeadm
Token Generated
Token Generated

Step 2: First, copy the given command & paste it into the master node. Press “enter” from the keyboard to operate the command successfully.

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

It creates a Kubernetes configuration file to the home directory.

Paste the Given Commands to Home Directory
Paste the Given Commands to Home Directory

Step 3: Copy the given token & first, go to the “Slave1-EC2” machine.

kubeadm join 172.31.10.143:6443 - token julbp8.6qlh2ha13nkb30lg \
- discovery-token-ca-cert-hash sha256:661b9935ffce1ab2f9bac23ac58acec8a746262b3ef321d571734b73496217b4

Note: This token will be different for you.

Login as a root user via using the below-given command: sudo su –

Login as a root user
Login as a root user

Step 4: Paste the above given token here & press “enter” from the keyboard.

Paste the Token & Slave1 Joins the Cluster Successfully
Paste the Token & Slave1 Joins the Cluster Successfully

Step 5: Copy the given token & first, go to the “Slave2-EC2” machine.

kubeadm join 172.31.10.143:6443 - token julbp8.6qlh2ha13nkb30lg \
- discovery-token-ca-cert-hash sha256:661b9935ffce1ab2f9bac23ac58acec8a746262b3ef321d571734b73496217b4

Login as a root user via using the below-given command: sudo su –

Login as a root user
Login as a root user

Step 6: Paste the above given token here & press “enter” from the keyboard.

Paste the Token & Slave2 Joins the Cluster Successfully
Paste the Token & Slave2 Joins the Cluster Successfully

Both nodes (Slave1-EC2 & Slave-2 EC2)are ready to join.

D. Set Up the Network Connection for Kubernetes Cluster

Step 1: Install the “Calico Network” to run the cluster using the below-given command:

curl https://raw.githubusercontent.com/projectcalico/calico/v3.27.2/manifests/calico.yaml -O

Run the below-given command to check the “calico.yaml” file.

ls

The “calico.yaml” file will be shown.

Install the Calico Network
Install the Calico Network

Step 2: Run the below-given command to block the device or logical volume:

sudo swapoff –a
Block the Device or Logical Volume
Block the Device or Logical Volume

Step 3: Run the below-given command to deploy a network:

kubectl apply -f calico.yaml
Create a Network
Create a Network

Step 4: Run the below-given command to check that nodes are joined or not:

kubectl get nodes
Kubernetes Cluster Not Ready
Kubernetes Cluster Not Ready
Kubernetes Cluster Ready
Kubernetes Cluster Ready

Problem (2) Solution: Create a nginx deployment of 3 replicas

Step 1: Create a “deployment.yaml” file using the below-given command:

sudo nano deployment.yaml

Press “enter” from the keyboard.

Create the deployment.yaml file
Create the deployment.yaml file

Step 2: The deployment.yaml will be opened & paste this content in the “deployment.yaml” file.

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1
ports:
- containerPort: 80
deployment.yaml file code
deployment.yaml file code

Do “CTRL+X” & Type “Yes” from the Keyboard. Press “Enter” from keyboard & the “deployment.yaml” file will be saved.

Step 3: Write the below-given command to run the “deployment.yaml” file:

kubectl apply –f deployment.yaml
deployment created
deployment created

The deployment will be successfully created.

Step 4: Write the below-given command for finding the created deployment with given numbers:

kubectl get deploy
Run the Deployment Command
Run the Deployment Command
Three Deployments Available
Three Deployments Available

The Deployments will be successfully created mentioned in the assignment.

Step 5: If we run the below-given command:

kubectl get pods
Run the Pods Command
Run the Pods Command
Pods are Running
Pods are Running

All pods will be in the running state.

Next Kubernetes Assignments:

How to Create A NodePort Service & Deploy NGINX on It — Kubernetes Assignment 2

How to Change the Replicas for the Deployment — Kubernetes Assignment 3

How to Change the NodePort Service to Cluster IP — Kubernetes Assignment 4

How to Deploy Ingress Service Using MiniKube in Kubernetes — Kubernetes Assignment 5

How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study

--

--