SDNS

A high-performance, recursive DNS resolver server with DNSSEC support, focused on preserving privacy.

This project is maintained by semihalev

Kubernetes DNS Middleware for SDNS

A high-performance, production-ready Kubernetes DNS middleware implementation for SDNS that provides full compatibility with Kubernetes DNS specifications.

Features

✅ Core DNS Resolution (100% Working)

  1. Service DNS Resolution
    • service.namespace.svc.cluster.local → Service ClusterIP
    • Headless services return all endpoint IPs
    • ExternalName services return CNAME records
    • Full IPv4 and IPv6 support (dual-stack ready)
  2. Pod DNS Resolution
    • pod-ip.namespace.pod.cluster.local → Pod IP
    • IPv4: 10-244-1-1.namespace.pod.cluster.local
    • IPv6: 2001-db8--1.namespace.pod.cluster.local
    • StatefulSet pods: pod-name.service.namespace.svc.cluster.local
  3. SRV Records
    • _port._protocol.service.namespace.svc.cluster.local
    • Returns port information for service discovery
    • Supports TCP, UDP, and SCTP protocols
  4. PTR Records (Reverse DNS)
    • IPv4: 1.0.96.10.in-addr.arpa → service/pod domain
    • IPv6: 1.0...0.2.ip6.arpa → service/pod domain
    • Supports both service and pod reverse lookups
  5. Kubernetes API Integration
    • Real-time synchronization with Kubernetes API
    • Watches Services, EndpointSlices, and Pods
    • Automatic updates when resources change
    • Graceful fallback to demo mode without cluster

🚀 Killer Mode Features (NEW!)

When killer_mode is enabled in the [kubernetes] configuration section, the middleware activates advanced performance features:

  1. Zero-Allocation Cache
    • Wire-format DNS message caching
    • Direct byte slice operations
    • No serialization overhead
    • Automatic memory management
  2. Lock-Free ML Predictor
    • Learns query patterns in real-time
    • Predictive prefetching for common queries
    • Markov chain-based sequence prediction
    • Background training without blocking
  3. Sharded Registry
    • 16 service shards + 32 pod shards
    • Concurrent read/write operations
    • Fine-grained locking for scalability
    • Hash-based shard distribution
  4. Performance Optimizations
    • Atomic query counters
    • Background goroutine pools
    • Predictive cache warming
    • Minimal lock contention

Architecture

┌─────────────────┐
│   SDNS Core     │
└────────┬────────┘
         │
┌────────▼────────────────────────────┐
│   Kubernetes Middleware             │
│  ┌──────────────────────────────┐  │
│  │ Killer Mode Components       │  │
│  │ • Zero-Alloc Cache          │  │
│  │ • ML Predictor              │  │
│  │ • Sharded Registry          │  │
│  └──────────────────────────────┘  │
└────────┬────────────────────────────┘
         │
    ┌────┴────┬──────────┬──────────┐
    │         │          │          │
┌───▼───┐ ┌──▼───┐ ┌────▼────┐ ┌───▼────┐
│Resolver│ │Cache │ │Registry │ │K8s     │
│        │ │      │ │         │ │Client  │
└────────┘ └──────┘ └─────────┘ └────────┘

File Structure

Core Components

Performance Components

IPv6 Support

Caching

Testing

Configuration

Add to your SDNS configuration:

# Kubernetes middleware configuration
[kubernetes]
# Enable Kubernetes DNS middleware
enabled = true

# Kubernetes cluster domain suffix
cluster_domain = "cluster.local"  # Default: cluster.local

# Enable killer mode for maximum performance
killer_mode = true                # Default: false

# Optional: specify kubeconfig path
# kubeconfig = "/path/to/kubeconfig"  # Uses in-cluster config by default

Usage

The middleware automatically:

  1. Connects to Kubernetes API (or uses demo data if not available)
  2. Watches for Service, EndpointSlice, and Pod changes
  3. Resolves DNS queries for Kubernetes resources
  4. Caches responses for optimal performance
  5. (Killer Mode) Learns patterns and prefetches likely queries

Query Examples

# Service lookup
dig @localhost service-name.namespace.svc.cluster.local

# Pod by IP
dig @localhost 10-244-1-1.namespace.pod.cluster.local

# SRV record
dig @localhost _http._tcp.service-name.namespace.svc.cluster.local SRV

# Reverse lookup
dig @localhost -x 10.96.0.1

# IPv6 service
dig @localhost service-name.namespace.svc.cluster.local AAAA

Performance Metrics

Standard Mode

Killer Mode

Test Coverage

Current test coverage: 80.0%

All major Kubernetes DNS patterns are tested:

Development

Running Tests

# Run all tests with race detection
make test

# Run specific test
go test -v -race -run TestName

