The Domain Name System (DNS) is a hierarchical and decentralized naming system used to translate human-friendly domain names, such as www.example.com, into IP addresses that identify computers on a network. DNS plays a crucial role in service discovery and name resolution.
OKD provides a built-in DNS to ensure that services can be reached by their DNS names. This helps maintain stable communication even if the underlying IP addresses change. When you start a pod, environment variables for service names, IP addresses, and ports are created automatically to enable the pod to communicate with other services.
Key DNS terms
-
CoreDNS: CoreDNS is the DNS server and provides name resolution for services and pods.
-
DNS names: services are assigned DNS names based on their namespace and name. For example, a service named my-service
in the default
namespace would have the DNS name my-service.default.svc.cluster.local
.
-
Domain names: Domain names are the human-friendly names used to access websites and services, such as example.com
.
-
IP addresses: IP addresses are numerical labels assigned to each device connected to a computer network that uses IP for communication. An example of an IPv4 address is 192.0.2.1
. An example of an IPv6 address is 2001:0db8:85a3:0000:0000:8a2e:0370:7334
.
-
DNS servers: DNS servers are specialized servers that store DNS records. These records map domain names to IP addresses. When you type a domain name into your browser, your computer contacts a DNS server to find the corresponding IP address.
-
Resolution process: A DNS query is sent to a DNS resolver. The DNS resolver then contacts a series of DNS servers to find the IP address associated with the domain name. The resolver will try using the name with a series of domains, such as <namespace>.svc.cluster.local
, svc.cluster.local
, and cluster.local
. This process stops at the first match. The IP address is returned to your browser and then connects to the web server using the IP address.
Example: DNS use case
For this example, a front-end application is running in one set of pods and a back-end service is running in another set of pods. The front-end application needs to communicate with the back-end service. You create a service for the back-end pods that gives it a stable IP address and DNS name. The front-end pods use this DNS name to access the back-end service regardless of changes to individual pod IP addresses.
By creating a service for the back-end pods, you provide a stable IP and DNS name, backend-service.default.svc.cluster.local
, that the front-end pods can use to communicate with the back-end service. This setup would ensure that even if individual pod IP addresses change, the communication remains consistent and reliable.
The following steps demonstrate an example of how to configure front-end pods to communicate with a back-end service using DNS.
-
Create the back-end service.
-
Deploy the back-end pods.
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
labels:
app: backend
spec:
replicas: 3
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend-container
image: your-backend-image
ports:
- containerPort: 8080
-
Define a service to expose the back-end pods.
apiVersion: v1
kind: service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 8080
-
Create the front-end pods.
-
Define the front-end pods.
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
labels:
app: frontend
spec:
replicas: 3
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend-container
image: your-frontend-image
ports:
- containerPort: 80
-
Apply the pod definition to your cluster.
$ oc apply -f frontend-deployment.yaml
-
Configure the front-end to communicate with the back-end.
In your front-end application code, use the DNS name of the back-end service to send requests. For example, if your front-end application needs to fetch data from the back-end pod, your application might include the following code:
fetch('http://backend-service.default.svc.cluster.local/api/data')
.then(response => response.json())
.then(data => console.log(data));