Services and pods are "connected" through labels and selectors as we can see below.
Using external names to connect to servers:
The ExternalName service don't use selectors and rely on DNS.
A DNS name is assigned to ExternalName services.
Let's assume "external.myservice.com" is the database server we want to connect to.
Below is a Yaml configuration file for the ExternalName:
When an application in the kubernetes cluster wants to communicate with the external service, for example "external.myservice.com", it looks up the address "external_server.svc.default.cluster" using DNS.
The "external_server.svc.default.cluster" name becomes an alias for the external service "external.myservice.com".
Using the IP address to connect to an external service (servers):
To connect to an external service using it's IP address, we create a clusterIP service, then we create an Endpoint ( representing our external service ) to which the service will send traffic to
The Service and the Endpoint should have the same name "external_name".
When we don't mention any type for the service in the Yaml file, kubernetes creates a clutserIP service ( default service ).
Kubernetes creates the default clusterIP service with an IP address.
We also need to define the endpoint for the external service( database for example ) because we didn't define any selectors.
We create the endpoint that the above service will send traffic to.
The IP address of the endpoint is the IP address of our database server.
We create the endpoint as below:
Kubernetes treats the IP addresses in the endpoint as if they were pods.
The IP address in the endpoint "10.20.54.10" is the IP of our external service "database server".
We can connect to our database server without specifying the port. kubernetes does the port mapping for us.
We create all the objects above from their Yaml file, using the below command: