Deploying a Kubernetes Cluster with Nginx using Kubeadm

Deploying a Kubernetes Cluster with Nginx using Kubeadm

Kubernetes is a powerful open-source container orchestration system used to automate the deployment, scaling, and management of containerized applications. In simpler terms, Kubernetes helps manage and run applications packaged in containers, which are self-contained units of software that can run consistently across different computing environments.

Kubernetes helps automate tasks such as deploying containers, scaling them up or down based on demand, and rolling out updates without downtime. It provides a centralized platform for managing containers and their associated resources, making it easier to deploy and manage applications at scale. With Kubernetes, developers and IT teams can focus on building and delivering applications, while the platform takes care of the underlying infrastructure.

Deploy a Kubernetes Cluster for 3 Nodes

Setup an AWS EC2 Instance

We need 3 EC2 Instances (Master and Test)

Log in to an AWS account using a user with admin privileges and ensure your region is set to us-east-1 N. Virginia.

Move to the EC2 console. Click Launch Instance.

For name use Kube-master

For name use Kube-slave01

For name use Kube-slave02

Select AMIs as Ubuntu and select Instance Type as t2.medium. Select my keypair and select the default security group

Installation Kubernetes in Master and Slave Instances

Commands that are commonly used in both machines

sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt update
sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

Check the latest documentation

We installed Docker

Docker is an open-source platform for building, packaging, and deploying applications in containers. Containers provide a lightweight and portable way to package and run software consistently across different computing environments, making it easier to develop, test, and deploy applications. With Docker, developers can package an application and its dependencies into a single container image that can be deployed anywhere that supports Docker, making it easier to build, test, and deploy applications quickly and consistently, regardless of the underlying infrastructure. Docker has become a popular choice for building and deploying modern, cloud-native applications due to its ease of use and portability.

We installed Kubeadm

Kubeadm is used to form the cluster or we can say it creates a cluster.

We installed Kubectl

Kubectl is used to control all the services which are present in Kubernetes.

We installed Kubelet

Kubelet is the security of the pods.

I am creating a script file to execute all the commands directly at once for that I have created a file

sudo nano install.sh

bash install.sh

On master, it is done now the same thing I have to repeat in my slaves.

Kube-master

I entered as a root user

sudo su
kubeadm init

Once we initialized this command it will generate a token and that generated token is pasted into the slave01 and slave02 machines to form the connection and create a cluster of these nodes.

Kube-Slave01

I entered this machine as a root user and now I am pasting the token directly here

Kube-slave02

I entered this machine as a root user and now I am pasting the token directly here

Both Kube-slave01 and Kube-slave02 has joined the Cluster.

Kube-master

I exited from my master machine

As we can see to start the cluster we need to execute three commands one by one which is shown in the above picture and I am mentioning these commands below

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

Deploy CNI after executing all three commands and this command will us to setup the networks we required to perform all the kubeadm functionalities

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

To check my nodes and my connection we use the command

kubectl get nodes -o wide

Create a nginx deployment of 3 replicas

Kube-master, we are creating a yaml file

sudo nano nginxdeployment.yml

In which we will write our yaml code

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
        ports: 
        - containerPort: 80

After saving this file, use the command to execute this file

kubectl create -f (yaml file name)

Now, my pods are ready to check pods use the command

kubectl get pods -o wide

Now, time to check whether our content is present inside these pods or not so use the command and IP of the pod

curl (IP)

Create a Service of type NodePort for Nginx Deployment

I am creating another yaml file to start the service

sudo nano nginxservice.yml
apiVersion: v1
kind: Service
metadata:
  name: my-nginx-deployment
spec:
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30008
  selector: 
    app: nginx

After saving the file use the command to execute the yaml script

kubectl create -f (file name)

To check we use the command

kubectl get svc

Check the NodePort service on the browser to verify

Kube-master

Kube-slave01

Kube-slave02

Change the Replicas to 5 for the Deployment

For performing this action we have two ways to do this task

First way

Open the previous file by using the command

sudo nano nginxdeployment.yml

We can directly modify here

Instead of directly 5, I'll make it 4 replica

After saving the file use the command

kubectl apply -f nginxdeployment.yml
kubectl get deploy

Second way

Use the command

kubectl edit deploy

To edit the script click to i button

I have made the changes, once you made the changes to save the file press (esc then :wq!)

To deploy the changes use the command

kubectl get deploy

Change the Service type to Cluster IP

NodePort

With the Nodeport we are exposing the service outside the browser.

Cluster IP

With the ClusterIP we specifically used to host the service internally.

Earlier we are using the NodePort service and now we to change it to Cluster IP my-nginx-deployment

kubectl edit svc

Don't forget to press i to get into the insert mode and for saving the file use (esc then :wq!)

Change nodePort null and type ClusterIP

kubectl get svc