Easy-to-follow k8s cluster setup using kubeadm and minikube๐๏ธโ๏ธ
๐ Introduction
Hello readers! In our previous blog, we discussed Kubernetes, its architecture, and its benefits. While Kubernetes is popular, setting up a cluster can be quite challenging.
Today, we will explore two different methods for setting up a Kubernetes cluster. One method is using kubeadm, which creates a full-fledged cluster with a control plane and worker nodes. The other method involves using Minikube, which is a pared-down version of Kubernetes that gives you all the benefits of Kubernetes with a lot less effort all of the components of a k8s cluster and is created within a single docker container.
โ๏ธ Prerequisites
Before performing the installation, make sure that you have the following points covered to ensure a smooth installation and working.
two ec2 instances with at least t2.medium instance type because we require two cpus for our cluster to run.
you must execute commands with the user that has sudo privileges.
โ๏ธ Installation using kubeadm โ๏ธ
Kubeadm is a tool designed to offer kubeadm init and kubeadm join as best-practice, fast-track methods for creating Kubernetes clusters. It carries out the essential actions needed to establish a minimally viable cluster. By design, kubeadm focuses solely on bootstrapping and does not concern itself with provisioning machines.
๐ธ Setting up the control plane (master node) ๐๏ธ
Initial Steps
Since k8s requires a container runtime to create containers, we first need to install Docker. Execute the following commands on both the master and worker nodes to prepare them for Kubeadm:
apt update -y # install docker apt install docker.io -y # start docker service systemctl start docker # enable docker to run on system startup systemctl enable docker
Add Kubernetes Repository and Install Kubeadm
In this step, we will be adding Kubernetes repository and then installing kubeadm, kubectl, and kubelet. Execute the following commands to achieve the former.
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 apt update -y apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
Initializing the control plane
kubeadm init
Setting up kube config
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Add weave net plugin
Weave Net provides a network to connect all pods, implementing the Kubernetes model. Kubernetes uses the Container Network Interface (CNI) to join pods onto Weave Net.
Kubernetes implements many network features on top of the pod network. This includes Services, Service Discovery via DNS and Ingress into the cluster. WeaveDNS is disabled when using the Kubernetes addon.
Execute the following command to add the Weavenet plugin.
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
Generate a token to add worker nodes
To register a node as a worker in a control plane we need a token. Executing the following command on the control plane will generate the new token as well as the join command which you can execute at your worker node to join the cluster.
kubeadm token create --print-join-command
๐ธ Setting up worker nodes ๐ทโโ๏ธ
Reset kubeadm and join the worker node.
This is a step to reset all changes if we have accidentally executed any command to create a control plane on the worker node.
kubeadm reset pre-flight checks
Execute the join command on the worker node that you got while generating the join token on the control plane:
kubeadm join <master-node-ip>:6443 --token <token> --discovery-token-ca-cert-hash <hash> --v=5
To verify if the worker node has joined the cluster execute the following command:
kubectl get nodes
Labelling your worker nodes:
Optionally we can label our worker nodes with names to identify them for our specific use cases.
kubectl label node <node-name> node-role.kubernetes.io/worker=worker
To confirm the successful configuration of your worker nodes, you can run a demo pod to ensure that everything is functioning correctly.
โ๏ธ Quick installation using minikube โกโก
For a quick installation, we can use the minikube, which will run a single-node Kubernetes cluster locally for development and testing purposes.
Initial Steps
apt update -y # install docker apt install docker.io -y # start docker service systemctl start docker # enable docker to run on system startup systemctl enable docker
Install supporting packages:
# basic required packages apt install -y curl wget apt-transport-https
Install minikube
# install minikube curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 # make minikube executable and move to the /user/local/bin directory chmod +x minikube sudo mv minikube /usr/local/bin/
Install kubectl
This is a command line tool to manage the control plane and worker nodes
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" # make minikube executable and move to the /user/local/bin directory chmod +x kubectl sudo mv kubectl /usr/local/bin/
minikube commands
The commands will help you manage minikube cluster.
# start minikube minikube start --driver=docker # check cluster status minikube status # stop minikube minikube stop # delete minikube minikube delete
โ๏ธ Let's run a pod in k8s cluster
A pod is the smallest object in Kubernetes. We can create it using the kubectl command, where we need to provide all the specifications for the pod.
However, there is an alternative method in which we input all the specifications in a YAML file and pass it to kubectl. This approach is quite convenient, and that's exactly what we'll be doing!
Create two files, nginx-pod.yaml and nginx-service.yaml, with the content provided below.
# nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx # Add this label
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx # Use the correct label
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
After creating nginx-pod.yaml
and nginx-service.yaml
run the following commands:
kubectl create -f nginx-pod.yaml
kubectl create -f nginx-service.yaml
To access the nginx web page first execute the following command to get the port of the service that is exposing the pod kubectl get service nginx-service
This will produce the following output:
Use the port number beside 80: (30629 in this case) to access the nginx webpage which will be as follows.
๐ Conclusion
Awesome, now you can set up a fully-fledged k8s cluster as well as a mini version of a cluster which can prove very useful for local development. But keep in mind that minikube is never used in production.
In the next blogs we will discover how to run applications, how networking works on kubernetes and much more, so stay tuned and keep learning.
Subscribe to my newsletter
Read articles from Yashraj Jaiswal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Yashraj Jaiswal
Yashraj Jaiswal
Hey there! ๐ I'm Yashraj Jaiswal, a friendly web developer who's super excited about diving into the world of DevOps. I love coding and can't wait to learn more every day. As I go on this adventure, I'll be sharing my thoughts and discoveries about DevOps on my Hashnode blog. So, come along and let's explore the amazing world of web development and DevOps together! Don't forget to connect and share your own experiences too! ๐