Google Kubernetes Engine:上传health-check.yaml时健康检查验证报错
Hey there! Let's get those validation errors sorted out—they're pointing to a couple of structural issues with your Deployment YAML, and we can fix them easily without skipping validation (which you definitely shouldn't do, since that hides real problems).
What's Causing the Errors?
Let's break down the two error messages:
unknown field "containers" in io.k8s.api.extensions.v1beta1.DeploymentSpec: Thecontainersfield doesn't belong directly underDeployment.specin theextensions/v1beta1API version. Containers are part of the Pod template, so they need to be nested insidespec.template.spec.missing required field "template" in io.k8s.api.extensions.v1beta1.DeploymentSpec: Every Deployment requires atemplatesection—this defines the blueprint for the Pods that the Deployment will manage. It's a mandatory field, so omitting it triggers this error.
Bonus Note: Deprecated API Version
Quick heads up: extensions/v1beta1 for Deployments is deprecated (it was removed in Kubernetes 1.22). For stability, you should switch to the apps/v1 API version instead—it's the current stable version for Deployments.
Corrected YAML Structure
Here's how to fix your health-check.yaml (I'll use the recommended apps/v1 version, but I'll note the differences if you need to stick with extensions/v1beta1 temporarily):
apiVersion: apps/v1 kind: Deployment metadata: name: health-check-deployment # Replace with your deployment name spec: replicas: 1 # Adjust replica count as needed selector: # Required for apps/v1: matches labels in the Pod template matchLabels: app: health-check template: metadata: labels: app: health-check # Matches the selector above spec: containers: # Now correctly nested under template.spec - name: health-check-container image: your-image:tag # Replace with your container image # Add your container config here (ports, probes, env vars, etc.) livenessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 5 periodSeconds: 10
If You Must Use extensions/v1beta1:
The structure is similar, but you don't need the selector field (though it's still good practice to include it):
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: health-check-deployment spec: replicas: 1 template: metadata: labels: app: health-check spec: containers: - name: health-check-container image: your-image:tag # Your container config
Why Skipping Validation Is a Bad Idea
Using --validate=false might let you apply the YAML, but it won't fix the underlying issue. The Deployment will fail to create Pods because the structure is invalid—you'll end up with more headaches later. Always fix the validation errors instead of bypassing them.
内容的提问来源于stack exchange,提问作者Nick Parsons

