From GitHub to K8s: 3-Tier Guide
Guide to Deploying a 3-Tier Web Application on Kubernetes
Deploying applications in Kubernetes can seem daunting, especially for newcomers in the DevOps space. However, with a structured approach, you can successfully deploy a 3-tier web application sourced from GitHub. This guide will walk you through each step, ensuring you understand the process thoroughly.
What is a 3-Tier Architecture?
A 3-tier architecture consists of three distinct layers:
Frontend: The user interface (UI) that users interact with.
Backend: The server-side logic that processes requests and handles application data.
Database: The storage layer where data is saved and retrieved.
In this tutorial, we will deploy a simple web application with these three tiers using Kubernetes.
Prerequisites
Before we begin, ensure you have the following:
A Kubernetes cluster (you can use Minikube for local development).
Docker installed on your machine.
kubectl
command-line tool installed to interact with your Kubernetes cluster.A GitHub repository containing your application code.
Step 1: Set Up Your Local Kubernetes Cluster
For this tutorial, we'll useMinikube, which allows you to run a single-node Kubernetes cluster locally. Follow these steps to set it up:
Install Minikube:
On Windows/Mac, you can use Homebrew:
bashbrew install minikube
On Linux, follow the instructions on the Minikube installation page.
Start Minikube:
bashminikube start
Verify Minikube is Running:
bashkubectl get nodes
This command should show your Minikube node in aReady
state.
Step 2: Clone Your GitHub Repository
Next, clone your GitHub repository where your application code resides:
bashgit clone https://github.com/yourusername/your-repo.git
cd your-repo
Step 3: Containerize Your Application
Each tier of your application needs to be containerized using Docker. Create aDockerfile
for each component.Example Dockerfile for Backend (Node.js):
text# Dockerfile for Backend
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Example Dockerfile for Frontend (React):
text# Dockerfile for Frontend
FROM node:14 as build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Example for Database (MySQL):
You can use the official MySQL image without needing a custom Dockerfile.
Step 4: Build and Push Docker Images
Build your Docker images and push them to a container registry (like Docker Hub):
bash# Build images
docker build -t yourusername/frontend:latest ./frontend
docker build -t yourusername/backend:latest ./backend
# Push images to Docker Hub
docker push yourusername/frontend:latest
docker push yourusername/backend:latest
Make sure to replaceyourusername
with your actual Docker Hub username.
Step 5: Create Kubernetes Manifests
Kubernetes uses YAML files to define the desired state of applications. Create deployment and service files for each tier.frontend-deployment.yaml:
textapiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
spec:
replicas: 2
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: yourusername/frontend:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
app: frontend
backend-deployment.yaml:
textapiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
spec:
replicas: 2
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: yourusername/backend:latest
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: ClusterIP
ports:
- port: 3000
targetPort: 3000
selector:
app: backend
database-deployment.yaml(for MySQL):
textapiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 3306
---
apiVersion:v1
kind=Service
metadata:name:mysql-service
spec:type=ClusterIP
ports:-port=3306 targetPort=3306
selector:{app:mysql}
Step 6: Deploy to Kubernetes
Now that you have created the necessary manifests, apply them to your Kubernetes cluster:
bashkubectl apply -f frontend-deployment.yaml
kubectl apply -f backend-deployment.yaml
kubectl apply -f database-deployment.yaml
Step 7: Verify Deployment
Check if all pods are running correctly:
bashkubectl get pods
You should see all three tiers deployed successfully. To access the frontend service, use the following command to get the external IP:
bashminikube service frontend-service --url
Conclusion
Congratulations! You have successfully deployed a complete 3-tier web application on Kubernetes using source code from GitHub. This process involved setting up a local Kubernetes cluster with Minikube, containerizing your application using Docker, creating deployment manifests, and applying them to your cluster. As you become more familiar with Kubernetes, consider exploring advanced topics such as CI/CD integration, scaling applications, and monitoring with tools like Prometheus and Grafana. Happy deploying!