https://blog.alphabravo.io/part-7-deploying-applications-to-kubernetes-step-by-step-guide/
To deploy an application on Kubernetes, you'll need to follow these steps, starting with prerequisites, then creating and containerizing your application, defining a Kubernetes deployment and service, and finally, deploying and verifying your app.
Prerequisites
Before starting, ensure you have:
- A working Kubernetes cluster (e.g., Minikube or a managed service)
kubectl command-line tool installed and configured
- Docker installed for containerizing your application
- A container registry account (e.g., Docker Hub)
- Basic understanding of YAML syntax
Step-by-Step Deployment Guide
Step 1: Creating a Simple Application
Create a basic web application, for example, a Flask app named app.py:
from flask import Flask
import os
import socket
app = Flask(__name__)
@app.route("/")
def hello():
html = """Hello {name}!
Hostname: {hostname}"""
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname())
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
Also, create a requirements.txt file for dependencies:
flask==2.0.1
Step 2: Containerizing Your Application
Create a Dockerfile to package your application:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
EXPOSE 80
CMD ["python", "app.py"]
Build the Docker image and push it to a registry (replace yourusername with your Docker Hub username):
docker build -t my-flask-app:v1 .
docker tag my-flask-app:v1 yourusername/my-flask-app:v1
docker push yourusername/my-flask-app:v1
Step 3: Creating Your Kubernetes Deployment Manifest
Create a deployment.yaml file to define your application's deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app-deployment
spec:
selector:
matchLabels:
app: flask-app
replicas: 2
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask-app
image: yourusername/my-flask-app:v1 # Use your image here
ports:
- containerPort: 80
env:
- name: NAME
value: "Kubernetes Explorer"
Step 4: Deploying Your Application
Apply the deployment manifest using kubectl:
kubectl apply -f deployment.yaml
Step 5: Verifying Your Deployment
Check the status of your deployment and pods:
kubectl get deployments
kubectl get pods
Step 6: Checking Application Logs
Monitor your application logs by replacing <pod-name> with one of your pod names:
kubectl logs <pod-name>
kubectl logs -f <pod-name>
Step 7: Exposing Your Application with a Service
Create a service.yaml file to expose your application externally:
apiVersion: v1
kind: Service
metadata:
name: flask-app-service
spec:
selector:
app: flask-app
ports:
- port: 80
targetPort: 80
type: LoadBalancer
Apply the service manifest:
kubectl apply -f service.yaml
Step 8: Accessing Your Application
Get the external IP address of your service:
kubectl get services
If using Minikube, you can open your application in a browser using:
minikube service flask-app-service
Handling Updates
To update your application (e.g., to v2 image), edit deployment.yaml to point to the new image and apply it again:
kubectl apply -f deployment.yaml
kubectl rollout status deployment/flask-app-deployment
To roll back to a previous version, use:
kubectl rollout undo deployment/flask-app-deployment
Scaling Your Application
To scale your application (e.g., to 5 replicas):
kubectl scale deployment flask-app-deployment --replicas=5