自定义Endpoint资源可以将集群外部的服务如mysql、zookeeper,引入到集群内来。这种集群外的服务除非使用service的externalName,否则用Service来引入是不合适的,因为他不会自动创建出endpoint,也没有标签来选择集群外部的服务。

我们可以直接在集群上创建一个endpoint资源,为endpoint资源指定端点为集群外部的资源,前提是endpoint资源能和集群外部资源通信。在endpoint之上人为的创建一个Service资源。当客户端访问Service时就相当于访问到集群外部的服务。

手动创建的endpoint存在缺点,缺点在于手动创建的endpoint无法进行就绪状态检测。如果需要将某个端点定义为未就绪状态,则需要手动修改配置清单并重新应用。

Endpoint资源使用格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: v1
kind: Endpoint
metadata: # 对象元数据
name:
namespace:
subsets: # 端点对象的列表
- addresses: # 处于“就绪”状态的端点地址对象列表
- hostname <string> # 端点主机名
ip <string> # 端点的IP地址,必选字段
nodeName <string> # 节点主机名
targetRef: # 提供了该端点的对象引用
apiVersion <string> # 被引用对象所属的API群组及版本
kind <string> # 被引用对象的资源类型,多为Pod
name <string> # 对象名称
namespace <string> # 对象所属的名称究竟
fieldPath <string> # 被引用的对象的字段,在未引用整个对象时使用,常用于仅引用
# 指定Pod对象中的单容器,例如spec.containers[1]
uid <string> # 对象的标识符;
notReadyAddresses: # 处于“未就绪”状态的端点地址对象列表,格式与address相同
ports: # 端口对象列表
- name <string> # 端口名称;
port <integer> # 端口号,必选字段;
protocol <string> # 协议类型,仅支持UDP、TCP和SCTP,默认为TCP;
appProtocol <string> # 应用层协议;

Endpoint资源清单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
apiVersion: v1
kind: Endpoints
metadata:
name: mysql-external
namespace: default
subsets:
- addresses:
- ip: 172.16.11.51
- ip: 172.16.11.52
ports:
- name: mysql
port: 3306
protocol: TCP
notReadyAddresses:
- ip: 172.16.11.53
---
apiVersion: v1
kind: Service
metadata:
name: mysql-external
namespace: default
spec:
type: ClusterIP
ports:
- name: mysql
port: 3306
targetPort: 3306
protocol: TCP