Getting Started with Nginx, Docker, and Kubernetes on Windows

dazevedo

Daniel Azevedo

Posted on September 23, 2024

Getting Started with Nginx, Docker, and Kubernetes on Windows

Hi devs :)

If you’ve been hearing a lot about Docker, Kubernetes, and Nginx, you’re not alone! These technologies are transforming how we deploy and scale applications. While they might seem intimidating at first, this post will break things down for beginners. By the end, you’ll have a basic Nginx server running in a Docker container, managed by Kubernetes on your Windows machine.

What You Need to Know

  • Nginx: A high-performance web server, reverse proxy, and load balancer. It’s lightweight and great for serving web content.
  • Docker: A platform that allows you to package applications and their dependencies into lightweight containers. These containers can run anywhere, making it easier to develop, ship, and scale applications.
  • Kubernetes: A container orchestration platform. It helps you manage and scale containerized applications, ensuring they are running smoothly across multiple environments.

Setting Up Your Environment on Windows

Before we begin, let’s set up our development environment. We’ll be using Docker Desktop (which comes with Kubernetes support) and Minikube to simulate a Kubernetes cluster.

Step 1: Install Docker Desktop

  1. Download Docker Desktop for Windows from here.
  2. Follow the installation instructions.
  3. Enable Kubernetes support in Docker Desktop by going to the Settings > Kubernetes tab and checking Enable Kubernetes.

Step 2: Install Minikube

Minikube allows you to run Kubernetes locally. Download and install it by following the instructions here.

Step 3: Install kubectl

The kubectl command-line tool allows you to interact with your Kubernetes cluster. You can install kubectl by following the guide here.


Step 4: Dockerize Your Nginx Application

Let’s start by creating a simple Nginx web server using Docker. We’ll build a Docker image that serves a static HTML file.

  1. Create a Directory for Your Project: Open a terminal (PowerShell or Command Prompt) and create a project folder:
   mkdir nginx-docker-k8s
   cd nginx-docker-k8s
Enter fullscreen mode Exit fullscreen mode
  1. Create an HTML File: In this directory, create a simple index.html file:
   <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Nginx on Docker & Kubernetes</title>
   </head>
   <body>
       <h1>Hello from Nginx, Docker, and Kubernetes on Windows!</h1>
   </body>
   </html>
Enter fullscreen mode Exit fullscreen mode
  1. Create a Dockerfile: In the same directory, create a Dockerfile to package your Nginx web server:
   # Use the official Nginx image from Docker Hub
   FROM nginx:alpine

   # Copy the HTML file into the Nginx server
   COPY index.html /usr/share/nginx/html/
Enter fullscreen mode Exit fullscreen mode
  1. Build the Docker Image: Build your Docker image by running the following command in the terminal:
   docker build -t my-nginx-app .
Enter fullscreen mode Exit fullscreen mode
  1. Run the Docker Container: Now that we’ve built the image, let’s run it as a container:
   docker run -d -p 8080:80 my-nginx-app
Enter fullscreen mode Exit fullscreen mode
  1. Test It: Open your browser and go to http://localhost:8080. You should see your custom HTML page!

Step 5: Deploying Nginx to Kubernetes

Now that we have Nginx running in a Docker container, let’s move it to Kubernetes.

  1. Create a Kubernetes Deployment: Create a file named nginx-deployment.yaml with the following content:
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: nginx-deployment
   spec:
     replicas: 2
     selector:
       matchLabels:
         app: nginx
     template:
       metadata:
         labels:
           app: nginx
       spec:
         containers:
         - name: nginx
           image: my-nginx-app
           ports:
           - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

This file defines a Kubernetes deployment with 2 replicas (two copies of your Nginx app) running in the cluster.

  1. Apply the Deployment: Use the kubectl command to apply this deployment:
   kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Create a Kubernetes Service: To access the Nginx app from outside the Kubernetes cluster, we need to create a Service:
   apiVersion: v1
   kind: Service
   metadata:
     name: nginx-service
   spec:
     type: NodePort
     selector:
       app: nginx
     ports:
       - protocol: TCP
         port: 80
         targetPort: 80
         nodePort: 30000
Enter fullscreen mode Exit fullscreen mode
  1. Apply the Service: Run the following command to expose your Nginx app to the outside world:
   kubectl apply -f nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Access the Nginx App: In your browser, go to http://localhost:30000 and you should see your Nginx app running in the Kubernetes cluster!

Key Takeaways

  1. Nginx + Docker: Docker makes it easy to package and run applications like Nginx in isolated containers, ensuring they run the same in any environment.

  2. Nginx + Kubernetes: Kubernetes provides a robust platform to manage and scale your Docker containers. With Kubernetes, you can ensure your app stays available, even under high traffic.

  3. Windows Compatibility: While Docker and Kubernetes are typically associated with Linux, they work just as well on Windows with tools like Docker Desktop and Minikube.


Final Thoughts

Starting with Nginx, Docker, and Kubernetes on Windows is a great way to explore the world of containerized applications. You’ll gain insight into how modern apps are deployed, scaled, and managed. Don’t be afraid to experiment—play around with scaling, add new features to your Nginx app, and explore more Kubernetes objects!

💖 💪 🙅 🚩
dazevedo
Daniel Azevedo

Posted on September 23, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related