8.02 DNS Basics
Abstract
DNS (Domain Name System) resolves human-readable names into IP addresses.
In Linux and Kubernetes environments, DNS is critical for service discovery, application connectivity, and troubleshooting.
Why DNS Is Needed
Without DNS, systems communicate using IP addresses.
Instead of remembering an IP address, we can use a hostname:
Note
DNS converts names like db, web.mycompany.com, or www.google.com into IP addresses.
Local Name Resolution with /etc/hosts
A Linux host can resolve names using the local /etc/hosts file.
Example:
Now the host can resolve:
Tip
/etc/hosts is useful for small labs, temporary mappings, and quick testing.
Important Behavior of /etc/hosts
The system trusts whatever is written in /etc/hosts.
Example:
If this entry exists, the host may resolve google.com to 192.168.1.11.
Warning
Incorrect /etc/hosts entries can override real DNS records and cause confusing troubleshooting issues.
Why DNS Servers Are Needed
Managing /etc/hosts manually does not scale.
Problems with /etc/hosts:
- Must be updated on every host
- Difficult to manage in large environments
- IP changes require many updates
- Easy to create inconsistent records
A DNS server centralizes hostname-to-IP mappings.
Success
DNS servers make name resolution scalable and easier to manage.
Configure DNS Server on Linux
DNS servers are configured in:
Example:
This tells the host to query DNS server 192.168.1.100.
Note
In many modern Linux systems, /etc/resolv.conf may be managed by NetworkManager or systemd-resolved.
Local File vs DNS Server
Linux can resolve names from:
/etc/hosts- DNS server
The lookup order is controlled by:
Example:
This means:
- Check local files first
- Then query DNS
Tip
If DNS resolution behaves unexpectedly, check both /etc/hosts and /etc/nsswitch.conf.
Example: Local Entry Overrides DNS
Local file:
DNS server:
If /etc/nsswitch.conf has:
Then the system resolves test as:
Warning
Local /etc/hosts entries can hide DNS server records.
Public DNS Forwarding
If an internal DNS server does not know a record like:
it can forward the request to a public DNS server.
Common public DNS server:
Example /etc/resolv.conf:
Tip
In production, configure forwarding at the internal DNS server instead of manually adding public DNS on every host.
Domain Names
Domain names are structured hierarchically.
Example:
Breakdown:
| Part | Meaning |
|---|---|
. |
Root |
.com |
Top-level domain |
google |
Domain |
www |
Subdomain |
Other top-level domains:
| TLD | Example Purpose |
|---|---|
.com |
Commercial/general |
.net |
Network-related |
.edu |
Education |
.org |
Organization/non-profit |
.io |
Technology/startups |
DNS Resolution Flow
Example:
Resolution flow:
Note
DNS responses are often cached to improve performance and reduce repeated lookups.
Internal Company DNS
Organizations commonly use internal domains.
Example:
Common records:
| Hostname | Purpose |
|---|---|
web.mycompany.com |
Web application |
mail.mycompany.com |
Mail system |
drive.mycompany.com |
File storage |
pay.mycompany.com |
Payroll |
hr.mycompany.com |
HR system |
sql.mycompany.com |
Database |
Success
Internal DNS improves service discovery across teams and environments.
Search Domains
Search domains allow short names to resolve to full domain names.
Example /etc/resolv.conf:
Now this command:
may resolve as:
Multiple search domains:
Tip
Search domains are useful inside organizations and Kubernetes clusters, but too many search domains can slow DNS lookups.
DNS Record Types
Common DNS record types:
| Record Type | Purpose | Example |
|---|---|---|
| A | Maps hostname to IPv4 address | web-server → 192.168.1.1 |
| AAAA | Maps hostname to IPv6 address | web-server → 2001:db8::1 |
| CNAME | Maps one name to another name | food.web-server → eat.web-server |
Note
A records are the most common basic DNS records.
Testing DNS with ping
You can test name resolution using:
or:
Warning
ping is not always the best DNS troubleshooting tool because firewall rules or ICMP blocking can affect results.
Testing DNS with nslookup
Use nslookup to query DNS directly.
Example output:
Note
nslookup queries DNS servers and does not use /etc/hosts.
Testing DNS with dig
dig provides detailed DNS query information.
Useful output sections:
- question section
- answer section
- TTL
- DNS server response
- query time
Tip
dig is preferred for deeper DNS troubleshooting.
DNS in Kubernetes Context
Kubernetes uses DNS heavily for service discovery.
Example Kubernetes DNS name:
Inside a cluster, applications usually connect using service names instead of Pod IPs.
Example
A Pod can connect to a database service using a name like mysql.default.svc.cluster.local.
Production Best Practices
Recommended
- Use centralized DNS management
- Avoid hardcoding IP addresses
- Keep
/etc/hostsentries minimal - Use internal DNS for private services
- Configure DNS forwarding properly
- Monitor DNS latency and failures
- Use
digandnslookupfor troubleshooting - Document internal domain naming standards
Do's
- Use DNS names instead of IP addresses
- Configure
/etc/resolv.confcorrectly - Check
/etc/nsswitch.confduring troubleshooting - Use search domains carefully
- Use internal DNS for private services
- Validate DNS records after changes
Don'ts
- Don't rely on
/etc/hostsfor large environments - Don't leave stale host entries
- Don't add too many search domains
- Don't assume
pingfailure always means DNS failure - Don't expose internal DNS records publicly
Danger
Incorrect DNS configuration can break application connectivity, service discovery, and production traffic routing.
Troubleshooting Checklist
When DNS fails, check:
- Is the hostname correct?
- Is
/etc/hostsoverriding DNS? - Is
/etc/resolv.confpointing to the right DNS server? - Is
/etc/nsswitch.conflookup order correct? - Can the DNS server be reached?
- Does
nslookupreturn the expected IP? - Does
digshow the correct answer section? - Are search domains causing unexpected lookups?
Quick Commands
cat /etc/hosts
cat /etc/resolv.conf
cat /etc/nsswitch.conf
ping db
nslookup www.google.com
dig www.google.com
Summary
Quote
- DNS resolves names to IP addresses
/etc/hostsprovides local name resolution/etc/resolv.confdefines DNS servers and search domains/etc/nsswitch.confcontrols lookup ordernslookupanddighelp troubleshoot DNS- Kubernetes depends heavily on DNS for service discovery