In an enterprise, it’s very common to have a hybrid application topology consisting of a cloud-native frontend and a legacy backend eg. database cluster. If you are using Kubernetes cluster for your cloud-native applications, then there is an easy way – or let’s say a Kubernetes-native way, to integrate your cloud-native applications with legacy backends.
Normally a Kubernetes service provides an abstraction of the Pods (endpoints) deployed in a cluster. Additionally Kubernetes also allows creating services to abstract backends (endpoints) that are outside of Kubernetes cluster.
Let’s say we have an application deployed via Kubernetes which needs to access a legacy database backend at IP address 10.0.0.10 and port 1040.
First, we need to create a Kubernetes endpoint to map the legacy database backend and then create a service mapping the endpoint.
The following shows the specification for the endpoint describing the database backend.
# cat legacy-ep.yaml kind: Endpoints apiVersion: v1 metadata: name: db-cluster subsets: - addresses: - ip: 10.0.0.10 ports: - port: 1040
The following command will create an endpoint to map the legacy database backend.
# kubectl create -f legacy-ep.yaml
The following shows the service specification for exposing the above endpoint to any Kubernetes Pod.
# cat legacy-service.yaml apiVersion: v1 kind: Service metadata: labels: name: db-cluster name: db-cluster spec: ports: # the port that this service should serve on - port: 1040 externalIPs: [172.16.0.2]
The following command will create a Kubernetes service and expose the service at 172.16.0.2:1040 thereby enabling access from any Kubernetes Pod.
# kubectl create -f legacy-service.yaml
Once the Kubernetes service for the legacy database is created any Kubernetes Pod (or service) should be able to access it by leveraging Kubernetes service discovery mechanisms viz. environment variables or DNS.