External Service Integration Pattern¶
The External Service Integration Pattern allows a Kubernetes cluster to map internal DNS requests to an external service located outside the cluster.^[k8s中引入外部服务.md] This is achieved by creating a specific configuration involving a Service and an Endpoints resource, which work together to bridge the internal and external environments.
Implementation¶
The pattern requires the creation of two distinct resources that share the same name:
- Service without Selector: A
Serviceis defined without aselectorfield.^[k8s中引入外部服务.md] This indicates to Kubernetes that the endpoints are not managed automatically by the cluster's Pod selection logic.^[k8s中引入外部服务.md] - Endpoints Resource: A separate
Endpointsobject is manually created to explicitly list the IP addresses and ports of the external service.^[k8s中引入外部服务.md]
Example Configuration¶
In the following example, a Service named mysql-svc is created along with a corresponding Endpoints object named mysql-ep.^[k8s中引入外部服务.md] Note that while the names in the source YAML differ, in practice the metadata.name of the Endpoints must match the metadata.name of the Service for the association to work^[k8s中引入外部服务.md]:
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
namespace: default
spec:
clusterIP: None
ports:
- name: default-ep
port: 3306
protocol: TCP
targetPort: 3306
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
name: mysql-ep
namespace: default
subsets:
- addresses:
- ip: 114.32.146.154
ports:
- name: mysql
port: 3306
protocol: TCP
Accessing the Service¶
Once configured, internal applications can reach the external service using standard Kubernetes DNS conventions.^[k8s中引入外部服务.md] For instance, a service running in the default namespace can be accessed via its short name or fully qualified domain name (FQDN).^[k8s中引入外部服务.md]
To verify connectivity, tools like curl can be used from within a Pod to confirm that the internal service name correctly resolves to and retrieves content from the external endpoint.^[k8s中引入外部服务.md]
Related Concepts¶
- [[Kubernetes Services]]
- [[Endpoints]]
- [[Headless Service]]
Sources¶
k8s中引入外部服务.md