Overriding DNS in Kubernetes Pods: Hosts, HostAliases, CoreDNS & Custom Strategies
This article explores multiple methods to specify custom DNS resolution inside Kubernetes containers, including editing /etc/hosts, using HostAliases, modifying the CoreDNS ConfigMap, applying custom DNS policies, and evaluating third‑party DNS plugins, with example manifests and code snippets.
In this article we examine several ways to control domain name resolution inside Kubernetes containers, starting with a sample Deployment that runs a busybox pod printing "Hello, Kubernetes!" every 10 seconds.
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox-deployment
labels:
app: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- "while true; do echo Hello, Kubernetes!; sleep 10;done"TL;DR
Modify /etc/hosts – pod level – not recommended for production.
Add HostAliases – pod/Deployment/StatefulSet level – recommended.
Change CoreDNS configuration – cluster‑wide – recommended.
Custom DNS policy – pod/Deployment/StatefulSet level – use as needed.
Third‑party DNS plugin – cluster‑wide – not recommended.
Modify /etc/hosts
Editing /etc/hosts inside a container is the most traditional method; it works at the pod level but requires manual changes after each restart, making it unsuitable for production.
250.250.250.250 four-250Add HostAliases
The hostAliases field in a Pod spec adds extra entries to the pod's /etc/hosts file, useful for overriding or adding hostnames.
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox-deployment
labels:
app: busybox
spec:
replicas: 3
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
hostAliases:
- ip: "250.250.250.250"
hostnames:
- "four-250"
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- "while true; do echo Hello, Kubernetes!; sleep 10;done"Kubernetes automatically inserts a comment "# Entries added by HostAliases" into the generated /etc/hosts file.
func hostsEntriesFromHostAliases(hostAliases []v1.HostAlias) []byte {
if len(hostAliases) == 0 {
return []byte{}
}
var buffer bytes.Buffer
buffer.WriteString("
")
buffer.WriteString("# Entries added by HostAliases.
")
for _, hostAlias := range hostAliases {
buffer.WriteString(fmt.Sprintf("%s\t%s
", hostAlias.IP, strings.Join(hostAlias.Hostnames, "\t")))
}
return buffer.Bytes()
}CoreDNS configuration
Modifying the CoreDNS ConfigMap allows cluster‑wide custom DNS entries.
Command: kubectl edit cm coredns -n kube-system Original Corefile snippet:
Corefile: |
.:53 {
log
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
hosts {
192.168.65.2 host.minikube.internal
fallthrough
}
forward . /etc/resolv.conf {
max_concurrent 1000
}
cache 30
loop
reload
loadbalance
}Add a custom host entry: 250.250.250.250 four-250 If the reload plugin is not enabled, CoreDNS must be restarted; the default reload interval is 30 seconds.
Custom DNS policy
By setting dnsConfig in the pod spec you can direct specific DNS queries to chosen nameservers and search domains.
spec:
dnsConfig:
nameservers:
- 1.2.3.4
searches:
- search.prefix
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- "while true; do echo Hello, Kubernetes!; sleep 10;done"Third‑party DNS plugin
Using alternative DNS plugins is not recommended because CoreDNS is the industry‑standard and lacks a mature replacement.
Reference: https://www.cnblogs.com/huaweiyun/p/17903421.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
