Setting Up a Kubernetes Cluster with Kubeadm and Deploying Nginx Pod
By - GJS
Setting up the Kubernetes cluster using Kubeadm :
Create two EC2 instances( master and worker) with t2.medium instance type and connect to them :
The master node is shown by blue terminal :
The worker node is shown by black terminal
Now we need to install docker on both machines. To install docker on EC2 follow this blog or write the below commands :
sudo su apt update -y apt install docker.io -y systemctl start docker systemctl enable docker
Now we need to install Kubeadm on both machines :
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
Now, on the master node, type :
kubeadm init
Now on Master node exit from root user and type :
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Now on Master node, type the command given below :
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
Now write the below command on Worker node :
sudo su kubeadm reset pre-flight checks
Now on the Master node, type the following command to see the join token and copy it :
kubeadm token create --print-join-command
Paste the above copied command on the Worker node with "--v=5" at the end :
kubeadm join 172.31.68.251:6443 --token zb04ld.qanvbj48903e6av7 --discovery-token-ca-cert-hash sha256:033d73daf2f70803dd2754691d08b0048e5ffe2abfe7a33445bbba31cca18bd8 --v=5
You will get some message like this :
because port 6443 is not opened for Master node.
Now go to the master node and create inbound rule for port 6443 :
Now you can see the following
On the Master node type the following commands to see the node :
kubectl get nodes kubectl get nodes -o wide
Deploying the nginx pod :
Write the following command on the Master node :
kubectl run ngnix --image=ngnix --restart=Always
The explaination of the above command is :
kubectl run
: This is the base command to create a new resource in the cluster.ngnix
: This is the name you're giving to the resource you're creating. In this case, you're creating a resource named "ngnix".--image=nginx
: This flag specifies the Docker image to use for creating the resource. In this case, you're using the "nginx" image, which is a popular web server and reverse proxy.--restart=Always
: This flag indicates the desired restart policy for the created resource. Setting it to "Always" means that Kubernetes will automatically restart the resource if it terminates or fails.Putting it all together, the command
kubectl run ngnix --image=nginx --restart=Always
creates a new resource named "ngnix" in the Kubernetes cluster. It uses the "nginx" Docker image to create the resource, and specifies the restart policy as "Always", ensuring that the resource is automatically restarted if it fails or terminates. This command would typically create a Deployment resource, which manages the desired state and lifecycle of pods running the Nginx container.
Now go to the worker node and type "docker ps", you will find nginx running :
Now if I kill this container from worker and check the pods on Master :
My pod is running with 1 restart
Thank you :
Dear readers,
I want to express my heartfelt gratitude for your continuous support and engagement with my blog. Your readership and valuable feedback have been instrumental in shaping the content and direction of my writing. Thank you for being a part of this journey and for inspiring me to keep sharing my thoughts and ideas. Your presence means the world to me, and I am truly grateful for your unwavering support.
Warmest regards, Gaurav Ji Srivastava