# Check coverage
go test -cover -coverprofile=coverage.out
go tool cover -html=coverage.out

Code Style

Limitations

Future Enhancements

  1. Metrics & Observability
    • Prometheus metrics integration
    • Query latency histograms
    • Cache hit/miss ratios
    • Prediction accuracy tracking
  2. Advanced Features
    • DNS policies (ClusterFirst, ClusterFirstWithHostNet)
    • Pod DNS Config/Policy support
    • Topology-aware endpoint routing
    • Service mesh integration
  3. Performance
    • SIMD optimization for packet processing
    • eBPF integration for kernel bypass
    • Hardware offload support
    • NUMA-aware sharding

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

This middleware is part of SDNS and follows the same license terms.

Implementation Details

Standard Mode Operation

In standard mode, the middleware provides reliable Kubernetes DNS resolution with:

Killer Mode Operation

When killer_mode = true, the middleware switches to high-performance mode:

  1. Query Processing: Direct wire-format responses from cache
  2. Cache Management: Zero-allocation operations with automatic cleanup
  3. Registry Access: Lock-free reads with sharded writes
  4. Predictive Prefetching: ML-based query prediction and pre-warming

Kubernetes Integration

The middleware can operate in three modes:

  1. Full Integration: Connected to Kubernetes API with real-time updates
  2. Demo Mode: Pre-populated test data when Kubernetes is unavailable
  3. Standalone: Works without any Kubernetes cluster for testing

Configuration Priority

  1. If enabled = false, the middleware returns nil (not loaded)
  2. If kubeconfig is specified, it uses that configuration
  3. Otherwise, tries in-cluster configuration
  4. Falls back to demo mode if connection fails

Kubernetes Deployment

RBAC Requirements

When running inside a Kubernetes cluster, SDNS needs the following permissions:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: sdns-kubernetes-dns
rules:
- apiGroups: [""]
  resources: ["services", "pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["discovery.k8s.io"]
  resources: ["endpointslices"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: sdns-kubernetes-dns
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: sdns-kubernetes-dns
subjects:
- kind: ServiceAccount
  name: sdns
  namespace: sdns-system

Example Deployment

apiVersion: v1
kind: Namespace
metadata:
  name: sdns-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sdns
  namespace: sdns-system
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: sdns-config
  namespace: sdns-system
data:
  sdns.toml: |
    version = "1.6.0"
    bind = ":53"
    directory = "/var/lib/sdns"
    
    [kubernetes]
    enabled = true
    cluster_domain = "cluster.local"
    killer_mode = true
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sdns
  namespace: sdns-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sdns
  template:
    metadata:
      labels:
        app: sdns
    spec:
      serviceAccountName: sdns
      containers:
      - name: sdns
        image: ghcr.io/semihalev/sdns:latest
        ports:
        - containerPort: 53
          protocol: UDP
          name: dns-udp
        - containerPort: 53
          protocol: TCP
          name: dns-tcp
        volumeMounts:
        - name: config
          mountPath: /etc/sdns
        - name: data
          mountPath: /var/lib/sdns
        args: ["-c", "/etc/sdns/sdns.toml"]
      volumes:
      - name: config
        configMap:
          name: sdns-config
      - name: data
        emptyDir: {}

Troubleshooting

Common Issues

  1. Middleware not loading
    • Ensure enabled = true in the [kubernetes] section
    • Check SDNS logs for initialization messages
  2. No Kubernetes connection
    • Verify kubeconfig path is correct
    • Check if running inside a Kubernetes cluster
    • Ensure proper RBAC permissions for Services, Pods, and EndpointSlices
  3. High memory usage in killer mode
    • Normal behavior due to pre-allocation
    • Cache automatically manages memory with TTL expiration
    • Monitor with the stats API endpoint
  4. Queries not resolving
    • Verify cluster_domain matches your Kubernetes cluster
    • Check if services/pods exist in Kubernetes
    • Enable debug logging in SDNS

Monitoring

Prometheus Metrics

SDNS exposes Prometheus metrics that include DNS query statistics:

# Access Prometheus metrics endpoint
curl http://localhost:8080/metrics

The metrics include:

Internal Statistics

The Kubernetes middleware maintains internal statistics accessible through the Stats() method:

These statistics are used internally and logged periodically when killer mode is enabled.

Summary

This implementation provides a blazing-fast, production-ready Kubernetes DNS middleware for SDNS. It handles all standard Kubernetes DNS patterns while offering an optional “killer mode” that leverages advanced techniques like zero-allocation caching, ML-based prediction, and lock-free data structures to achieve exceptional performance that makes CoreDNS look slow.

The middleware is designed to be a drop-in solution for Kubernetes DNS needs, with automatic fallback mechanisms ensuring it works in any environment - from production Kubernetes clusters to local development setups.