Skip to content

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.

ping 192.168.1.11

Instead of remembering an IP address, we can use a hostname:

ping db

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:

cat >> /etc/hosts
192.168.1.11 db

Now the host can resolve:

ping db

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:

192.168.1.11 google.com

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:

/etc/resolv.conf

Example:

nameserver 192.168.1.100

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:

  1. /etc/hosts
  2. DNS server

The lookup order is controlled by:

/etc/nsswitch.conf

Example:

hosts: files dns

This means:

  1. Check local files first
  2. Then query DNS

Tip

If DNS resolution behaves unexpectedly, check both /etc/hosts and /etc/nsswitch.conf.


Example: Local Entry Overrides DNS

Local file:

192.168.1.115 test

DNS server:

192.168.1.116 test

If /etc/nsswitch.conf has:

hosts: files dns

Then the system resolves test as:

192.168.1.115

Warning

Local /etc/hosts entries can hide DNS server records.


Public DNS Forwarding

If an internal DNS server does not know a record like:

www.facebook.com

it can forward the request to a public DNS server.

Common public DNS server:

8.8.8.8

Example /etc/resolv.conf:

nameserver 192.168.1.100
nameserver 8.8.8.8

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:

www.google.com

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:

apps.google.com

Resolution flow:

Client
Organization DNS
Root DNS
.com DNS
Google DNS
IP Address

Note

DNS responses are often cached to improve performance and reduce repeated lookups.


Internal Company DNS

Organizations commonly use internal domains.

Example:

mycompany.com

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:

nameserver 192.168.1.100
search mycompany.com

Now this command:

ping web

may resolve as:

web.mycompany.com

Multiple search domains:

search mycompany.com prod.mycompany.com

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:

ping db

or:

ping web.mycompany.com

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.

nslookup www.google.com

Example output:

Server:  8.8.8.8
Address: 8.8.8.8#53

Name:    www.google.com
Address: 172.217.0.132

Note

nslookup queries DNS servers and does not use /etc/hosts.


Testing DNS with dig

dig provides detailed DNS query information.

dig www.google.com

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:

service-name.namespace.svc.cluster.local

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/hosts entries minimal
  • Use internal DNS for private services
  • Configure DNS forwarding properly
  • Monitor DNS latency and failures
  • Use dig and nslookup for troubleshooting
  • Document internal domain naming standards

Do's

  • Use DNS names instead of IP addresses
  • Configure /etc/resolv.conf correctly
  • Check /etc/nsswitch.conf during troubleshooting
  • Use search domains carefully
  • Use internal DNS for private services
  • Validate DNS records after changes

Don'ts

  • Don't rely on /etc/hosts for large environments
  • Don't leave stale host entries
  • Don't add too many search domains
  • Don't assume ping failure 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/hosts overriding DNS?
  • Is /etc/resolv.conf pointing to the right DNS server?
  • Is /etc/nsswitch.conf lookup order correct?
  • Can the DNS server be reached?
  • Does nslookup return the expected IP?
  • Does dig show 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/hosts provides local name resolution
  • /etc/resolv.conf defines DNS servers and search domains
  • /etc/nsswitch.conf controls lookup order
  • nslookup and dig help troubleshoot DNS
  • Kubernetes depends heavily on DNS for service discovery