✅ 19. DevOps & Deployment untuk Aplikasi Python
📌 Tujuan:
- Menggunakan Docker & Kubernetes untuk menjalankan aplikasi Python di lingkungan produksi.
- Menerapkan CI/CD (Continuous Integration & Continuous Deployment) untuk otomatisasi.
- Menggunakan Serverless (AWS Lambda) untuk menjalankan Python tanpa server.
📌 Durasi: 2-3 bulan
1️⃣ Docker & Kubernetes – Containerization
🔹 Apa itu Docker?
Docker memungkinkan aplikasi berjalan dalam container yang terisolasi, sehingga bisa dipindahkan antar sistem tanpa masalah.
📌 Manfaat Docker untuk AI & Web Apps:
✅ Menjalankan aplikasi Python AI/Web di berbagai platform.
✅ Mempermudah deployment ke cloud (AWS, Google Cloud, Azure).
✅ Meningkatkan skalabilitas dengan Kubernetes.
🔹 Instalasi Docker
- Windows/Mac: Download Docker Desktop
- Linux:
sudo apt update
sudo apt install docker.io
- Cek apakah Docker sudah terpasang:
docker --version
🔹 Membuat Dockerfile untuk Aplikasi Python
Buat file Dockerfile:
# Gunakan image Python resmi
FROM python:3.9
# Atur direktori kerja
WORKDIR /app
# Salin semua file ke dalam container
COPY . /app
# Install dependensi
RUN pip install -r requirements.txt
# Jalankan aplikasi
CMD ["python", "app.py"]
🔥 Selanjutnya: Jalankan aplikasi Python di dalam container:
docker build -t my-python-app .
docker run -p 5000:5000 my-python-app
2️⃣ Kubernetes – Orkestrasi Container
📌 Apa itu Kubernetes?
Kubernetes (K8s) digunakan untuk mengelola banyak container secara otomatis.
🔹 Instalasi Minikube (Simulasi Kubernetes)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
🔹 Menjalankan Aplikasi di Kubernetes
Buat file deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app
spec:
replicas: 2
selector:
matchLabels:
app: python-app
template:
metadata:
labels:
app: python-app
spec:
containers:
- name: python-app
image: my-python-app
ports:
- containerPort: 5000
Jalankan di Kubernetes:
kubectl apply -f deployment.yaml
kubectl get pods
🔥 Selanjutnya: Gunakan Kubernetes di Cloud (AWS EKS, Google GKE, Azure AKS) untuk deployment skala besar!
3️⃣ CI/CD – Continuous Integration & Deployment
📌 Apa itu CI/CD?
CI/CD otomatisasi proses testing, build, dan deployment aplikasi setiap kali kode diperbarui.
🔹 Contoh CI/CD dengan GitHub Actions
Buat file .github/workflows/deploy.yml:
name: Deploy Python App
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest
- name: Deploy to server
run: ssh user@server 'cd /app && git pull && systemctl restart app'
🔥 Selanjutnya: Gunakan Jenkins, GitLab CI, atau AWS CodePipeline untuk workflow CI/CD yang lebih kompleks!
4️⃣ Serverless dengan AWS Lambda
📌 Apa itu Serverless?
Serverless memungkinkan menjalankan kode Python tanpa server.
🔹 Instal AWS CLI & Serverless Framework
pip install awscli
npm install -g serverless
🔹 Contoh Deploy Python API ke AWS Lambda
Buat file handler.py:
def hello(event, context):
return {
"statusCode": 200,
"body": "Hello from Serverless Python!"
}
Buat file serverless.yml:
service: python-serverless
provider:
name: aws
runtime: python3.9
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
Deploy ke AWS Lambda:
serverless deploy
🔥 Selanjutnya: Gunakan AWS Lambda + API Gateway untuk membangun API tanpa server!
📌 Kesimpulan
✅ Docker → Menjalankan aplikasi dalam container yang bisa dipindahkan.
✅ Kubernetes → Mengelola banyak container secara otomatis.
✅ CI/CD → Otomatisasi testing, build, dan deployment kode.
✅ Serverless → Menjalankan Python tanpa server di AWS Lambda.
🚀 Selanjutnya: Gunakan teknologi ini untuk aplikasi AI & marketplace otomatis! 🔥
Tidak ada komentar:
Posting Komentar