AWS Kops gossip集群中如何创建带域名后缀的K8s Pod及Service?
Let's break this down clearly first: Kubernetes enforces strict naming rules for Pod (and most resource) metadata names—they must match the DNS_LABEL regex [a-z]([-a-z0-9]*[a-z0-9])?, which means no multiple dots, uppercase letters, or special characters beyond hyphens. This is a hard constraint from the API server, so setting the hostname field alone won't fix it (that only affects the Pod's internal hostname, not its resource name in the cluster).
Here are the actionable solutions based on your actual goal:
1. If you want Pods to be accessible via api-manager.iot.test.co.nz in the cluster
You don't need to name the Pods with that full domain—instead, use Kubernetes' built-in DNS features plus CoreDNS customization to map that domain to your Pods:
Step 1: Deploy your Pods with valid metadata names + configure hostname/subdomain
First, create your Deployment with a valid Pod name pattern (the Deployment will auto-suffix Pods with unique IDs, e.g., api-manager-7f9d6c8b4-2xqzv). Add hostname and subdomain to the Pod template (note: subdomain must also follow DNS_LABEL rules, so replace dots with hyphens):
apiVersion: apps/v1 kind: Deployment metadata: name: api-manager-deployment namespace: your-namespace spec: replicas: 3 selector: matchLabels: app: api-manager template: metadata: labels: app: api-manager spec: hostname: api-manager subdomain: iot-test-co-nz # Valid DNS_LABEL format (no dots) containers: - name: api-manager-container image: your-image:tag ports: - containerPort: 80
Step 2: Create a Headless Service matching the subdomain
Headless Services let Kubernetes assign a stable DNS name to each Pod. Name this Service exactly the same as the subdomain you set above:
apiVersion: v1 kind: Service metadata: name: iot-test-co-nz namespace: your-namespace spec: clusterIP: None # Mark as headless selector: app: api-manager ports: - port: 80 targetPort: 80
Now each Pod will have a FQDN like api-manager.iot-test-co-nz.your-namespace.svc.clusters.test.k8s.local.
Step 3: Map your custom domain via CoreDNS
To make api-manager.iot.test.co.nz resolve to these Pods, edit the CoreDNS ConfigMap in the kube-system namespace:
kubectl edit configmap coredns -n kube-system
Add a custom zone to the Corefile section to forward your domain to the Headless Service's DNS:
api-manager.iot.test.co.nz:53 { errors cache 30 forward . iot-test-co-nz.your-namespace.svc.clusters.test.k8s.local }
Save the changes, then restart CoreDNS to apply the new config:
kubectl rollout restart deployment coredns -n kube-system
Now any cluster-internal request to api-manager.iot.test.co.nz will resolve to your Pods.
2. If you only need the Pod's internal hostname to be api-manager.iot.test.co.nz
If you don't need cluster-wide access via that domain, just want the Pod itself to report that hostname, you can set the hostname field directly (Kubernetes allows more flexibility here than metadata names):
spec: template: spec: hostname: api-manager.iot.test.co.nz containers: - name: api-manager-container image: your-image:tag
Just remember the Pod's metadata name (auto-generated by the Deployment) will still follow the valid DNS_LABEL format—this only changes what the Pod sees as its own hostname.
Key Takeaway
You can't use a full domain with multiple dots as a Pod's metadata name (it's a non-negotiable Kubernetes rule), but you can achieve either cluster-wide access via that domain or set the Pod's internal hostname to match it using the steps above.
内容的提问来源于stack exchange,提问作者Shahar Hamuzim Rajuan

