Deployments with Docker & Kubernetes (Beginner friendly)

Deployments with Docker & Kubernetes (Beginner friendly)

WHAT IS DOCKER ?

Docker is designed to help developers build, share, and run container applications. It’s an open platform for developing, shipping, and running applications.

So in simple language, what is a containerized application ?

A docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

The docker container create (or shorthand: docker create ) command creates a new container from the specified image, without starting it. When creating a container, the Docker daemon creates a writeable container layer over the specified image and prepares it for running the specified command.

Okay too much to take now? No problem lets learn how to go step by step.

Step 1 : Application Ready with Docker File

After your application is ready on your local and is ready to be containerized. It is also important that your application contains a docker file in it. The Docker file is a script that contains instructions on how to build the Docker image.

If you are unaware of how to write a docker file check it here : Docker File

Step 2 : Docker Image Build

After completion of Step 1, we need to create a docker Image for the same.
docker build -t appname .

After this verify your docker image, if its built or not by using the following command.
docker images

Now test the image locally to check if it’s running or not :
docker run -d -p HOST_PORT:CONTAINER_PORT imagename

HOST_PORT: The port number on your host machine where you want to receive traffic.

CONTAINER_PORT: The port number within the container that's listening for connections.

Step 3 : Pushing to Docker Registry HUB

If you want to push the image to a registry, you must tag it properly. For Docker Hub, use your username as part of the tag.

docker tag imagename yourusername/imagename:latest

After this Log in to Docker Hub :

docker login

Enter your credentials.
To push to Docker Hub, use:

docker push yourusername/imagename:latest

Now you can check it Docker Hub if your image is published there or not.

Now moving on to Kubernetes. What is Kubernetes ? Why do we exactly need it ?
Lets see below .

What is Kubernetes ?
Kubernetes, also known as K8s, is an open source system for automating deployment, scaling, and management of containerized applications.

Where there are multiple microservices we prefer to use Kubernetes for the same.
There are alternatives to it, but Kubernetes provide more control of the infrastructure to the DevOps Teams.

Lets see how we can deploy our application/image that is currently in docker Hub to kubernetes.

Step 4 : Creating a Minikube Cluster.
If you do not have minikube installed on your local then follow this to install.
Minikube Installation guide

After Installation,
To create a cluster use :
minikube start

After the process of creation of Cluster,
Use the following Command to create your Kubernetes deployment

kubectl create deployment deploymentName --image=imagename:latest

To check if deployment was done or not, use :
kubectl get deployments

To create a service for external use for the deployment use :
kubectl expose deployment deploymentName --type=NodePort --port=8080

Give your port name according to your deployment port.

Check your created service by using the below command :
kubectl get services

Here after sometime you can get a designated nodeport to access your application locally.

In there is entire process there must be a common doubt that, where are the pods exactly?

The above diagram is of a node inside a cluster, here minikube uses a single node cluster, you can use other cloud platforms such as GKE,EKS for multinode clusters.

In the diagram you can see,
that inside a node there are pods running in which the containerized apps are stored (images converted to containerized apps)

To check pods running in your current deployed application use :
kubectl get pods

Congratulations ! That’s how you a create your first K8s Deployment.

Do Follow, like and comment for any further doubts. Will be coming up with more such useful blogs in Cloud and Kubernetes.