Kubernetes集群内DNS服务访问咨询:Pod的DNS访问URL是什么?
Hey there! Let's sort out how to access your pods using Kubernetes DNS, since you've already confirmed direct Node IP access works. Here's what you need to know:
First, a critical note: Kubernetes DNS doesn't resolve Pod IPs directly
Pods are ephemeral by nature, so Kubernetes DNS is designed to work with Services—stable endpoints that route traffic to your pods. If you haven't created Services for your two pods yet, that's the first must-do step.
Step 1: Create Services for your pods
Each pod needs a corresponding Service that targets its listening port, with a selector matching the pod's labels. Here are example configurations:
Gateway Pod (listening on 80)
Save this as gateway-svc.yaml:
apiVersion: v1 kind: Service metadata: name: gateway-svc namespace: default spec: selector: # Replace this with your gateway pod's actual label (e.g., app: gateway) app: gateway ports: - protocol: TCP port: 80 # Service port (can match pod port, or use a different value) targetPort: 80 # Must exactly match your pod's listening port
Backend Pod (listening on 90)
Save this as backend-svc.yaml:
apiVersion: v1 kind: Service metadata: name: backend-svc namespace: default spec: selector: # Replace this with your backend pod's actual label (e.g., app: backend) app: backend ports: - protocol: TCP port: 90 targetPort: 90
Apply these Services to your cluster:
kubectl apply -f gateway-svc.yaml -f backend-svc.yaml
Step 2: Verify Services are linked to pods
Make sure the Services are correctly targeting your pods by checking the ENDPOINTS field:
kubectl get svc -n default
You should see your pod IPs listed under ENDPOINTS for each Service. If not, double-check that the Service's selector matches the labels on your pods.
Step 3: Correct DNS access URLs
Once Services are set up, you can access them via DNS using these formats (from within the cluster):
For the gateway pod (80 port):
- Simplified (same namespace):
curl gateway-svc - Full qualified domain name (FQDN):
curl gateway-svc.default.svc.cluster.local
- Simplified (same namespace):
For the backend pod (90 port):
- Simplified:
curl backend-svc:90 - FQDN:
curl backend-svc.default.svc.cluster.local:90
- Simplified:
Quick troubleshooting tip
If DNS still isn't working, test resolution from within a cluster pod to confirm:
kubectl run -it --rm dns-test --image=busybox:1.28 -- nslookup gateway-svc.default.svc.cluster.local
This should return the Service's cluster IP if DNS is configured correctly.
内容的提问来源于stack exchange,提问作者Martin Dvoracek

