diff --git a/SCM/build_image_flymoon_admin.groovy b/SCM/build_image_flymoon_admin.groovy index 8157a56..6accc92 100644 --- a/SCM/build_image_flymoon_admin.groovy +++ b/SCM/build_image_flymoon_admin.groovy @@ -18,9 +18,9 @@ pipeline { listSize: "1" ) choice( - name: 'BUILD_env', + name: 'NAME_SPACES', choices: ['sit', 'test', 'prod'], - description: '选择构建的环境配置:' + description: '选择存放镜像的仓库命名空间:' ) string( name: 'CUSTOM_TAG', @@ -30,7 +30,7 @@ pipeline { } environment { REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 - NAMESPACE = "lessie${params.BUILD_env}" // 命名空间根据choices的选择拼接 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接 IMAGE_NAME = "flymoon-admin" // 镜像名(固定前缀) CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID } diff --git a/SCM/build_image_flymoon_agent.groovy b/SCM/build_image_flymoon_agent.groovy index e4b55ad..7abcf6a 100644 --- a/SCM/build_image_flymoon_agent.groovy +++ b/SCM/build_image_flymoon_agent.groovy @@ -18,7 +18,7 @@ pipeline { listSize: "1" ) choice( - name: 'BUILD_env', + name: 'NAME_SPACES', choices: ['sit', 'test', 'prod'], description: '选择存放镜像的仓库命名空间:' ) @@ -30,7 +30,7 @@ pipeline { } environment { REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 - NAMESPACE = "lessie${params.BUILD_env}" // 命名空间根据choices的选择拼接 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接 IMAGE_NAME = "flymoon-agent" // 镜像名(固定前缀) CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID } diff --git a/SCM/build_image_flymoon_payment.groovy b/SCM/build_image_flymoon_payment.groovy index 03a6085..67338ca 100644 --- a/SCM/build_image_flymoon_payment.groovy +++ b/SCM/build_image_flymoon_payment.groovy @@ -18,7 +18,7 @@ pipeline { listSize: "1" ) choice( - name: 'BUILD_env', + name: 'NAME_SPACES', choices: ['sit', 'test', 'prod'], description: '选择存放镜像的仓库命名空间:' ) @@ -30,7 +30,7 @@ pipeline { } environment { REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 - NAMESPACE = "lessie${params.BUILD_env}" // 命名空间根据choices的选择拼接 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接 IMAGE_NAME = "flymoon-payment" // 镜像名(固定前缀) CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID } diff --git a/SCM/build_image_go_lessie_sourcing_api.groovy b/SCM/build_image_go_lessie_sourcing_api.groovy index 8c1dcd5..8c0aeb1 100644 --- a/SCM/build_image_go_lessie_sourcing_api.groovy +++ b/SCM/build_image_go_lessie_sourcing_api.groovy @@ -17,7 +17,7 @@ pipeline { listSize: "1" ) choice( - name: 'BUILD_env', + name: 'NAME_SPACES', choices: ['sit', 'test', 'prod'], description: '选择存放镜像的仓库命名空间:' ) @@ -29,7 +29,7 @@ pipeline { } environment { REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 - NAMESPACE = "lessie${params.BUILD_env}" // 命名空间根据choices的选择拼接 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接 IMAGE_NAME = "go_lessie-sourcing-api" // 镜像名(固定前缀) CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID } diff --git a/SCM/build_image_lessie_ai_web.groovy b/SCM/build_image_lessie_ai_web.groovy new file mode 100644 index 0000000..b9ec84d --- /dev/null +++ b/SCM/build_image_lessie_ai_web.groovy @@ -0,0 +1,213 @@ +pipeline { + agent any + tools{ + node 'node20.15.0' + } + 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+构建次数_分支名_短哈希_构建时间 ”' + ) + } + environment { + REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间根据choices的选择拼接 + IMAGE_NAME = "flymoon-admin" // 镜像名(固定前缀) + CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID + } + + stages { + stage('拉取代码') { + steps { + git branch: "${params.Code_branch}", credentialsId: 'fly_gitlab_auth', url: 'http://106.53.194.199/root/web/jennie.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._-]+$/ + + // 判断最终Tag + 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('Maven 编译') { + steps { + sh "cd ${WORKSPACE}/ && mvn clean install -Dmaven.test.skip=true" + } + } + + 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 { + // 推送主镜像(带唯一 Tag) + sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + + // 推送 latest 标签(包含命名空间) + sh "docker tag ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG} ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:latest" + sh "docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:latest" + } + } + } + } + + post { + always { + script { + echo "开始清理本地旧镜像,仅保留最近 3 个构建版本 + latest" + // 保留镜像数量 + def keepCount = 3 + def imagePrefix = "${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}" + // 获取当前镜像的 IMAGE ID + def currentImageId = sh(script: "docker images -q ${imagePrefix}:${IMAGE_TAG}", returnStdout: true).trim() + // 获取所有该镜像的本地版本(按创建时间排序) + def allImages = sh(script: "docker images ${imagePrefix} --format '{{.ID}} {{.Tag}}' | sort -rk2", returnStdout: true).trim().split('\n') + if (allImages.size() > keepCount + 1) { // +1 保留 latest + def oldImages = allImages.drop(keepCount + 1) + echo "发现 ${oldImages.size()} 个旧镜像需要清理" + + oldImages.each { line -> + def parts = line.split(' ') + def imageId = parts[0] + def tag = parts[1] + + // 跳过 latest + if (tag != "latest") { + echo "删除旧镜像: ${imagePrefix}:${tag}" + sh "docker rmi -f ${imagePrefix}:${tag} || true" + } + } + } else { + echo "当前镜像数未超过 ${keepCount + 1} 个,无需清理" + } + sh """ + echo "当前镜像状态:" + docker images ${REGISTRY}/${NAMESPACE}/${IMAGE_NAME} --format 'table {{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}\\t{{.Size}}' + """ + // 无论成败都登出,清理凭证 + sh "docker logout ${REGISTRY}" + echo "容器仓库已登出,本地凭证已清理" + } + } + success { + // 输出构建结果 + echo "镜像构建成功!" + echo "镜像地址:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:${IMAGE_TAG}" + echo "latest 标签地址:${REGISTRY}/${NAMESPACE}/${IMAGE_NAME}:latest" + echo "对应代码提交:${GIT_COMMIT_SHORT}(${GIT_COMMIT_MSG})" + } + failure { + // 输出构建结果 + echo "镜像构建失败!" + } + } +} \ No newline at end of file diff --git a/SCM/build_image_lessie_sourcing_agents.groovy b/SCM/build_image_lessie_sourcing_agents.groovy index 6820689..f98b8fd 100644 --- a/SCM/build_image_lessie_sourcing_agents.groovy +++ b/SCM/build_image_lessie_sourcing_agents.groovy @@ -15,7 +15,7 @@ pipeline { listSize: "1" ) choice( - name: 'BUILD_env', + name: 'NAME_SPACES', choices: ['sit', 'test', 'prod'], description: '选择存放镜像的仓库命名空间:' ) @@ -27,7 +27,7 @@ pipeline { } environment { REGISTRY = "uswccr.ccs.tencentyun.com" // 镜像仓库地址 - NAMESPACE = "lessie${params.BUILD_env}" // 命名空间 + NAMESPACE = "lessie${params.NAME_SPACES}" // 命名空间 IMAGE_NAME = "lessie-sourcing-agents" // 镜像名(固定前缀) CREDENTIALS_ID = "dxin_img_hub_auth" // 容器仓库凭证ID } diff --git a/k8s_yaml/kubectl 命令/删除资源命令.md b/k8s_yaml/kubectl 命令/删除资源命令.md new file mode 100644 index 0000000..e69de29 diff --git a/k8s_yaml/kubectl 命令/常用命令.md b/k8s_yaml/kubectl 命令/常用命令.md index ed794d4..8a19d3f 100644 --- a/k8s_yaml/kubectl 命令/常用命令.md +++ b/k8s_yaml/kubectl 命令/常用命令.md @@ -2,4 +2,11 @@ kubectl logs --tail=10 test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie # 查看指定命名空间的 Pod 和节点信息 -kubectl get pods -n test-lessie -o wide \ No newline at end of file +kubectl get pods -n test-lessie -o wide + +# 基本删除命令 +kubectl delete deployment test-lessie-sourcing-api-deployment -n +kubectl delete -f test-lessie-go-api.yaml + + + diff --git a/k8s_yaml/public/ConfigMap/test-default-conf.yaml b/k8s_yaml/public/ConfigMap/test-default-conf.yaml new file mode 100644 index 0000000..d3bbf03 --- /dev/null +++ b/k8s_yaml/public/ConfigMap/test-default-conf.yaml @@ -0,0 +1,44 @@ +apiVersion: v1 +data: + default.conf: |- + server { + listen 80; + server_name _; + + # 前端静态文件 + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + # 精确匹配 index.html,禁用缓存 + location = /index.html { + root /usr/share/nginx/html; + add_header Cache-Control "no-cache, no-store, must-revalidate"; + } + + # 静态资源开启长缓存(带 hash) + location ~* \.(js|css|woff2|json|svg|png|jpg|jpeg|gif|ico|ttf|otf|eot|mp4|webm|webp)$ { + root /usr/share/nginx/html; + add_header Cache-Control "public, max-age=31536000, immutable"; + access_log off; + } + } +kind: ConfigMap +metadata: + creationTimestamp: "2025-10-30T06:21:32Z" + managedFields: + - apiVersion: v1 + fieldsType: FieldsV1 + fieldsV1: + f:data: + .: {} + f:default.conf: {} + manager: tke-platform-api + operation: Update + time: "2025-10-30T06:21:32Z" + name: test-default-conf + namespace: test-lessie + resourceVersion: "1846361542" + uid: 9361cebf-903c-4312-bc84-b6056cb054c7 diff --git a/k8s_yaml/public/ConfigMap/test-default.conf b/k8s_yaml/public/ConfigMap/test-default.conf new file mode 100644 index 0000000..e7a9c49 --- /dev/null +++ b/k8s_yaml/public/ConfigMap/test-default.conf @@ -0,0 +1,24 @@ +server { + listen 80; + server_name _; + + # 前端静态文件 + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + # 精确匹配 index.html,禁用缓存 + location = /index.html { + root /usr/share/nginx/html; + add_header Cache-Control "no-cache, no-store, must-revalidate"; + } + + # 静态资源开启长缓存(带 hash) + location ~* \.(js|css|woff2|json|svg|png|jpg|jpeg|gif|ico|ttf|otf|eot|mp4|webm|webp)$ { + root /usr/share/nginx/html; + add_header Cache-Control "public, max-age=31536000, immutable"; + access_log off; + } +} \ No newline at end of file diff --git a/k8s_yaml/概念知识点/架构变化.md b/k8s_yaml/概念知识点/架构变化.md new file mode 100644 index 0000000..bb02e94 --- /dev/null +++ b/k8s_yaml/概念知识点/架构变化.md @@ -0,0 +1,111 @@ +# 架构变化 +``` +用户访问域名 + ↓ +腾讯云 CLB(负载均衡) + ↓ +Ingress Controller(Ingress Pod) + ↓ +前端 Nginx Pod(静态资源) + ↓ +后端服务 Pod +``` + +## 原架构:前后端一体反代 + +- 拓扑: + DNS -> Nginx(域名证书 + 静态资源 + 反向代理) -> 后端服务 +- 特点: + - 域名证书与反向代理配置在同一 Nginx 配置文件中 + - Nginx 承担两种职责: + - 提供前端静态资源(HTML / JS / CSS) + - 反向代理接口到后端服务 + +## Kubernetes 化后的角色拆分 + +| 原功能 | Kubernetes 中的角色 | 描述 | +|---|---|---| +| SSL 终止 / 域名路由 | Ingress Controller(如 NGINX Ingress、Traefik) | 负责 HTTPS 证书管理、域名路由与转发 | +| 提供静态资源 | 前端 Pod(Nginx 容器) | 仅负责静态文件服务,不再负责后端代理 | +| 应用后端 | 后端 Pod(Deployment + Service) | 提供 API 服务,由 Ingress 转发调用 | + +## 前端 Pod 的 Docker 镜像 +- 仅提供静态资源,不负责代理: + - 静态文件目录:`/usr/share/nginx/html/`,放编译好的前端静态文件 + - Nginx 配置:`/etc/nginx/conf.d/default.conf`,为简化版,仅用于静态服务 +- 示例 `default.conf`: +```nginx +server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri /index.html; + } +} +``` +- 该配置可放入 ConfigMap,或直接 bake 到镜像中。无需 `upstream` / `proxy_pass`,反代由 Ingress 处理。 + +## Ingress 取代原 nginx.conf 的“反向代理部分” +- 示例 Ingress(由 Ingress Controller 生成对应的 Nginx 配置): +```yaml +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: jennie-frontend + namespace: jennie + annotations: + kubernetes.io/ingress.class: nginx + cert-manager.io/cluster-issuer: letsencrypt-prod +spec: + tls: + - hosts: + - s1.jennie.im + secretName: jennie-im-tls + rules: + - host: s1.jennie.im + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: jennie-frontend + port: + number: 80 + - path: /api/ + pathType: Prefix + backend: + service: + name: jennie-backend + port: + number: 8100 + - path: /sit-api/ + pathType: Prefix + backend: + service: + name: sit-api + port: + number: 8070 +``` +- SSL 证书通过 Secret 提供,例如: + `kubectl create secret tls jennie-im-tls --cert=... --key=...` +- Ingress 的路由规则等同于原 `nginx.conf` 中的多个 `location` 块,Ingress Controller 自动生成底层配置。 + +## 请求流向对比 +- 旧:客户端 -> Nginx (SSL + 反代 + 静态资源) -> 后端 +- 新:客户端 -> Ingress Controller Pod (SSL 终止 + 路由) -> 前端 Pod 或 后端 Pod + - 静态资源(/)由 Ingress 转发到前端 Pod + - API 请求(/api/*)由 Ingress 转发到后端 Pod + - 转发逻辑在 Ingress,不在前端 Pod 内 + +## ConfigMap、证书文件放置 + +| 项目 | 放置位置 | 说明 | +|---|---|---| +| 前端 `nginx.conf` | ConfigMap 或镜像内 | 若使用 Nginx 提供静态资源,可 bake 进镜像或挂载 ConfigMap | +| SSL 证书 | Secret | `type: kubernetes.io/tls` | +| Ingress 路由规则 | Ingress YAML | 替代原 `server` / `location` 的路由/反代配置 |