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
andbackend-image:latest
ready.
Steps:
- 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!
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>
- 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
- 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
- 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.
- 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
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
).
- 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.
Top comments (0)