DEV Community

Cover image for Day 33: Deploying a Three-Tier App on Kubernetes: A Simple Guide
Arbythecoder
Arbythecoder

Posted on

Day 33: Deploying a Three-Tier App on Kubernetes: A Simple Guide

Imagine building a house. You need a strong foundation (database), sturdy walls (backend), and a beautiful façade (frontend). Deploying a multi-tier application on Kubernetes is similar! This guide simplifies deploying a three-tier application (frontend, backend, database) on Kubernetes, making it accessible even for beginners. We'll use simple YAML files and straightforward commands.

Prerequisites:

  • A Kubernetes cluster (minikube, kind, or a cloud provider like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS) are all good options).
  • kubectl configured to communicate with your cluster.
  • Docker (or a container registry) to build and store your application images. We'll assume you already have images named frontend-image:latest and backend-image:latest ready.

Steps:

  1. The Foundation: Database Deployment (MySQL)

We start with the database, our application's foundation. Create a file named mysql-deployment.yaml with the following content:

   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: mysql
   spec:
     replicas: 1
     selector:
       matchLabels:
         app: mysql
     template:
       metadata:
         labels:
           app: mysql
       spec:
         containers:
         - name: mysql
           image: mysql:5.7
           ports:
           - containerPort: 3306
           envFrom:
             - secretRef:
                 name: mysql-password # IMPORTANT: Create a secret for this password!
Enter fullscreen mode Exit fullscreen mode

Important: Before applying this, create a Kubernetes Secret to store your MySQL root password securely:

   kubectl create secret generic mysql-password --from-literal=MYSQL_ROOT_PASSWORD=<your_strong_password>
Enter fullscreen mode Exit fullscreen mode
  1. The Walls: Backend Deployment

Next, we build the backend. Create backend-deployment.yaml:

   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: backend
   spec:
     replicas: 2
     selector:
       matchLabels:
         app: backend
     template:
       metadata:
         labels:
           app: backend
       spec:
         containers:
         - name: backend
           image: backend-image:latest
           ports:
           - containerPort: 8080
Enter fullscreen mode Exit fullscreen mode
  1. The Facade: Frontend Deployment

Finally, the frontend—the part your users see. Create frontend-deployment.yaml:

   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: frontend
   spec:
     replicas: 2
     selector:
       matchLabels:
         app: frontend
     template:
       metadata:
         labels:
           app: frontend
       spec:
         containers:
         - name: frontend
           image: frontend-image:latest
           ports:
           - containerPort: 80
Enter fullscreen mode Exit fullscreen mode
  1. Connecting the Pieces: Services

Now, let's connect everything. Create services.yaml:

   apiVersion: v1
   kind: Service
   metadata:
     name: frontend-service
   spec:
     selector:
       app: frontend
     ports:
     - protocol: TCP
       port: 80
       targetPort: 80
     type: LoadBalancer # Or NodePort depending on your setup

   # Repeat for backend-service and mysql-service, adjusting ports accordingly.
Enter fullscreen mode Exit fullscreen mode
  1. Deployment and Verification

Deploy your application:

   kubectl apply -f mysql-deployment.yaml
   kubectl apply -f backend-deployment.yaml
   kubectl apply -f frontend-deployment.yaml
   kubectl apply -f services.yaml
Enter fullscreen mode Exit fullscreen mode

Check the status of your deployments and services using kubectl get deployments and kubectl get services. You should see your services with external IPs (if using LoadBalancer).

  1. Testing

Access your application through the external IP of the frontend-service. Ensure all components communicate correctly.

Conclusion:

You've successfully deployed a multi-tier application on Kubernetes! This simplified guide provides a solid foundation. Remember to adjust configurations (like service type, resource limits, and persistent volumes for the database) based on your specific needs. Happy deploying!

Remember to replace placeholders like <your_strong_password> with your actual values. Also, consider adding sections on persistent storage for the database and more robust error handling for a production-ready deployment.

Imagine monitoring actually built for developers

Billboard image

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay