今天是第十二天我們可以寫一個k8s租車公司資料庫管理系統,以下是我的程式碼
首先,建立一個 MySQL 資料庫來儲存租車公司的數據。你可以使用一個 MySQL Helm chart 或直接編寫一個 Kubernetes 的 StatefulSet
部署。
MySQL 部署範例 (mysql-deployment.yaml):
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
labels:
app: mysql
spec:
serviceName: "mysql"
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password123"
- name: MYSQL_DATABASE
value: "rentaldb"
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
這個 StatefulSet
定義了一個 MySQL 5.7 容器,並附帶了一個持久的儲存卷(Persistent Volume)來保存資料。你可以在 MYSQL_DATABASE
環境變數中指定租車公司的數據庫名稱。
後端服務可以使用 Python 的 Flask 框架來處理 API 請求,並與資料庫互動。這個範例展示了如何將一個 Flask 應用程式容器化。
Flask 應用程式範例 (app.py):
from flask import Flask, request, jsonify
import mysql.connector
app = Flask(__name__)
def get_db_connection():
connection = mysql.connector.connect(
host="mysql", # MySQL 服務名稱
user="root",
password="password123",
database="rentaldb"
)
return connection
@app.route('/cars', methods=['GET'])
def get_cars():
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute('SELECT * FROM cars')
cars = cursor.fetchall()
cursor.close()
conn.close()
return jsonify(cars)
@app.route('/rent', methods=['POST'])
def rent_car():
conn = get_db_connection()
cursor = conn.cursor()
data = request.get_json()
cursor.execute('INSERT INTO rentals (car_id, customer_id, rental_date) VALUES (%s, %s, NOW())',
(data['car_id'], data['customer_id']))
conn.commit()
cursor.close()
conn.close()
return jsonify({"message": "Car rented successfully!"}), 201
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Dockerfile 用來將這個應用程式容器化:
Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
requirements.txt 依賴庫清單:
Flask==2.0.1
mysql-connector-python==8.0.26
接下來,我們將 Flask 應用部署到 Kubernetes。
Flask 應用程式部署 (flask-deployment.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app
labels:
app: flask-app
spec:
replicas: 2
selector:
matchLabels:
app: flask-app
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask-app
image: <your-dockerhub-username>/flask-app:latest
ports:
- containerPort: 5000
env:
- name: MYSQL_HOST
value: "mysql"
- name: MYSQL_USER
value: "root"
- name: MYSQL_PASSWORD
value: "password123"
- name: MYSQL_DB
value: "rentaldb"
---
apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
selector:
app: flask-app
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
這個 Deployment
定義了 Flask 應用程式的副本數量,並且它與 MySQL 資料庫相連。Service
則用來暴露 Flask 應用程式,以便外部流量可以通過 LoadBalancer 存取。
我可以使用 Helm 來自動化這個部署,或建立一個 CI/CD 流程(例如使用 GitHub Actions 或 Jenkins),以便在每次程式碼變更時,自動重建 Docker 容器並更新 Kubernetes 部署。
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
labels:
app: mysql
spec:
serviceName: "mysql"
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password123"
- name: MYSQL_DATABASE
value: "rentaldb"
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
rentaldb
的資料庫。/var/lib/mysql
中,並將此存儲對應到 Kubernetes 的 Persistent Volume。這段程式碼的主要目的是確保資料庫服務在 Kubernetes 集群上運行,並且資料持久化。
這段程式碼定義了 Flask 後端應用程式,負責處理租車公司系統的 API。
from flask import Flask, request, jsonify
import mysql.connector
app = Flask(__name__)
def get_db_connection():
connection = mysql.connector.connect(
host="mysql", # MySQL 服務名稱
user="root",
password="password123",
database="rentaldb"
)
return connection
@app.route('/cars', methods=['GET'])
def get_cars():
conn = get_db_connection()
cursor = conn.cursor(dictionary=True)
cursor.execute('SELECT * FROM cars')
cars = cursor.fetchall()
cursor.close()
conn.close()
return jsonify(cars)
cars
表中選取所有的車輛。@app.route('/rent', methods=['POST'])
def rent_car():
conn = get_db_connection()
cursor = conn.cursor()
data = request.get_json()
cursor.execute('INSERT INTO rentals (car_id, customer_id, rental_date) VALUES (%s, %s, NOW())',
(data['car_id'], data['customer_id']))
conn.commit()
cursor.close()
conn.close()
return jsonify({"message": "Car rented successfully!"}), 201
car_id
和 customer_id
。rentals
資料表,並設置租賃日期為當前時間 (NOW()
)。這個 Dockerfile 用來將 Flask 應用程式容器化。
Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
/app
。/app
目錄。app.py
來運行 Flask 應用程式。apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app
labels:
app: flask-app
spec:
replicas: 2
selector:
matchLabels:
app: flask-app
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask-app
image: <your-dockerhub-username>/flask-app:latest
ports:
- containerPort: 5000
env:
- name: MYSQL_HOST
value: "mysql"
- name: MYSQL_USER
value: "root"
- name: MYSQL_PASSWORD
value: "password123"
- name: MYSQL_DB
value: "rentaldb"
---
apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
selector:
app: flask-app
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
replicas: 2
),即會有兩個 Flask 副本運行。type: LoadBalancer
指定將服務暴露給外部使用者,並映射到 Kubernetes 的外部負載均衡器。這樣的架構可以確保 Flask 應用程式與 MySQL 資料庫運行於 Kubernetes 集群中,並且應用程式的多副本能夠均衡處理請求,保證高可用性。