Launch Kubernetes Cluster through kubadm and Run a MySQL pod

Table of contents

Welcome to day 31 of the #90DaysOfDevOps challenge initiated by Shubham Londhe . In today's blog post, we'll dive into the process of launching a Kubernetes cluster using kubeadm, with one master node and one worker node. Once the cluster is up and running, we'll also deploy a MySQL pod on the cluster. Let's get started!

Prerequisites: Before we begin, ensure that you have the following prerequisites in place:

  1. Two Linux-based machines (physical or virtual) with Ubuntu 18.04 or higher installed. In this project, I am using Ubuntu 20.04 with Instance type t2.medium.

To set up a Kubernetes cluster through kubadm, follow these steps:

Step 1: Launch Two Instances One is Master and the other a worker

I am connecting to my CMD through ssh as follows.

Step 2: Install Docker on both Master and Worker.

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

Step 3: Install kubadm on both Master and worker.

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
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list

Step 4: Again update the system.

sudo apt update -y

Step 5: Install Kubeadm, Kubectl and Kubelet in both Master and Worker.

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

Step 6: Initialize the master node by running the following command:

Execute the following commands only on Master.

kubeadm init

After the initialization process completes, make a note of the kubeadm cluster export command displayed in the output. We will need it to export the configurations to use the cluster.

As we are the root user, we need to run the following command in the master only.

export KUBECONFIG=/etc/kubernetes/admin.conf

Step 7: Configure Networking on Master:

Install a pod network add-on to enable networking between pods. In this example, we'll use weave-daemonset-k8s as the network plugin.

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

Step 8: Print a token for node connection on Master:

kubeadm token create --print-join-command

Step 9: Reset the Kubadm checks on Worker so that we can join the fresh server:

kubeadm reset pre-flight checks

Step 9: Provide the token to the Worker:

Paste the join command copied from Master onto the worker node and append --v=5 at the end.

To get this(above) console output first we need to follow the step 10.

Step 10: Open port 6443 on the Master instance:

This is done so that Workers can connect to the Master through this port.

Step 11: Verify the nodes by running the command in Master:

kubectl gets nodes

Create first MySQL Pod :

  1. Create a YAML file named mysql-pod.yaml and paste the following content into it.

apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod
spec:
  containers:
  - name: mysql
    image: mysql:latest
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: yourpassword
    ports:
      - containerPort: 3306
  1. Deploy the MySQL pod by running the command:
kubectl apply -f mysql-pod.yaml
  1. Verify that the pod is running using the command on Master:
kubectl get pods

  1. Verify that the image is created using the command on Worker:
docker ps

Also, we can see that we can bash into the container using the container id from the above command.

Congratulations! We have successfully launched a Kubernetes cluster using kubeadm, consisting of one master node and one worker node. Additionally, we have deployed a MySQL pod on the cluster. This demonstrates the power and flexibility of Kubernetes for managing containerized applications and services.

Remember to clean up your cluster by deleting the resources once you're done experimenting to avoid unnecessary resource consumption. Use the kubectl delete command to remove the deployed pod and other resources.

In the next blog post, we will explore more advanced topics in the realm of DevOps. Also, I will be deploying two projects using K8s. So, stay tuned and let me know if there is any correction.