gke-manifest-generation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGKE Manifest Generation Skill
GKE清单生成技能
This skill provides guidelines, tooling integration, and templates to translate
natural language descriptions or application code changes into secure,
compliant, and cost-effective Kubernetes YAML manifests optimized for both GKE
Autopilot and GKE Standard clusters.
本技能提供指南、工具集成和模板,可将自然语言描述或应用代码变更转换为针对GKE Autopilot和GKE Standard集群优化的、安全合规且具成本效益的Kubernetes YAML清单。
Core Rules & Verification
核心规则与验证
When generating or updating YAML manifests, you must strictly adhere to the
following rules:
生成或更新YAML清单时,必须严格遵守以下规则:
1. Namespace & Resource Isolation
1. 命名空间与资源隔离
- Explicit Namespace: Always declare explicitly in the metadata of every resource (Deployments, Services, ConfigMaps, Secrets, PVCs, Roles, bindings). Map it to the namespace configured in your active
namespace: {namespace}. Never omit the namespace.SETTINGS.md - Dedicated ServiceAccount: Avoid using the namespace's ServiceAccount. Always create and reference a dedicated
default(e.g.,ServiceAccount) for each microservice.devteam-agent-sa
- 显式命名空间:始终在每个资源(Deployments、Services、ConfigMaps、Secrets、PVCs、Roles、bindings)的metadata中显式声明。将其映射到您当前
namespace: {namespace}中配置的命名空间。绝不能省略命名空间。SETTINGS.md - 专用ServiceAccount:避免使用命名空间的ServiceAccount。应为每个微服务创建并引用专用的
default(例如ServiceAccount)。devteam-agent-sa
2. GKE Resource Tuning (Autopilot & Standard)
2. GKE资源调优(Autopilot与Standard)
-
Resources Requests & Limits: Always specify CPU and Memory requests and limits for all containers.
- GKE Autopilot: Requests determine pod billing directly; requests and limits must be equal. If they differ, Autopilot will automatically scale requests up to match limits, which can significantly increase costs.
- GKE Standard: Requests ensure stable scheduling and bin-packing; limits prevent resource starvation/noisy-neighbor issues.
-
Density Defaults: For stateless apps or sidecars on GKE Standard, default to conservative requests (e.g.,or
requests.cpu: "100m","200m"orrequests.memory: "256Mi") with burstable limits. Use a reasonable overcommit ratio for limits (e.g., 2x to 4x requests, like"512Mi"tolimits.cpu: "400m", and"800m"tolimits.memory: "512Mi"). Avoid excessive overcommit limits (like"1Gi"for alimits.cpu: "4"request) to prevent severe CPU throttling and latency degradation under heavy scheduling load, particularly in environments without guaranteed node shares.100m -
Spot VMs for Staging/Dev: For non-production workloads (e.g., namespaces containing,
-test, or-dev), or if the user requests cost optimization, automatically target GKE Spot VMs. This requires injecting both the-stagingtargeting Spot VMs AND the corresponding toleration to tolerate the Spot VM taint:nodeSelectoryamlnodeSelector: cloud.google.com/gke-spot: "true" tolerations: - key: "cloud.google.com/gke-spot" operator: "Equal" value: "true" effect: "NoSchedule"(On GKE Standard, this assumes a Spot node pool is configured).
-
资源请求与限制:始终为所有容器指定CPU和内存的请求(requests)与限制(limits)。
- GKE Autopilot:请求值直接决定Pod计费;请求值与限制值必须相等。若两者不同,Autopilot会自动将请求值扩容至与限制值匹配,这可能大幅增加成本。
- GKE Standard:请求值确保调度稳定和资源打包效率;限制值可防止资源耗尽或“噪声邻居”问题。
-
密度默认值:针对GKE Standard上的无状态应用或边车容器,默认使用保守的请求值(例如或
requests.cpu: "100m","200m"或requests.memory: "256Mi"),并配置可突发的限制值。为限制值设置合理的超配比例(例如请求值的2至4倍,如"512Mi"至limits.cpu: "400m","800m"至limits.memory: "512Mi")。避免过度超配限制值(例如请求值为"1Gi"时设置100m),以防在高调度负载下出现严重的CPU节流和延迟恶化,尤其是在无节点资源保障的环境中。limits.cpu: "4" -
预发布/开发环境使用Spot VM:针对非生产工作负载(例如名称包含、
-test或-dev的命名空间),或用户要求成本优化时,自动指定GKE Spot VM。这需要同时注入针对Spot VM的-staging和对应的容忍度(toleration),以容忍Spot VM的污点:nodeSelectoryamlnodeSelector: cloud.google.com/gke-spot: "true" tolerations: - key: "cloud.google.com/gke-spot" operator: "Equal" value: "true" effect: "NoSchedule"(在GKE Standard上,此配置假设已配置Spot节点池)。
3. Container Security Hardening (Pod Security Standards)
3. 容器安全加固(Pod安全标准)
- Non-Root Execution: Always configure at the Pod level (and container level if overriding) to run as a non-root user (e.g.,
securityContext,runAsNonRoot: true,runAsUser: 10000,runAsGroup: 10000). This is strictly enforced on GKE Autopilot and is a critical security baseline for GKE Standard.fsGroup: 10000 - Minimal Privileges: Always set and
allowPrivilegeEscalation: false.seccompProfile: {type: RuntimeDefault} - Read-Only Root Filesystem: Set to prevent modifications to the container image filesystem.
readOnlyRootFilesystem: true- Writable Directory Fallback: If is enabled, mount a local
readOnlyRootFilesystemvolume toemptyDiror/tmpto allow applications (like Java/Nginx) to write temp files without crashing./var/run/
- Writable Directory Fallback: If
- Secret Volume Mounting: Prefer mounting Secrets as read-only files
(configured in the spec with
volumes) instead of mapping them as environment variables, unless the application framework exclusively supports env-var based configuration. This prevents secrets leaking into application logs.defaultMode: 0400
- 非根用户执行:始终在Pod级别(若需覆盖则在容器级别)配置,以非根用户身份运行(例如
securityContext,runAsNonRoot: true,runAsUser: 10000,runAsGroup: 10000)。这在GKE Autopilot上是强制要求,也是GKE Standard的关键安全基线。fsGroup: 10000 - 最小权限:始终设置和
allowPrivilegeEscalation: false。seccompProfile: {type: RuntimeDefault} - 只读根文件系统:设置以防止修改容器镜像文件系统。
readOnlyRootFilesystem: true- 可写目录 fallback:若启用,需挂载本地
readOnlyRootFilesystem卷到emptyDir或/tmp,以允许应用(如Java/Nginx)写入临时文件而不崩溃。/var/run/
- 可写目录 fallback:若启用
- 密钥卷挂载:优先将Secrets以只读文件形式挂载(在配置中设置
volumes),而非映射为环境变量,除非应用框架仅支持基于环境变量的配置。这可防止密钥泄露到应用日志中。defaultMode: 0400
4. Health Checking (Mandatory Probes)
4. 健康检查(强制探针)
-
Liveness & Readiness Probes: Every Deployment container must define bothand
livenessProbe.readinessProbe- Web/API: Use probes.
httpGet - TCP Services: Use probes.
tcpSocket - Databases/Caches: Use command-based probes (e.g.,
exec).exec.command: ["redis-cli", "ping"]
- Web/API: Use
-
Startup Probes for Slow-Starting Apps: For applications with slow boot times (e.g., Java spring boot, complex Python scripts, LLM model servers), you must also define a. When a
startupProbeis defined, the liveness and readiness probes are disabled until it succeeds, preventing Kubernetes from prematurely killing the pod during startup:startupProbeyamlstartupProbe: httpGet: path: /healthz port: 8080 failureThreshold: 30 periodSeconds: 10 -
Sensible Defaults: Setto
initialDelaySeconds: 5depending on startup time (e.g., Java requires a longer delay than Go/Nginx).15
-
存活与就绪探针:每个Deployment容器必须同时定义和
livenessProbe。readinessProbe- Web/API:使用探针。
httpGet - TCP服务:使用探针。
tcpSocket - 数据库/缓存:使用基于命令的探针(例如
exec)。exec.command: ["redis-cli", "ping"]
- Web/API:使用
-
慢启动应用的启动探针:针对启动时间较长的应用(如Java Spring Boot、复杂Python脚本、LLM模型服务器),必须同时定义。当定义
startupProbe后,存活和就绪探针会在其成功前禁用,防止Kubernetes在启动阶段过早终止Pod:startupProbeyamlstartupProbe: httpGet: path: /healthz port: 8080 failureThreshold: 30 periodSeconds: 10 -
合理默认值:根据启动时间设置至
initialDelaySeconds: 5(例如Java所需延迟比Go/Nginx更长)。15
5. Services & Ingress Routing
5. 服务与Ingress路由
- Internal ClusterIP: Default all internal microservices to . Never use
type: ClusterIPortype: LoadBalancerunless the workload is explicitly intended to be publicly accessible from the internet.NodePort - Port Naming: Always assign clear, standard names to service and
container ports (e.g., or
name: http-web) to enable automatic protocol discovery, tracing, and Web App routing.name: grpc-api - Prefer Gateway API: When exposing APIs externally, prioritize using GKE
Gateway API (and
Gatewayresources) over legacyHTTPRouteobjects to enable advanced L7 routing and security features (e.g., Cloud Armor).Ingress
- 内部ClusterIP:所有内部微服务默认使用。除非工作负载明确需要从互联网公开访问,否则绝不要使用
type: ClusterIP或type: LoadBalancer。NodePort - 端口命名:始终为服务和容器端口分配清晰、标准的名称(例如或
name: http-web),以支持自动协议发现、追踪和Web应用路由。name: grpc-api - 优先使用Gateway API:对外暴露API时,优先使用GKE Gateway API(和
Gateway资源)而非传统HTTPRoute对象,以启用高级L7路由和安全功能(例如Cloud Armor)。Ingress
6. Volume Mounts, StorageClasses & subPath Safety
6. 卷挂载、StorageClass与subPath安全
- Avoid Directory Overwrites: When mounting a or
ConfigMapto an application directory containing other files (like Nginx public directories), always useSecretto overlay only the specific file. Caveat: Note that containers usingsubPathvolume mounts do not receive automatic configuration updates if the underlying ConfigMap or Secret is modified; pods must be restarted manually to pick up changes.subPath - StorageClass Selection: Use the correct GKE storage class in
PersistentVolumeClaims:
- CSI Driver Clusters (Autopilot & Modern Standard): Use (default balanced PD) or
standard-rwo(SSD PD).premium-rwo - Legacy Standard Clusters: Use (default PD) or
standard(SSD PD) ifpremium/standard-rwoare not configured.premium-rwo - Database rule: Use SSD storage classes (or
premium-rwo) only when the prompt explicitly requests high IOPS, low latency, or database storage.premium
- CSI Driver Clusters (Autopilot & Modern Standard): Use
- 避免目录覆盖:将ConfigMap或Secret挂载到包含其他文件的应用目录(如Nginx公共目录)时,务必使用仅覆盖特定文件。 注意:使用
subPath卷挂载的容器不会在底层ConfigMap或Secret修改时自动接收配置更新;必须手动重启Pod才能获取变更。subPath - StorageClass选择:在PersistentVolumeClaims中使用正确的GKE存储类:
- CSI驱动集群(Autopilot与现代Standard):使用(默认平衡型PD)或
standard-rwo(SSD PD)。premium-rwo - 传统Standard集群:若未配置/
standard-rwo,则使用premium-rwo(默认PD)或standard(SSD PD)。premium - 数据库规则:仅当明确要求高IOPS、低延迟或数据库存储时,才使用SSD存储类(或
premium-rwo)。premium
- CSI驱动集群(Autopilot与现代Standard):使用
7. High Availability on GKE
7. GKE高可用性
- Topology Spread: For deployments with >1 replica, use or
podAntiAffinitywithtopologySpreadConstraintsto distribute pods across GKE nodes and availability zones.topologyKey: "kubernetes.io/hostname" - PodDisruptionBudget: For deployments with >1 replica, declare a
to guarantee minimum replica availability during voluntary GKE node upgrades and maintenance cycles.
PodDisruptionBudget
- 拓扑分布:对于副本数>1的部署,使用或
podAntiAffinity并设置topologySpreadConstraints,以在GKE节点和可用区之间分布Pod。topologyKey: "kubernetes.io/hostname" - PodDisruptionBudget:对于副本数>1的部署,声明以确保在GKE节点自愿升级和维护周期内的最小副本可用性。
PodDisruptionBudget
8. Updates & Server-Side Apply Reconciliations
8. 更新与Server-Side Apply协调
- Stable List Keys: Under Kubernetes Server-Side Apply (SSA), elements in
associative lists (like volumes, volume mounts, ports, and container
definitions) are matched and merged by their unique identifier keys
(typically ). You must keep the
namekey stable when modifying properties of an existing list item. Renaming thenamekey will cause SSA to create a brand new entry and leave the old entry intact (orphaned) rather than modifying it.name - Minimal Diff: Make only the changes requested. Adhere closely to existing labels, annotations, and conventions.
- 稳定列表键:在Kubernetes Server-Side Apply(SSA)下,关联列表(如volumes、volume mounts、ports和container定义)中的元素通过其唯一标识键(通常为)进行匹配和合并。修改现有列表项的属性时,必须保持
name键稳定。重命名name键会导致SSA创建全新条目并保留旧条目(孤立),而非修改原有条目。name - 最小差异:仅进行请求的变更。严格遵循现有标签、注解和约定。
Specialty Workloads: GKE AI/Inference Serving (vLLM, TGI, etc.)
专用工作负载:GKE AI/推理服务(vLLM、TGI等)
For model serving workloads, prioritize using optimized tooling like GKE
Inference Quickstart if available. If generating manually:
- GPU Request & Allocation:
- Always request in both
nvidia.com/gpuandrequests.limits - Add a or node affinity targeting the desired GKE accelerator tag (e.g.,
nodeSelector).cloud.google.com/gke-accelerator: nvidia-l4
- Always request
- Shared Memory Boost:
- Model servers require high shared memory () for inter-process communications. Always declare and mount an
/dev/shmvolume withemptyDirtomedium: Memory./dev/shm
- Model servers require high shared memory (
- Weight Loading Optimization:
- Mount model weight directories (like GCS buckets) using the GKE GCS Fuse
CSI driver () as
csi.storage.gke.iofor efficient cold-starts.readOnly: true
- Mount model weight directories (like GCS buckets) using the GKE GCS Fuse
CSI driver (
针对模型服务工作负载,优先使用优化工具(如GKE Inference Quickstart,若可用)。若手动生成:
- GPU请求与分配:
- 始终在和
requests中请求limits。nvidia.com/gpu - 添加或节点亲和性,指定目标GKE加速器标签(例如
nodeSelector)。cloud.google.com/gke-accelerator: nvidia-l4
- 始终在
- 共享内存优化:
- 模型服务器需要高共享内存()用于进程间通信。始终声明并挂载
/dev/shm卷,设置emptyDir并映射到medium: Memory。/dev/shm
- 模型服务器需要高共享内存(
- 权重加载优化:
- 使用GKE GCS Fuse CSI驱动()将模型权重目录(如GCS存储桶)以
csi.storage.gke.io方式挂载,实现高效冷启动。readOnly: true
- 使用GKE GCS Fuse CSI驱动(
Tooling & Grounding Guidelines
工具与参考指南
When generating manifests, you should leverage the following tooling to reduce
hallucinations and optimize configurations:
-
Inference Workloads (GKE Inference Quickstart CLI):
-
Make sure you have the Google Cloud SDK installed.
-
For all AI/LLM inference workloads (e.g. model serving), you must prioritize using theCLI GKE Inference Quickstart command to generate the optimized manifests instead of writing them manually:
gcloudbashgcloud container ai profiles manifests create \ --model={model_name} \ --model-server={server_name} \ --accelerator-type={accelerator_type} \ --output=manifest \ --output-path={output_file_path} -
Constraint: You must include all resources returned by this command (Deployments, Services, PodMonitoring, etc.) without filtering.
-
-
Grounding in Official Documentation (Developer Knowledge API):
- For GKE-specific features, API defaults, manifest examples, or security
contexts, you must query Google's developer knowledge base to
retrieve official GKE documentation:
- : Use this to ask direct questions (e.g., "How to configure GCS Fuse CSI driver in GKE"). This is the preferred tool for general queries.
answer_query - : Use this to search for relevant GKE guides or examples when you don't have a specific question.
search_documents - : Use this to fetch full document contents when you have a specific document ID.
get_document
- For GKE-specific features, API defaults, manifest examples, or security
contexts, you must query Google's developer knowledge base to
retrieve official GKE documentation:
生成清单时,应利用以下工具减少幻觉并优化配置:
-
推理工作负载(GKE Inference Quickstart CLI):
-
确保已安装Google Cloud SDK。
-
针对所有AI/LLM推理工作负载(如模型服务),必须优先使用CLI的GKE Inference Quickstart命令生成优化清单,而非手动编写:
gcloudbashgcloud container ai profiles manifests create \ --model={model_name} \ --model-server={server_name} \ --accelerator-type={accelerator_type} \ --output=manifest \ --output-path={output_file_path} -
约束:必须包含此命令返回的所有资源(Deployments、Services、PodMonitoring等),不得过滤。
-
-
官方文档参考(开发者知识API):
- 针对GKE特定功能、API默认值、清单示例或安全上下文,必须查询Google开发者知识库以获取官方GKE文档:
- :用于直接提问(例如*"如何在GKE中配置GCS Fuse CSI驱动"*)。这是通用查询的首选工具。
answer_query - :当没有特定问题时,用于搜索相关GKE指南或示例。
search_documents - :当拥有特定文档ID时,用于获取完整文档内容。
get_document
- 针对GKE特定功能、API默认值、清单示例或安全上下文,必须查询Google开发者知识库以获取官方GKE文档:
Reference Examples
参考示例
For detailed, production-ready manifest templates, consult the following
reference guides:
- Basic Hardened Nginx Workload: Production-ready deployment with dedicated service account, security contexts, probes, anti-affinity, and PodDisruptionBudget.
- Network Policy: Default-deny ingress network policy and selective ingress allowance for specific apps.
- AI/LLM Inference Workload: GPU resource
allocation, Workload Identity, GCS FUSE CSI driver mounting, shared memory boost, and startup probes.
/dev/shm - GKE Gateway API Routing: Exposing workloads
using GKE L7 Gateway API (and
Gatewayresources).HTTPRoute
如需详细的生产就绪清单模板,请查阅以下参考指南:
- 基础加固Nginx工作负载:包含专用ServiceAccount、安全上下文、探针、反亲和性和PodDisruptionBudget的生产就绪部署。
- 网络策略:默认拒绝Ingress的网络策略,以及针对特定应用的选择性Ingress允许规则。
- AI/LLM推理工作负载:GPU资源分配、Workload Identity、GCS FUSE CSI驱动挂载、共享内存优化和启动探针。
/dev/shm - GKE Gateway API路由:使用GKE L7 Gateway API(和
Gateway资源)暴露工作负载。HTTPRoute