service-configmap-deployment-template.yaml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. # ============================================================
  2. # 通用 ConfigMap 模板(非敏感配置)
  3. # 用于所有 34 个微服务
  4. # ============================================================
  5. # 此模板展示如何为微服务创建 ConfigMap
  6. # 将 [SERVICE_NAME] 替换为实际的服务名称(如 shop-recycle-payment)
  7. # 将 [PORT] 替换为实际的端口号(从 conf/application.yml 中获取)
  8. ---
  9. apiVersion: v1
  10. kind: ConfigMap
  11. metadata:
  12. name: [SERVICE_NAME]-config
  13. namespace: default
  14. labels:
  15. app: [SERVICE_NAME]
  16. config-type: public
  17. version: "1.0.0"
  18. data:
  19. # 应用配置文件(非敏感部分)
  20. application.yml: |
  21. server:
  22. port: [PORT]
  23. servlet:
  24. context-path: [CONTEXT_PATH] # 可选,如 /login-center
  25. spring:
  26. application:
  27. name: [SERVICE_NAME]
  28. main:
  29. allow-bean-definition-overriding: true
  30. cloud:
  31. nacos:
  32. discovery:
  33. server-addr: ${NACOS_SERVER_ADDR:nacos.bak.com}:${NACOS_PORT:8848}
  34. service: ${spring.application.name}
  35. weight: 1
  36. # 数据库配置(如果需要)
  37. # datasource:
  38. # type: com.zaxxer.hikari.HikariDataSource
  39. # driver-class-name: com.mysql.cj.jdbc.Driver
  40. # url: jdbc:mysql://[DB_HOST]:3306/[DATABASE_NAME]?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
  41. # username: ${DB_USERNAME} # 从 Secret 注入
  42. # password: ${DB_PASSWORD} # 从 Secret 注入
  43. # hikari:
  44. # connection-timeout: 30000
  45. # maximum-pool-size: [POOL_SIZE] # 根据服务调整,通常 10-100
  46. # minimum-idle: 1
  47. # Redis Sentinel 配置
  48. redis:
  49. sentinel:
  50. master: mymaster
  51. nodes:
  52. - redis.jxfx1.com:27000
  53. - redis.jxfx2.com:27000
  54. - redis.jxfx3.com:27000
  55. password: ${REDIS_PASSWORD} # 从 Secret 注入
  56. database: ${REDIS_DATABASE:15}
  57. # RabbitMQ 配置
  58. rabbitmq:
  59. host: mq.bak.com
  60. port: 5672
  61. username: ${RABBITMQ_USERNAME} # 从 Secret 注入
  62. password: ${RABBITMQ_PASSWORD} # 从 Secret 注入
  63. virtualHost: [VHOST_NAME] # 如 shop-recycle-msg, shop-recycle-order-image 等
  64. # Logging 配置
  65. logging:
  66. level:
  67. org.springframework: INFO
  68. com.ssm: DEBUG
  69. # Dubbo 配置(如果需要)
  70. # dubbo:
  71. # application:
  72. # name: ${spring.application.name}
  73. # provider:
  74. # registry:
  75. # address: nacos://${NACOS_SERVER_ADDR:nacos.bak.com}:${NACOS_PORT:8848}
  76. # filter: tracing
  77. # protocol:
  78. # name: dubbo
  79. # port: [DUBBO_PORT]
  80. # consumer:
  81. # registry:
  82. # address: nacos://${NACOS_SERVER_ADDR:nacos.bak.com}:${NACOS_PORT:8848}
  83. # MyBatis Plus 配置(如果需要)
  84. # mybatis-plus:
  85. # mapper-locations: classpath:/mapper/*Mapper.xml
  86. # global-config:
  87. # id-type: 0
  88. # db-column-underline: true
  89. # logic-delete-value: 1
  90. # logic-not-delete-value: 0
  91. # configuration:
  92. # map-underscore-to-camel-case: true
  93. # cache-enabled: false
  94. # MongoDB 配置(如果需要)
  95. # data:
  96. # mongodb:
  97. # database: recycle
  98. # host: mg.bak.com
  99. # port: 27017
  100. # username: ${MONGODB_USERNAME} # 从 Secret 注入
  101. # password: ${MONGODB_PASSWORD} # 从 Secret 注入
  102. # 业务配置
  103. nacos:
  104. service-address: nacos.bak.com
  105. port: 8848
  106. # Swagger 配置
  107. swagger:
  108. show: false
  109. is-reveal-error: true
  110. # 应用配置文件(properties 格式)
  111. application.properties: |
  112. spring.application.name=[SERVICE_NAME]
  113. spring.protocol.name=spring
  114. server.port=[PORT]
  115. ---
  116. # ============================================================
  117. # 通用 Deployment 模板
  118. # ============================================================
  119. apiVersion: apps/v1
  120. kind: Deployment
  121. metadata:
  122. name: [SERVICE_NAME]
  123. namespace: default
  124. labels:
  125. app: [SERVICE_NAME]
  126. service-type: [SERVICE_TYPE] # 如:backend, web, gateway
  127. version: "1.0.0"
  128. spec:
  129. replicas: [REPLICAS] # dev=1, staging=2, prod=3
  130. strategy:
  131. type: RollingUpdate
  132. rollingUpdate:
  133. maxSurge: 1
  134. maxUnavailable: 0
  135. selector:
  136. matchLabels:
  137. app: [SERVICE_NAME]
  138. template:
  139. metadata:
  140. labels:
  141. app: [SERVICE_NAME]
  142. service-type: [SERVICE_TYPE]
  143. version: "1.0.0"
  144. annotations:
  145. prometheus.io/scrape: "true"
  146. prometheus.io/port: "[PORT]"
  147. prometheus.io/path: "/actuator/prometheus"
  148. spec:
  149. containers:
  150. - name: [SERVICE_NAME]
  151. image: [IMAGE_REGISTRY]/[SERVICE_NAME]:[IMAGE_TAG]
  152. imagePullPolicy: IfNotPresent
  153. # 端口配置
  154. ports:
  155. - name: http
  156. containerPort: [PORT]
  157. protocol: TCP
  158. # 环境变量 - 来自 ConfigMap
  159. envFrom:
  160. - configMapRef:
  161. name: [SERVICE_NAME]-config
  162. # 环境变量 - 来自 Secret(敏感信息)
  163. env:
  164. # JVM 参数
  165. - name: JAVA_OPTS
  166. value: "-Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Xss256k -XX:+DisableExplicitGC"
  167. - name: TZ
  168. value: "Asia/Shanghai"
  169. # 数据库凭证(从 common-db-credentials Secret)
  170. - name: DB_USERNAME
  171. valueFrom:
  172. secretKeyRef:
  173. name: common-db-credentials
  174. key: db-username
  175. - name: DB_PASSWORD
  176. valueFrom:
  177. secretKeyRef:
  178. name: common-db-credentials
  179. key: db-password
  180. - name: DB_HOST
  181. valueFrom:
  182. secretKeyRef:
  183. name: common-db-credentials
  184. key: db-host
  185. # Redis 凭证(从 common-redis-credentials Secret)
  186. - name: REDIS_PASSWORD
  187. valueFrom:
  188. secretKeyRef:
  189. name: common-redis-credentials
  190. key: redis-password
  191. - name: REDIS_DATABASE
  192. valueFrom:
  193. secretKeyRef:
  194. name: common-redis-credentials
  195. key: redis-database
  196. # RabbitMQ 凭证(从 common-rabbitmq-credentials Secret)
  197. - name: RABBITMQ_USERNAME
  198. valueFrom:
  199. secretKeyRef:
  200. name: common-rabbitmq-credentials
  201. key: rabbitmq-username
  202. - name: RABBITMQ_PASSWORD
  203. valueFrom:
  204. secretKeyRef:
  205. name: common-rabbitmq-credentials
  206. key: rabbitmq-password
  207. # 微信凭证(仅 5 个需要微信配置的服务)
  208. # - name: WECHAT_STORE_APP_SECRET
  209. # valueFrom:
  210. # secretKeyRef:
  211. # name: wechat-credentials
  212. # key: store-app-secret
  213. # 挂载 ConfigMap
  214. volumeMounts:
  215. - name: config-volume
  216. mountPath: /app/conf
  217. readOnly: true
  218. # 资源限制
  219. resources:
  220. requests:
  221. cpu: [REQUEST_CPU] # 根据服务调整,通常 100m-500m
  222. memory: [REQUEST_MEMORY] # 根据服务调整,通常 256Mi-1Gi
  223. limits:
  224. cpu: [LIMIT_CPU] # 通常为 requests 的 2-4 倍
  225. memory: [LIMIT_MEMORY] # 通常为 requests 的 2 倍
  226. # 健康检查 - 就绪探针
  227. readinessProbe:
  228. httpGet:
  229. path: /actuator/health/readiness
  230. port: http
  231. scheme: HTTP
  232. initialDelaySeconds: 30
  233. periodSeconds: 10
  234. timeoutSeconds: 5
  235. successThreshold: 1
  236. failureThreshold: 3
  237. # 健康检查 - 存活探针
  238. livenessProbe:
  239. httpGet:
  240. path: /actuator/health/liveness
  241. port: http
  242. scheme: HTTP
  243. initialDelaySeconds: 60
  244. periodSeconds: 15
  245. timeoutSeconds: 5
  246. successThreshold: 1
  247. failureThreshold: 3
  248. # 启动检查
  249. startupProbe:
  250. httpGet:
  251. path: /actuator/health
  252. port: http
  253. scheme: HTTP
  254. initialDelaySeconds: 0
  255. periodSeconds: 5
  256. timeoutSeconds: 3
  257. successThreshold: 1
  258. failureThreshold: 30
  259. # 优雅关闭
  260. lifecycle:
  261. preStop:
  262. exec:
  263. command: ["/bin/sh", "-c", "sleep 15"]
  264. # 卷配置
  265. volumes:
  266. - name: config-volume
  267. configMap:
  268. name: [SERVICE_NAME]-config
  269. items:
  270. - key: application.yml
  271. path: application.yml
  272. - key: application.properties
  273. path: application.properties
  274. # Pod 调度策略
  275. affinity:
  276. podAntiAffinity:
  277. preferredDuringSchedulingIgnoredDuringExecution:
  278. - weight: 100
  279. podAffinityTerm:
  280. labelSelector:
  281. matchExpressions:
  282. - key: app
  283. operator: In
  284. values:
  285. - [SERVICE_NAME]
  286. topologyKey: kubernetes.io/hostname
  287. # 容忍污点
  288. tolerations:
  289. - key: "apps"
  290. operator: "Equal"
  291. value: "true"
  292. effect: "NoSchedule"
  293. # 安全上下文
  294. securityContext:
  295. runAsNonRoot: false
  296. runAsUser: 0
  297. # 终止宽限期
  298. terminationGracePeriodSeconds: 30
  299. ---
  300. # ============================================================
  301. # 通用 Service 模板
  302. # ============================================================
  303. apiVersion: v1
  304. kind: Service
  305. metadata:
  306. name: [SERVICE_NAME]
  307. namespace: default
  308. labels:
  309. app: [SERVICE_NAME]
  310. service-type: [SERVICE_TYPE]
  311. annotations:
  312. description: "[SERVICE_DESCRIPTION]"
  313. spec:
  314. type: ClusterIP
  315. selector:
  316. app: [SERVICE_NAME]
  317. ports:
  318. - name: http
  319. port: [PORT]
  320. targetPort: [PORT]
  321. protocol: TCP
  322. sessionAffinity: ClientIP
  323. sessionAffinityConfig:
  324. clientIP:
  325. timeoutSeconds: 10800