0
0
Kubernetesdevops~5 mins

ExternalName service type in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to access an external service using a Kubernetes service name. ExternalName service type lets you create a service inside Kubernetes that points to an external DNS name. This way, your apps can use the service name as if it was inside the cluster.
When you want to access a database hosted outside your Kubernetes cluster using a simple service name.
When you have a legacy service running outside Kubernetes but want to unify access through Kubernetes service names.
When you want to avoid changing application code that expects a service name, but the actual service is external.
When you want to route traffic to an external API or service using Kubernetes DNS.
When you want to simplify service discovery for external services in your cluster.
Config File - externalname-service.yaml
externalname-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: external-db
  namespace: default
spec:
  type: ExternalName
  externalName: db.example.com

This file creates a Kubernetes Service named external-db in the default namespace.

The type: ExternalName tells Kubernetes this service points to an external DNS name.

The externalName: db.example.com is the external DNS address this service will resolve to.

Commands
This command creates the ExternalName service in Kubernetes so your cluster can resolve 'external-db' to 'db.example.com'.
Terminal
kubectl apply -f externalname-service.yaml
Expected OutputExpected
service/external-db created
This command verifies that the ExternalName service was created and shows its details.
Terminal
kubectl get service external-db
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE external-db ExternalName <none> db.example.com <none> 10s
This command shows detailed information about the ExternalName service, including the external DNS it points to.
Terminal
kubectl describe service external-db
Expected OutputExpected
Name: external-db Namespace: default Labels: <none> Annotations: <none> Selector: <none> Type: ExternalName IP: <none> ExternalName: db.example.com Session Affinity: None Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: ExternalName services let you map a Kubernetes service name to an external DNS name for easy access inside the cluster.

Common Mistakes
Using ExternalName service type but specifying ports or selectors.
ExternalName services do not support ports or selectors because they only map to an external DNS name.
Do not include ports or selectors in the ExternalName service spec; only specify 'type: ExternalName' and 'externalName'.
Trying to use ExternalName service to route traffic to an IP address instead of a DNS name.
ExternalName requires a DNS name, not an IP address, because it works via DNS resolution.
Use a valid DNS name for 'externalName', not a raw IP address.
Summary
Create an ExternalName service YAML file specifying the external DNS name.
Apply the YAML file with kubectl to create the service in the cluster.
Verify the service exists and points to the external DNS using kubectl get and describe.