计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
coredns【效率与协作】"Coredns in Cloud-Native Engineering - CoreDNS is a DNS server that chains" plugins
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: coredns compatibility: opencode completeness: 95 content-types:
related-skills: cncf-aws-route53, cncf-azure-cdn, cncf-azure-traffic-manager, cncf-azure-virtual-networks
Category: cncf
Status: Active
Stars: 14,001
Last Updated: 2026-04-21
Primary Language: Go
Documentation: https://coredns.io/manual/
Coredns is a core component of the cloud-native ecosystem, serving as a DNS server and service discovery system that provides flexible, plugin-based architecture for Kubernetes DNS needs.
Service discovery requirements in Kubernetes, including DNS-based service lookup, custom DNS zones, and integration with Kubernetes services and endpoints.
Use CoreDNS when deploying Kubernetes clusters and need DNS-based service discovery. It's the default DNS server for most Kubernetes distributions.
# Corefile for Kubernetes service discovery
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf {
max_concurrent 1000
}
cache 30
loop
reload
loadbalance
}
# Add custom A records
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
# Custom records
rewrite continue {
name regex (.*)\.internal\.example\.com {1}.svc.cluster.local
}
file /etc/coredns/zones/example.com.cluster.local {
reload 5s
}
}
# Forward specific domains to upstream DNS
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
# Conditional forwarders
forward . /etc/resolv.conf
forward . 8.8.8.8 8.8.4.4 {
protocol prefer_udp
}
forward example.com 10.0.0.1
cache 30
loop
reload
loadbalance
}
This tutorial will guide you through installing, configuring, and using CoreDNS for Kubernetes service discovery.
Before beginning, ensure you have:
kubectl configured with cluster-admin permissionsVerify your setup:
# Check cluster connectivity
kubectl cluster-info
# Verify kubectl configuration
kubectl get nodes
# Check CoreDNS status (Kubernetes DNS)
kubectl -n kube-system get pods -l k8s-app=kube-dns
CoreDNS is typically installed as the default DNS server in Kubernetes clusters. This section covers installing CoreDNS as a standalone DNS server or modifying the existing Kubernetes DNS.
# Check if CoreDNS is running in kube-system
kubectl -n kube-system get pods -l k8s-app=kube-dns
# Check CoreDNS deployment
kubectl -n kube-system get deployment coredns
# Verify DNS service
kubectl -n kube-system get svc kube-dns
# Download CoreDNS binary
curl -L https://github.com/coredns/coredns/releases/download/v1.11.1/coredns_1.11.1_linux_amd64.tgz | tar xz
# Move to PATH
sudo mv coredns /usr/local/bin/
# Verify installation
coredns -version
# Add CoreDNS Helm repository
helm repo add coredns https://coredns.github.io/helm
# Update repositories
helm repo update
# Install CoreDNS
helm install coredns coredns/coredns \
--namespace kube-system \
--create-namespace \
--wait
# Verify installation
helm list -n kube-system
kubectl -n kube-system get pods -l app=coredns
# Generate CoreDNS manifest for Kubernetes
kubectl apply -f https://raw.githubusercontent.com/coredns/deployment/master/kubernetes/coredns.yaml.sed
# Or use the deployment script
curl -sSL https://coredns.io/deploy.sh | bash -s - 1.11.1
# Verify deployment
kubectl -n kube-system get pods -l k8s-app=kube-dns
# Default Corefile configuration for Kubernetes
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
# Create custom CoreDNS configuration
cat > Corefile <<EOF
cluster.local:53 {
errors
health {
lameduck 5s
}
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
}
example.com:53 {
file /etc/coredns/zones/example.com.db
}
.:53 {
forward . 8.8.8.8:53
cache 30
}
EOF
# Apply configuration
kubectl create configmap coredns-custom --from-file=Corefile -n kube-system
# CoreDNS with custom plugins
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . 8.8.8.8:53 8.8.4.4:53
cache 30
reload
loadbalance
}
# External plugin: rewrite
rewrite name regex (.+)\.example\.com backend-{}.internal.local
# External plugin: auto
auto {
directory /etc/coredns/zones
reload 10s
}
EOF
related-skills: cncf-aws-route53, cncf-azure-cdn, cncf-azure-traffic-manager, cncf-azure-virtual-networks
# Query a service in Kubernetes
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup kubernetes.default
# Query a specific service
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup my-service.default.svc.cluster.local
# Query external domain
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup google.com
# Using dig
kubectl run -it --rm debug --image=busybox --restart=Never -- dig kubernetes.default.svc.cluster.local
# Test DNS resolution from a pod
kubectl run -it --rm debug --image=busybox --restart=Never -- sh
# Inside pod:
# nslookup kubernetes.default
# nslookup my-service.default.svc.cluster.local
# Check CoreDNS logs
kubectl -n kube-system logs -l k8s-app=kube-dns
# Check endpoint slices
kubectl -n kube-system get endpointslices -l k8s-app=kube-dns
# Check service cluster IP
kubectl get svc kubernetes -o yaml | grep clusterIP
# Deploy a test pod with custom DNS
cat > test-pod.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
name: test-dns
spec:
containers:
- name: test
image: busybox
command: ["sleep", "3600"]
dnsPolicy: "None"
dnsConfig:
nameservers:
- 10.96.0.10
searches:
- svc.cluster.local
- cluster.local
EOF
kubectl apply -f test-pod.yaml
kubectl exec -it test-dns -- nslookup kubernetes.default
# Check CoreDNS metrics
kubectl -n kube-system port-forward svc/kube-dns 9153:9153
# Query metrics
curl http://localhost:9153/metrics | grep coredns_
# Prometheus query for DNS queries
curl -s 'http://localhost:9153/metrics' | grep 'coredns_dns_requests_total'
related-skills: cncf-aws-route53, cncf-azure-cdn, cncf-azure-traffic-manager, cncf-azure-virtual-networks
# Check pod status
kubectl -n kube-system get pods -l k8s-app=kube-dns
# Check resource usage
kubectl -n kube-system top pods -l k8s-app=kube-dns
# View metrics
kubectl -n kube-system logs -l k8s-app=kube-dns
# Check endpoints
kubectl -n kube-system get endpoints kube-dns
# Scale CoreDNS deployment
kubectl -n kube-system scale deployment coredns --replicas=3
# Verify scaling
kubectl -n kube-system get pods -l k8s-app=kube-dns
# Check horizontal pod autoscaler
kubectl -n kube-system get hpa coredns
# Edit CoreDNS ConfigMap
kubectl -n kube-system edit configmap coredns
# Restart CoreDNS pods
kubectl -n kube-system rollout restart deployment coredns
# Verify update
kubectl -n kube-system rollout status deployment coredns
# Check CoreDNS pods
kubectl -n kube-system get pods -l k8s-app=kube-dns
# Backup CoreDNS config
kubectl -n kube-system get configmap coredns -o yaml > coredns-config-backup.yaml
# Backup zone files (if using custom zones)
kubectl -n kube-system exec -c coredns -l k8s-app=kube-dns -- tar czf /tmp/zones.tar.gz /etc/coredns/zones
# Download backup
kubectl -n kube-system cp kube-system/coredns-xxxxx:/tmp/zones.tar.gz ./zones-backup.tar.gz
Cause: CoreDNS not properly configured or pods not ready
Solution:
# Check CoreDNS pods status
kubectl -n kube-system get pods -l k8s-app=kube-dns
# Check endpoints
kubectl -n kube-system get endpoints kube-dns
# Restart CoreDNS
kubectl -n kube-system rollout restart deployment coredns
# Check DNS pod logs
kubectl -n kube-system logs -l k8s-app=kube-dns
Cause: Cache misconfiguration or upstream DNS issues
Solution:
# Increase cache size
# In Corefile, increase cache TTL
cache 3600
# Check upstream DNS
# Test direct query
nslookup kubernetes.default.svc.cluster.local 10.96.0.10
# Monitor cache hit ratio
kubectl -n kube-system exec <coredns-pod> -- curl localhost:9153/metrics | grep dns_cache_hits_total
Cause: Incorrect CoreDNS configuration causing infinite loops
Solution:
# Remove loop detection (if false positive)
# Add 'loop off' to Corefile
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa
forward . /etc/resolv.conf
loop off # Disable loop detection
cache 30
reload
}
# Or fix configuration to prevent actual loops
Cause: Upstream DNS server issues or network policies
Solution:
# Check upstream DNS
kubectl -n kube-system exec -it <pod> -- nslookup google.com 8.8.8.8
# Configure specific upstream servers
forward . 8.8.8.8 1.1.1.1
# Check network policies
kubectl get networkpolicies --all-namespaces
# Test DNS from cluster
kubectl run -it --rm --image=busybox debug -- nslookup google.com
Cause: Insufficient memory allocation
Solution:
# Increase memory limit in deployment
kubectl -n kube-system set resources deployment coredns --limits=memory=256Mi --requests=memory=128Mi
# Reduce cache size
cache 1800
# Limit concurrent queries
forward . /etc/resolv.conf {
max_concurrent 500
}
Content generated automatically. Verify against official documentation before production use.
Cause: Kubernetes service not found, CoreDNS not watching correct namespace, or service endpoint missing
Solution: Verify service exists with kubectl get svc, check CoreDNS logs, and ensure endpoints exist for the service.
Cause: Upstream DNS servers unreachable, CoreDNS not responding, or network issues between pods and CoreDNS
Solution: Test CoreDNS connectivity from pod, verify upstream DNS servers are reachable, and check network policies allowing DNS traffic.
Cause: Cache misconfiguration, CoreDNS not reloading configuration, or upstream DNS returning wrong data
Solution: Adjust cache TTL settings, force CoreDNS reload, and verify upstream DNS server responses.
Cause: Configuration errors, resource constraints, or file descriptor limits
Solution: Validate Corefile syntax, check pod logs for errors, increase resource limits, and raise file descriptor limits.
Cause: Forward plugin not configured, upstream DNS blocking requests, or firewall rules blocking DNS
Solution: Configure forward plugin for external domains, verify upstream DNS configuration, and ensure network allows DNS queries.
Use this skill when:
Assess Requirements — Understand the use case, scale, integration needs, and existing infrastructure. Checkpoint: Document requirements, constraints, and success criteria.
Design Architecture — Plan component interactions, data flow, and deployment strategy using cloud-native best practices. Checkpoint: Verify the architecture addresses all requirements and follows CNCF conventions.
Implement & Configure — Create manifests, configurations, and deployment scripts. Include resource limits, health checks, and observability hooks. Checkpoint: Validate all YAML against schema and test in a staging environment.
Deploy & Monitor — Apply manifests to the cluster, verify component health, and confirm observability is working. Checkpoint: Confirm all pods/services are running, probes passing, and metrics/alerts configured.
These links are resolved at load time — content is fetched and inlined to provide the latest documentation.