今天是第十一天我們可以寫一個k8s 學校ESG資料庫管理系統,以下是程式碼
這個系統的主要功能可以包括:
以下是簡單的架構和Kubernetes設置方案:
Node.js
或 Python (Flask/Django)
開發API服務,提供ESG數據的增刪改查。PostgreSQL
或 MySQL
作為ESG數據庫。React.js
或 Vue.js
開發前端應用,通過API展示數據。Deployment
用於管理後端和前端應用的Pod。Service
用於公開API與前端的訪問。PersistentVolume
和 PersistentVolumeClaim
用於存儲資料庫數據。Ingress
用於公開外部HTTP/HTTPS流量。apiVersion: apps/v1
kind: Deployment
metadata:
name: esg-backend
labels:
app: esg-backend
spec:
replicas: 2
selector:
matchLabels:
app: esg-backend
template:
metadata:
labels:
app: esg-backend
spec:
containers:
- name: esg-backend
image: your-dockerhub-account/esg-backend:latest
ports:
- containerPort: 3000
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: esg-db-secret
key: database-url
apiVersion: apps/v1
kind: Deployment
metadata:
name: esg-frontend
labels:
app: esg-frontend
spec:
replicas: 2
selector:
matchLabels:
app: esg-frontend
template:
metadata:
labels:
app: esg-frontend
spec:
containers:
- name: esg-frontend
image: your-dockerhub-account/esg-frontend:latest
ports:
- containerPort: 80
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: esg-postgres
labels:
app: esg-postgres
spec:
replicas: 1
selector:
matchLabels:
app: esg-postgres
template:
metadata:
labels:
app: esg-postgres
spec:
containers:
- name: postgres
image: postgres:13
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: "esgdb"
- name: POSTGRES_USER
value: "user"
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: esg-db-secret
key: postgres-password
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-storage
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: postgres-pvc
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: esg-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: esg-school-system.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: esg-frontend
port:
number: 80
- path: /api
pathType: Prefix
backend:
service:
name: esg-backend
port:
number: 3000
esg-backend
)這部分負責將後端應用(例如使用Node.js)部署到Kubernetes集群中。
apiVersion: apps/v1
kind: Deployment
metadata:
name: esg-backend
labels:
app: esg-backend
spec:
replicas: 2
selector:
matchLabels:
app: esg-backend
template:
metadata:
labels:
app: esg-backend
spec:
containers:
- name: esg-backend
image: your-dockerhub-account/esg-backend:latest
ports:
- containerPort: 3000
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: esg-db-secret
key: database-url
apps/v1
,適用於 Deployment
。Deployment
,用於部署和管理無狀態的應用程式。esg-backend
) 和標籤 (labels
),標籤用來分類和識別不同的資源。Deployment
的規格:
Pod
標籤,這裡指定 app: esg-backend
。Pod
模板的內容:
esg-backend
。your-dockerhub-account/esg-backend:latest
,這需要您在Docker Hub上已有此映像。3000
,對應後端服務的應用端口。DATABASE_URL
來指定資料庫連接字串,並從Kubernetes的 Secret
資源中提取此值。esg-frontend
)這部分用來部署前端應用,假設使用React或Vue.js。
apiVersion: apps/v1
kind: Deployment
metadata:
name: esg-frontend
labels:
app: esg-frontend
spec:
replicas: 2
selector:
matchLabels:
app: esg-frontend
template:
metadata:
labels:
app: esg-frontend
spec:
containers:
- name: esg-frontend
image: your-dockerhub-account/esg-frontend:latest
ports:
- containerPort: 80
esg-backend
部署的結構類似。這部分部署一個 PostgreSQL
資料庫來存儲學校的ESG數據。使用PersistentVolumeClaim來持久化存儲,保證資料庫數據在Pod重啟後依然存在。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
ReadWriteOnce
表示這個卷可以被單個節點以讀寫模式掛載。5GiB
。接著是 PostgreSQL
的部署:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: esg-postgres
labels:
app: esg-postgres
spec:
replicas: 1
selector:
matchLabels:
app: esg-postgres
template:
metadata:
labels:
app: esg-postgres
spec:
containers:
- name: postgres
image: postgres:13
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: "esgdb"
- name: POSTGRES_USER
value: "user"
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: esg-db-secret
key: postgres-password
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-storage
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: postgres-pvc
postgres:13
映像來部署 PostgreSQL 13 資料庫。5432
,這是PostgreSQL預設的服務端口。esgdb
。Secret
資源獲取,增加了安全性。/var/lib/postgresql/data
以持久化存儲資料庫數據。Ingress負責路由外部的HTTP/HTTPS流量到內部的前端和後端服務。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: esg-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: esg-school-system.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: esg-frontend
port:
number: 80
- path: /api
pathType: Prefix
backend:
service:
name: esg-backend
port:
number: 3000
esg-school-system.example.com
。/
路徑被路由到 esg-frontend
服務,前端應用。/api
路徑被路由到 esg-backend
服務,後端API。這是一個基於Kubernetes的學校ESG資料庫管理系統,通過多個部署檔案來設定前端、後端和資料庫服務。系統包含前端應用(如React或Vue)、後端應用(如Node.js),以及PostgreSQL資料庫,並且使用PersistentVolumeClaim來確保資料持久性。Ingress負責將外部的HTTP/HTTPS請求路由到對應的前後端服務。整個系統利用Kubernetes的部署特性實現可擴展性和高可用性這兩個特性。