Kubernetes Playground Pt. 1

Kubernetes Playground Pt. 1

ยท

5 min read

Let's continue our Journey to DevOps, so in this blog, we will do some hands-on practicals using Kubernetes. We will learn ho we can deploy a pod, a deployment along with a service.

To learn what are pods, deployments, service and other terms in Kubernetes do refer my previous blog from here - Kubernetes Previous Blog

Let's first begin with deploying a simple and a basic pod step-by-step process.

Prerequisite - Kubernetes Installed in your system (Minikube or Kind)

In real world or in production environment we don't deploy a pod instead we directly deploy a Deployment with replica-sets which further creates Pods automatically with a self-healing behaviour.

Deploying a Pod in a K8s Cluster

Step 1 - To deploy a pod in a cluster, go to the official documentation of Kubernetes and copy the (.yaml) file of Pod and save it in your system. You can refer the below link and screenshot.

Kubernetes Official Documentation for Pods

The default image we have used to test the pod is nginx:1.14.2

Step 2 - Once you have created and saved the pod.yml in your system now deploy it in the Kubernetes Cluster using the below command. You can also verify the file using "ls" command.

kubectl apply -f pod.yml
  • -f is used for giving the "file_name"

Now our pod is deployed in the K8s Cluster.

Step 3 - Verify that the pod is deployed with nginx image using the below command.

kubectl get pods #to get the list of pods in the cluster

You can check that the AGE is the time from the deployment of pod.

Step 4 - To access the NGINX image in your web browser here comes the role Service in Kubernetes, which we will learn later.

You can also change the image according to your configurations.

So now your simple basic pod is deployed in the NODE of Kubernetes Cluster.

Deploying a Deployment in K8s Cluster

In the production environment we always deploy a deployment in the node of Kubernetes Cluster because of its Auto-healing advantage.

Step 1 - In the first step copy the simple basic deployment file like we did above from the official documentation of Kubernetes page.

Kubernetes Official Documentation for Deployment

After saving the Deployment file in your local system. You can notice the replicas are 3 by default as well as the image is nginx:1.14.2 and the containerPort is 80.

Step 2 - Save the deployment.yml and deploy it in the node of your Kubernetes cluster.

kubectl apply -f deployment.yml
  • -f option is used to give the "file_name"

You can check that our deployment is deployed in the node of kubernetes cluster and three pods are automatically created.

Step 3 - Check the number of pods and the deployment is deployed using a single command.

kubectl get all
OR
kubectl get deployment
&
kubectl get pods

You can check that three pods are automatically created and are in running state using the deployment file and the application is deployed in all the three pods.

Step 4 - To access the application from the web browser we have to first deploy the SERVICE. This we will learn in the upcoming process.

So, now our deployment is also successfully deployed or created in the node with 3 replicas of Pods containing our application.

Deploying a Service in K8s Cluster

So now to access our application over the browser we have to deploy a SERVICE inside our Kubernetes cluster. By default the ClusterIP service is created inside our K8s node.

Step 1 - To apply a service we have to create a service.yml file and deploy it in the cluster containing the port of on which we have to access our application.

Step 2 - Go to the official documentation of service in the Kubernetes using the below link and copy the service code and create it of type NodePort Mode.

Kubernetes Service Page

In the service.yml add the selector, the type of service(NodePort), change the targetPort according to your deployment.yml file and add the port of your nodePort to access the application that specific port.

Step 3 - Save the service.yml in the same directory where your deployment or pod.yml file is located and apply it in the kubernetes node using the below command.

kubectl apply -f service.yml

Step 4 - Now our service is created you can validate all the deployments (pod, deployment and services) using the below command and check the type of service deployed.

kubectl get all

You can verify that the service/nginx is just now created with type NodePort on the port number 30002 and now you can access the application.

Accessing the application

In this final process to access the application get the IP address of the the node of your kubernetes cluster and the port number.

Step 1 - Get the IP address of your node in the K8s cluster using the below command

minikube ip #For Minikube Node
OR
kubectl get nodes -o jsonpath='{.items[0].status.addresses[0].address}' #For Kind Node

Step 2 - After getting the IP address of your node check the port of your service (30002 in my case) and copy the IP address and paste it in your web browser along with the port number and you can check that your application is now live and running.

You can check in the address bar that the IP address of the node and the port number is used to access the application.

Our nginx application is successfully running inside a container in a pod and in a node of Kubernetes cluster.

Conclusion

So that's all for this blog where we understand and done some practicals using Kubernetes and deployed a simple application and accessed it using SERVICE.

My next blog will soon be published, so stay tuned and subscribe to my hashnode channel for getting updates.

THANKYOU!! HAPPY LEARNING ๐Ÿš€

ย