From 05fa6f98a061786925cb098ab86a0ae8d3f2c601 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 1 Aug 2025 15:20:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E7=BD=B2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 5 +- Makefile | 6 +- k8s.yml | 150 ++++++++++++++++++++++++++++++++++++++++++++++ server/Dockerfile | 39 ++++++++++-- server/Makefile | 8 +-- update.sh | 80 +++++++++++++++++++++++++ 6 files changed, 273 insertions(+), 15 deletions(-) create mode 100644 k8s.yml create mode 100755 update.sh diff --git a/Dockerfile b/Dockerfile index 7e74b46..3ba42de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:21.6.2 AS builder +FROM m.daocloud.io/docker.io/library/node:21.6.2 AS builder WORKDIR /src @@ -6,9 +6,10 @@ RUN npm install -g pnpm COPY . . +RUN pnpm config set registry https://registry.npmmirror.com RUN make build-all -FROM node:21.6.2-slim +FROM m.daocloud.io/docker.io/library/node:21.6.2-slim COPY --from=builder /src/dist/ /apps/dist/ COPY --from=builder /src/node_modules/ /apps/node_modules/ diff --git a/Makefile b/Makefile index f276fbc..0e72588 100644 --- a/Makefile +++ b/Makefile @@ -41,11 +41,11 @@ start-prod: .PHONY: build-image build-image: - docker build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} . + nerdctl -nk8s.io build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} . .PHONY: push-image push-image: - docker push ${DOCKER_IMAGE}:${VERSION} + nerdctl -nk8s.io push ${DOCKER_IMAGE}:${VERSION} .PHONY: release -release: build-image push-image \ No newline at end of file +release: build-image push-image diff --git a/k8s.yml b/k8s.yml new file mode 100644 index 0000000..0f39f1a --- /dev/null +++ b/k8s.yml @@ -0,0 +1,150 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + meta.helm.sh/release-name: hami-webui + meta.helm.sh/release-namespace: kube-system + labels: + app.kubernetes.io/component: hami-webui + app.kubernetes.io/instance: hami-webui + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: hami-webui + app.kubernetes.io/version: 1.0.5 + helm.sh/chart: hami-webui-1.0.5 + name: hami-webui + namespace: kube-system + resourceVersion: '215714495' +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app.kubernetes.io/component: hami-webui + app.kubernetes.io/instance: hami-webui + app.kubernetes.io/name: hami-webui + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + creationTimestamp: null + labels: + app.kubernetes.io/component: hami-webui + app.kubernetes.io/instance: hami-webui + app.kubernetes.io/name: hami-webui + spec: + containers: + - args: + - /apps/dist/main + command: + - node + env: + - name: TZ + value: Asia/Shanghai + image: 'projecthami/hami-webui-fe:dev' + imagePullPolicy: IfNotPresent + name: hami-webui-fe-oss + ports: + - containerPort: 3000 + name: http + protocol: TCP + resources: + limits: + cpu: 200m + memory: 500Mi + requests: + cpu: 200m + memory: 500Mi + securityContext: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + - args: + - '--conf' + - /apps/config/config.yaml + command: + - /apps/server + env: + - name: TZ + value: Asia/Shanghai + image: 'projecthami/hami-webui-be:dev' + imagePullPolicy: IfNotPresent + name: hami-webui-be-oss + ports: + - containerPort: 8000 + name: metrics + protocol: TCP + resources: + limits: + cpu: 50m + memory: 250Mi + requests: + cpu: 50m + memory: 250Mi + securityContext: {} + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + volumeMounts: + - mountPath: /apps/config/ + name: config + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: {} + serviceAccount: hami-webui + serviceAccountName: hami-webui + terminationGracePeriodSeconds: 30 + volumes: + - configMap: + defaultMode: 420 + name: hami-webui-config + name: config + +--- +apiVersion: v1 +kind: Service +metadata: + annotations: + meta.helm.sh/release-name: hami-webui + meta.helm.sh/release-namespace: kube-system + labels: + app.kubernetes.io/component: hami-webui + app.kubernetes.io/instance: hami-webui + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: hami-webui + app.kubernetes.io/version: 1.0.5 + helm.sh/chart: hami-webui-1.0.5 + name: hami-webui + namespace: kube-system + resourceVersion: '207243046' +spec: + clusterIP: 10.96.3.221 + clusterIPs: + - 10.96.3.221 + externalTrafficPolicy: Cluster + internalTrafficPolicy: Cluster + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack + ports: + - name: http + nodePort: 29999 + port: 3000 + protocol: TCP + targetPort: http + - name: metrics + nodePort: 5063 + port: 8000 + protocol: TCP + targetPort: metrics + selector: + app.kubernetes.io/component: hami-webui + app.kubernetes.io/instance: hami-webui + app.kubernetes.io/name: hami-webui + sessionAffinity: None + type: NodePort + + diff --git a/server/Dockerfile b/server/Dockerfile index adeb8db..c2f8a3f 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,17 +1,44 @@ -FROM --platform=$BUILDPLATFORM golang:1.23.1 AS builder +FROM --platform=$BUILDPLATFORM m.daocloud.io/docker.io/library/golang:1.23.1 AS builder WORKDIR /src - ARG TARGETARCH -COPY . . +# 设置 Go 模块代理为清华源,避免国外网络超时 +ENV GO111MODULE=on \ + GOPROXY=https://goproxy.cn,direct + +# 替换 APT 源为清华,并清理默认源配置 +RUN set -eux; \ + release=$(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release); \ + echo "Using Debian release: $release"; \ + echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian $release main contrib non-free\n\ +deb http://mirrors.tuna.tsinghua.edu.cn/debian $release-updates main contrib non-free\n\ +deb http://mirrors.tuna.tsinghua.edu.cn/debian-security $release-security main contrib non-free" \ + > /etc/apt/sources.list && \ + rm -rf /etc/apt/sources.list.d/* && \ + apt-get update && \ + apt-get install -y --no-install-recommends protobuf-compiler && \ + apt-get clean && rm -rf /var/lib/apt/lists/* -RUN apt-get update && apt-get install -y --no-install-recommends protobuf-compiler +COPY . . RUN make build TARGET_ARCH=${TARGETARCH} -FROM --platform=$TARGETPLATFORM debian:stable-slim +# 运行阶段镜像 +FROM --platform=$TARGETPLATFORM m.daocloud.io/docker.io/library/debian:stable-slim + +# 如果运行时不需要 APT,这一段可以删除(否则保留) +RUN set -eux; \ + release=$(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release); \ + echo "Using Debian release: $release"; \ + echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian $release main contrib non-free\n\ +deb http://mirrors.tuna.tsinghua.edu.cn/debian $release-updates main contrib non-free\n\ +deb http://mirrors.tuna.tsinghua.edu.cn/debian-security $release-security main contrib non-free" \ + > /etc/apt/sources.list && \ + rm -rf /etc/apt/sources.list.d/* && \ + apt-get update && \ + apt-get clean && rm -rf /var/lib/apt/lists/* COPY --from=builder /src/build/ /apps/ - COPY --from=builder /src /src + diff --git a/server/Makefile b/server/Makefile index fee817d..05d4723 100644 --- a/server/Makefile +++ b/server/Makefile @@ -56,15 +56,15 @@ build-local: # 5. Docker image commands .PHONY: build-image build-image: - docker build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} . + nerdctl -nk8s.io build --platform linux/amd64 -t ${DOCKER_IMAGE}:${VERSION} . .PHONY: push-image push-image: - docker push ${DOCKER_IMAGE}:${VERSION} + nerdctl -nk8s.io push ${DOCKER_IMAGE}:${VERSION} .PHONY: save-image save-image: - docker save -o $(BUILD_PATH)hami_webui_release_${VERSION}.tar ${DOCKER_IMAGE}:${VERSION} + nerdctl -nk8s.io save -o $(BUILD_PATH)hami_webui_release_${VERSION}.tar ${DOCKER_IMAGE}:${VERSION} gzip -f $(BUILD_PATH)hami_webui_release_${VERSION}.tar # 6. Debugging and Testing @@ -94,4 +94,4 @@ build: install-deps generate build-linux # 9. Release workflow .PHONY: release -release: build-image push-image \ No newline at end of file +release: build-image push-image diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..0e43ff4 --- /dev/null +++ b/update.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +# 定义颜色输出(可选,让提示更清晰) +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# 打印错误信息并退出 +error_exit() { + echo -e "${RED}Error: $1${NC}" >&2 + exit 1 +} + +# 打印普通提示 +info_msg() { + echo -e "${GREEN}$1${NC}" +} + +# 更新前端 +update_frontend() { + info_msg "Updating frontend..." + cd /root/hami || error_exit "Failed to enter /root/hami directory" + make build-image DOCKER_IMAGE=projecthami/hami-webui-fe VERSION=dev || error_exit "Frontend build failed" + UPDATED=1 +} + +# 更新后端 +update_backend() { + info_msg "Updating backend..." + cd /root/hami/server || error_exit "Failed to enter /root/hami/server directory" + make build-image DOCKER_IMAGE=projecthami/hami-webui-be VERSION=dev || error_exit "Backend build failed" + UPDATED=1 +} + +# 重启 Kubernetes 服务 +restart_service() { + info_msg "Restarting service with k8s.yml..." + cd /root/hami || error_exit "Failed to enter /root/hami directory" + kubectl delete -f k8s.yml || error_exit "Failed to delete k8s resources" + sleep 2 # 可选:等待资源清理 + kubectl create -f k8s.yml || error_exit "Failed to create k8s resources" + info_msg "Service restarted successfully." +} + +# 显示用法 +usage() { + echo "Usage: $0 {all|frontend|backend}" + exit 1 +} + +# 主逻辑 +UPDATED=0 + +case "$1" in + all) + update_frontend + update_backend + ;; + frontend) + update_frontend + ;; + backend) + update_backend + ;; + "") + echo "Error: No argument provided." + usage + ;; + *) + echo "Error: Invalid argument '$1'." + usage + ;; +esac + +# 如果执行了任何更新,则重启服务 +if [ "$UPDATED" -eq 1 ]; then + restart_service +fi + +info_msg "Update and restart process finished."