Master Kubernetes: A Beginner's Guide to Setting Up a Cluster for Your Projects

Master Kubernetes: A Beginner's Guide to Setting Up a Cluster for Your Projects

Introduction:

Welcome to the exciting world of Kubernetes! If you're a developer looking to level up your game, you've come to the right place. In this step-by-step guide, we'll walk through setting up a Kubernetes cluster to host your Node Todo-App. Don't worry if you're new to Kubernetes – we'll break it down into simple, easy-to-understand steps.

How to setup kubernetes Cluster ?

We will be going step by step in order to set up the k8s cluster, For now we will be going to AWS cloud services.

Here we will take 2 instance of t2.medium as it needs more than one cpu and 4gbi memory. After running both the instance kindly name them differently as in my case the instance on which i will be making the master node is named as "Kuberentes Master" and worker node is called as "Kubernetes Worker" as shown below.

Connect your both instances using terminal of your local machine side by side. After connecting it kindly run these commands on both Master and Worker nodes.

Run the following commands on both the master and worker nodes to prepare them for kubeadm.

# using 'sudo su' is not a good practice.
sudo apt update
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo apt install docker.io -y

sudo systemctl enable --now docker # enable and start in single command.

# Adding GPG keys.
curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg

# Add the repository to the sourcelist.
echo 'deb https://packages.cloud.google.com/apt 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

After running these commands on your terminal on both Master and Worker instances, We will step up instances one by one.

Firstly the "kubernetes Master" node:

Master Node

  1. Initialize the Kubernetes master node.
sudo kubeadm init
  1. Set up local kubeconfig (both for root user and normal user):
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  1. Apply Weave network:
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
  1. Generate a token for worker nodes to join:
sudo kubeadm token create --print-join-command
  1. Expose port 6443 in the Security group for the Worker to connect to Master Node.

Worker Node

  1. Run the following commands on the worker node.
sudo kubeadm reset pre-flight checks
  1. Paste the join command you got from the master node and append --v=5 at the end. Make sure either you are working as sudo user or use sudo before the command.

Verify Cluster Connection

On Master Node:

kubectl get nodes

After this verification, you will get the nodes running.

Therefore, your both Master and worker nodes are interconnected successfully.

Making of DIR

kindly create a new directory named "Project"

cd into it.

Create one more directory inside Project dir called "todo-app"

kindly cd into it.

From here we will create three .yml files which are

  1. pod.yml

  2. deployment.yml

  3. service.yml

Creation of Namespace

what is Namespace ?

a namespace is a way to divide cluster resources between multiple users, teams, or applications. It serves as a virtual cluster within a physical cluster, allowing you to create isolated environments for different purposes.

kubectl create namespace todo-namespace

you can see the namespace created by command

kubectl get namespace

There will be different default namespaces present already and its fine. Your newly created namespace will be listed somewhere at the bottom.

The main aim of creating namespace is to make those three yaml files into namespace we created i.e "todo-namespace".

POD:

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

The shared context of a Pod is a set of Linux namespaces, cgroups, and potentially other facets of isolation - the same things that isolate a container. Within a Pod's context, the individual applications may have further sub-isolations applied.

A Pod is similar to a set of containers with shared namespaces and shared filesystem volumes.

How to create a pod ?

vim pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: todo-app
  namespace: todo-namespace
spec:
  containers:
    - name: todo-app
      image: trainwithshubham/node-app-test-new
      ports:
        - containerPort: 8000

Note: Here image has been taken from dockerHub( trainwithShubham/node-app-test-new).

after creating and saving this pod.yml.

Apply it forcefully to create and run the node, following code to run in command is

kubectl apply -f pod.yml

Deployment:

A Deployment provides declarative updates for Pods and ReplicaSets.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

How to Create a Deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-deployment
  namespace: todo-namespace
  labels:
    app: todo-label
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo-label
  template:
    metadata:
      name: todo-app
      labels:
        app: todo-label
    spec:
      containers:
      - name: todo-container
        image: trainwithshubham/node-app-test-new
        ports:
          - containerPort: 8000

after creating and saving this deployment.yml file, kindly apply it using the command

kubectl apply -f deployment.yml

Services:

In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.

The Service API, part of Kubernetes, is an abstraction to help you expose groups of Pods over a network. Each Service object defines a logical set of endpoints (usually these endpoints are Pods) along with a policy about how to make those pods accessible.

How to create a service file:

apiVersion: v1
kind: Service
metadata:
  name: todo-service
  namespace: todo-namespace
spec:
  type: NodePort
  selector:
    app: todo-label
  ports:
    - port: 80
      targetPort: 8000
      nodePort: 30007

apply this service.yml file using the followin command

kubectl apply -f service.yml

After successfully applying all the .yml files you will see the following number of replicas created + one original node and its copies. You can see from the below image the command used to see the pods inside the specific namespace.

Note

If we simply run the command

kubectl get pods

we wouldn't get any result. Therefore, the command should be

kubectl get pods -n todo-namespace

here -n stands for name of namespace that you have created earlier following the name as shown above.

Ports Opening:

Head on to AWS on master node, open the following ports as we have used in the service.yml file such as targetPort:8000 and nodePort:30007.

Move towards the "Kubernetes Worker" instance, copy the public ip address of the following node and paste it succeeding the nodePort number in new tab of browser in following way:

172.8.34.67:30007

Congratulations! You will see your Todo List App.

Conclusion

Kubernetes may seem daunting at first, but with patience and practice, you'll soon master it like a pro. So go ahead, experiment, and unleash the full potential of your applications with Kubernetes!

"The future belongs to those who believe in the beauty of their dreams." - Eleanor Roosevelt

Did you find this article valuable?

Support CodeAryan by becoming a sponsor. Any amount is appreciated!