You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kubernetes集群内DNS服务访问咨询:Pod的DNS访问URL是什么?

Accessing Kubernetes Pods via DNS: Correct URL Format

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
  • For the backend pod (90 port):

    • Simplified: curl backend-svc:90
    • FQDN: curl backend-svc.default.svc.cluster.local:90

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:53:56