Docker vs Kubernetes : Breaking down the differences

In the world of modern application development, Docker and Kubernetes(k8s) are the talk of the town and these technologies have gained quite a lot of traction in recent years.

Since both of these technologies are associated with containerization and deployment, people often confuse Kubernetes (k8s) and Docker. Lets try to understand each of them around what they are and what purpose they serve for application development and deployment.

Docker

Docker is like a shipping container that allows you to package an application and all its dependencies into a portable unit, which can run on any infrastructure that supports Docker. It provides a consistent runtime environment and enables applications to run reliably across different environments. Think of Docker as a single package that contains everything your application needs to run.

Kubernetes (k8S)

Kubernetes, on the other hand, is like a shipping yard that helps you manage and orchestrate multiple Docker containers across a cluster of machines. It provides a way to deploy, scale, and manage containerized applications, making it easier to manage complex applications in a distributed environment. Think of Kubernetes as the conductor of an orchestra, coordinating multiple Docker containers to work together seamlessly.

In summary, Docker provides a way to package and distribute applications, whereas Kubernetes provides a way to manage and orchestrate those applications at scale. Together, they enable organizations and teams to build and deploy complex applications with ease, making them an essential part of modern application development and deployment.

Running your first container on Docker

Lets go through an example where we will run a PostgreSQL database container on Docker.

Follow the steps below :

  1. First, you need to ensure that Docker is installed on your machine. You can check if Docker is installed by running the following command in your terminal:
docker --version
  1. If Docker is not installed, you can download and install it from the official Docker website: https://www.docker.com/products/docker-desktop
  2. Once Docker is installed, you can run the following command to pull the latest version of the PostgreSQL image from Docker Hub:
docker pull postgres
  1. After the image has been downloaded, you can create a new Docker container and start the PostgreSQL server by running the following command:
docker run --name my-postgres-db -e POSTGRES_PASSWORD=mysecretpwd -d postgres

This command will create a new Docker container named “my-postgres-db”, set the environment variable “POSTGRES_PASSWORD” to “mysecretpwd” (which will be used as the password for the default “postgres” user), and start the PostgreSQL server in the background.

  1. To connect to the running container, you can use the following command:
docker exec -it my-postgres psql -U postgres

This command will connect to the PostgreSQL server running inside the container and start the psql command-line tool, using the default “postgres” user.

That’s it! You now have a running Docker container of PostgreSQL database.💯

Running your first pod on Kubernetes

Here are the steps to run a Kubernetes pod for PostgreSQL :

  1. First things first , ensure that you have a running Kubernetes cluster. You can create a cluster using a cloud provider like AWS or Google Cloud, or you can install a local development environment like Minikube. You can follow the instructions available at kubernetes website : https://kubernetes.io/docs/tasks/tools/
  2. Once you have a running Kubernetes cluster, you can create a YAML file that defines the PostgreSQL pod. Here’s an example YAML file:

postgres_pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: my-postgres-pod
spec:
  containers:
    - name: postgres-container
      image: postgres
      env:
    - name: POSTGRES_PASSWORD
      value: mysecretpwd
    ports:
    - containerPort: 5432

This YAML file defines a pod named “my-postgres-pod” that contains a single container named “postgres-container”. The container uses the official PostgreSQL Docker image and sets the environment variable “POSTGRES_PASSWORD” to “mysecretpwd”. It also exposes port 5432 for incoming connections.

  1. To create the PostgreSQL pod, you can run the following command:
kubectl apply -f my-postgres-pod.yaml

This command tells Kubernetes to apply the configuration defined in the “my-postgres-pod.yaml” file and create the PostgreSQL pod.

  1. To check if the pod is running, you can run the following command:
kubectl get pods

This command lists all the pods in the Kubernetes cluster. You should see the “my-postgres-pod” pod in the list, along with its status.

  1. To connect to the PostgreSQL server running inside the pod, you can use the following command:
kubectl port-forward my-postgres-pod 5432:5432

This command sets up a port forwarding between your local machine and the PostgreSQL pod, so you can connect to the PostgreSQL server using a PostgreSQL client.

And that’s it! Now, you have a running Kubernetes pod for PostgreSQL. 💯

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top