Cloud Infrastructure 101
Cloud in a nutshell: imagine the internet is a giant warehouse full of computers, companies like amazon, google, microsoft own these warehouses as they’re the cloud providers.
You technically have to pay them to rent space in their warehouse.
Why do we need something like Docker?
Docker solves the dependency problem. It packages your app and all its dependencies into a container. This container is like a self-contained unit. It doesn't matter what's on the host machine, the container has everything it needs.
Advantages are:
- Consistency: Your app runs the same way everywhere: on your laptop, in the cloud, on any server that has Docker.
- Portability: Containers are lightweight and portable. You can easily move them between different environments.
- Efficiency: Containers share the host operating system kernel, making them more efficient than virtual machines.
Basics of Docker and Docker Commands to know:
“Think of Docker as a virtual shipping company”
You package your app into containers (like shipping containers), and Docker helps you manage them. These commands are your instruction manual.”
docker run <image_name>
: This creates and starts a container from an image. Think of it as sending out a shipment.<image_name>
is like the label on the container.
Example:
docker run hello-world
(runs a "hello world" container).docker ps
: Shows you the currently running containers. Like checking which shipments are currently in transit.docker ps -a
shows all containers, even the ones that have stopped.
docker stop <container_id>
: Stops a running container. Like recalling a shipment.<container_id>
is the unique ID of the container (you can get it fromdocker ps
).
docker rm <container_id>
: Removes a stopped container. Like deleting a shipment from your records.
docker images
: Lists all the Docker images you have. Like checking your warehouse for available shipping containers.
docker build -t <image_name> .
: Builds a Docker image from a Dockerfile (a blueprint for your container).t
tags the image with a name. The.
means the Dockerfile is in the current directory. Think of it as preparing a new shipment. Example:docker build -t my-app .
docker pull <image_name>
: Downloads a Docker image from a registry (like Docker Hub). Like ordering a new shipping container.
Example:
docker pull ubuntu
(downloads the Ubuntu image).docker push <image_name>
: Uploads a Docker image to a registry. Like sending out your custom shipping container. You usually need to log in to the registry first.
docker volume create <volume_name>
: Creates a Docker volume. Volumes are used to persist data even if the container is removed. Like having a separate storage unit for your shipments.
docker run -v <volume_name>:<path_in_container> <image_name>
: Mounts a volume to a container. This makes the data in the volume accessible to the container.
docker-compose up
: Starts multiple containers defined in adocker-compose.yml
file. Useful for running complex applications with multiple services.
docker-compose down
: Stops and removes the containers defined in adocker-compose.yml
file.
Why do we need something like Kubernetes?
Kubernetes solves the deployment, scaling, and management problems. It automates the process of deploying and managing containers.
- Automated Deployment: Kubernetes handles deploying your containers to multiple servers. No more manual configuration.
- Scaling: Kubernetes can automatically scale your application up or down based on demand.
- Self-Healing: If a container crashes, Kubernetes automatically restarts it. No more panicking in the middle of the night. 😂
- Load Balancing: Kubernetes distributes traffic across multiple containers, ensuring that no single container is overloaded.
In short:
- Docker: Solves the "it works on my machine" problem.
- Kubernetes: Solves the "how do I manage a bunch of these things" problem.
Practically deploying an app on cloud (GCP)
- Code: Write a simple web app (using whatever framework you like, my choice is Next.js, the t3 stack).
- Dockerize: Create a Dockerfile that packages your app into a container.
- Kubernetes: Create a Kubernetes deployment and service to run your containerized app.
- GCP: Create a Google Kubernetes Engine (GKE) cluster on GCP.
- Deploy: Deploy your K8s manifests to your GKE cluster.
- Expose: Create a load balancer to expose your app to the internet.