How to Deploy a Kubernetes Cluster & Create 3 Replica of Nginx Deployment — Kubernetes Assignment 1
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:
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”.
Step 2: Click on “Launch Instance”.
Step 3: Choose “Name” as “EC2” as of now. After creating the three instances, we will change the name.
Step 4: Choose “AMI” as “Ubuntu”. While “AMI Version” as “Ubuntu Server 22.04 LTS (HVM), SSD Volume Type”.
Step 5: Choose “Instance type” as “t2.medium”. While select “key pair (login)” as “Docker”.
Step 6: Click on “Edit” in “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”.
Choose “Type” as “All traffic” & “Source type” as “Anywhere”.
Step 8: In “Summary”, choose “Number of Instances” as “3”. Click on
“Launch Instance”.
Step 9: “Three new instances” will be successfully launched. Click on “Any Instance Hyperlink”.
Step 10: The “Instance” section will be opened. Click on “Cross Sign”.
Step 11: All three “EC2” instances will be shown.
Step 12: Now, we will rename all these instances. First one as “Master-EC2”, Second one as “Slave1-EC2” & third one as “Slave-2”.
B. Run the Given Kubernetes Installation Script to Master & Slaves Node
Step 1: Select the “Master-EC2” & click on “Connect”.
Step 2: Click on “Connect” again.
Step 3: Run the below-given command to update the machine. The machine will be successfully updated.
sudo apt-get update
Step 4: Create a file “install.sh” using the below-given command:
nano install.sh
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
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.
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”.
Step 10: Again, click on “Connect”.
Step 11: Update the machine using the below-given command:
sudo apt-get update
Step 12: Create a file “install.sh” using the below-given command & press “Enter” from keyboard.
nano install.sh
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
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.
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”.
Step 16: Again, click on “Connect”.
Step 17: Update the machine using the below-given command:
sudo apt-get update
Step 18: Create a file “install.sh” using the below-given command:
nano install.sh
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
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.
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.
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.
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 –
Step 4: Paste the above given token here & press “enter” from the keyboard.
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 –
Step 6: Paste the above given token here & press “enter” from the keyboard.
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.
Step 2: Run the below-given command to block the device or logical volume:
sudo swapoff –a
Step 3: Run the below-given command to deploy a network:
kubectl apply -f calico.yaml
Step 4: Run the below-given command to check that nodes are joined or not:
kubectl get nodes
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.
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
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
The deployment will be successfully created.
Step 4: Write the below-given command for finding the created deployment with given numbers:
kubectl get deploy
The Deployments will be successfully created mentioned in the assignment.
Step 5: If we run the below-given command:
kubectl get pods
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