Launching your First Kubernetes Cluster with Nginx running
Day 31 of 90daysofdevops
What is minikube?
Minikube is a tool that quickly sets up a local Kubernetes cluster on macOS, Linux, and Windows. It can deploy as a VM, a container, or on bare metal.
Minikube is a pared-down version of Kubernetes that gives you all the benefits of Kubernetes with a lot less effort.
This makes it an interesting option for users who are new to containers, and also for projects in the world of edge computing and the Internet of Things.
Features of minikube
Supports the latest Kubernetes release (+6 previous minor versions)
Cross-platform (Linux, macOS, Windows)
Deploy as a VM, a container, or on bare-metal
Multiple container runtimes (CRI-O, containerd, docker)
Direct API endpoint for blazing-fast image load and build
Advanced features such as LoadBalancer, filesystem mounts, FeatureGates, and network policy
Addons for easily installed Kubernetes applications
Supports common CI environments
Define Pod
Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.
A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.
A Pod's contents are always co-located and co-scheduled, and run in a shared context. A Pod models an application-specific "logical host": it contains one or more application containers that are relatively tightly coupled.
Types of Installation of K8s
Mini Kube (Docker Inside Docker DIND) → least use in Prod → Easiest
Kubeadm :→ Baremetal (open-source tool) → Used in Prod → Intermediate
Managed K8s Cluster
AWS → EKS (Elastic Cloud Kubernetes)
Azure → AKS (Azure Kubernetes Service)
GCP → GKE (Google Kubernetes Engine)
KIND (Kubernetes in Docker)
Task 1: Install minikube on your local
Create a new VM instance having 2 CPUs, 4GB of free memory, 20 GB of free disk space.
When creating a new EC2 instance select t2.medium.
Install Docker in your system.
sudo apt update -y sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker sudo systemctl status docker
Add the user to the docker group
sudo usermod -aG docker $USER && newgrp docker
Install Minikube in system.
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube
And then install Kubelet.
sudo snap install kubectl --classic
Start Minikube as per the image and check Minikube status
minikube start --driver=docker
Check if minikube cluster has been set up successfully or not by checking pods or namespace status.
kubectl get pods kubectl get namespace
Task 2: Create your first pod on Kubernetes through minikube.
To create a pod, we have to write a YAML file which is a.k.a Manifest file. So to create a pod for NGINX we have to pass the values & attributes in key-value format.
In the manifest file, we are passing values:
apiVersion → Kubernetes Version
Kind → Type of deployment
metadata → More Details about pod
container → Details of containers in object
containerPort → The port where the pod will deploy
apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
Run the kubectl command to create a pod.
kubectl apply -f pod.yml
Check the pod's status by kubectl get pods, you can see a NGINX pod is created successfully by it's status
kubectl get pods
Run the kubectl get pods -o wide command to get more detailed information about the pod-like IP, node, age of node, and status.
To check if nginx is running locally or not, do we have to ssh the minikube go inside the minikube cluster. Then curl the IP address of the pod.
#Get the IP kubectl get pods -o wide # SSH into minikube minikube ssh # Curl the IP address to access the NGINX curl http://<IP-Addr>
Task 3: Create NGINX pod on K8s through Kubeadm
Create 2 VM instances for Master and Node.
Install Docker on both Master & Node
sudo apt update -y sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker sudo systemctl status docker
Install Kebeadm on both master and node.
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
Again update the system.
sudo apt update -y
Install Kubeadm,Kubectl and Kebelet in both Master and Node.
sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
Connect Master with Node:
Initialized Kubeadm:
Run the following command only on Master:
sudo su kubeadm init
Setup the kubeconfig for the current user
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Finish the Master Setup using the following Command:
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
Now create a token to join the Master & Node connection
kubeadm token create --print-join-command
We will get nodes for master
After that open port 6443 in master
Then on the Worker Node reset the checks so it can't assign as Master.
sudo su kubeadm reset pre-flight checks
Paste the Join command on the worker node and append --v=5 at the end
Verify by running the command in Master:
kubectl gets nodes
Create the Nginx Pod
By default, the kubectl run command creates a deployment and a replica set along with the pod. If you only want to create a pod without creating a deployment or replica set, you can use the --restart=Never flag.
But if you pass --restart=Always, if your pod is deleted or having an issue, then a new pod will be replaced immediately.
kubectl run nginx --image=nginx --restart=Never
Now we can see the docker container in the worker node
docker ps
To check if the pods are running or not
kubectl get pods
Get the details of the pod
kubectl get pods -o wide
To delete a pod in
# kubectl delete pod <pod-name> kubectl delete pod nginx
Thank You,
I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey.
Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.
~ Abhisek Moharana 😊