From f70ba958c7406745a0f0da8a118934335d7bc624 Mon Sep 17 00:00:00 2001 From: Agent Date: Fri, 20 Mar 2026 05:12:22 +0000 Subject: [PATCH] Add K8s deployment files, restructure to backend/frontend --- k8s/backend/deployment.yaml | 158 ++++++++++++++++++++++++++++ k8s/backend/init-sql-configmap.yaml | 98 +++++++++++++++++ init.sql => k8s/backend/init.sql | 0 k8s/backend/service.yaml | 38 +++++++ k8s/configmap.yaml | 10 ++ k8s/frontend/deployment.yaml | 47 +++++++++ k8s/frontend/service.yaml | 13 +++ k8s/secret.yaml | 7 ++ 8 files changed, 371 insertions(+) create mode 100644 k8s/backend/deployment.yaml create mode 100644 k8s/backend/init-sql-configmap.yaml rename init.sql => k8s/backend/init.sql (100%) create mode 100644 k8s/backend/service.yaml create mode 100644 k8s/configmap.yaml create mode 100644 k8s/frontend/deployment.yaml create mode 100644 k8s/frontend/service.yaml create mode 100644 k8s/secret.yaml diff --git a/k8s/backend/deployment.yaml b/k8s/backend/deployment.yaml new file mode 100644 index 0000000..8528e9a --- /dev/null +++ b/k8s/backend/deployment.yaml @@ -0,0 +1,158 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: todo-backend + labels: + app: todo-backend +spec: + replicas: 2 + selector: + matchLabels: + app: todo-backend + template: + metadata: + labels: + app: todo-backend + spec: + containers: + - name: todo-backend + image: todo-backend:latest + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8080 + env: + - name: SPRING_DATASOURCE_URL + valueFrom: + configMapKeyRef: + name: todo-config + key: DATABASE_URL + - name: SPRING_DATASOURCE_USERNAME + valueFrom: + configMapKeyRef: + name: todo-config + key: DATABASE_USERNAME + - name: SPRING_DATASOURCE_PASSWORD + valueFrom: + secretKeyRef: + name: todo-secret + key: DATABASE_PASSWORD + - name: SPRING_REDIS_HOST + valueFrom: + configMapKeyRef: + name: todo-config + key: REDIS_HOST + - name: SPRING_REDIS_PORT + valueFrom: + configMapKeyRef: + name: todo-config + key: REDIS_PORT + resources: + requests: + memory: "512Mi" + cpu: "250m" + limits: + memory: "1Gi" + cpu: "500m" + livenessProbe: + httpGet: + path: /actuator/health + port: 8080 + initialDelaySeconds: 60 + periodSeconds: 10 + readinessProbe: + httpGet: + path: /actuator/health + port: 8080 + initialDelaySeconds: 30 + periodSeconds: 5 +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: todo-postgres + labels: + app: todo-postgres +spec: + serviceName: todo-postgres + replicas: 1 + selector: + matchLabels: + app: todo-postgres + template: + metadata: + labels: + app: todo-postgres + spec: + containers: + - name: postgres + image: postgres:15-alpine + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: building_materials + - name: POSTGRES_USER + valueFrom: + configMapKeyRef: + name: todo-config + key: DATABASE_USERNAME + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: todo-secret + key: DATABASE_PASSWORD + volumeMounts: + - name: postgres-data + mountPath: /var/lib/postgresql/data + - name: init-sql + mountPath: /docker-entrypoint-initdb.d + resources: + requests: + memory: "256Mi" + cpu: "100m" + limits: + memory: "512Mi" + cpu: "500m" + volumes: + - name: postgres-data + emptyDir: {} + - name: init-sql + configMap: + name: todo-init-sql +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: todo-redis + labels: + app: todo-redis +spec: + serviceName: todo-redis + replicas: 1 + selector: + matchLabels: + app: todo-redis + template: + metadata: + labels: + app: todo-redis + spec: + containers: + - name: redis + image: redis:7-alpine + ports: + - containerPort: 6379 + command: ["redis-server", "--requirepass", ""] + resources: + requests: + memory: "128Mi" + cpu: "50m" + limits: + memory: "256Mi" + cpu: "200m" + volumeMounts: + - name: redis-data + mountPath: /data + volumes: + - name: redis-data + emptyDir: {} diff --git a/k8s/backend/init-sql-configmap.yaml b/k8s/backend/init-sql-configmap.yaml new file mode 100644 index 0000000..94272d1 --- /dev/null +++ b/k8s/backend/init-sql-configmap.yaml @@ -0,0 +1,98 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: todo-init-sql +data: + init.sql: | + -- 初始化数据库脚本 + -- 此文件会在 PostgreSQL 首次启动时自动执行 + CREATE TABLE IF NOT EXISTS "user" ( + id VARCHAR(36) PRIMARY KEY, + username VARCHAR(50) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + real_name VARCHAR(100), + phone VARCHAR(20), + role VARCHAR(20) DEFAULT 'sales', + status INTEGER DEFAULT 1, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS category ( + id VARCHAR(36) PRIMARY KEY, + name VARCHAR(100) NOT NULL, + parent_id VARCHAR(36), + sort_order INTEGER DEFAULT 0, + status INTEGER DEFAULT 1, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS product ( + id VARCHAR(36) PRIMARY KEY, + name VARCHAR(200) NOT NULL, + category_id VARCHAR(36), + unit VARCHAR(20), + price DECIMAL(10,2), + stock_quantity INTEGER DEFAULT 0, + status INTEGER DEFAULT 1, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS customer ( + id VARCHAR(36) PRIMARY KEY, + name VARCHAR(200) NOT NULL, + phone VARCHAR(20), + address VARCHAR(500), + contact_person VARCHAR(100), + remark TEXT, + status INTEGER DEFAULT 1, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS "order" ( + id VARCHAR(36) PRIMARY KEY, + order_no VARCHAR(50) NOT NULL UNIQUE, + customer_id VARCHAR(36), + sales_user_id VARCHAR(36), + total_amount DECIMAL(10,2) DEFAULT 0, + status VARCHAR(20) DEFAULT 'pending', + remark TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS order_item ( + id VARCHAR(36) PRIMARY KEY, + order_id VARCHAR(36) NOT NULL, + product_id VARCHAR(36) NOT NULL, + quantity INTEGER NOT NULL, + unit_price DECIMAL(10,2) NOT NULL, + subtotal DECIMAL(10,2) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS stock ( + id VARCHAR(36) PRIMARY KEY, + product_id VARCHAR(36) NOT NULL, + warehouse VARCHAR(100), + quantity INTEGER DEFAULT 0, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS stock_flow ( + id VARCHAR(36) PRIMARY KEY, + product_id VARCHAR(36) NOT NULL, + type VARCHAR(20) NOT NULL, + quantity INTEGER NOT NULL, + remark VARCHAR(500), + operator_id VARCHAR(36), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + + -- 插入默认管理员 + INSERT INTO "user" (id, username, password, real_name, role) + VALUES ('admin', 'admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EH', '管理员', 'admin') + ON CONFLICT (username) DO NOTHING; diff --git a/init.sql b/k8s/backend/init.sql similarity index 100% rename from init.sql rename to k8s/backend/init.sql diff --git a/k8s/backend/service.yaml b/k8s/backend/service.yaml new file mode 100644 index 0000000..1db6795 --- /dev/null +++ b/k8s/backend/service.yaml @@ -0,0 +1,38 @@ +apiVersion: v1 +kind: Service +metadata: + name: todo-backend +spec: + type: ClusterIP + selector: + app: todo-backend + ports: + - port: 8080 + targetPort: 8080 + protocol: TCP +--- +apiVersion: v1 +kind: Service +metadata: + name: todo-postgres +spec: + type: ClusterIP + selector: + app: todo-postgres + ports: + - port: 5432 + targetPort: 5432 + protocol: TCP +--- +apiVersion: v1 +kind: Service +metadata: + name: todo-redis +spec: + type: ClusterIP + selector: + app: todo-redis + ports: + - port: 6379 + targetPort: 6379 + protocol: TCP diff --git a/k8s/configmap.yaml b/k8s/configmap.yaml new file mode 100644 index 0000000..ba937c7 --- /dev/null +++ b/k8s/configmap.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: todo-config +data: + DATABASE_URL: "jdbc:postgresql://todo-postgres:5432/building_materials" + DATABASE_USERNAME: "postgres" + REDIS_HOST: "todo-redis" + REDIS_PORT: "6379" + API_BASE_URL: "http://todo-backend:8080" diff --git a/k8s/frontend/deployment.yaml b/k8s/frontend/deployment.yaml new file mode 100644 index 0000000..a42d3ff --- /dev/null +++ b/k8s/frontend/deployment.yaml @@ -0,0 +1,47 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: todo-frontend + labels: + app: todo-frontend +spec: + replicas: 2 + selector: + matchLabels: + app: todo-frontend + template: + metadata: + labels: + app: todo-frontend + spec: + containers: + - name: todo-frontend + image: todo-frontend:latest + imagePullPolicy: IfNotPresent + ports: + - containerPort: 80 + env: + - name: API_BASE_URL + valueFrom: + configMapKeyRef: + name: todo-config + key: API_BASE_URL + resources: + requests: + memory: "64Mi" + cpu: "50m" + limits: + memory: "128Mi" + cpu: "200m" + livenessProbe: + httpGet: + path: / + port: 80 + initialDelaySeconds: 10 + periodSeconds: 10 + readinessProbe: + httpGet: + path: / + port: 80 + initialDelaySeconds: 5 + periodSeconds: 5 diff --git a/k8s/frontend/service.yaml b/k8s/frontend/service.yaml new file mode 100644 index 0000000..c6b12bf --- /dev/null +++ b/k8s/frontend/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: todo-frontend +spec: + type: NodePort + selector: + app: todo-frontend + ports: + - port: 80 + targetPort: 80 + protocol: TCP + nodePort: 30080 diff --git a/k8s/secret.yaml b/k8s/secret.yaml new file mode 100644 index 0000000..bb8f2ea --- /dev/null +++ b/k8s/secret.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Secret +metadata: + name: todo-secret +type: Opaque +stringData: + DATABASE_PASSWORD: "postgres"