Kubernetes Deployment untuk Flask

Kubernetes Deployment untuk Flask

Sun Jun 07 2026
468 words · 5 minutes

Di artikel ini, kita akan membahas cara mendeploy aplikasi Flask ke Kubernetes secara lengkap dan praktis.


1. Mengapa Menggunakan Kubernetes untuk Flask?

Keunggulan Kubernetes:

  • Auto-scaling (Horizontal Pod Autoscaler)
  • Self-healing (otomatis restart container yang gagal)
  • Load balancing built-in
  • Rolling updates tanpa downtime
  • Service discovery & konfigurasi terpusat
  • Cocok untuk arsitektur microservices

Jika aplikasi Flask Anda hanya kecil-menengah, Docker + Docker Compose sudah cukup. Kubernetes lebih cocok untuk aplikasi yang kompleks dan membutuhkan skalabilitas tinggi.


2. Persiapan Docker Image

Sebelum deploy ke Kubernetes, pastikan aplikasi Flask sudah dibungkus dalam Docker image yang baik.

Contoh Dockerfile untuk Production

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt gunicorn
COPY . .
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:create_app('production')"]

Build image:

Terminal window
docker build -t my-flask-app:latest .

3. Konsep Dasar Kubernetes

KonsepPenjelasan
PodUnit terkecil yang bisa di-deploy (biasanya 1 container)
DeploymentMengelola replicasi dan update Pod
ServiceLoad balancing antar Pod
IngressMengatur akses dari luar cluster (HTTP/HTTPS)
ConfigMapMenyimpan konfigurasi non-sensitive
SecretMenyimpan data sensitif (password, API key)

4. Deployment Manifest

Buat file deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app
spec:
replicas: 3
selector:
matchLabels:
app: flask-app
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask
image: my-flask-app:latest
ports:
- containerPort: 8000
env:
- name: FLASK_ENV
value: "production"
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"

Terapkan:

Terminal window
kubectl apply -f deployment.yaml

5. Service

Buat file service.yaml:

apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
selector:
app: flask-app
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: ClusterIP

6. Ingress (Akses dari Luar)

Buat file ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: flask-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: flask.yourdomain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: flask-service
port:
number: 80

7. ConfigMap & Secret

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
name: flask-config
data:
FLASK_ENV: "production"
LOG_LEVEL: "INFO"

Secret

apiVersion: v1
kind: Secret
metadata:
name: flask-secret
type: Opaque
stringData:
SECRET_KEY: "your-super-secret-key"
DATABASE_URL: "postgresql://user:pass@db:5432/mydb"

Gunakan di Deployment:

envFrom:
- configMapRef:
name: flask-config
- secretRef:
name: flask-secret

8. Health Checks (Liveness & Readiness Probe)

Sangat penting untuk production:

livenessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8000
initialDelaySeconds: 5
periodSeconds: 5

Pastikan aplikasi Flask memiliki endpoint /health dan /ready.


9. Horizontal Pod Autoscaler (HPA)

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: flask-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: flask-app
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70

10. Best Practices Kubernetes untuk Flask

PraktikRekomendasi
Gunakan non-root user di containerTingkatkan keamanan
Set resource requests & limitsHindari resource starvation
Gunakan liveness & readiness probeSelf-healing & traffic management
Simpan secret di Kubernetes SecretJangan hardcode
Gunakan ConfigMap untuk konfigurasiLebih fleksibel
Implementasikan HPAAuto scaling berdasarkan beban
Gunakan Ingress + TLSAkses aman dari luar
Rolling Update strategyZero downtime deployment
Monitor dengan Prometheus + GrafanaObservability

Kesimpulan

Kubernetes memberikan kekuatan yang sangat besar untuk menjalankan aplikasi Flask di production, terutama ketika Anda membutuhkan:

  • Skalabilitas otomatis
  • High availability
  • Zero-downtime deployment
  • Manajemen konfigurasi yang terpusat

Namun, Kubernetes juga memiliki kurva belajar yang cukup curam. Untuk proyek kecil atau menengah, Docker Compose atau platform managed seperti Render, Railway, atau Heroku mungkin lebih efisien.

Jika proyek Anda sudah besar, kompleks, dan membutuhkan skalabilitas tinggi, maka Kubernetes adalah pilihan yang tepat.


Thanks for reading!

Kubernetes Deployment untuk Flask

Sun Jun 07 2026
468 words · 5 minutes