今天是第二十八天我們可以寫一個k8s電力分配資料庫管理系統,以下是我的程式碼
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: "mysql"
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "root_password"
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
apiVersion: apps/v1
kind: Deployment
metadata:
name: power-management-api
spec:
replicas: 3
selector:
matchLabels:
app: power-management-api
template:
metadata:
labels:
app: power-management-api
spec:
containers:
- name: api
image: your-docker-image:latest
ports:
- containerPort: 8080
env:
- name: DB_HOST
value: "mysql"
- name: DB_PASSWORD
value: "root_password"
apiVersion: v1
kind: Service
metadata:
name: power-management-service
spec:
selector:
app: power-management-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: power-management-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: power-management-api
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: "mysql"
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "root_password"
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
apps/v1
是 StatefulSet 這種控制器所使用的 API 版本。name: mysql
為這個 StatefulSet 資源命名為 mysql
。"root_password"
。/var/lib/mysql
是 MySQL 的資料庫目錄,將其與 PersistentVolumeClaim (PVC) 連接,保證儲存持久化。apiVersion: apps/v1
kind: Deployment
metadata:
name: power-management-api
spec:
replicas: 3
selector:
matchLabels:
app: power-management-api
template:
metadata:
labels:
app: power-management-api
spec:
containers:
- name: api
image: your-docker-image:latest
ports:
- containerPort: 8080
env:
- name: DB_HOST
value: "mysql"
- name: DB_PASSWORD
value: "root_password"
power-management-api
。app: power-management-api
的標籤。api
。DB_HOST: mysql
) 和 root 密碼 (DB_PASSWORD: root_password
)。apiVersion: v1
kind: Service
metadata:
name: power-management-service
spec:
selector:
app: power-management-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
power-management-service
。app: power-management-api
的標籤找到對應的 Pod。apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: power-management-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: power-management-api
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
power-management-hpa
。power-management-api
這個 Deployment。這些 Kubernetes 部署文件涵蓋了建立一個包含 MySQL 資料庫和應用服務的基本架構。使用 StatefulSet 來管理持久化的 MySQL,Deployment 來管理 API 應用,並通過 Service 將應用公開給外部網絡。最後,Horizontal Pod Autoscaler 確保應用在高流量下能夠自動擴展,保障系統的彈性與穩定性。