From 53090845cd0c805db2ecb3a90ea96efcbdd287e5 Mon Sep 17 00:00:00 2001 From: dxin Date: Fri, 7 Nov 2025 18:06:59 +0800 Subject: [PATCH] + --- SCM/s1_go_lessie_api.groovy | 286 ++++++++++++++++++ SCM/s1_python_lessie_agents.groovy | 266 ++++++++++++++++ ...ssie-agents.yaml => s1-lessie-agents.yaml} | 59 ++-- k8s_yaml/s1/s1-lessie-go-api.yaml | 2 +- 4 files changed, 590 insertions(+), 23 deletions(-) create mode 100644 SCM/s1_go_lessie_api.groovy create mode 100644 SCM/s1_python_lessie_agents.groovy rename k8s_yaml/s1/{test-lessie-agents.yaml => s1-lessie-agents.yaml} (56%) diff --git a/SCM/s1_go_lessie_api.groovy b/SCM/s1_go_lessie_api.groovy new file mode 100644 index 0000000..c53968b --- /dev/null +++ b/SCM/s1_go_lessie_api.groovy @@ -0,0 +1,286 @@ +pipeline { + agent any + tools{ + go 'go1.24.0' + } + parameters { + gitParameter( + branchFilter: 'origin/(.*)', + defaultValue: 'dxin', + name: 'Code_branch', + type: 'PT_BRANCH_TAG', + selectedValue: 'DEFAULT', + sortMode: 'NONE', + description: '选择代码分支: ', + quickFilterEnabled: true, + tagFilter: '*', + listSize: "1" + ) + choice( + name: 'NAME_SPACES', + choices: ['sit', 'test', 'prod'], + description: '选择存放镜像的仓库命名空间:' + ) + string( + name: 'CUSTOM_TAG', + defaultValue: '', + description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”' + ) + booleanParam( + name: 'DEPLOY_AFTER_BUILD', + defaultValue: true, + description: '是否构建完镜像后部署?' + ) + } + environment { + KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称 + + REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接(这个镜像仓库的命名空间) + IMAGE_NAME = "go_lessie-sourcing-api" // 镜像名(固定前缀,这里是指构建后的镜像名) + CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID + + Deployment_name = "s1-lessie-go-api-deployment" // 工作负载资源清单文件名 + Pod_container_name = "s1-lessie-go-api" // pod内运行的容器名 + K8s_namespace = "sit" // 这是k8s集群的命名空间 + + } + stages { + stage('Checkout代码') { + steps { + git branch: "${params.Code_branch}", + credentialsId: 'fly_gitlab_auth', + url: 'http://106.53.194.199/go/lessie-sourcing-api.git' + } + } + stage('获取信息') { + steps { + script { + // 获取最近一次提交的哈希值(短格式,前8位) + env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD',returnStdout: true).trim() + // 获取最近一次提交的作者 + env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an',returnStdout: true).trim() + // 获取最近一次提交的时间(格式化) + env.GIT_COMMIT_TIME = sh( + script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S', + returnStdout: true + ).trim() + // 获取最近一次提交的备注信息(转义特殊字符,避免构建失败) + env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim() + + // Jenkins构建次数 + def buildNumber = env.BUILD_NUMBER // Jenkins内置变量,直接获取当前Job的构建序号 + // 当前分支名(处理/为-,如feature/docker_1015 → feature-docker_1015) + def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim() + def formattedBranch = branchName.replace('/', '-').replace('_', '-') // 替换分支名中的/和_为- + // 构建时间(格式:202510181215,年-月-日-时-分,无分隔符) + def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim() + def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}" + + def customTag = params.CUSTOM_TAG?.trim() + def tagPattern = ~/^[a-zA-Z0-9._-]+$/ + + if (customTag && customTag ==~ tagPattern) { + echo "✅ 使用自定义镜像 Tag: ${customTag}" + env.IMAGE_TAG = customTag + } else if (customTag) { + echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}" + + def confirmed = true + timeout(time: 1, unit: 'MINUTES') { + try { + input( + message: """⚠️ Tag 命名不规范: + ${customTag} + + 将使用自动生成的 Tag: + ${defaultTag} + + 是否继续构建?""", + ok: '确认' + ) + } catch (err) { + // 用户点击“取消”或中断 + echo "🚫 用户取消构建" + confirmed = false + } + } + + if (confirmed) { + echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}" + env.IMAGE_TAG = defaultTag + } else { + error("流水线已终止。") + } + } else { + env.IMAGE_TAG = defaultTag + echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}" + } + } + } + } + stage('依赖&构建') { + steps { + sh """ + cd ${WORKSPACE}/ + export GOVCS="git.deeplink.media:git,*:git" + echo "Jenkins 当前使用的 Go 路径:\$(which go)" + echo "Jenkins 当前使用的 Go 版本:\$(go version)" + echo "拉依赖" + go mod tidy -v -x + make build-linux + chmod +x ./build/lessie-sourcing-api + """ + } + } + stage('登录容器仓库') { + steps { + withCredentials([usernamePassword( + credentialsId: env.CREDENTIALS_ID, + usernameVariable: 'REGISTRY_USER', + passwordVariable: 'REGISTRY_PWD' + )]) { + sh ''' + echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin + ''' + } + } + } + stage('构建容器镜像') { + steps { + script { + // 构建镜像,添加标签信息 + sh """ + docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \ + --label "git-branch='${params.Code_branch}'" \ + --label "git-commit='${GIT_COMMIT_SHORT}'" \ + --label "git-author='${GIT_AUTHOR}'" \ + --label "git-message='${GIT_COMMIT_MSG}'" \ + --label "build-time='${GIT_COMMIT_TIME}'" \ + . + """ + } + } + } + stage('推送镜像到仓库') { + steps { + script { + sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + } + } + } + + stage('部署到K8S') { + when { + expression { return params.DEPLOY_AFTER_BUILD } + } + steps { + sh """ + echo "=== 更新 Deployment 镜像 ===" + kubectl set image deployment/${Deployment_name} ${Pod_container_name}=${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} -n ${K8s_namespace} + echo "=== 添加注解 ===" + kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${GIT_COMMIT_MSG}" --overwrite -n ${K8s_namespace} + echo "=== 查看历史版本 ===" + kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace} + echo "=== 查看所使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + """ + } + } + stage('检查部署情况') { + when { + expression { return params.DEPLOY_AFTER_BUILD } + } + steps { + echo "检测部署状态并验证新版本运行情况" + sh """ + echo "=== 检查 Deployment 滚动更新状态 ===" + kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=180s + + if [ \$? -ne 0 ]; then + echo "❌ 部署超时或失败,开始收集诊断信息..." + echo "=== 查看当前 Pods 状态 ===" + kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide + + echo "=== 查看最近的事件 ===" + kubectl get events -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp | tail -n 20 + + echo "=== 查看最近一个失败 Pod 的详细描述 ===" + FAILED_POD=\$(kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --field-selector=status.phase!=Running -o jsonpath='{.items[0].metadata.name}') + if [ ! -z "\$FAILED_POD" ]; then + kubectl describe pod \$FAILED_POD -n ${K8s_namespace} || true + kubectl logs \$FAILED_POD -n ${K8s_namespace} --tail=50 || true + fi + + echo "=== 回滚到上一个版本 ===" + kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace} + + exit 1 + fi + + echo "=== 检查 Pods 是否全部 Ready ===" + kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide + + echo "=== 获取最新 Pod 名称 ===" + NEW_POD=\$(kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}') + + echo "=== 新 Pod 启动日志(最近20行) ===" + kubectl logs \$NEW_POD -n ${K8s_namespace} --tail=20 || true + + echo "✅ 部署成功:\$NEW_POD 已正常运行" + """ + } + } + + } + post { + always { + script { + echo "开始清理本地旧镜像,仅保留最近 2 个构建版本" + def keepCount = 2 + def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}" + + // 获取所有镜像(按创建时间排序,越新的越前) + def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.CreatedAt}}' | sort -rk2", returnStdout: true).trim() + if (!allImagesRaw) { + echo "未找到任何镜像,无需清理" + return + } + + def allImages = allImagesRaw.split('\n') + if (allImages.size() <= keepCount) { + echo "当前镜像数未超过 ${keepCount} 个,无需清理" + return + } + + def oldImages = allImages.drop(keepCount) + echo "发现 ${oldImages.size()} 个旧镜像需要清理" + + oldImages.each { line -> + def imageTag = line.split(' ')[0] + echo "删除旧镜像: ${imageTag}" + sh(returnStatus: true, script: "docker rmi -f \"${imageTag}\" || true") + } + + echo "清理完成,当前镜像状态:" + sh """ + docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}' + """ + + sh "docker logout ${REGISTRY}" + echo "容器仓库已登出,本地凭证已清理" + } + } + success { + // 输出构建结果 + echo "镜像构建成功!" + echo "镜像地址:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + echo "对应代码提交:${GIT_COMMIT_SHORT}(${GIT_COMMIT_MSG})" + } + failure { + // 输出构建结果 + echo "部署有错误,请检查!" + } + } +} \ No newline at end of file diff --git a/SCM/s1_python_lessie_agents.groovy b/SCM/s1_python_lessie_agents.groovy new file mode 100644 index 0000000..ecdb1bf --- /dev/null +++ b/SCM/s1_python_lessie_agents.groovy @@ -0,0 +1,266 @@ +pipeline { + agent any + + parameters { + gitParameter( + branchFilter: 'origin/(.*)', + defaultValue: 'dxin', + name: 'Code_branch', + type: 'PT_BRANCH', + selectedValue: 'DEFAULT', + sortMode: 'NONE', + description: '选择代码分支:', + quickFilterEnabled: true, + tagFilter: '*', + listSize: "1" + ) + choice( + name: 'NAME_SPACES', + choices: ['sit', 'test', 'prod'], + description: '选择存放镜像的仓库命名空间:' + ) + string( + name: 'CUSTOM_TAG', + defaultValue: '', + description: '可选:自定义镜像 Tag (字母、数字、点、下划线、短横线), 留空则自动生成 “ v+构建次数_分支名_短哈希_构建时间 ”' + ) + booleanParam( + name: 'DEPLOY_AFTER_BUILD', + defaultValue: true, + description: '是否构建完镜像后部署?' + ) + } + environment { + KUBECONFIG = credentials('k8s-test-config-admin') // k8s 凭证 ID, Jenkins 中配置的凭证名称 + + REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间 + IMAGE_NAME = "lessie-sourcing-agents" // 镜像名(固定前缀) + CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID + + Deployment_name = "s1-lessie-agents-deployment" // 工作负载资源清单文件名 + Pod_container_name = "s1-lessie-agents" // pod内运行的容器名 + K8s_namespace = "sit" // 这是k8s集群的命名空间 + } + + stages { + stage('拉取代码') { + steps { + // 拉取指定分支代码(通过参数 params.Code_branch 动态指定) + git branch: "${params.Code_branch}", + credentialsId: 'fly_gitlab_auth', + url: 'http://106.53.194.199/python/lessie-sourcing-agents.git' + } + } + + stage('获取提交信息') { + steps { + script { + env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=format:%an', returnStdout: true).trim() + env.GIT_COMMIT_TIME = sh(script: 'git log -1 --pretty=format:%ct | xargs -I {} date -d @{} +%Y%m%d-%H%M%S', returnStdout: true).trim() + env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=format:%s | sed -e \'s/"/\\"/g\'', returnStdout: true).trim() + + def buildNumber = env.BUILD_NUMBER + def branchName = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim() + def formattedBranch = branchName.replace('/', '-').replace('_', '-') + def buildTime = sh(script: 'date +%Y%m%d%H%M', returnStdout: true).trim() + def defaultTag = "v${buildNumber}_${formattedBranch}_${GIT_COMMIT_SHORT}_${buildTime}" + + def customTag = params.CUSTOM_TAG?.trim() + def tagPattern = ~/^[a-zA-Z0-9._-]+$/ + + if (customTag && customTag ==~ tagPattern) { + echo "✅ 使用自定义镜像 Tag: ${customTag}" + env.IMAGE_TAG = customTag + } else if (customTag) { + echo "⚠️ 自定义 Tag '${customTag}' 不符合规范,将使用默认生成的 Tag: ${defaultTag}" + + def confirmed = true + timeout(time: 1, unit: 'MINUTES') { + try { + input( + message: """⚠️ Tag 命名不规范: + ${customTag} + + 将使用自动生成的 Tag: + ${defaultTag} + + 是否继续构建?""", + ok: '确认' + ) + } catch (err) { + // 用户点击“取消”或中断 + echo "🚫 用户取消构建" + confirmed = false + } + } + + if (confirmed) { + echo "✅ 用户确认使用自动生成的 Tag:${defaultTag}" + env.IMAGE_TAG = defaultTag + } else { + error("流水线已终止。") + } + } else { + env.IMAGE_TAG = defaultTag + echo "未输入自定义 Tag, 使用自动生成规则: ${env.IMAGE_TAG}" + } + } + } + } + + stage('登录容器仓库') { + steps { + withCredentials([usernamePassword( + credentialsId: env.CREDENTIALS_ID, + usernameVariable: 'REGISTRY_USER', + passwordVariable: 'REGISTRY_PWD' + )]) { + sh ''' + echo "$REGISTRY_PWD" | docker login ${REGISTRY} -u ${REGISTRY_USER} --password-stdin + ''' + } + } + } + + stage('构建容器镜像') { + steps { + script { + // 构建镜像,添加标签信息 + sh """ + docker build -t ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} \ + --label "git-branch='${params.Code_branch}'" \ + --label "git-commit='${GIT_COMMIT_SHORT}'" \ + --label "git-author='${GIT_AUTHOR}'" \ + --label "git-message='${GIT_COMMIT_MSG}'" \ + --label "build-time='${GIT_COMMIT_TIME}'" \ + . + """ + } + } + } + + stage('推送镜像到仓库') { + steps { + script { + sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + echo "推送镜像成功:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + } + } + } + + stage('部署到K8S') { + when { + expression { return params.DEPLOY_AFTER_BUILD } + } + steps { + sh """ + echo "=== 更新 Deployment 镜像 ===" + kubectl set image deployment/${Deployment_name} ${Pod_container_name}=${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} -n ${K8s_namespace} + echo "=== 添加注解 ===" + kubectl annotate deployment/${Deployment_name} kubernetes.io/change-cause="${GIT_COMMIT_MSG}" --overwrite -n ${K8s_namespace} + echo "=== 查看历史版本 ===" + kubectl rollout history deployment/${Deployment_name} -n ${K8s_namespace} + echo "=== 查看所使用的镜像 ===" + kubectl get deployment ${Deployment_name} -n ${K8s_namespace} -o=jsonpath='{.spec.template.spec.containers[*].image}' + """ + } + } + + stage('检查部署情况') { + when { + expression { return params.DEPLOY_AFTER_BUILD } + } + steps { + echo "检测部署状态并验证新版本运行情况" + sh """ + echo "=== 检查 Deployment 滚动更新状态 ===" + kubectl rollout status deployment/${Deployment_name} -n ${K8s_namespace} --timeout=180s + + if [ \$? -ne 0 ]; then + echo "❌ 部署超时或失败,开始收集诊断信息..." + echo "=== 查看当前 Pods 状态 ===" + kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide + + echo "=== 查看最近的事件 ===" + kubectl get events -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp | tail -n 20 + + echo "=== 查看最近一个失败 Pod 的详细描述 ===" + FAILED_POD=\$(kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --field-selector=status.phase!=Running -o jsonpath='{.items[0].metadata.name}') + if [ ! -z "\$FAILED_POD" ]; then + kubectl describe pod \$FAILED_POD -n ${K8s_namespace} || true + kubectl logs \$FAILED_POD -n ${K8s_namespace} --tail=50 || true + fi + + echo "=== 回滚到上一个版本 ===" + kubectl rollout undo deployment/${Deployment_name} -n ${K8s_namespace} + + exit 1 + fi + + echo "=== 检查 Pods 是否全部 Ready ===" + kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} -o wide + + echo "=== 获取最新 Pod 名称 ===" + NEW_POD=\$(kubectl get pods -l app=${Pod_container_name} -n ${K8s_namespace} --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1].metadata.name}') + + echo "=== 新 Pod 启动日志(最近20行) ===" + kubectl logs \$NEW_POD -n ${K8s_namespace} --tail=20 || true + + echo "✅ 部署成功:\$NEW_POD 已正常运行" + """ + } + } + } + + post { + always { + script { + echo "开始清理本地旧镜像,仅保留最近 2 个构建版本" + def keepCount = 2 + def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}" + + // 获取所有镜像(按创建时间排序,越新的越前) + def allImagesRaw = sh(script: "docker images ${imagePrefix} --format '{{.Repository}}:{{.Tag}} {{.CreatedAt}}' | sort -rk2", returnStdout: true).trim() + if (!allImagesRaw) { + echo "未找到任何镜像,无需清理" + return + } + + def allImages = allImagesRaw.split('\n') + if (allImages.size() <= keepCount) { + echo "当前镜像数未超过 ${keepCount} 个,无需清理" + return + } + + def oldImages = allImages.drop(keepCount) + echo "发现 ${oldImages.size()} 个旧镜像需要清理" + + oldImages.each { line -> + def imageTag = line.split(' ')[0] + echo "删除旧镜像: ${imageTag}" + sh(returnStatus: true, script: "docker rmi -f \"${imageTag}\" || true") + } + + echo "清理完成,当前镜像状态:" + sh """ + docker images ${imagePrefix} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}' + """ + + sh "docker logout ${REGISTRY}" + echo "容器仓库已登出,本地凭证已清理" + } + } + success { + // 输出构建结果 + echo "镜像构建成功!" + echo "镜像地址:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + echo "对应代码提交:${GIT_COMMIT_SHORT}(${GIT_COMMIT_MSG})" + } + failure { + // 输出构建结果 + echo "有失败步骤!" + } + } +} \ No newline at end of file diff --git a/k8s_yaml/s1/test-lessie-agents.yaml b/k8s_yaml/s1/s1-lessie-agents.yaml similarity index 56% rename from k8s_yaml/s1/test-lessie-agents.yaml rename to k8s_yaml/s1/s1-lessie-agents.yaml index 1a73fa0..3e8949f 100644 --- a/k8s_yaml/s1/test-lessie-agents.yaml +++ b/k8s_yaml/s1/s1-lessie-agents.yaml @@ -4,18 +4,18 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: test-lessie-agents-deployment - namespace: test-lessie + name: s1-lessie-agents-deployment + namespace: sit labels: - app: test-lessie-agents - environment: test + app: s1-lessie-agents + environment: s1 project: lessie spec: replicas: 1 selector: matchLabels: - app: test-lessie-agents - environment: test + app: s1-lessie-agents + environment: s1 project: lessie strategy: type: RollingUpdate # 滚动更新策略 @@ -25,8 +25,8 @@ spec: template: metadata: labels: - app: test-lessie-agents - environment: test + app: s1-lessie-agents + environment: s1 project: lessie spec: imagePullSecrets: @@ -43,8 +43,8 @@ spec: path: /data/logs/lessie-agents/ type: DirectoryOrCreate containers: - - name: test-lessie-agents # 容器名称 - image: uswccr.ccs.tencentyun.com/lessietest/lessie-sourcing-agents:v0.0.2 # 容器镜像 + - name: s1-lessie-agents # 容器名称 + image: uswccr.ccs.tencentyun.com/lessiesit/lessie-sourcing-agents:v0.0.2 # 容器镜像 imagePullPolicy: Always # 镜像拉取策略拉 env: - name: POD_NAME @@ -69,7 +69,7 @@ spec: memory: "2Gi" # 容器请求分配1Gi内存(这会实际预留) limits: cpu: "2" # 最多可以使用2个CPU核心 - memory: "10Gi" # 容器最多可以使用8Gi内存 + memory: "10Gi" # 容器最多可以使用10Gi内存 volumeMounts: - name: aws-credentials-volume mountPath: /root/.aws/ @@ -78,33 +78,48 @@ spec: - name: lessie-logs-volume mountPath: /data/webapps/lessie_sourcing_agents/logs/ subPathExpr: lessie-agents-$(POD_NAME) + readinessProbe: # 就绪探针,用于判断容器是否已准备好接收流量 + httpGet: + path: /health + port: 8000 + initialDelaySeconds: 20 # 就绪探测在容器启动后等待多少秒才开始第一次探测(避免应用启动未完成即被判为不就绪) + periodSeconds: 10 # 就绪探测的间隔秒数,每隔多少秒执行一次探测 + timeoutSeconds: 5 # 单次就绪探测的超时时间(秒),超过则该次探测视为失败 + failureThreshold: 3 # 连续失败多少次后认为就绪探测失败(Pod 不再被视为就绪) + livenessProbe: # 存活探针,用于判断容器是否仍然健康,失败会触发重启 + httpGet: + path: /health + port: 8000 + initialDelaySeconds: 10 # 存活探测在容器启动后等待多少秒才开始第一次探测 + periodSeconds: 30 # 存活探测的间隔秒数 + timeoutSeconds: 5 # 单次存活探测的超时时间(秒) + failureThreshold: 3 # 连续失败多少次后认为容器不健康并触发重启 --- # ---------------------------- # Service -# 集群内部:http://test-lessie-agents-svc.test-lessie.svc.cluster.local:8000 -# 集群外部:http://:38000 -# curl -v -X POST http://43.153.12.208:30800/api/chat/stream -H "Content-Type: application/json" -d '{"message": "hello"}' +# 集群内部:http://s1-lessie-agents-svc.sit.svc.cluster.local:8000 + +# curl -v -X POST http://xxxxxxx:8000/api/chat/stream -H "Content-Type: application/json" -d '{"message": "hello"}' # ---------------------------- apiVersion: v1 kind: Service metadata: - name: test-lessie-agents-svc - namespace: test-lessie + name: s1-lessie-agents-svc + namespace: sit labels: - app: test-lessie-agents - environment: test + app: s1-lessie-agents + environment: s1 project: lessie spec: - type: NodePort + type: ClusterIP selector: # 必须匹配 Deployment 的 labels 才能关联 Pod - app: test-lessie-agents - environment: test + app: s1-lessie-agents + environment: s1 project: lessie ports: - name: http port: 8000 # ClusterIP 内部端口 targetPort: 8000 # 容器端口 - nodePort: 30800 # 节点对外端口(30000-32767) diff --git a/k8s_yaml/s1/s1-lessie-go-api.yaml b/k8s_yaml/s1/s1-lessie-go-api.yaml index ef18abb..8276be0 100644 --- a/k8s_yaml/s1/s1-lessie-go-api.yaml +++ b/k8s_yaml/s1/s1-lessie-go-api.yaml @@ -38,7 +38,7 @@ spec: type: DirectoryOrCreate containers: - name: s1-lessie-go-api # 容器名称 - image: uswccr.ccs.tencentyun.com/lessiesit/go_lessie-sourcing-api:v0.0.1 # 容器镜像 + image: uswccr.ccs.tencentyun.com/lessiesit/go_lessie-sourcing-api:v13_dxin_d96a403_202511071407 # 容器镜像 imagePullPolicy: Always # 镜像拉取策略 ,总是拉 env: - name: POD_NAME